EECS1030M Test 3

Wednesday February 25, 2015, 13:00-14:15
Lab 02
Version B


Programming question

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

package test3;

/**
 * This class represents an elastic band.  Each elastic band
 * has a length.
 * 
 * @author Franck van Breugel
 */
public class ElasticBand 
{
    private double length;

    /**
     * Initializes this elastic band with the given length.
     * 
     * @param length the length of this elastic band.
     * @pre. length > 0
     */
    private ElasticBand(double length) 
    {
	this.setLength(length);
    }

    /**
     * Returns the length of this elastic band.
     * 
     * @return the length of this elastic band.
     */
    public double getLength() 
    {
	return this.length;
    }

    /**
     * Sets the length of this elastic band to the given length.
     * 
     * @param length the new length of this elastic band.
     * @pre. length > 0
     */
    public void setLength(double length) 
    {
	this.length = length;
    }
    
    /**
     * Returns an elastic band with the given length.
     * 
     * @param length the length of the elastic band.
     * @pre. length > 0
     * @return an elastic band with the given length.
     */
    public static ElasticBand getInstance(double length)
    {
	return new ElasticBand(length);
    }
    
    /**
     * Tests whether this elastic band is the same as the
     * given object.  Two elastic bands are the same if
     * their length differ by less than 0.01.
     * 
     * @param object an object.
     * @return true if this elastic band is the same as the
     * given object, false otherwise.
     */
    public boolean equals(Object object)
    {
	final double EPSILON = 0.01;
	
	boolean equal;
	if (object != null && this.getClass() == object.getClass())
	{
	    ElasticBand other = (ElasticBand) object;
	    equal = Math.abs(this.getLength() - other.getLength()) < EPSILON;
	}
	else
	{
	    equal = false;
	}
	return equal;
    }
    
    /**
     * Stretches the length of this elastic band by the given factor.
     * For example, if the length of this elastic band is 3 and it
     * is stretched by a factor 1.5 then its length becomes 4.5.
     * 
     * @param factor the stretch factor
     * @pre. factor > 0 && factor < 5
     */
    public void stretch(double factor)
    {
	this.setLength(factor * this.getLength());
    }
}


Other questions

Question 1

Instead of accessing the attribute name using this.name, it is better to use this.getName(). Which of the following provides the best rationale?
A. It may avoid code duplication.
B. It may make it easier to modify the representation of the attribute name.
C. A and B.
D. Neither A nor B.



Question 2

The class Circle has an attribute named radius of type int. To implement the singleton 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 method is invoked on a(n) object (reference)



Question 4

Delegating from one constructor to another is known as constructor chaining



Question 5

The class IP has an attribute named address of type String and implements the multiton design pattern. The mutator setAddress is private. If we make the mutator setAddress public, will the IP class still implement the multiton design pattern? Explain your answer.
Consider the following code snippet.

IP first = IP.getInstance("1.1");
IP second = IP.getInstance("1.2");
second.setAddress("1.1");
We now have two different IP objects with the same address. Hence, the IP class does not implement the multiton design pattern any more.



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 = age;
   }

   /**
    * Increments the age of this person.
    */
   public void increment()
   {
      this.age++;
   }
}
Is
//@ invariant this.age >= 0
a class invariant? Explain your answer.
Consider the following code snippet.
Person person = new Person(-1);
Note that the class invariant this.age >= 0 is not true after the invocation of the constructor. Hence, this.age >= 0 is not a class invariant.