Homework 4
(revised)
CS409 - Spring 2000
Due: Thursday, Feb 24
Revisions:
- The method toString() has been added to Rectangle. You
need to write this method to ensure that your Rectangle can be printed.
- A correction has been made in the code for the IllegalArgumentException
below (the word "new" was omitted in the earlier version).
- The explanation of contains() has been expanded to indicate
what should be done with points on the Rectangle's boundary.
- The explanation of intersect() has been expanded to indicate
what should be done when Rectangles don't intersect.
- Some useful Java information has been added and some Java information that
applied only to a previous version of J++ has been omitted.
This is your first assignment that involves programming. It is meant to be
a moderately easy in order to give everyone a chance to try out Java. Even
though this assignment is not difficult you should start early to ensure that
you have enough time to complete it. Programming, especially when using an
unfamiliar language, can be time-consuming.
You need to create a class that implements the ADT Rectangle. Rectangle is
actually a hyper-rectangle since it is supposed to work for any dimension.
Here are the operations, in Java form, that Rectangle needs to handle:
- public Rectangle (int[] lowerCorner, int[] upperCorner);
Construct a new Rectangle. It is an error if the corners are different
dimensions. Also each coordinate of the lower corner is supposed to be
less than or equal to the corresponding coordinate of the upper corner; it
is an error if this does not hold.
- public int dimension();
Report this Rectangle's dimension.
- public boolean contains (int[] point);
Determine whether the given point is contained within this Rectangle.
It is an error if the point is the wrong dimension. Points on the
boundary are considered to be contained within the Rectangle.
- public Rectangle intersect (Rectangle rect);
Report the Rectangle that is the intersection of this Rectangle and rect.
If the rectangles don't intersect then null should be returned.
- public String toString ();
Report a String representation of this Rectangle. Any kind of
reasonable and useful representation is acceptable.
Java has an extensive set of Exceptions that can be used to handle various
kinds of errors. The errors mentioned above fall into the category of
IllegalArgumentException. If such an error occurs then the following java
code can be used:
throw new IllegalArgumentException("Useful info about problem");
What to turn in: We want to see two things:
- The file containing your implementation of class Rectangle.
- The output from a test program that uses your class. This test
program is not yet online, but when it is there will be a link to it on the
HW04 Web page.
Some Useful Java Information
- Printing (to standard output) is done using System.out.println(String).
- You can see the Output window by choosing Output in the Other Windows
submenu of the View menu. You can print the contents of the Output
window by clicking on it on and then selecting Print... from the File menu.
- Strings can be concatenated by using + (i.e., "a" + "bc"
is the same as "abc"). An int that appears where a String is
expected is automatically converted to the appropriate String. Strings
are immutable (i.e., once created they can't be changed). If you want
to add to an existing string (as you might want to do in toString(),
for instance), you should use StringBuffer. StringBuffer's append()
method lets you append to an existing StringBuffer.
- A Rectangle with lower corner (0,0) and upper corner (2,1) can be created
with the following code:
new Rectangle( new int[] {0,0}, new int[] {2,1});
Note the use of (unnamed) integer arrays.