In this assignment you will refine the Circle class in order to practice the fol
ID: 3729071 • Letter: I
Question
In this assignment you will refine the Circle class in order to practice the following advanced class writing techniques:
1)Aggregation
2)Multiple Constructors
3)Copy Constructors
4)The toString method
5)The equals method
The final goal is to create a class with the following design. Class uses doubles, instead of the floats in the first assignment. The new members and methods are listed in green.
Class Circle
Point center // This sentence is green
double radius
Circle(Point o, double r) //This sentence is green
Circle(double xValue, double yValue, double r) // green
Circle() //green
Circle(Circle c) //green
Point getCenter() //green
void setCenter(Point p) //green
void setX(double value)
double getX()
void setY(double value)
double getY()
void setRadius(double value)
double getRadius()
double getArea()
String toString() //green
boolean equals(Circle c) //green
boolean doesOverlap(Circle c)
Step 1: The Point class
Begin by downloading Point.java. Take some time to familiarize yourself with the class. It is a simple class for storing an x and y coordinate.
Add a method named distance that accepts a Point as an argument and return the distance between this argument, and the point that receives the invocation. It should have the following signature:
public double distance(Point other)
Step 2: Aggregation
In the previous circle class assignment the class was given three instance members: x, y, and radius. Create a new Circle class that uses has only two members: a member named center of type Point, and member radius of type double.
Rewrite the existing setters and getters for x and y so that they use the center member. Add setters and getters for the center, so that users of the class can access the point directly.
Rewrite the doesOverlap method so that it uses the center member.
Step 3: Constructors
Create four constructors. Each constructor is described below:
The two argument constructor. This constructor should accept a Point object and double. It should initialize the new objects center member so that it refers to a copy of the point argument, and the radius member to the double value.
The three argument constructor. This constructor should accept three double values, for x, y, and radius respectively. It should create a new Point object to store the x and y values, and store this Point object in the center member. It should store the third double value into the radius member. Note that you can use the two argument constructor to write the three argument constructor.
The no-argument constructor. This constructor should create a new Point object with an x and y coordinate of 0, and use this point object to initialize the center member. It should set the radius to 1.
The copy constructor. This constructor should accept a Circle as an argument. It should set the center member to a copy of the point argument’s center member. It should set the radius member to the point argument’s radius value;
Step 4: toString
Write a toString method for your Circle class. You format the string however you like, as long as it includes the objects x, y, and radius values.
Step 5: equals
Write an equals method that returns true if the argument Circle has the same x, y and radius values as the receiving circle.
What to Submit?
Submit all of the files necessary to compile your project. That is probably Circle.java, Point.java, and code that contains a main method that tests each of the new features. You can put your main method in the circle class, or in a separate tester class. The main method should construct circles using all four constructors. It should test the doesOverlap, toString, and equals methods.
Point center // This sentence is green
double radius
Circle(Point o, double r) //This sentence is green
Circle(double xValue, double yValue, double r) // green
Circle() //green
Circle(Circle c) //green
Point getCenter() //green
void setCenter(Point p) //green
void setX(double value)
double getX()
void setY(double value)
double getY()
void setRadius(double value)
double getRadius()
double getArea()
String toString() //green
boolean equals(Circle c) //green
boolean doesOverlap(Circle c)
Explanation / Answer
public class Point{
public double x,y; // the co-ordinates
public Point(Point o){
this.x = o.x;
this.y = o.y;
}
public Point (double x,double y){
this.x = x;
this.y = y;
}
public double distance(Point o){
return Math.sqrt((x-o.x)*(x-o.x) + (y-o.y)*(y-o.y));
}
}
public class Circle{
public Point center;
public double radius;
public Circle(Point o,double r){
center = o;
radius = r;
}
public Circle(double xValue, double yValue,double r){
center = new Point(xValue,yValue);
radius = r;
}
public Circle(){
center = new Point(0.0,0.0);
this.radius = 1;
}
public Circle(Circle c){
this.center= c.center;
this.radius = c.radius;
}
public Point getCenter(){
return center;
}
public void setCenter(Point p){
this.center = p;
}
public void setX(double value){
this.center.x = value;
}
public void setY(double value){
this.center.y = value;
}
public double getX(){
return center.x;
}
public double getY(){
return center.y;
}
public void setRadius(double value){
this.radius = value;
}
public double getRadius(){
return radius;
}
public double getArea(){
return 2*Math.PI*radius*radius;
}
public String toString(){
return "XCo-ordinate: " + center.x + " YCo-ordinate: " + center.y + " Radius: " + radius ;
}
public boolean equals(Circle c){
if(this.center.x == c.getX() && this.center.y == c.getY() && this.radius == c.getRadius())
return true;
else return false;
}
public boolean doesOverlap(Circle c)
{
double distance = center.distance(c.center);
if(distance > (c.radius+this.radius))
return false;
else return true;
}
public static void main(String[] args)
{
Circle c= new Circle(1.0,2.0,3.0);
Point p= new Point(1.0,2.0);
Circle c1= new Circle(p,3.0);
System.out.println(c.equals(c1));
Circle c2= new Circle();
Circle c3= new Circle(c1);
System.out.println(c.equals(c2));
System.out.println(c2.toString());
System.out.println(c3.equals(c1));
System.out.println(c3.doesOverlap(c1));
}
}
Output::
java Circle
true
false
XCo-ordinate: 0.0 YCo-ordinate: 0.0 Radius: 1.0
true
true