UNIVERSITY OF TORONTO
Faculty of Arts and Science
and University of Toronto at Scarborough
DECEMBER EXAMINATIONS 1999
CSC 108H1 F and CSC A06H3 F
Duration — 3 hours
Examination aids allowed: Textbook (Arnow & Weiss, Introduction to Programming using Java) and API reference (Appendix O from Lewis & Loftus, Java Software Solutions, or the entire book by Lewis & Loftus).
Check that this examination paper has 9 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. [12 marks = 2 x 6]
Some of the following programs contain errors. For each program, either circle the line where the compiler or run-time system would report an error, or write "No error" beside the program.
Hint: There are no errors in the lines beginning "import", "public class", "public static void main", or "BufferedReader in".
And remember: you are to circle the line where the error would be reported, not the line where the programmer made a mistake.
(a)
public class Program { public static void main (String[] args) { int x = Integer.parseInt("five"); System.out.println(x); } }
(b)
public class Program { public static void main (String[] args) { int a = 5; int b = reset(a); System.out.println(a + " " + b); } private static void reset (int x) { x = 15; } }
(c)
public class Program { public static void main (String[] args) { int h; val(h); System.out.println(h); } private static void val (int h) { System.out.println(Math.sqrt(h)); } }
(d)
public class Program { public static void main (String[] args) { double h = 5; print(h); } private static void print (int x) { System.out.println(x); } }
(e)
public class Program { public static void main (String[] args) { int[] list; list = set(5); list[0] = 2; } private static int[] set (int h) { return new int[h]; } }
(f) import java.io.*; public class Program { public static void main (String[] args) throws IOException { BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); String[] text = new String[10]; text[5] = in.readLine(); if (text[5].length() > 3) text[5].charAt(1) = 'a'; System.out.println(text[5]); } }
2. [16 marks = 4 + 6 x 2]
Here is a program that runs without error. There are questions below it to be answered.
import java.io.*; import java.util.*; class Person { public String name; public Person (String name) { this.name = name; } } class Changer { public static void change (Vector[] a, int b) { for (int i = 0; i < b; i++) { int c = a[i].size(); for (int d = 0; d < c; d++) a[i].setElementAt(new Person("Doug"), d); // A System.out.println("changed"); // B } } } public class Test { public static void main (String[] args) throws IOException { BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); int size = Integer.parseInt(in.readLine()); int A_SIZE = size; int V_SIZE = size; Vector[] a = new Vector[A_SIZE]; for (int i = 0; i < A_SIZE; i++) { a[i] = new Vector(); for (int j = 0; j < V_SIZE;j++) a[i].addElement(new Person("Nick")); } Changer.change(a, A_SIZE); for (int i = 0; i < A_SIZE; i++) for (int j = 0; j < V_SIZE; j++) System.out.println(((Person)a[i].elementAt(j)).name); } }
(a) If the input contains one line consisting of the string "2", what output does the program produce?
(b) If the input is "2", as in (a), how many times is the line labelled "//A" executed? Circle the value that best represents the correct answer.
0 1 2 3 4 5 6 7 8
(c) If the input is "2", as in (a), how many times is the line labelled "//B" executed?
0 1 2 3 4 5 6 7 8
(d) If the input is "5", how many times is the line labelled "//A" executed?
0 1 2 sqrt(5) 5 5*sqrt(5) 10 5*log(5) 25
(e) If the input is "5", how many times is the line labelled "//B" executed?
0 1 2 sqrt(5) 5 5*sqrt(5) 10 5*log(5) 25
(f) If the input contains a string representing the number N, how many times is the line labelled "//A" executed?
0 1 log(N) sqrt(N) N N2 N3 N*log(N)
(g) If the input contains a string representing the number N, how many times is the line labelled "//B" executed?
0 1 log(N) sqrt(N) N N2 N3 N*log(N)
3. [14 marks = 7 + 7]
(a) Suppose that a String variable "line" has been declared and given a value, like this:
String line; line = in.readLine(); // or maybe line gets a value in some other way; anyway, it has a value.
Write a fragment of code that prints the number of characters at the beginning of line that are the same as the first character in line. You may assume that line contains at least 1 character.
Examples:
If line is "aaabcd", then the output is 3.
If
line is "abcd", then the output is 1.
If line is
"a", then the output is 1.
If line is "aabaaa",
then the output is 2. (Later occurrences of the same character do not count.)
If line is "aaAaaa", then the output is 2. (Upper-case and
lower-case are different.)
Here are the first two lines of your code fragment. You can assume that the input handler "in" has already been declared, along with all the necessary headers for the program, the class and the "main" method.
String line; line = in.readLine(); // You write the rest.
(b) For data compression and other purposes, we often need to look for runs of repeated characters in data. For example, in the string
abbbbccdefffffg
there is a run of 4 b’s, a run of 2 c’s, and a run of 5 f’s. To be exact, we also consider that the a, the d, the e and the g are all runs of length 1.
In this question, you must complete the method longestRun, which is begun below. Here are some examples of values returned by longestRun:
parameter: abbbbccdefffffg return value: 5
(This is the example above.)
parameter: a
return value: 1
parameter: xxbbbxx
return value: 3 (The two runs of x’s are separate
runs.)
parameter: aaAa return value: 2
(Upper-case and lower-case are different.)
parameter: the empty
string return value: 0
Here is the beginning of the method you are to complete:
// longestRun: returns the length of the longest run in "data". public static int longestRun (String data) { // You write the rest.
4. [18 marks = 8 + 4 + 6]
A supermarket (or large grocery store) uses a class "PurchaseItem" to model customers’ purchases. All purchases are described by a string name, such as "bananas" or "cheese". This name appears together with the price of the item on a customer’s receipt (or bill). As well as a name, all purchases have a cost, and the cost also appears on the receipt. However, different kinds of purchases have costs calculated in different ways: some are weighed, some are counted, some are simply labelled with a single price, and so on.
The supermarket uses the class PurchaseItem to model a single purchase. It is an abstract class, because the method getCost() cannot be defined in a general way for all PurchaseItems. Here is the class:
abstract public class PurchaseItem { private String name; public PurchaseItem (String name) { this.name = name; } public String getName () { return name; } abstract public double getCost(); }(a) One kind of purchase is an item that has to be weighed. It has two special attributes: the weight in kilograms, and the cost per unit weight. This kind of item is modelled by the class WeighedItem, which extends the class PurchaseItem. Write the Java code for WeighedItem, including the constructor, the method getCost(), and any instance variables you need.
// Write the beginning of your class definition here: // Write the constructor here: // Write getCost() here: // Write any instance variables here:
(b) We want to give customers a receipt or printed bill. The items on the receipt look like this:
bananas 1.35 kg @ 0.20 0.27 cheese 1.10 kg @ 2.40 2.64
Write a method toString() for the WeighedItem class that will return a string representing the information needed for this style of receipt. Do not worry about the details of formatting.
(c) Suppose that whenever a customer appears at the checkout counter, we model the contents of the grocery cart — the things the customer wants to buy, that is — as a Vector called purchases. Each element of purchases is an object of type PurchaseItem.
Write a method printReceipt() that prints a customer receipt, with each item printed using its toString() method. At the end, print the total cost. Your method must take a Vector of purchases as a parameter.
Assume that all the subclasses of PurchaseItem have appropriate toString() and getCost() methods.