CSC108H: Introduction to Computer Science
Department of Computer Science, University of Toronto

St. George Campus, Summer 2000


L0101 Midterm Solution

Question 1

a
a 
a 

Question 2

The program does not print a statement that correctly reflects the relationship between Jim and Mary. If uncorrected, the program will print:

My name is Jim and I am 2 years older than Mary.
My name is Mary and I am 2 years younger than Jim.

One possible correction is to change the line int diff = other.age - age; to int diff = age - other.age;.

Question 3

a) return a > 1 && a <= 3;

b) return s.indexOf(t) == a && s.charAt(a) == 'b';

Question 4

public class VCR {
    
    private String manufacturer;
    private String showsToRecord;
    private int numShowsToRecord;

    public VCR(String m) {
        manufacturer = m;
        showsToRecord = "";
        numShowsToRecord = 0;
    }

    public String getManufacturer() { return manufacturer; }
    public String getShowsToRecord() { return showsToRecord; }
    public int getNumShowsToRecord() { return numShowsToRecord; }

    public void programShowToRecord(String show, String day, String time) {
        showsToRecord += show + " " + day + " " + time + "\n";
        numShowsToRecord++;
    }
}

Question 5

public class TestAccount {
    public static void main(String[] args) {
        Account a1 = new Account(50.0);
        Account a2 = new Account(25.0);
        Account a3 = a1;
        Account a4 = new Account(20.0);
        a1.transfer(a2, 20.0);
    }
}

Last updated on 2000-07-10 by Ray Ortigas.