University of Toronto
csc148--Introduction to Computer Science, Spring 1998
First Term Test -- Horton's section: Solutions!


 
Duration: 		 50 minutes 
Aids allowed: 		 None

Make sure that your examination booklet has 5 pages (including this one). Write your answers in the spaces provided. Write legibly.
 
Family Name: 		  
First Name:  		  
Student # :		   
Tutor (circle one):

Mary Ellen Foster David Suydam Nitin Ramdenee Neil Gower Michael Neff

 


Question 1

[10 marks in total]

Consider the following simple ADT for information about a single sports team.  

A Team Record consists of the number of games played by the team this season; the number of games won by the team this season (we will assume that any game that wasn't won was lost -- there are no ties); the total number of points scored by the team this season; and the total number of points scored against the team this season. The following operations can be performed on a Team Record:

Write a Java class that implements this ADT. Marks will be awarded for good design.
Hint: This should be an extremely simple class. It does not require a linked list or an array.

class TeamRecord {

        private int played;
        private int wins;
        private int by;
        private int against;

        public void recordGame (int teamScore, int opponentScore) {
           by += teamScore;
           against += opponentScore;
           if (teamScore > opponentScore)
                wins++;
           played++;
        }

        public int percentage () {
           return (wins*100)/played;
        }

        public int plusMinus () {
           return by - against;
        }

}


Question 2

[10 marks in total]

Consider the following class:

       class Student {
          private int studentNumber;
          private String name;
          private double gpa;
 
          public Student (int num; String who) {
              studentNumber = num;
              name = who;
              gpa = 0.0;
          }
          public double getGpa () {
              return gpa;
          }
          // Other useful methods would go here.
       }

(a)  [1 mark]
The variables studentNumber, name, and gpa are called ``instance variables''. What does this mean?

     Every instance of the Student class, created using the ``new'' statement in Java, 
     will have these three variables.

(b)  [2 marks]
The Student method is a ``constructor''. How does Java know that it is a constructor?

     (1) It has the same name as the class, and (2), it has no return type -- 
     not even void.

(c)  [4 marks]
Write a fragment of Java code that (1) declares and constructs an array of 100 elements, each of which is of type Student, and (2) constructs 100 instances of Student and stores them in the array. You may give all the students the name ``Fred'' and student number 111.

        Student[] list = new Student[100];
        for (int i=0; i<100; i++)
                list[i] = new Student (111, "Fred");

(d)  [3 marks]
Assume that more sensible Student data has been stored into your array. Write a fragment of Java code that prints out the number of Students in your array whose gpa is above 3.0.

        int count = 0;
        for (int i=0; i<100; i++)
                if (list[i].getGpa() > 3.0)
                        count++;
        System.out.println(count);


Question 3

[7 marks in total]

Assume that we have already defined the following class:

       class Node {
          public int data;
          public Node nextOne;

          public Node (int initialValue) {
              data = initialValue;
              nextOne = null;
          }
       }

(a)  [3 marks]
Assume code has been executed that leaves us in the state shown below on the left. (Nodes are shown without data values because these are irrelevant to the question.)

Write Java code that will bring us to the state shown on the right. That is, it will swap the ends of the two linked lists.

       // We need a temp variable to hold onto here1.next; otherwise we'd lose track of it.
       Node temp = here1.next;
       here1.next = here2.next;
       here2.next = temp;

 

(b)  [4 marks]
Trace the following code and produce a diagram showing the exact state of affairs after it has been executed.

       Node a = new Node(15);
       Node b = new Node(99);
       Node c = a;                     
       b.nextOne = a;
       c.data = -3;
       c = new Node(42);
       a.nextOne = c;

Click here for the solution. (You'll need postscript viewing software. If you don't have it at home, try looking at this at the cdfpc lab.)

 


Question 4

[8 marks]

Assume that both the Node class from question 3 and the following class have been defined:

       class ListOfInts {
          // Refers to the front node in the list.
          private Node theList;    
          // Number of nodes currently in the list.
          private int numElements;  
       
          public ListOfInts() {
               theList = null;
               numElements = 0;
          }
          public int size() {
               return numElements;
          }
          // Other useful methods would go here.
       }

Complete the method below, which is to be added to the ListOfInts class. It adds a given integer to the list, placing it after the nth existing element. (We'll consider the node at the beginning of a list to be node 1.) Assume that the stated preconditions are met by any call to your method.

       // Preconditions: 1 <= n <= the size of the list.
       public void insertAfterNth (int newValue, int n) {

            Node spot = theList;
            for (int i=1; i<= (n-1); i++) {
                    spot = spot.nextOne;
            }
            Node temp = new Node (newValue);
            temp.nextOne = spot.nextOne;
            spot.nextOne = temp;
      }

Diane Horton
Wed Feb 4 17:05:46 EST 1998