Circle Assignment 2
Circle Assignment 2
Assignment 02
Create one java file using all the following classes. Save the file with proper file name.
1. Create a class called MyPoint, which models a 2D point with x and y coordinates. It
contains:
a. Two instance variables x (int) and y (int).
b. A "no-argument" constructor that construct a point at (0, 0).
c. A constructor that constructs a point with the given x and y coordinates.
d. Getter and setter for the instance variables x and y.
setX(int x): set the value of x coordinate by passing the value
setY(int y): set the value of y coordinate by passing the value
getX() : return the value of x coordinate
getY() : return the value of y coordinate
e. A method setXY() to set both x and y.
f. A toString() method that returns a string description of the instance in the format
"(x, y)".
g. A method called distance(int x, int y) that returns the distance as a double value from
this point to another point at the given (x, y) coordinates.
h. An overloaded distance(MyPoint p) that returns the distance as a double value from this
point to the given MyPoint instance p.
Distance of two points is calculates as given bellow.
distance of two points= √(𝒙𝟏 − 𝒙𝟐)𝟐 + (𝒚𝟏 − 𝒚𝟐)𝟐
2. Create a class called MyCircle, which models a circle with a center (x, y) and a radius. The
MyCircle class uses an instance of MyPoint class (created in the previous exercise) as its
center.
The class should contain:
a. Two private instance variables: center (an instance of MyPoint) and radius (int).
b. A constructor that constructs a circle with the given center's (x, y) coordinates and
radius.
c. An overloaded constructor that constructs a MyCircle by giving a MyPoint instance as
center, and radius.
d. Various getters and setters.
setCenter(MyPoint p): set the center coordinates by passing the MyPoint instance
as center.
getCenter(): return the coordinate of the center point as MyPoint
setRadius(int r): set the value of radius by passing the value
getRadius(): return the value of the radius
e. A toString() method that returns a string description of this instance in the format "Circle
@ (x, y) radius=r".
f. A getArea() method that returns the area of the circle in double.