Homework 2
CS202, Summer 2006
Due: Tuesday, July 18
Please upload your source code files using CSNS. Note
that file
uploading will be disabled automatically after 11:59PM
of the due date, so please turn in your work on time.
1. Write a Point class which has two private double
fields x and y, which correspond to
the x and y coordinates of a point. Include the following methods in
the class:
- (5pt) A constructor which takes no parameters, and set both x
and
y to zero.
- (5pt) A constructor which takes two parameters, and set x
and y
to the given values.
- (5pt) A constructor which takes a Point object as
parameter, and
set x, and y to the coordinates of the given point.
- (10pt) Getter and setter methods for both x and y. For
example, getX
returns the x coordinate, setX sets the x coordinate to a given
value.
- (10pt) A method distance which returns the distance to a
given
point. Note that this method should take only one parameter, e.g. Point
p, not two parameters.
2. Re-use your Point class and write a new class Rectangle.
Rectangle
has two Point fields, lowerLeft and upperRight,
which correspond to the
two corner points of a rectangle. Include the following methods in the
class:
- (5pt) A constructor which takes no parameters. By default the
rectangle has two corner points (0,0) and (0,0). Note that in this case
the rectangle degenerates to a point, but that's fine.
- (5pt) A constructor which takes two Point parameters.
- (20pt) Methods width, height, perimeter,
and area, which return
the width, height, perimeter, and area of the rectangle, respectively.
- (15pt) A method isOverlapping, which checks if a
rectangle
overlaps with a given rectangle. Note that overlapping includes the
case where one rectangle is completely inside another rectangle.
3. (10pt) Write a GeometryTest class which only has a main
method. Inside the main method, create a few Point and Rectangle
objects, and properly test all methods of these two
classes, including all the constructors.
4. (20pt) [Textbook] 29.21
5. (+30pt) [Textbook] 29.22
HINT: to avoid long switch statement, you may consider using arrays. For example, create an array
String ones[] = { "", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN" }
then the string representation of a single digit number n is simply ones[n].