EECS1030Z Test 4

Thursday March 18, 2015, 14:30-15:45
Lab 01
Version H


package test4;

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


/**
 * A rectangle that can move through a sequence of positions.
 * 
 * An animated rectangle has a list of positions that it can move to.
 * A client adds new positions to the list by invoking the addPosition
 * method. When the play method is invoked, the rectangle
 * moves to each position in the list starting from the first position
 * and stopping at the last position. Each time the
 * rectangle moves to a new position it is also drawn to the StdDraw instance.
 * 
 * @author EECS1030_2014_15W
 *
 */
public class AnimatedRectangle extends Rectangle {

  private List positions;

  /**
   * Create an animated rectangle whose coordinates of the lower-left corner are given
   * by position and the upper-right corner are given by
   * oppCorner.
   *
   * The list of positions contains only the initial position position.
   * 
   * @param position
   *            the coordinates of the lower-left corner of this rectangle
   * @param oppCorner
   *            the coordinates of the upper-right corner of this rectangle
   * @pre. position.getX() < oppCorner.getX() && position.getY() < oppCorner.getY()
   */
  public AnimatedRectangle(Point2D position, Point2D oppCorner) {
    super(position, oppCorner);
    this.positions = new ArrayList();
    this.positions.add(new Point2D(position));
  }

  /**
   * Returns a list of positions that are used to animate this rectangle. Modifying
   * the list returned by this method does not modify the list of
   * positions used to animate this rectangle.
   * 
   * @return a list of positions that are used to animate this rectangle
   */
  public List getPositionList() {
    return new ArrayList(this.positions);
  }

  /**
   * Add a position to the end of the list of positions used to animate this rectangle.
   * 
   * @param c the position to add
   */
  public void addPosition(Point2D c) {
    this.positions.add(c);
  }

  /**
   * Animate the rectangle by moving the rectangle to each position in its list
   * of positions starting from the first element in the list. Each time the
   * rectangle is moved it is also drawn to the StdDraw instance.
   */
  public void play() {
    for (Point2D p : this.positions) {
      this.draw();
      this.setPosition(p);
    }
  }

  /**
   * Compares this animated rectangle to the specified object. The result is
   * true if and only if the argument is not null and is an AnimatedRectangle
   * object that has the same position, opposite corner, and position list as
   * this animated rectangle.
   * 
   * @param obj
   *          the object to compare
   * @return true if the given object represents a rectangle with the same
   *         position, opposite corner, and position list as this rectangle,
   *         false otherwise
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object obj) {
    boolean eq = super.equals(obj);
    if (eq) {
      AnimatedRectangle other = (AnimatedRectangle) obj;
      int lastThis = this.positions.size() - 1;
      int lastOther = other.positions.size() - 1;
      eq = this.positions.get(0).equals(other.positions.get(0)) &&
          this.positions.get(lastThis).equals(other.positions.get(lastOther));
    }
    return eq;
  }

  /**
   * Returns a hash code for this animated rectangle.
   * 
   * @return a hash code for this animated rectangle.
   * @see test4.Rectangle#hashCode()
   */
  @Override
  public int hashCode() {
    return super.hashCode() + 31 * this.positions.hashCode();
  }

}

Other questions

Question 1

What is the main advantage of using composition over aggregation? (Be concise in your answer; it must be 80 characters or shorter in length)

Type your answer here: The class can ensure class invariants on mutable parts of its state.

Question 2

Suppose that a method header states that it throws an exception of type X:

public void someMethod() throws X

If a child class overrides the method, what types of exceptions can the child class version of the method throw?

A. any type that is a child of Exception
B. any type that is a child of RuntimeException
C. any type that is a child of X
D. any type that is a parent of X

Question 3

A student extends the class test4.AnimatedRectangle to create a child class lab4.BetterAnimatedRectangle. They override the method play and add the precondition that the rectangle must not move to a position that is outside of the boundaries of the StdDraw window.

Has the student done anything wrong? Explain your answer with regards to adding the precondition.


Yes, by adding the precondition that the rectangle must not move to a position that is outside of the boundaries of the StdDraw window, the precondition on the child class version of the method has been strengthened.