UNIVERSITY OF TORONTO
Faculty of Arts and Science
DECEMBER EXAMINATIONS 1997
CSC 108F
Duration  3 hours
Examination aids allowed: textbook only (Lewis & Loftus, Java Software
Solutions).
Check that this examination paper has 11 pages (including this cover
page and 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. [20 marks = 12 + 8]
 
(a) Each of these program fragments contains at least one logical
or syntax error. Describe the worst error briefly. (There is intended
to be just one error in each case, but if you think you have found
more than one, judge for yourself which is worst.)

public static int f (int x) {
if (x >= 0)
return x*12.6;
else
return x/0.126;
}
**
Type error: return type is integer, but return expression is of
type double.
**

// Sum 1/i**2, for i = 1, 2, ..., N
public static double invSquares (int N) {
double sum = 0;
for (int i=1; i<=N; i++)
sum += 1/(i*i);
return sum;
}
**
Logical error: 1/(i*i) always truncates because this uses integer
division. 1/(i*i) is always 0 for i>1.
Note: To fix this you must cast using one of the following:
sum += 1.0/(i*i);
sum += (double)1/(i*i);
sum += 1/(double)(i*i);
**

public static void m (String s) {
return ("my input is " + s);
}
and in main():
System.out.println (m("hi"));
**
Return type error: println wants a String to print, but m("hi")
doesn't return anything. The return type for
method m should be String.
**

in main():
int count;
while (true) {
s = stdin.readLine(); // Assume stdin is defined as usual.
if (s.equals("quit"))
break;
count += 1;
System.out.println(s);
}
System.out.println("number of lines read = " + count);
**
Initialization error: count isn't initialized to 0 before the loop starts.
**
(b) Consider the following Java program:
import java.io.*;
public class Wow {
public static void main (String[] args) throws IOException {
DataInputStream stdin = new DataInputStream (System.in);
final int SAVE = 10;
int temp = 999;
int num = Integer.parseInt (stdin.readLine());
while (num > 0) {
if (num < temp) { //
if (num > SAVE) { //
temp = num; //
System.out.println (temp); //
} //
else //
System.out.println (""); //
} //
else //
System.out.println (""); //
num = Integer.parseInt (stdin.readLine());
}
System.out.println("RESULT: " + temp);
}
}
i) What does this program output if given the numbers 5 10 15 20 1,
in that order, as input?
**


15

RESULT: 15
**
ii) What does this program output if given the numbers 20 25 5 15 1,
in that order, as input?
**
20


15
RESULT: 15
**
iii) What does this program output if given the numbers 1000 5 2000 
1, in that order, as input?
**



RESULT: 999
**
iv) Rewrite the two ifstatements (marked by "//") with just one if
statement that performs the exact same function.
**
if (num < temp && num > SAVE) {
temp = num;
System.out.println (temp);
}
else {
System.out.println ("");
}
**
2. [15 marks = 1 + 2 + 12]
 
Assume that a Fraction class has been defined as follows:
class Fraction {
private int numerator;
private int denominator;
public Fraction (int a, int b) {
numerator = a;
denominator = b;
reduce();
}
// Returns this fraction's numerator
public int getNumerator () {
return numerator;
}
// Returns this fraction's denominator;
public int getDenominator () {
return denominator;
}
// Prints this fraction.
public void print () {
System.out.println(numerator + "/" + denominator);
}
// Reduces this Fraction to its simplest form.
// For example, 6/8 would be reduced to 3/4.
public void reduce () {
// Details omitted (but you do not have to complete this method).
}
// Returns true if this Fraction equals otherFrac, and false otherwise.
public boolean equals (Fraction otherFrac) {
// Must first reduce both myself and fraction otherFrac.
reduce();
otherFrac.reduce();
// Now see if we're equal.
if (numerator == otherFrac.getNumerator() &&
denominator == otherFrac.getDenominator())
return true;
else
return false;
}
// Divides this fraction in half.
public void halve () {
denominator = denominator * 2;
reduce();
}
}
(a) What variables does the method reduce have to examine and
possibly change?
**
numerator and denominator of the fraction being reduced.
**
(b) How can the equals method compare two Fractions for equality
when it only has one Fraction parameter?
**
The two fractions compared are (1) the parameter, and
(2) the fraction whose equals method is being called.
**
(c) For each code fragment below, circle the right answer to
indicate whether it will run successfully or produce an error message.
If it runs successfully, state what the output will be. If it produces
an error message, explain the problem (but do not simply give the
contents of the error message).
Fraction f1 = new Fraction(3, 5);
Fraction f2 = new Fraction(3, 5);
Fraction f3 = f1;
if (f1 == f2)
System.out.println("alpha");
if (f1 == f3)
System.out.println("beta");
if (f1.equals(f2))
System.out.println("gamma");
if (f1.equals(f3))
System.out.println("delta");
Runs successfully
Produces an error message
**
Circle "Runs successfully"
Output: beta
gamma
delta
**
Fraction[] list;
list = new Fraction[15];
System.out.print("The numerators are: ");
for (int i=0; i<15; i++)
System.out.print(list[i].getNumerator());
Runs successfully
Produces an error message
**
Circle "Produces an error message"
Reason: Because the elements of "list" have not been initialized.
**
Fraction apple = new Fraction (1, 2);
Fraction peach = new Fraction (4, 5);
Fraction pear = apple;
peach.halve();
pear.halve();
apple.print();
peach.print();
pear.print();
Runs successfully
Produces an error message
**
Circle "Runs successfully"
Output: 1/4
2/5
1/4
**
3. [20 marks = 10 + 10]
 
(a) Consider a twodimensional array that contains integers in a
triangular pattern. The array is square, but only a triangular part is
used, as in this example:
2
1 7
3 6 8
4 2 5 3
Write a method that sums the numbers along the perimeter of the
triangle  the first column, the bottom row and the diagonal. In our
example, the sum is:
2 + 1 + 3 + 4 + 2 + 5 + 3 + 8 + 7 = 35.
The first line of the method is given below. Notice that the method
takes just one argument, the array itself. The array is square, and
its length is the number of rows and columns.
public static int perimeterSum (int[][] A) {
// You write the rest:
**
int sum = 0;
// The lefthand side (the first column)
for (int i=0; i= first) {
second = first;
first = next;
}
else if (next > second) {
second = next;
}
}
System.out.println (second);
}
**