Java Circle Class Original Circle Class program for referen
Java Circle Class
------------------------------------------------------------------------------------------------------------------------------
Original Circle Class program for reference:
Solution
public class Point {
double x;
double y;
public Point() {
// TODO Auto-generated constructor stub
this.x = 0;
this.y = 0;
}
/**
* @param x
* @param y
*/
public Point(double x, double y) {
this.x = x;
this.y = y;
}
/**
* @return the x
*/
public double getX() {
return x;
}
/**
* @param x
* the x to set
*/
public void setX(double x) {
this.x = x;
}
/**
* @return the y
*/
public double getY() {
return y;
}
/**
* @param y
* the y to set
*/
public void setY(double y) {
this.y = y;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return \"(\" + x + \", \" + y + \")\";
}
}
public class Circle {
Point center;
double radius;
/**
* @param center
* @param radius
*/
public Circle(Point center, double radius) {
this.center = center;
this.radius = radius;
}
/**
* @param x
* @param y
* @param radius
*/
public Circle(double x, double y, double radius) {
this.center = new Point(x, y);
this.radius = radius;
}
/**
*
*/
public Circle() {
// TODO Auto-generated constructor stub
this.center = new Point();
this.radius = 1;
}
/**
* @param c
*/
public Circle(Circle c) {
// TODO Auto-generated constructor stub
this(c.getCenter(), c.getRadius());
}
/**
* @return the center
*/
public Point getCenter() {
return center;
}
/**
* @param center
* the center to set
*/
public void setCenter(Point center) {
this.center = center;
}
/**
* @return the x
*/
public double getX() {
return this.center.getX();
}
/**
* @param x
* the x to set
*/
public void setX(double x) {
this.center.setX(x);
}
/**
* @return the y
*/
public double getY() {
return this.center.getY();
}
/**
* @param y
* the y to set
*/
public void setY(double y) {
this.center.setX(y);
}
/**
* @return the radius
*/
public double getRadius() {
return radius;
}
/**
* @param radius
* the radius to set
*/
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return \"Circle [center=\" + center + \", radius=\" + radius + \"]\";
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((center == null) ? 0 : center.hashCode());
long temp;
temp = Double.doubleToLongBits(radius);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Circle other = (Circle) obj;
if (center == null) {
if (other.center != null)
return false;
} else if (!center.equals(other.center))
return false;
if (Double.doubleToLongBits(radius) != Double
.doubleToLongBits(other.radius))
return false;
return true;
}
/**
* @param c
* @return
*/
public boolean doesOverlap(Circle c) {
double distance = Math.pow(
(this.getX() - c.getX()) * (this.getX() - c.getX())
+ (this.getY() - c.getY()) * (this.getY() - c.getY()),
0.5);
if (distance > (c.getRadius() + this.getRadius()))
return true;
else
return false;
}
}
public class MyCircleTester {
public static void main(String[] args) {
Circle circle1 = new Circle();
Circle circle2 = new Circle(1, 2, 5);
Circle circle3 = new Circle(new Point(1, 3), 4);
System.out.println(\"Circle 1:\" + circle1);
System.out.println(\"Circle 2:\" + circle2);
System.out.println(\"Circle 3:\" + circle3);
System.out.println(\"Circle 1 Area : \" + circle1.getArea());
System.out.println(\"Circle 2 Area : \" + circle2.getArea());
System.out.println(\"Circle 3 Area : \" + circle3.getArea());
if (circle1.doesOverlap(circle2))
System.out.println(\"circle 1 and circle 2 are overlap\");
else
System.out.println(\"circle 1 and circle 2 are not overlap\");
}
}
OUTPUT:
Circle 1:Circle [center=(0.0, 0.0), radius=1.0]
Circle 2:Circle [center=(1.0, 2.0), radius=5.0]
Circle 3:Circle [center=(1.0, 3.0), radius=4.0]
Circle 1 Area : 3.141592653589793
Circle 2 Area : 78.53981633974483
Circle 3 Area : 50.26548245743669
circle 1 and circle 2 are not overlap




