EECS1030Z Test 4

Wednesday March 17, 2015, 14:30-15:45
Lab 02
Version C


package test4;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

/**
 * A rectangle that changes color every time it is drawn. A
 * ColorChangingRectangle is a composition of a list of colors.
 * 
 * 

* The first time that the rectangle is drawn, it is drawn using the first color * in its list of colors. The second time the rectangle is drawn, it is drawn * using the second color in its list of colors. This continues until the last * color in its list of colors is used to draw the rectangle; the cycle of * colors then repeats starting from the first color in the list. * * @author EECS1030_2014_15W * */ public class ColorChangingRectangle extends Rectangle { private List colors; private int currentColor; /** * Constructs a rectangle whose bottom-left corner is given by * position and its top-right corner is given by * oppCorner. The initial color of the rectangle is * Color.BLUE. * * @param position * the coordinates of the bottom-left corner of the rectangle * @param oppCorner * the coordinates of the top-right corner of the rectangle */ public ColorChangingRectangle(Point2D position, Point2D oppCorner) { super(position, oppCorner); this.colors = new ArrayList(); this.colors.add(Color.BLUE); this.currentColor = -1; } /** * Returns a list of colors that are used to draw this rectangle. Modifying * the list returned by this method does not modify the list of * colors used to draw this rectangle. * * @return a list of colors that are used to draw this rectangle */ public List getColors() { return new ArrayList(this.colors); } /** * Get the current color of the rectangle, assuming that the * rectangle has already been drawn at least once. The current * color is the color that was most recently used to draw the rectangle. * * @pre. draw() has already been invoked for this rectangle * @return the current color of the rectangle */ public Color getCurrentColor() { return this.colors.get(this.currentColor); } /** * Get the next color of the rectangle. The next color is the color of * the rectangle that the next invocation of draw() * will produce (assuming that addColor is not * invoked before the next invocation of draw). * * @return the color that the next invocation of draw() * will use to draw this rectangle */ public Color getNextColor() { return this.colors.get((this.currentColor + 1) % this.colors.size()); } private Color nextColor() { this.currentColor++; this.currentColor %= this.colors.size(); return this.getCurrentColor(); } /** * Add a color to the end of the list of colors for this rectangle. * * @param c the color to add */ public void addColor(Color c) { this.colors.add(c); } /** * Draws a filled rectangle using the next color of the rectangle. * Use the method StdDraw.setPenColor(Color) to change * the pen color used by the StdDraw object. * * @see test4.Rectangle#draw() */ @Override public void draw() { StdDraw.setPenColor(this.getNextColor()); super.draw(); this.nextColor(); } }


Other questions

Question 1

What is the main difference between composition and aggregation? (Be concise; your answer must be shorter than 50 characters)

Type your answer here: Composition implies ownership

Question 2

Suppose that a method has a parameter named x. Choose the strongest precondition on the value of x from the following choices:

A. @pre. none
B. @pre. x > 0
C. @pre. x < 10
D. @pre. x > 0 && x < 10

Question 3

A student extends the class test4.Rectangle to create a child class lab4.Square that represents a square. They implement a constructor that ensures the square has sides of equal length. They also override the method setOppositeCorner to ensure that the upper-right corner is modified in such a way that the modified square has sides of equal length.

Has the student done anything wrong? Explain your answer.


Yes, they have weakened the postcondition compared to the superclass version of the method.

The superclass version of setOppositeCorner has the postcondition that coordinates of the upper-corner of this rectangle are changed to the new coordinates of the upper-corner of this rectangle; i.e.,

this.oppCorner.getX() == newOppCorner.getX() and this.oppCorner.getY() == newOppCorner.getY()

The overridden version of the method has a weaker postcondition; it can meet the postcondition of the superclass method if and only if the coordinates of newOppCorner are the coordinates for a square.