CPSC 124, Fall 2005
Answers to Quiz #4


Question 1: Give two different reasons why it is a good idea to use named constants such as "final static int INTEREST_RATE = 0.0375;" instead of using literal numbers in a program.

Answer: Some possible reasons include:

  1. Using a name makes is more meaningful and makes the program easier to read and understand.
  2. It is easier to change the value of the constant, since it only has to be changed in one place. A literal number, on the other hand, would have to be changed every place where it occurs in the code.
  3. It is safer, since it makes it more difficult to accidently use the wrong value in the program code.

Question 2: What does the value null represent in Java?

Answer: null is a pointer value that does not point anywhere. A variable of class type cannot hold an actual object; it can only hold a pointer or reference to an object. The value null can be stored in a variable of class type to indicate that it does not point to any object. (Note: a "variable of class type" is just one whose type is given as a class, rather than as one of the primitive types.)


Question 3: Explain the term garbage collection as it relates to the Java programming language.

Answer: When there are no longer any references to an object, that object is of no further use since there is absolutely no way for the program to refer to it. The memory occupied by the object should be reclaimed so that it can be reused for other objects. In Java, this happens automatically. The system "garbage collects" objects that are no longer accessible.


Question 4: Suppose that a class definition begins:

            public class QuizQuestion {
                public int firstNum, secondNum;
                   ...

Explain why it is illegal to refer to QuizQuestion.firstNum and QuizQuestion.secondNum.

Answer: QuizQuestion.firstNum and QuizQuestion.secondNum would refer to static variables in the class, but firstNum and secondNum are not static. In fact, there is no such thing as QuizQuestion.firstNum or QuizQuestion.secondNum. Whenever an object of type QuizQuestion is created, that object will contain instance variables named firstNum and secondNum, but the instance variables have to be referred to through the object, not through the class.


Part 2: Write a complete class named PairOfDice with two public instance variables to represent the values showing on a pair of dice, a method for rolling the dice, and a constructor that initializes the dice to any values that you like. (Do not try to make a completely safe class.)

Answer: This is example is taken almost directly from the textbook. Here is one possible answer:

         public class PairOfDice {
         
            public int firstDie;   // Value showing on one of the die.
            public int secondDie;  // Value showing on the other die.
           
            public PairOfDice() {  // Constructor.  (An alternative would be to roll the dice)
               firstDie = 3;
               secondDie = 4;
            }
            
            public void roll() {   // Method for rolling the dice.
               firstDie   = (int)(6 * Math.random());
               secondDie  = (int)(6 * Math.random());
            }
            
         }

David Eck, 28 October 2005