Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

ITE 1101 - Fundamentals of Programming

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.

3. Create a public class called TestMyCircle and do the followings


a. Create point p1 using no argument constructor.
b. Set the X,Y coordinates
c. Create point p2 using two arguments constructor.
d. Display the distance between p1 and p2 points.
e. Display the distance between p1 and given point by passing the x, y coordinates of that
point.
f. Create a circle c1 using two arguments constructor. Pass the point p1 as the center.
g. Display the area of the circle.
h. Create a circle c2 using three arguments constructor.
i. Display the area of the circle c2.

You might also like