Aids allowed: Textbook and API reference.
Length: 50 minutes
First Name: ___________________________
Last Name: ___________________________
Student ID: ___________________________
Tutor: ___________________________
Your Mark:
1. _______ / 10
2. _______ / 10
3. _______ / 5
Total _______ / 25
1. [10 marks]
Complete the Java program begun below. It is intended to read one line
of input and print it out again after substituting each occurence of
the letter 'e' (lower case only) with an A, then a B, then a C, then a
D, etc. You can assume that the input line has no more than 26
occurences of the letter 'e'.
Here is an example of input and the corresponding output:
Input: Eva. Please, there will be no noise!
Output: Eva. PlAasB, thCrD will bE no noisF!
import java.io.*; public class Replace { public static void main (String[] args) throws IOException { BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); // You write the rest. // Read in a line of text System.out.println ("Enter a line of text:"); String line = in.readLine(); String result = ""; // Stores the output line int capCounter = 0; // Keeps track of how far we are away // from capital 'A' in the ASCII table. // Loop to count through each character in the original line. for (int i=0; i<line.length(); i++) { // The character was an 'e', so replace it the ASCII value // of 'A' plus our 'capCounter' as described above. Cast // the result back to character. if (line.charAt(i) == 'e') { result += (char)('A' + capCounter); capCounter++; } else result += line.charAt(i); } // Print out the new line. System.out.println ("Line with e's replaced:"); System.out.println (result); } }
// Same as above solution, except it does not create a new // variable to hold the line with e's replaed. Instead it // replaces characters in the String that was read in. import java.io.*; public class Replace2 { public static void main (String[] args) throws IOException { BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); // You write the rest. // Read in a line of text System.out.println ("Enter a line of text:"); String line = in.readLine(); int capCounter = 0; // Keeps track of how far we are away // from capital 'A' in the ASCII table. // Loop to count through each character in the original line. for (int i=0; i < line.length(); i++) { // The character was an 'e', so replace it the ASCII value // of 'A' plus our 'capCounter' as described above. Cast // the result back to character. if (line.charAt(i) == 'e') { line = line.substring(0,i) + (char)('A' + capCounter) + line.substring(i+1); capCounter++; } } // Print out the new line. System.out.println ("Line with e's replaced:"); System.out.println (line); } }
// Same as above solution, except instead of casting an ASCII value // to a character, it gets each upper case character from a string // contant (see CAPS below) that stores all upper case charaters. // Solutions #1 and #2 are more eloquent than this solution. import java.io.*; public class Replace3 { public static void main (String[] args) throws IOException { BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); // You write the rest. // Read in a line of text System.out.println ("Enter a line of text:"); String line = in.readLine(); final String CAPS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int capCounter = 0; // Keeps track of how far we are away // from capital 'A' in the ASCII table. // Loop to count through each character in the original line. for (int i=0; i < line.length(); i++) { // The character was an 'e', so replace it the ASCII value // of 'A' plus our 'capCounter' as described above. Cast // the result back to character. if (line.charAt(i) == 'e') { line = line.substring(0,i) + CAPS.charAt(capCounter) + line.substring(i+1); capCounter++; } } // Print out the new line. System.out.println ("Line with e's replaced:"); System.out.println (line); } }
2. [10 marks]
(a) Write a class "Cat" to describe cats that a pet store might be
selling. Assume that the characteristics of cats that we wish to model
are as follows:
the price (an amount such as 24.99)
the colour (a string such as "black").
the age of the cat in years (rounded up to the closest whole number;
for example, a 3 month old cat would have an age of 1.)
All these characteristics should be set in the constructor, and "get" methods should be provided for each of them. In addition, you must make a 'toString' method that returns a string representing all of the information you've stored for the cat. You must choose your own method names (except for 'toString'), parameter lists, and return types.
class Cat { private double price; // Cost of cat private String colour; // Colour of cat private int age; // Age of cat in years public Cat (double thePrice, String theColour, int theAge) { age = theAge; price = thePrice; colour = theColour; } public double getPrice () { return price; } public String getColour () { return colour; } public int getAge () { return age; } public String toString () { String result; result = "Price: " + price + "\n"; result += "Colour: " + colour + "\n"; result += "Age: " + age; return result; } }
(b) Fill in the main method below with the statements to do these two tasks:
i) Create a Cat object that is 3 years old, is orange, and has a price of $15.99.
ii) Use the 'toString' method to display all information about the Cat object that you have created in step (i).
public class PetStore { public static void main (String[] args) { // You write the rest. // Create cat object as desribed. Cat pumpkin = new Cat (15.99, "orange", 3); // Call toString method to print cat info. This line could // also have been: System.out.println (pumpkin.toString()); System.out.println (pumpkin); } }
3. [5 marks]
Here is a Java program, which compiles and runs without error
messages. Write the output from the program in the space below it.
class Teacher { private String name; // Teacher's name private int size; // Size of class public Teacher (int theSize, String theName) { size = theSize; name = theName; } public int getSize () { return size; } public void compare (Teacher theTeacher) { if (theTeacher.size > 16) System.out.println ("You have a lot of students."); else if (size < 17) System.out.println ("I don't have many students."); else if (theTeacher.size < size) System.out.println ("You have more than me."); else System.out.println ("I have less than you."); } public void increment (int num) { size = num + 1; num++; System.out.println ("Incremented to " + num); } } public class School { public static void main(String args[]) { Teacher one = new Teacher (18, "Sue"); Teacher two = new Teacher (16, "Ed"); one.compare (two); int number = 3; one.increment (number); System.out.println (number); two = one; System.out.println (two.getSize()); } }
Write the output of this program here:
You have more than me.
Incremented to 4
3
4