You are on page 1of 5

May 21, 2021

For this assignment you will need to create a class and the client code (Main.java) to test your class.

Important: Stick to using the names described in this assignment for all methods. You are welcome to
design and write additional code to help you accomplish this task, but you must hide it using
encapsulation (i.e., private fields and methods).

Instructions:

Suppose that rectangles in a Cartesian plane are represented by a class Rectangle that has four private
fields (file name should be Rectangle.java):

 Integer left – the x-coordinate of the edge


 Integer bottom – the y-coordinate of the bottom edge
 Integer width – the width of the rectangle
 Integer height – the height of the rectangle

1. Define the class.

2. Write a constructor method called Rectangle that has no parameters, which sets all fields to
zero.

3. Write a second constructor method that has four integer parameters, which sets all fields of the
class (in the order listed above). The constructor should replace any negative length parameters
with zero (e.g. width & height). Here is the what the invocation (call) looks like:

Rectangle r = new Rectangle(left, bottom, width, height);

4. Write a third constructor method called Rectangle that has a Rectangle parameter, which sets all
fields of the new Rectangle equal to the other. Ensure they are not linked objects.

5. Write a mutator method called set that has four integer parameters representing the four fields of
the class (in the order listed above). The mutator method should replace any negative length
parameters with zero. Note: coordinates can still be negative.
Rectangle r = new Rectangle(); //uninitialized
r.set(left, bottom, width, height);

6. Write a toString instance method. For the rectangle with a lower left corner located at (-3,2) and
having a width of 4 and a height of 5, the method should return exactly
“base: (-3,2) w: 4 h: 5”. (Note: The bolded values are samples only)
May 21, 2021

7. Write an instance method, area, that returns the integer area of the calling rectangle. Have it
return zero for lines and points.

8. Write an instance method, perimeter, that returns the perimeter of the calling rectangle. Ensure
that the perimeter of any straight line is the line’s length (not twice its length).

9. Write an instance method, contains, that has one parameter of type Rectangle. The method
should return true if every point of the specified rectangle (i.e. passed by the explicit parameter) is
within the implicit parameter (i.e. the object invoking the instance method). It should return false
otherwise.

For example, a.contains(b) would return true if the rectangle b is entirely within rectangle a.

10. Write a class method, intersection, that has two Rectangle parameters. The method should
return the rectangle formed by the area common to the two rectangles (i.e. the rectangle formed
where they overlap with each other – see diagram below).

a. If they do not intersect, the method should return a Rectangle where all the fields are
set to zero.
b. If the rectangles only touch, but do not overlap, then the width or height may be zero,
but all other parameters should be properly calculated and stored.

Rectangle A Intersection Rectangle C

Rectangle B

Feel free to use this resource to help you visualize other scenarios. ALL should work:
https://www.khanacademy.org/computer-programming/rectx-y-width-height-radius/839496660

11. For Part 10, create a separate file that displays pictorially all the tests you conducted to test your
intersection method. (Include coordinates and diagrams for each unique test case). You may
May 21, 2021

decide to use Desmos or other graphing software to show these test cases. There should be a
clear organization to your file. Make a PDF and upload into your Replit
Name this file TestCases.pdf

12. Write a class method, totalPerimeter, that has two Rectangle parameters. The method
should return the total perimeter, as an integer, of the figure formed by the two rectangles. It
should only count those portions that are on the edges of the exterior of the resulting figure. If one
rectangle is completely contained by the other, then return the perimeter of the outer rectangle. If
the rectangles do not intersect, the method should return the sum of the individual perimeters.

Proper coding procedures, including documentation, doc comments, and meaningful variable names are
expected.

13. Level 4+ (up to 100%) - All of the above but also including a proper client code with these additional
features:

 Have the testing program prompt a user for each of the four fields for two rectangles and then
make use of ALL of the class and instance methods. Have this output to the console in an
organized fashion.
 As well, output the following specific outputs to a named text file called Rectangles.txt
(see below)
 Have the information from the rectangle be organized and outputted with labels formatted
exactly as follows: (Note: Bolded values are samples only)
 Prevent in your Client Code any/all compiler and run-time errors to the testing program
 Have a public instance method called toFile to accomplish this part

Sample output text file: Place two tabs \t\t between

Rectangle Class: separate quantities


X coordinate Rectangle A: -50 Y coordinate Rectangle A: 100
Width A: 100 Height A: 150
Area A: 15000 px^2 Perimeter A: 500 px
X coordinate Rectangle B: 0 Y coordinate Rectangle B: 50
Width B: 100 Height B: 100
Area B: 10000 px^2 Perimeter B: 400 px
Intersection Rectangle of Rect. A & Rect. B: base: (0,100) w: 50 h: 50
Total Perimeter for Both: ________ px

(Value not shown here purposely. Figure it out.)


May 21, 2021

*BONUS* – Not for anything but Fun if you have a PC. - *You do not need to do this for any portion of
the grade.

Working in Processing.org

 Use processing to have your program become a visualized program. Have two rectangles start
at opposite sides of the screen and move to their final resting places.

Choice 1: Involve the text file created in part 13. To be used as the input for processing. You will need to
review documentation on the BufferedReader in processing’s reference pages.

OR

Choice 2: Allow user to click and drag the outline of two rectangles to be used as the input for
processing.

 Have the rectangles start off at the outside and move towards their final resting places with a
smooth transition.
 Involve different colours as part of your rectangle class (new data fields – one for the
stroke() and one for the fill().
 Once the two rectangles have reached their final resting place, if they intersect, have this cause
some type of reaction involving the newly created intersected rectangle.
 Be creative! (i.e. Have the new rectangle pop or explode or move around)
May 21, 2021

Grading Scheme:

Do Parts in order. Recognize no attempt at parts 11 & 12 guarantees less than 80%

Part I R 1 2 3 4 4+
1
2
3
4
5
6
7
8
9
10
11
12
13

You might also like