Discussion 3 handout

Objectives

Reference: Point class

You may want to reference the full source code for Point and MutablePoint as you work on your own class design.

Task 1: Bounding box operations

A bounding box is an axis-aligned rectangle in a 2D plane that encloses (“bounds”) one or more 2D shapes. They may be used to operate on those shapes as a group and to optimize queries over large numbers of shapes.

As a class, brainstorm some operations that an object representing a bounding box might want to support.

Task 2: Bounding box interface

Select one person in your group with a laptop to construct your response in IDEA. Create a new project named “Lab03”. Download the source code for the Point class and move it into the project’s “src” directory.

Write a Java interface to represent an immutable / mutable (identify which on your worksheet) bounding box type supporting the linked bounding box operations. Specify preconditions and postconditions as appropriate.

Then, on your worksheet, write client code to perform the following tasks using your interface:

  1. Given a variable b of type BoundingBox / MutableBoundingBox (don’t worry about how it was initialized), move that variable’s box so that it is centered on the origin.
  2. Write a function that takes two BoundingBoxes / MutableBoundingBoxes as parameters and compute the area of their overlap.

You may be asked to share this client code with the class and to explain the specifications of the interface methods that it uses.

Task 3: Representations of state

Brainstorm at least two different sets of fields that a class could use in order to implement your interface. Each set is a representation of an abstract bounding box’s state.

For each representation, identify any class invariants that should be imposed on the fields. Finally, look over your interface and identify any methods that you think would be easier to implement using one representation or the other.

Task 4: Implementing a bounding box class I

Your group will be assigned a representation to use; write this on your worksheet:

Create a new class in IDEA and declare that it implements your interface. Your TA should demonstrate how to use IDEA’s hints to generate stubs for all of your interface’s methods. As a group, work on the following pieces of the implementation (those who are not typing should be checking the code for correctness and good style):

  1. Add fields for your representation and document the class invariant.
  2. Write a private checkInvariant() method that returns true if the invariant is satisfied.
  3. Write a constructor for your class. Identify and assert any preconditions restricting its parameters, and assert checkInvariant() in the appropriate place.
  4. Implement the methods width(), height(), centroid(), and contains(Point). Write your implementation of contains(Point) on your worksheet and be prepared to share it with the class.
  5. Implement the method for scaling the box. If your type is mutable, assert that your invariant still holds after mutation.
  6. Create a Main class with a main method and use it to test your client code for centering a bounding box using your new (partial) implementation.

Task 5: Implementation II

You may not have time to complete these tasks during section, but they are still good practice.

  1. Add a toString() method to your class. Note that this method does not need to be declared in the interface because it is inherited from class Object. Discuss what string representation of your class would be most useful (e.g. is it possible to reconstruct an instance of your class from its string representation?).
  2. Implement the method for intersecting two bounding boxes. Consider whether this task would be easier or more difficult with another representation.
  3. If your type is immutable, implement the equals method for it, referencing the guide in JavaHyperText.
  4. Consider which methods can be implemented without accessing your representation’s fields (that is, by only calling other interface methods). These methods could actually be implemented in the interface itself as default methods. What would be some pros and cons of doing so?

Submission

  1. Open the assignment page for “Discussion activity 3” in CMSX
  2. [Recorder] Find the “Group Management” section and invite each group member
  3. [Others] Refresh the page and accept your invitation
  4. [Recorder] Take a picture of your work and save as either a JPEG or a PDF file named “discussion_responses”. After all invitations have been accepted, upload your picture as your group’s submission.
    • Recommended scanning apps: Microsoft Office Lens, Adobe Scan, Genius Scan, Evernote Scannable

Ensure that your group is formed and your work submitted before the Friday evening deadline.

Tips and reminders