EECS1030M Test 3

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


Programming question

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

package test3;

/**
 * This class represents a counter.  Each counter has a count.
 * 
 * @author Franck van Breugel
 */
public class Counter 
{
    private long count;

    /**
     * Initializes this counter with the given count.
     * 
     * @param count the count of this counter.
     * @pre. count >= 0 
     */
    private Counter(long count) 
    {
	this.setCount(count);
    }

    /**
     * Returns the count of this counter.
     * 
     * @return the count of this counter.
     */
    public long getCount() 
    {
	return this.count;
    }

    /**
     * Sets the count of this counter to the given count.
     * 
     * @param count the new count of this counter.
     * @pre. count >= 0
     */
    private void setCount(long count) 
    {
	this.count = count;
    }
    
    /**
     * Increments the count of this counter.
     */
    public void increment()
    {
	this.setCount(this.getCount() + 1);
    }
    
    /**
     * Resets the counter of this counter to 0.
     */
    public void reset()
    {
	this.setCount(0);
    }
    
    /**
     * Compares this counter with the given other counter.
     * Returns a negative integer/zero/positive integer if
     * the count of this counter is smaller than/equal/greater
     * than the counter of the other counter.
     * 
     * @param other a counter.
     * @return a negative integer/zero/positive integer if
     * the count of this counter is smaller than/equal/greater
     * than the counter of the other counter.
     */
    public int compareTo(Counter other)
    {
	return (int) (this.getCount() - other.getCount());
    }
    
    /**
     * Returns a copy of the given counter if that counter is
     * not null.  Otherwise, a counter with count 0 is returned.
     * 
     * @param counter counter to be copied.
     * @return a copy of the given counter if counter != null,
     * a counter with count 0 otherwise.
     */
    public static Counter copy(Counter counter)
    {
	Counter copy;
	if (counter == null)
	{
	    copy = new Counter(0);
	}
	else
	{
	    copy = new Counter(counter.getCount());
	}
	return copy;
    }
}


Other questions

Question 1

Consider the following equals method of the Rectangle class.

public boolean equals(Object)
{
   boolean equal;
   if (object != null && this.getClass() == object.getClass())
   {
      Rectangle other = (Rectangle) object;
      ...
   }
   ...
}
Why do we use the cast (Rectangle) object when we have already checked that object is a Rectangle using this.getClass() == object.getClass()?
A. This is an example of defensive programming.
B. This makes the code more efficient.
C. This makes the compiler happy.



Question 2

The class Circle has an attribute named radius of type int. To implement the multiton design pattern, which of the following is most appropriate?
A. private static Map<Integer, Circle> instance;
B. private static List<Circle> instance;
C. private static Set<Circle> instance;
D. private static Circle instance;



Question 3

A non-static attribute is part of the state of a(n) object



Question 4

The design pattern that ensures that there is at most one instance of a class per state is known as the multiton design pattern



Question 5

The class Printer has an attribute named busy of type boolean and implements the singleton design pattern. The mutator setBusy is private. If we make the mutator setBusy public, will the Printer class still implement the singleton design pattern? Explain your answer.
Although we can now change the state of the Printer object, we still cannot create more than one Printer object. Hence, it still implements the singleton design pattern.



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.
    */
   public Person(int age)
   {
      this.age = Math.abs(age);
   }

   /**
    * Increments the age of this person.
    */
   public void increment()
   {
      this.age++;
   }
}
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. Since we use this.age = Math.abs(age), we can conclude that this.age >= 0.
  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++.