CPSC 124, Winter 1998

Quiz Number 4


This is the fourth quiz given in CPSC 124: Introductory Programming, Winter 1998. See the information page for that course for more information.

The answers given here are sample answers that would receive full credit. However, they are not necessarily the only correct answers.


Question 1: Explain carefully what null means in Java, and why this special value is necessary.

Answer: When a variable is of object type (that is, declared with a class as its type rather than one of Java's primitive types), the value stored in the variable is not an object. Objects exists in a part of memory called the heap, and the variable holds a pointer or reference to the object. Null is a special value that can be stored in a variable to indicate that it does not actually point to any object.


Question 2: What is a constructor? What is the purpose of a constructor in a class?

Answer: A constructor is a special kind of subroutine in a class that has the same name as the name of the class and that has no return type, not even void. A constructor is called with the new operator when an object is created. Its main purpose is to initialize the newly created object, but in fact, it can do anything that the programmer wants it to do.


Question 3: For this problem, you should write a very simple but complete class. The class represents a counter that counts 0, 1, 2, 3, 4,.... The name of the class should be Counter. It has one private instance variable representing the value of the counter. It has two instance methods: increment() adds one to the counter value, and getValue() returns the current counter value. Write a complete definition for the class, Counter.

Answer: Here is a possible answer. (Note that the initialization of the instance variable, value, to zero is not really necessary, since it would be initialized to zero anyway if no explicite initialization were provided.)

                   public class Counter {
                         // An object of this class represents a counter that
                         // counts up from zero.

                      private int value = 0;     // current value of the counter

                      public void increment() {  // add one to the value of the counter
                         value++;
                      }

                      public int getValue() {    // get the current value of the counter
                         return value;
                      }

                   } // end of class Counter

Question 4: The following program segment is meant to simulate tossing a coin 100 times. It should use two Counter objects, headCount and tailCount, to count the number of heads and the number of tails. (The Counter class is defined in problem 3. Fill in the blanks so that it will do so.

          Counter headCount = new Counter();
          Counter tailCount = new Counter();
          for (int flip = 0; flip < 100; flip++) {
             if (Math.random() < 0.5)
                 _________________________ ;   // count a "head"
             else
                 _________________________ ;   // count a "tail"
          }
          console.putln("There were " + _______________________ + " heads.");
          console.putln("There were " + _______________________ + " tails.");

Answer: The variable headCount is a variable of type Counter, so the only thing that you can do with it is call the instance methods headCount.increment() and headCount.getValue(). Similarly for tailCount. Here is the program with calls to these instance methods filled in:

          Counter headCount = new Counter();
          Counter tailCount = new Counter();
          for (int flip = 0; flip < 100; flip++) {
             if (Math.random() < 0.5)
                 headCount.increment() ;   // count a "head"
             else
                 tailCount.increment() ;   // count a "tail"
          }
          console.putln("There were " + headCount.getValue() + " heads.");
          console.putln("There were " + tailCount.getValue() + " tails.");

David Eck, 13 February 1998