EECS1030M Test 3

Wednesday February 25, 2015, 16:00-17:15
Lab 01
Version E


Programming question

Implement the SINCard class. A skeleton can be found here.

package test3;

import java.util.HashMap;
import java.util.Map;

/**
 * A Social Insurance Number card has a number.
 * 
 * @author Franck van Breugel
 */
public class SINCard 
{
    private int number;
    
    private static Map<Integer, SINCard> instance = new HashMap<Integer, SINCard>();

    /**
     * Initializes this card with the given number
     * 
     * @param number the number of this card.
     * @pre. number >= 1000000000 && number <= 999999999
     */
    private SINCard(int number) 
    {
	this.setNumber(number);
    }

    /**
     * Returns the number of this card.
     * 
     * @return the number of this card.
     */
    public int getNumber() 
    {
	return this.number;
    }

    /**
     * Sets the number of this card to the given number.
     * 
     * @param number the new number of this card.
     * @pre. number >= 1000000000 && number <= 999999999
     */
    private void setNumber(int number) 
    {
	this.number = number;
    }
    
    /**
     * Returns a string representation of this card.
     * This string consists of the number of the card.
     * 
     * @return a string representation of this card.
     */
    public String toString()
    {
	return "" + this.getNumber();
    }
    
    /**
     * Tests whether the number of this card is even.
     * 
     * @return true if the number of this card is even,
     * false otherwise.
     */
    public boolean isEven()
    {
	return this.getNumber() % 2 == 0;
    }
    
    /**
     * Returns a SIN card with the given number.
     *
     * @param number the new number of this card.
     * @pre. number >= 1000000000 && number <= 999999999
     * @return a SIN card with the given number.
     */
    public static SINCard getInstance(int number)
    {
	if (!SINCard.instance.containsKey(number))
	{
	    SINCard.instance.put(number, new SINCard(number));
	}
	return SINCard.instance.get(number);
    }
    
    /**
     * Tests whether a card with the given number has been created.
     * 
     * @param number a number.
     * @return true if a card with the given number has been created,
     * false otherwise.
     */
    public static boolean exists(int number)
    {
	return SINCard.instance.containsKey(number);
    }
}


Other questions

Question 1

In the Rectangle class, why do we define an equals method with a parameter of type Object, instead of an equals method with a parameter of type Rectangle?
A. So that we can compare the Rectangle with more objects.
B. So that we can sort Rectangles.
C. So that we override the equals method of the Object class.



Question 2

Suppose that one of the attributes of the class is named price and that one of the methods in the class has a parameter also named price. Which of the following statements is correct?
A. Due to the name clash, the class does not compile.
B. If that method contains the statement price = 0, then the attribute is assigned the value 0.
C. If that method contains the statement price = 0, then the parameter is assigned the value 0.



Question 3

A static method is invoked on a(n) class



Question 4

A constructor of class X that takes an object of type X as its only argument is know as a(n) copy constructor



Question 5

The class Printer has an attribute named busy of type boolean and implements the singleton design pattern. Two Printer objects are the same if their busy attributes have the same value. Do we have to override the equals method of the Object class? Explain your answer.
Since the Printer class implements the singleton design pattern, there can only be at most one Printer object. Hence, this Printer object can only be equal to itself. This is exactly captured by the equals method of the Object class. Therefore, we do not have to override the equals method of the Object class.



Question 6

Consider the following class.

public class Person 
{
   private int age;

   /**
    * Initializes this person with the given age.
    *
    * @param age the age of this person.
    * @pre. age >= 0
    */
   public Person(int age)
   {
      this.age = age;
   }

   /**
    * Increments the age of this person.
    */
   public void increment()
   {
      this.decrease(-1);
   }

   /**
    * Decrements the age of this person with the given amount.
    * 
    * @param delta the amount the age of this person is decreased.
    */
   private void decrease(int delta)
   {
      this.age -= delta;
   }
}
Is
//@ invariant this.age >= 0
a class invariant? Explain your answer.
The following two constraints have to be met:
  1. The class invariant this.age >= 0 has to be true after the invocation of the constructor, provided that the precondition age >= 0 is met. Obviously, if age >= 0 then this.age >= 0 after the assignment this.age = age.
  2. The class invariant this.age >= 0 has to be maintained by the increment method. If this.age >= 0 holds before the invocation of the increment method, then this.age >= 0 also holds after the execution of the statement this.age -= -1.
Note that, since the method decrease is private, it does not have to maintain the class invariant. Hence, this.age >= 0 is a class invariant.