CSE1030 Test 2T

Tue Nov 12
5:00PM - 6: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.


There are many computing applications that involve searching a collection of points to find all of the points that match some criteria. Consider the collection of 1,000 two-dimensional points shown to the left. Suppose that we are interested in finding all points in the neighbourhood of the center of the image (which happens to be the point having coordinates (0, 0))).

A possible matching criteria is equality: A point matches if it is equal to the center point. In this case, there is only one matching point (shown in red).

Another possibility is to use the center point to separate the points into two sides: A point matches if it is to the left of of the center point. In this case, all of the points to the left of the gray line match.

Another possibility is to use the center point to define the center of a neighbourhood: A point matches if it is inside the square surrounding the center point. In this case, all of the points inside the gray square match.



In this test, you are required to implement a class hierarchy for different kinds of matchers:

PointMatcher has a protected field named point that it uses to store a Point2D reference. PointMatcher also has a method with signature matches(Point2D other) that returns true if point is equal to other.

PointLeftOf overrides matches(Point2D other) so that it returns true if other is to the left of point.

PointInSqaure overrides matches(Point2D other) so that it returns true if other is inside the square surrounding point.

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:

Starter code for the three classes is available:

A small test program is available:

Be warned that the tester does not test all aspects of your code! A correct program will produce output similar to the following image:

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. [2 marks] Does your PointMatcher implementation use aggregation or composition for its field named point? Explain why you chose aggregation or composition (no marks for guessing aggregation or composition; you must explain your answer).


2. Consider the following main method:

public static void main(String[] args) {

  Point2D p = new Point2D(0., 0.);
  PointMatcher m = new PointMatcher(p);

  Point2D q = m.getPoint();
  boolean b = (p == q);
  
}

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

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

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

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


3. [2 marks] Suppose that PointMatcher used an IPoint2D to represent point instead of a Point2D. Could PointMatcher use aggregation for its point 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 PointMatchers {

  // ...
  
  // Returns a new PointInSquare matcher that matches a
  // square neighbourhood that is two times the size of
  // the square used by the given matcher.

  public static PointInSquare doubleSize(PointInSquare matcher) {
    return new PointInSquare(matcher.getPoint(),
                             2. * matcher.getHalfLength());
  }

  // ...
}

One of your students shows you their code:

  // somewhere in a main method
  PointMatcher m = new PointInSquare(new Point2D(), 1.);     // A
  
  PointInSquare s = PointMatchers.doubleSize(m);                 // B
  

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

(b) [1 mark] The student asks you what is the run-time (or actual) type of m; 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 you want to match all points that are inside a horizontal range as shown in the figure to the left. A point matches if it is to the left of the gray line and to the right of the blue line. You decide to create a class named PointInRange to implement this functionality, and you also choose to make it a subclass of PointMatcher.

(a) [2 marks] PointInRange requires two points to define the range: one for the gray line and one for the blue line. You decide to override setPoint so that it always sets the rightmost point (the point that defines the gray line). Your API for the overridden version of setPoint is:

  /**
   * Set the rightmost edge of the range that this PointInRange object
   * matches.
   * 
   * Precondition: p must be to the right of the left edge of the range
   *
   * @param p
   *          the point that defines the rightmost edge of the range
   */
  public void setPoint(Point2D p) {
    // implementation not shown
  }

Is the precondition acceptable? Explain your answer. (Hint: PointMatcher.setPoint has no preconditions.)

(b) [2 mark] Instead of using a precondition as in part 5(a), suppose that you had decided to throw an exception instead:

  /**
   * Set the rightmost edge of the range that this PointInRange object
   * matches.
   * 
   * @param p
   *          the point that defines the rightmost edge of the range
   *
   * @throws Exception if p is not to the right of the left edge of the range
   */
  public void setPoint(Point2D p) throws Exception {
    // implementation not shown
  }

Is throwing a checked exception acceptable? Explain your answer.

(c) Instead of using a precondition as in part 5(a) or throwing an exception as in part 5(b), suppose that you decide setPoint should set the point in the center of the range; you also add a new method setWidth to set the width of the range.

[2 marks] Do you need to override setPoint in PointInRange? Explain your answer.

[2 marks] Are you allowed to add the new method setWidth in PointInRange? Explain your answer.

(d) [3 marks] Instead of extending PointMatcher, you could choose to extend PointToLeft when implementing PointInRange. One reason for doing so is that PointToLeft already does half of the work for you by matching all points to the left of the rightmost edge of the range.

Is extending PointToLeft a good idea when implementing PointInRange? If your answer is yes, then provide one more reason why this is a good idea. If your answer is no, then explain why you think this is a bad idea.

Submit

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

submit 1030 Test2 PointMatcher.java PointLeftOf.java PointInSquare.java answers.txt