UNIVERSITY OF TORONTO Faculty of Arts and Science DECEMBER EXAMINATIONS 1998 CSC 108F SAMPLE ANSWERS -------------- These answers are the ones I used for my lecture section. Some of them are necessarily the same for all sections, but others may vary somewhat on account of different instructors' views on programming style. -- J. Clarke 1. [10 marks = 2 + 2 + 6] (a) Delete "this", and make print() "static". (b) Change "private" to "public" in line 2. (c) i) yes yes no ii) Delete this line: Philosopher x = new Philosopher ("Jacques"); iii) (2) 2. [20 marks] // Write the rest of the main( ) method here. --------------------------------------------- while (!word.equals (STOPPER)) { if (word.equals (PARAGRAPH)) builder.newLie(); else builder.addWord(word); word = text.nextWord(); } builder.newLine(); } // end main() } // end class TextFormatter // Write the rest of LineBuilder here. -------------------------------------- class LineBuilder { private int lineLength; private String line; // This is an addition. public void addWord (String newWord) { if (line.length() + 1 + newWord.length() > lineLength) newLine(); if (line.length() != 0) line += " "; line += newWord; } public LineBuilder (int length) { lineLength = length; line = ""; } public void newLine() { if (line.length() == 0) // If PARAGRAPH or STOPPER or word of return; // length = lineLength comes right after // a call to newLine(), do nothing. System.out.println(line); line = ""; } } 3. [10 marks] // (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. ... final int rows = A.length; final int cols = A[0].length; // Assume rows > 0. int[][] tran = new int[cols][rows]; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) tran[j][i] = A[i][j]; 4. [20 marks] import java.util.*; import java.io.*; public class Q4 { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader (new InputStreamReader (System.in), 1); final String STOPPER = "QQQ"; // a table of line lengths we've seen Vector table = new Vector(); // Read the input. while (true) { String line = in.readLine(); if (line.equals(STOPPER)) break; LengthRecord item = lookUp (table, line.length()); if (item != null) item.count++; else { item = new LengthRecord(); item.length = line.length(); item.count = 1; table.addElement (item); } } // Print the table contents. for (int i = 0; i < table.size(); i++) { LengthRecord item = (LengthRecord) table.elementAt(i); System.out.println ("length " + item.length + " occurrences " + item.count); } } // lookUp: retrieve the record for a given length from a table // If no such record found, return null. private static LengthRecord lookUp (Vector table, int length) { for (int i = 0; i < table.size(); i++) { LengthRecord element = (LengthRecord) table.elementAt(i); if (element.length == length) return element; } return null; } } class LengthRecord { public int length; public int count; } 5. [20 marks] public static double sqrt (double a) { if (a<0) throw new SqrtException(); // OK to omit definition of SqrtException double x = a/2; double xBetter = (x + a/x)/2; while (Math.abs(x - xBetter) > 0.00001*Math.abs(xBetter)) { x = xBetter; xBetter = (x + a/x)/2; } return xBetter; }