UNIVERSITY OF TORONTO Faculty of Arts and Science DECEMBER EXAMINATIONS 1998 CSC 108F Duration - 3 hours Examination aids allowed: textbook only (Lewis & Loftus, Java Software Solutions). Check that this examination paper has 10 pages (including this cover page but not the empty back cover page). Answer all questions in the space provided in this paper. When the response requires you to write a program, your marks will depend on the style of your program as well as its correctness. (However, comments are generally not necessary.) All programs must be written in Java. 1. [10 marks = 2 + 2 + 6] (a) The following program can be made correct by adding one word and deleting one word. Cross out the word to be deleted, and mark where the word to be added should go. What is the word to be added? class Main { public static void main (String[] args) { this.print(); } public void print() { System.out.println ("Hello world"); } } (b) The following program contains an error and will not compile. The error can be corrected by changing one word. Find the word and change it. class Student { private String name; public Student (String name) { this.name = name; } } class Main { public static void main (String[] args) { Student s = new Student ("Ganesh"); System.out.println (s.name); } } (c) Consider the following: interface Philosopher { public void pontificate(); } class AverageAcademic implements Philosopher { public String name; public AverageAcademic (String name) { this.name = name; } public void pontificate() { while (true) System.out.println (name); } } class Main { public static void main( String[] args) { AverageAcademic x = new AverageAcademic ("Jacques"); Philosopher x = new Philosopher ("Jacques"); x.pontificate(); Philosopher y = new AverageAcademic ("Hilbert"); System.out.println (y.name); } } i) Would it be possible to add another method in AverageAcademic that is not mentioned in Philosopher? Circle the correct answer. yes no ii) One of the lines containing "new" is wrong. Cross it out. iii) Which of the following error messages is most likely to occur when the compiler reaches the last two statements? Circle the correct answer. (1) Cannot convert an AverageAcademic reference to a Philosopher reference. (2) Cannot access 'name' from the Philosopher reference 'y'. 2. [20 marks] Complete the program below. It reads in a passage of text and prints it out again with improved spacing. The input is similar to the input format for Assignment 2. It may contain some blank lines or lines containing single tokens. It also may contain copies of the token "PPP" to indicate new paragraphs and must contain the token "QQQ" to indicate the end of the file. Your program must take this input and format it so that each line is as full as possible. When the current line is full, it must be printed, and the same happens when a "PPP" is read, regardless of how full the current line is. To fill a line, tokens are read from the input and added to the line in order. A "full" line is one with length equal to MAXLINELENGTH (which is already defined for you), or with length so close to MAXLINELENGTH that the next token from the input will make the line length greater than MAXLINELENGTH. You cannot put partial tokens on a line, so some lines may have to be printed when their length is less than MAXLINELENGTH. You can assume that there will be no word in the input that is longer than MAXLINELENGTH. The main method assumes that you have a class WordGetter available, with a method nextWord() that returns the next token from the input file. You do not have to write WordGetter. The program is started for you, with a main() method in the class "TextFormatter". You must complete the main method and the class "LineBuilder", which is begun for you on the next page. One of LineBuilder's methods is addWord(), and its header is included for you. You must complete addWord() and the contents of any other methods you decide are necessary. You may also add data members (instance variables) and a constructor. Here is an example of some sample input and the corresponding output, with MAXLINELENGTH set to 15. INPUT My paddle's keen and bright flashing with silver PPP Dip Dip & Swing QQQ OUTPUT My paddle's keen and bright flashing with silver Dip Dip & Swing And now, here is the program you are to complete: import java.io.*; class WordGetter { // You do not need to complete the constructor and method for this class. public WordGetter () {...} public String nextWord () {...} } public class TextFormatter { private static final int MAXLINELENGTH = 15; public static final String STOPPER = "QQQ"; public static final String PARAGRAPH = "PPP"; public static void main(String args[]) throws IOException { WordGetter text = new WordGetter(); LineBuilder builder = new LineBuilder (MAXLINELENGTH); String word = text.nextWord(); // Write the rest of the main( ) method here. class LineBuilder { private int lineLength; // addWord: add a word to the line accumulated so far. // If the line is full, print it. public void addWord (String newWord) { // Write the rest of LineBuilder here. You may continue to the next page if necessary. 3. [10 marks] The transpose of a rectangular two-dimensional array is its "mirror image". Here is an example: array A: 2 5 6 transpose of A: 2 7 7 8 9 5 8 6 9 Write a fragment of code (not an entire program) to create the transpose of an array A of integers. Call the transpose "tran". The fragment is begun for you, including part of the declaration of A. Complete the fragment to the point where tran exists and contains the correct values. It is not necessary to print tran. Assume that A is rectangular. That is, the rows of A are all the same length. // (beginning of program omitted) int [][] A = new int [...]; // details of A's instantiation omitted // Here we put values in A. That part is omitted too. ... // Now A exists and all its elements have been initialized. (They contain values.) // You write the rest, to create tran and give it values. // Hint: you can find out how big an array is by using its "length" field. 4. [20 marks] The input to a program contains lines of varying length. Write a program to count how many lines have length one, how many have length two, and so on. Here is an example of input and the corresponding output: input: Now output: length 3 occurrences 3 is the length 6 occurrences 1 time for length 8 occurrences 2 all good length 2 occurrences 1 men length 4 occurrences 1 to come to. QQQ Notice that the output can appear in any order; for example, the count of the length-3 lines comes before the count of length-2 lines. The end of the input is indicated by a line containing just "QQQ"; the length of this line is not counted. You can assume that the input does end, and that none of the lines is unreasonably long. But we are not defining "unreasonably long", and there is no particular number that is an upper limit on the length of a line. 5. [20 marks] One way to calculate the square root of a number is based on Newton's method: Let a be the number that we want to take the square root of. Suppose x is a guess at the square root. Then a better guess at the square root is: We repeat this process over and over, each time using previous xBetter as the current x. Eventually, when x and xBetter are nearly equal, we have the square root of a. Write a method to compute the square root of the number. The method must take one parameter, namely the number that we want the square root of, and the return value of the method must be the square root. Your method should use a/2 as the first guess at the square root of a. It should keep improving its estimate of the square root until the difference (x - xBetter) is less than 0.001% of xBetter. Your method should handle the unusual cases intelligently.