This assignment is due Wednesday, December 16.
This assignment makes use of the files contained in this zip file.
In this assignment you will write the code for two classes, Rectangle.java
and Circle.java
. In the zip file there are definitions for two other classes, Point.java
and Dimension.java
, that will be used by the Rectangle
and Circle
classes. The Point
and Dimension
classes are complete; you do not need to make any changes to those two files.
The zip file contains an outline for each of Rectangle.java
and Circle.java
. You need to complete those two files. You will complete them in steps. Each step has a test file that tests whether you have completed that step correctly.
For the first step, in the file Rectangle.java
define the following constructor.
public Rectangle(Point p, Dimension d)
Then, in Rectangle.java
define the following methods.
public void moveTo(int x, int y) // move this rectangle's corner to (x,y) public void moveTo(Point p) // move this rectangle's corner to p public void moveBy(int deltaX, int deltaY) // move the corner by the given amounts public void growTo(Dimension d) // change width and height to match the dimension d public void growBy(int deltaW, int deltaH) // change the width and height by the given amounts public boolean equals(Rectangle r) // all the data in this rectangle equals all the data in r
After you define those methods, the file Test_Step_1.java
should compile and when you run Test_Step_1.java
you should get output that looks exactly like Step_1_output.txt
.
For the second step, in the file Circle.java
define the following constructors.
public Circle(double radius) // the center defaults to (0,0) public Circle(Point center, double radius) public Circle(int x, int y, double radius) // the center is (x,y)
Then, in Circle.java
define the following methods.
public void moveTo(Point p) // move this circle's center to p public void moveTo(int x, int y) // move this circle's center to (x,y) public void moveBy(int deltaX, int deltaY) // move the center by the given amounts public void growTo(double radius) // change the radius to the new value public void growBy(double deltaR) // change the radius by the given amount public boolean equals(Circle c) // all the data in this circle equals all the data in c
After you define those methods, the file Test_Step_2.java
should compile and when you run Test_Step_2.java
you should get output that looks exactly like Step_2_output.txt
.
For the third step, in the file Rectangle.java
define the following three methods.
public Point getCenter() // return a point that is at the center of the rectangle public boolean contains(int x, int y) public boolean contains(Point p)
Each contains()
method determines if the given point is within the boundary of this rectangle. Remember that the rectangle is determined by its upper left-hand corner along with its width and height. So, for example, for the given point to be within this rectangle's boundary, the given x
must be between this.x
and this.x + this.width
. You need to figure out what the condition for the given y
value should be. Draw a picture.
(Hint: Write one of the contains()
methods in terms of the other one.)
After you define those three methods, the file Test_Step_3.java
should compile and when you run Test_Step_3.java
you should get output that looks exactly like Step_3_output.txt
.
For the fourth step, in the file Circle.java
define the following three methods.
public boolean overlap(Circle c) public boolean centeredIn(Rectangle r) public boolean centeredOn(Rectangle r) // the center of this circle is equal to the center of the rectangle
The overlap()
method determines if two circles overlap. Two circles overlap if the distance between their centers is less than the sum of their radii. Notice that the Point
class has a method distance()
that will compute the distance between two points.
The centeredIn()
method determines if the center of this circle is contained inside of the given rectangle. Define centeredIn()
using the contains()
method from the Rectangle
class.
The centeredOn()
method determines if the center of this circle is equal to the center of the given rectangle. Define centeredOn()
using the getCenter()
method from the Rectangle
class and the equals()
method from the Point
class.
After you define those three methods, the file Test_Step_4.java
should compile and when you run Test_Step_4.java
you should get output that looks exactly like Step_4_output.txt
.
Turn in a zip file called CS123Hw4Surname.zip
(where Surname
is your last name) containing your versions of Rectangle.java
and Circle.java
.
This assignment is due Wednesday, December 16.