CSE1030 Test 2R

Thu Nov 7
1:00PM - 2:20PM

Introduction

This test consists of a short programming question and some short answer written questions. I recommend budgeting 40 minutes for the programming question and 40 minutes for the written questions.

Please follow the instructions given in the test.

Setting Up

In eclipse:

  1. Create a new Java Project (perhaps called test2)
  2. In your project, create a new Package called cse1030.test2
  3. In the package you just created, create an empty text file named answers.txt for your answers to the written questions:
    1. select the File menu
    2. select New
    3. select File
    4. type answers.txt for the file name and press Finish

Programming Question

It is expected that your program compiles, meets the specifications given in the API, and follows the style guidelines for CSE1030. Javadoc comments are not required.


Shapes are common objects in computer programs. The bounding box of a shape is the rectangular box that fits tightly around the shape. Usually the bounding box is axis aligned (the sides of the box are horizontal and vertical). Bounding boxes are useful for many applications; for example, drawing programs will use the bounding box to change the size of the shape, and collision detection is often performed using bounding boxes.

A bounding box is defined by its width, height, and center point as shown in the image to the left.

Bounding boxes for a triangle, circle, and line are shown in the image to the left.



Implement the shape hierarchy shown below:

Each Shape has a rectangular bounding box that fits tightly around the shape. The Shape class contains protected fields to represent the bounding box (which are documented in the API).

The API for all of the classes in this lab can be found at the following link:

You will require the following the JAR file:

You are given some starter code:

Feel free to add fields and methods to the starter code.

The Shape parent class is implemented for you. You only need to implement Circle and Line. Hint: Read the description of the class at the beginning of the API for Shape, Circle, and Line carefully.

Written Questions

Type the answers to the written questions into the file answers.txt that you were instructed to create earlier. Make sure that you number your answers to match the questions.


1. Consider the following main method:

public static void main(String[] args) {

  double radius = 1.0;
  Point2D center = new Point2D(2.0, -1.5);
  Shape s = new Circle(radius, center);

  boolean b = center == s.getPosition();
  
}

(a) [1 mark] If Shape used aggregation for its field named position, what is the value of the variable named b?

(b) [1 mark] If Shape used aggregation for its field named position, how many Point2D objects are (probably) created in the main method?

(c) [1 mark] If Shape used composition for its field named position, what is the value of the variable named b?

(d) [1 mark] If Shape used composition for its field named position, how many Point2D objects are (probably) created in the main method?


2. [2 marks] Assume that Shape is implemented correctly. Does Shape use aggregation or composition for its field named position? Explain your answer (no marks for guessing aggregation or composition; you must explain your answer).


3. [2 marks] Suppose that Shape used an IPoint2D to represent position instead of a Point2D. Could Shape use aggregation for its position without creating a privacy leak? Explain your answer. (Recall that IPoint2D represents an immutable point.)


4. Imagine that you are teaching CSE1030. You provide the students with the following utility class:

public class Circles {

  // ...
  
  // returns a new Circle that is 2 times larger than c
  public static Circle doubleSize(Circle c) {
    return new Circle(2. * c.getRadius(), c.getPosition());
  }

  // ...
}

One of your students shows you their code:

  // somewhere in a main method
  Shape c = new Circle(1., new Point2D());         // A
  
  Circle cc = Circles.doubleSize(c);               // B
  

(a) [1 mark] The student asks you what is the declared type of c; what is your answer to the student?

(b) [1 mark] The student asks you what is the run-time (or actual) type of c; what is your answer to the student?

(c) [2 marks] The student asks you why is line A of the code shown correct; what is your answer to the student?

(d) [2 marks] The student asks you why is line B of code shown incorrect; what is your answer to the student?


5. Suppose that Shape had the following method:

/**
 * Changes the size of the shape so that its bounding box
 * has width w and height h.
 *
 * Precondition: w > 0 and h > 0
 *
 * Postcondition: the width is set to w and the height is set to h
 *
 * @param w
 *            the width of the shape's bounding box
 * @param h
 *            the height of the shape's bounding box
 *
 */
public abstract void setSize(double w, double h);

(a) [2 marks] The implementer of Circle cannot satisfy the contract of setSize; explain why.

(b) [1 mark] Can the implementer of Circle simply choose to not implement setSize? Explain your answer.

(c) Suppose that the implementer of Circle overrides setSize as follows:

/**
 * Changes the size of the Circle so that its bounding box
 * has width equal to w.
 *
 * Precondition: w > 0
 *
 * Postcondition: the width is set to w
 *
 * @param w
 *            the width of the shape's bounding box
 * @param h
 *            the height of the shape's bounding box (not used)
 *
 */
public void setSize(double w, double h) {

  // implementation not shown

}

     [2 marks] Is the change to the precondition acceptable? Explain your answer.

     [2 marks] Is the change to the postcondition acceptable? Explain your answer.

(d) Suppose that the implementer of Circle overrides setSize as follows:

/**
 * This is an unsupported operation for circles.
 *
 * Precondition: none
 *
 * Postcondition: throws an Exception
 *
 * @param w
 *            the width of the shape's bounding box
 * @param h
 *            the height of the shape's bounding box (not used)
 *
 */
public void setSize(double w, double h) throws Exception {

  throw new UnsupportedOperationException();

}

     [2 marks] Is throwing an exception acceptable? Explain your answer.

(e) [2 marks] The implementer of Line also cannot satisfy the contract of setSize; explain why.

Submit

Submit your answers to the written questions and your Java program when you are finished:

submit 1030 Test2 Circle.java Line.java answers.txt