University of Toronto -- Department of Computer Science CSC 108F - Fall 1998 Midterm test (Bellantoni's 1pm section) Aids allowed: Textbook only. Time: 50 minutes 1. [10 marks] Complete the Java program begun below. It is intended to read one line of input and print the reverse of the line, starting with the last character. If the line contains no characters, the program prints nothing. Here is an example of input and the corresponding output: input: Hello, Mary output: yraM ,olleH And now, the program that you must complete: public class HalfTheCharacters { public static void main (String[] args) throws IOException { BufferedReader in = new BufferedReader (new InputStreamReader (System.in),1); // You write the rest. Don't forget the "}"s ! String line = in.readLine(); for (int i=line.length()-1; i>=0; i--) { System.out.println (line.charAt(i)); } System.out.println(); } } 2. [10 marks] In this question, you must write part of a "main" method to solve a problem using two classes that you can assume are provided already. Here are the classes that are provided. The contents of the methods are omitted, because you don't need to know how they work. class Person { public int howManyChildren () { ... } // returns an integer that is the number of children the Person has public String getName() { ... } // returns the name of the Person } class PersonReader { public PersonReader (Reader r) { ... } // a constructor -- somewhat like BufferedReader's constructor public Person readPerson() throws IOException { ... } // reads enough input data to make an object of the class Person, // and returns that object. (This is like BufferedReader's // readLine() method, which reads and returns a String.) } Complete the program below. It should read the data about three people, storing each in an object of type Person. Then it should print the number of children of the Person who has the shortest name. (If two or more of the Persons have names of minimal length, then print the number of children of one of them.) public class HowManyChildrenOfShortestNamedPerson { public static void main (String[] args) throws IOException { PersonReader in = new PersonReader ( new InputStreamReader (System.in)); // You write the rest. Don't forget the "}"s ! Person p1 = in.readPerson(); Person p2 = in.readPerson(); Person p3 = in.readPerson(); Person shortNamePerson = p1; if (p2.getName().length() < shortNamePerson.getName().length()) { shortNamePerson = p2; } else if (p3.getName().length() < shortNamePerson.getName().length()) { shortNamePerson = p3; } System.out.println(shortNamePerson.howManyChildren()); } }