The objective of this assignment is to write classes and create objects.
Your task is to convert your code from assignment 1 into an object oriented program by writing your own class called Rectangle.
This class will have all the methods that you have written in assignment 1.
As in the previous assignment a rectangle is represented by the coordinates of its upper left-hand corner and its bottom right-hand corner (see
Assignment 1 for details).
It is important to know the definition of a
valid rectangle.
Valid rectangle is defined as the one where lower right coordinates are larger than the upper left coordinates. This means if we have a rectangle with the coordinates
(upper_x,upper_y,lower_x,lower_y),
then the coordinate,
lower_x, should always be greater than the coordinate,
upper_x and the coordinate,
lower_y, should be greater than the coordinate
upper_y.
Coordinates. The coordinates of a valid rectangle should be between 0 and 500.
For this assignment this means the
lower_x and the
lower_y should be less than
500, which is the upper limit of the rectangle size and
upper_x and
upper_y should not be less than 0.
The
Rectangle class that you are supposed to write should have the following methods:
- public Rectangle(int upper_x, int upper_y, int lower_x, int lower_y)
The constructor for the Rectangle class. Its parameters are the x and y
coordinates of the upper left and the bottom right corners. Run the following checks for
each rectangle.
-
Check the validity of the input rectangle.
If the rectangle is not valid, your constructor should raise an IllegalArgumentException. See the java documentation for details.
In your main you can check that this works correctly by constructing a rectangle within a try/catch block.
-
Check the coordinates of a valid input rectangle.
If the lower_x and the lower_y are larger than 500, then clip the
extra dimensions to restrict it to be 500 before saving the rectangle as an object
of class rectangle in the rectangles array. For e.g., if the input coordinates are
50,50,550,525,
before saving this rectangle make sure that lower_x and lower_y are
restricted to 500. This means you will be saving the above rectangle coordinates as
50,50,500,500,
In case top_x and top_y are less than 0, clip them to be 0.
- public Rectangle(Rectangle other)
This constructor is a copy constructor - it creates a rectangle whose coordinates are the same as the given rectangle.
- public boolean overlap(Rectangle other)
This method checks whether the instance of Rectangle overlaps the other instance of Rectangle provided as argument to the method.
- public boolean containedIn(Rectangle other)
This method checks whether the instance of Rectangle is contained in the other instance of the class.
- public boolean drag(int x,int y)
Moves center of the current instance.
This method modifies the coordinates of the rectangle such that it is centered at coordinates (x,y) without changing its size and returns true
if the rectangle is
valid and satifies the coordinate restrictions. Otherwise, if the
rectangle is invalid and/or coordinates are not valid after the drag, this method returns
false and does not save any modifications.
- public boolean resize(int x,int y)
This method modifies the coordinates of the rectangle such that its lower right corner is
at coordinates (x,y) and returns true if the new rectangle after resize is
valid and satifies the coordinate restrictions. Otherwise, if the
rectangle is invalid and/or coordinates are not valid after the resize, this method
returns false and does not save the modifications.
- public String toString()
This method returns a string containing the instance variables of an object. You can use
it to print an object.
- main
Your main should exercise your Rectangle class. Here's the sort of code you want to have there:
Rectangle rectangle1 = new Rectangle(x1, y1, x2, y2); // you'll need to put in numbers or declare the coordinate variables
Rectangle rectangle2 = new Rectangle(x3, y3, x4, y4);
System.out.println("rectangle1.overlap(rectangle2): " + rectangle1.overlap(rectangle2));
System.out.println("rectangle1.containedIn(rectangle2): " + rectangle1.containedIn(rectangle2));
// and some code that tries out the other methods
In order to display your rectangles you'll need to create an array of Rectangle objects. For example, to create an array containing the two rectangles you already instantiated:
Rectangle [] rectangles = new Rectangle[2];
rectangles[0] = rectangle1;
rectangles[1] = rectangle2;
//now you can call the display code:
new showRecs(rectangles);
// If you are not satisfied with the colors of the rectangles, drag the right lower corner of the window using your mouse or run the code again and different colors will show up.
The code for displaying the rectangles is provided in
Rectangle.java, which you should use as a starting point. All your code should be written within the class Rectangle.
Note that once you have created the array of rectangles you can also invoke the overlap method as:
rectangles[i].overlap(rectangles[j]), and similarly for other methods.
Note: In order to visualize your rectangles using showRecs(), you will have
to write the following four getters:
public int getUpperX()
public int getUpperY()
public int getLowerX()
public int getLowerY()
Submitting Your Assignment
Submit your Rectangle.java using the
checkin program as PA2.
Note that your code should compile on department Linux machines, and make sure that you are submitting the source code rather than the Java class file.