EECS1030Z Test 4

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


package test4;

/**
 * A rectangle shape. A rectangle is a composition of a Point2D
 * object that stores the coordinates of the upper-right corner of this
 * rectangle.
 * 
 * 

* The bounding box of this rectangle is the rectangular box formed by * the bottom-left corner of this rectangle (the Shape's position) * and the upper-right corner of this rectangle. * * @author EECS1030_2014_15W * */ public class Rectangle extends Shape { private Point2D oppCorner; /** * Create a rectangle whose coordinates of the lower-left corner are given * by position and the upper-right corner are given by * oppCorner. * * @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() * @pre. position.getY() < oppCorner.getY() */ public Rectangle(Point2D position, Point2D oppCorner) { super(position); this.setOppositeCorner(oppCorner); } /** * Get the coordinates of the upper-corner of this rectangle. The * coordinates of the upper-corner of this rectangle cannot be changed using * the Point2D object returned by this method. * * @return the coordinates of the upper-corner of this rectangle */ public Point2D getOppositeCorner() { return new Point2D(this.oppCorner); } /** * Change the coordinates of the upper-corner of this rectangle. * * @param newOppCorner * the new coordinates of the upper-corner of this rectangle * @pre. this.getPosition().getX() < newOppCorner.getX() * @pre. this.getPosition.getY() < newOppCorner.getY() */ public void setOppositeCorner(Point2D newOppCorner) { this.oppCorner = new Point2D(newOppCorner); } /** * Set the position of the bottom left corner of this shape's bounding box, * moving the rectangle to a new position without changing its size. * * @param newPos * the new position of the rectangle * @pre. newPos.getX() < this.getOppositeCorner.getX() * @pre. newPos.getY() < this.getOppositeCorner.getY() * @see test4.Shape#setPosition(test4.Point2D) */ public void setPosition(Point2D newPos) { Point2D pos = this.getPosition(); super.setPosition(newPos); double x = this.oppCorner.getX() + newPos.getX() - pos.getX(); double y = this.oppCorner.getY() + newPos.getY() - pos.getY(); this.oppCorner.setX(x); this.oppCorner.setY(y); } /** * Draw this rectangle on the StdDraw instance. Use the method * StdDraw.filledRectangle(Point2D, Point2D) to draw this * rectangle, but see the next paragraph for additional requirements * of this method. * *

* This method first draws a filled rectangle on the StdDraw instance using * the current pen color. This method then draws a filled X in * Color.RED marking the position of the shape. * * @see test4.Shape#draw() */ @Override public void draw() { StdDraw.filledRectangle(this.getPosition(), this.getOppositeCorner()); super.draw(); } }


Other questions

Question 1

Consider the following main method:

public static void main(String[] args) {

  Point2D p = new Point2D();
  Point2D q = new Point2D(1, 2);
  Rectangle r = new Rectangle(p, q);
  // HERE

  // ... followed by more code after this point
}

How many Point2D objects are in memory immediately after the rectangle constructor has run (i.e., when the main method reaches the line with the comment HERE)?

Type your answer here: 4 (p, q, the copy of p made by Shape, and the copy of q made by Rectangle)

Question 2

Suppose that a method has a postcondition that promises to return a value x. Choose the strongest postcondition on the value of x from the following choices:

A. returns a value x == 5
B. returns a value x > 0
C. returns a value x < 10
D. returns a value 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 have the following precondition:

@pre. same precondition as Rectangle.setOppositeCorner() AND
      (newOppCorner.getX() - this.getPosition().getX()) ==  (newOppCorner.getY() - this.getPosition().getY())

In plain English, the precondition ensures that the width and height of the modified square are equal.

Has the student done anything wrong? Explain your answer.


Yes, they have strengthened the precondition of the overridden method.

The student has kept the precondition from the superclass, and added the precondition that the width and height of the modified square are equal. This is a strengthening of the precondition.