EECS1030M Test 4

Wednesday March 18, 2015, 16:00-17:15
Lab 01
Version E


Programming question

Implement the Table class. A skeleton can be found here. The API can be found here. A jar with the Furniture and Leg classes can be found here.

package test4;

import java.util.HashSet;
import java.util.Set;

/**
 * A table is a piece of furniture.  Each table
 * has a collection of legs.  The table and its collection
 * of legs form a composition.
 */
public class Table extends Furniture implements Comparable<Table>
{
    private Set<Leg> legs;

    /**
     * Initializes this table with the given brand and the
     * given collection of legs.
     *
     * @param brand the brand of the table.
     * @pre. brand != null
     * @param legs the legs of the table.
     * @pre. legs != null
     */
    public Table(String brand, Set<Leg> legs)
	{
	    super(brand);
	    this.setLegs(legs);
	}
    
    /**
     * Returns the legs of this table.
     * 
     * @return the legs of this table.
     */
    public Set<Leg> getLegs() 
    {
	return new HashSet<Leg>(this.legs);
    }

    /**
     * Sets the legs of this table to the given collection of legs.
     * 
     * @param legs the legs of this table.
     * @pre. legs != null
     */
    public void setLegs(Set<Leg> legs) 
    {
	this.legs = new HashSet<Leg>(legs);
    }

    /**
     * Tests for equality of this table and the given other object.
     * Two tables are equal if they have the same brand and the
     * same number of legs.
     *
     * @param object Another object.
     * @return true if this table and the other are the same, false otherwise.
     */
    public boolean equals(Object object)
    {
	boolean equal;
        if (object != null && this.getClass() == object.getClass())
	{
	    Table other = (Table) object;
	    equal = super.equals(other) &&
	        this.getLegs().size() == other.getLegs().size();
	}
        else
	{
	    equal = false;
	}
        return equal;
    }

    /**
     * Compares this table with the given table.
     * This table is smaller than the other table if its unique id
     * is smaller than the unique id of the other table.
     *
     * @param other Another table.
     * @return (unique id of this table) -  (unique id of other table).
     * @throws NullPointerException if other is null.
     */
    public int compareTo(Table other)
    {
        return Integer.parseInt(this.toString()) - Integer.parseInt(other.toString());
    }
}


Other questions

Question 1

The UML diagram
denotes that (select the best answer)
A. Classes A and B are in the aggregation relation
B. Classes A and B are in the inheritance relation
C. Classes A and B are in the composition relation
D. Classes A and B are in the usage relation



Question 2

To delegate to the constructor of the super class, one uses the keyword super



Question 3

Consider the following classes.

public class CreditCard
{
   private double balance;

   /**
    * Checks if the balance of this card is positive.
    *
    * @throws Exception if the balance of this card is not positive.
    */
   public void checkPositive() throws Exception
   {
      if (this.balance <= 0)
      {
         throw new Exception("Balance is negative");
      }
   }
}

public class RewardCard extends CreditCard
{
   private int points;

   /**
    * Checks if the points balance of this card is positive.
    *
    * @throws Exception if the points balance of this card is not positive.
    */
   public void checkPositive() throws Exception
   {
      if (this.points <= 0)
      {
         throw new Exception("Points balance is negative");
      }
   }
}
Point out what is wrong in the above code. ("Use this.getBalance() instead of this.balance" is not the answer.)
The postcondition of the super class does not imply the postcondition of the sub class (the super class throws an exception when the balance is negative whereas the sub class throws an exception when the point balance is negative).