Homework 2
CS202, Summer 2004
Due: Thursday, July 8
Please email your solutions to csun@calstatela.edu,
and make sure to use CS202 Summer 04
HW2 as the subject of the email. The email should include your
name, and the .java files for all the exercises as attachments.
For all homework assignments and projects, failing to comply with the required
code conventions (see Review of
Language Basics, slide #7) will incur a
credit penalty up to 10% of the total credit for the assignment.
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) Get and set 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) [D&D] 11.24
5. (20pt) [D&D] 11.25