In this assignment you will write a class that stores and manages a collection of
Rectangle objects using an ArrayList.
You will need to turn in two Java files, one containing your Rectangle class, and the second containing
a class called
RectangleCollection.
To hold the rectangles your RectangleCollection should have a private instance variable of type ArrayList that holds objects of class Rectangle. The
RectangleCollection class that you are supposed to write needs have the following methods:
- public RectangleCollection(String fileName)
The constructor for the RectangleCollection class. Its parameter is a filename that has the
coordinates of the rectangles in the same format as in assignment 1 (here's an
example file).
- public RectangleCollection()
This constructor creates an empty RectangleCollection.
- public boolean add(Rectangle rectangle)
This method adds a rectangle to the collection by adding it to the end of list. The collection should not contain duplicates.
Returns true if the rectangle is added, false otherwise. If you are trying to add a null or a duplicate entry this method returns false for e.g., If
Rectangle r = null;
RectangleCollection rc = new RectangleCollection();
rc.add(r) should return false.
- public Rectangle get(int i)
Returns the Rectangle at index i in the collection. This method throws IndexOutOfBoundsException - if the index is out of range (i < 0 || i >= size())
- public boolean addAll(RectangleCollection other)
This method adds the rectangles stored in the other RectangleCollection. It should only add those rectangles that are not contained in the collection.
If adding was successful, return true, otherwise (if the other collection is null), return false.
- public boolean contains(Rectangle rectangle)
This method checks whether the collection of rectangles contains the given instance of
Rectangle. It returns true if the collection contains rectangle and false otherwise.
- public RectangleCollection intersect(RectangleCollection other)
This method returns a RectangleContainer that stores the Rectangle objects that are common to both lists.
In writing this method use the constructor that constructs an empty Rectangle, and the add method to iteratively populate that new instance that you created.
- public boolean equals(Object object)
returns true if object is a RectangleCollection that contains the same rectangles.
- main
Your main should exercise your Rectangle and RectangleCollection classes. 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);
RectangleCollection rc = new RectangleCollection();
rc.add(rectangle1);
rc.add(rectangle2);
System.out.println(rc);
// and some code that tries out the other methods
// alternatively construct a RectangleCollection from file:
RectangleCollection rc2 = new RectangleCollection(fileName);
RectangleCollection rc3 = rc.intersect(rc2);
In order to display your rectangles pass your instance of RectangleCollection to the showRecList constructor:
new showRecList(rc);
// 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
RectangleCollection.java, which you should use as a starting point. All your code should be written within the class RectangleCollection.
Note:
Most of the methods are easy to implement if you use the full functionality of the underlying ArrayList. Several of them depend on a correct implementation of an equals method in your Rectangle class!
Submitting Your Assignment
Submit your Rectangle.java and RectangleCollection.java in a tarfile
using the
checkin program as pa3.tar
You will learn the procedure to create a tar file in your recitation
Here are some of the commands that you will have to run in order to create
pa3.tar file.
## Create a directory called PA3 using the command
mkdir PA3
## It will create PA3 under the current directory. You can get the current directory by using the command
pwd
## Suppose your Rectangle.java and RectangleCollection.java are in the directory /s/bach/a/john, copy those files to PA3 folder using
cp /s/bach/a/john/Rectangle.java PA3/
cp /s/bach/a/john/RectangleCollection.java PA3/
## Now run the following tar command to create a tar file
tar -cvf pa3.tar PA3
Now you are ready to submit
pa3.tar using
checkin
program.
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.