EECS1030M Test 4

Wednesday March 18, 2015, 13:00-14:15
Lab 02
Version A


Programming question

Implement the CombinationLock class. A skeleton can be found here. The API can be found here. A jar with the Lock class can be found here.

package test4;

import java.util.ArrayList;
import java.util.List;

/**
 * A combination lock is a special kind of lock.  Each combination lock
 * has a combination.  The combination lock and its combination
 * form a composition.
 *
 */
public class CombinationLock extends Lock 
{
    private List<Integer> combination;

    /**
     * Initializes this combination lock to be open and have the given combination.
     * 
     * @param combination the combination of this combination lock.
     * @pre. combination != null
     */
    public CombinationLock(List<Integer> combination) 
    {
	super();
	this.setCombination(combination);
    }

    /**
     * Returns the combination of this combination lock.
     * 
     * @return the combination  of this combination lock.
     */
    public List<Integer> getCombination() 
    {
	return new ArrayList<Integer>(combination);
    }

    /**
     * Sets the combination of this combination lock to the 
     * given combination.
     * 
     * @param combination the new combination of this
     * combination lock.
     * @pre. combination != null
     */
    public void setCombination(List<Integer> combination) 
    {
	this.combination = new ArrayList<Integer>(combination);
    }

    /**
     * Tests whether this combination lock is the same as the given object.
     * Two combination locks are the same if they are either both open or
     * both closed and have the same combination.
     * 
     * @param object an object.
     * @return true if this lock is the same as the given object,
     * false otherwise.
     */
    public boolean equals(Object object) 
    {
	boolean equal;
	if (object != null && this.getClass() == object.getClass())
	{
	    CombinationLock other = (CombinationLock) object;
	    equal = super.equals(object) && 
		this.getCombination().equals(other.getCombination());
	}
	else
	{
	    equal = false;
	}
	return equal;
    }

    /**
     * Returns the hash code of this lock.
     * 
     * @return the sum of the digits of the combination of this combination lock
     * if this combination lock is open, the sum of the digits of the combination 
     * of this combination lock plus 1 if this lock is closed.
     */
    public int hashCode()
    {
	int sum = super.hashCode();
	for (Integer number : this.getCombination())
	{
	    sum += number;
	}
	return sum;
    }
}


Other questions

Question 1

Are non-static attributes of the super class inherited by the sub class? Select the best answer.
A. Yes.
B. No.
C. Only if they are private.
D. Only if they are public.



Question 2

Another name for the aggregation relation is the has-a relation



Question 3

Consider the following classes.

public class CreditCard
{
   private double balance;

   /**
    * Pay the given part of the balance of this card.
    * For example, if part is 3, then 1/3 of the balance is paid.
    *
    * @param part 1/part is paid.
    * @pre. part > 0
    */
   public void pay(int part)
   {
      this.balance = this.balance - this.balance / part;
   }
}

public class RewardCard extends CreditCard
{
   private int points;

   /**
    * Pay the given part of the balance of this card.
    * For example, if part is 3, then 1/3 of the balance is paid.
    *
    * @param part 1/part is paid.
    * @pre. part >= 0
    */
   public void pay(int part)
   {
      super.pay(part);
   }
}
Point out what is wrong in the above code. ("Use this.getBalance() instead of this.balance" is not the answer.)
The precondition in the sub class does not imply the precondition in the super class.