In this activity, you will explore implementing a class' methods and testing your implementation. These methods may use the class' instance variables, use parameters, and may return values. The class you will implement and test is a simplified representation of a rectangle.
We will use the following files in today's activity:
The first is the class you will be implementing, the second simply tests the first by calling its methods and checking the results are as expected. You should start by reading over the code in SimpleRectangle.java and making sure you understand it. When compiled and run, the output should indicate that two methods are not properly implemented. Your first task is to fix these methods so they pass all of the tests.
After you have corrected the current implementation, you should add the following additional methods and test them by copying and modifying the test methods.
perimeter,
that returns the perimeter of the rectangle. The rectangle's state should not change as a result of calling this method. move
that takes two integer parameters,
representing the number of pixels to move horizontally and vertically, and
moves the rectangle by changing its left and top coordinates. This method
should return nothing, but instead change the state of the rectangle. add
that takes two integer
parameters, representing the x- and y-coordinate of a point on the screen,
and expands the rectangle minimally such that it contains
the new point. This
method should return nothing, but instead change the state of the rectangle.union
that takes a SimpleRectangle
as a parameter, representing another rectangle, and returns a
new rectangle that is the union of the space occupied by both rectangles. This
method should not modify the state of the current rectangle or the rectangle passed as a
parameter.intersection
that
takes a SimpleRectangle
as a parameter, representing another
rectangle, and returns a new rectangle that is the
intersection of the space occupied by both rectangles. You will need to use
at least one if statement to determine if there is any intersection at all. If the
two rectangles do not intersect, then return an empty rectangle
(i.e., one with zero width and height). This method should not modify the
current rectangle or the rectangle passed as a parameter.
What additional test cases might be useful to determine if each method works correctly.