In: Computer Science
public class Point
{
protected double x, y; // coordinates of the Point
//Default constructor
public Point()
{
setPoint( 0, 0 );
}
//Constructor with parameters
public Point(double xValue, double yValue )
{
setPoint(xValue, yValue );
}
// set x and y coordinates of Point
public void setPoint(double xValue, double yValue )
{
x = xValue;
y = yValue;
}
// get x coordinate
public double getX()
{
return x;
}
// get y coordinate
public double getY()
{
return y;
}
// convert point into String representation
public String toString()
{
return "[" + String.format("%.2f", x)
+ ", " + String.format("%.2f", y) + "]";
}
//Method to compare two points
public boolean equals(Point otherPoint)
{
return(x == otherPoint.x &&
y == otherPoint.y);
}
//Method to compare two points
public void makeCopy(Point otherPoint)
{
x = otherPoint.x;
y = otherPoint.y;
}
public Point getCopy()
{
Point temp = new Point();
temp.x = x;
temp.y = y;
return temp;
}
// print method
public void printPoint()
{
System.out.print("[" + String.format("%.2f", x)
+ ", " + String.format("%.2f", y) + "]");
}
} // end class Point
**********************************************************************************************************************************************************************
Every circle has a center and radius. Given the radius, we can determine the circle’s area and circumference. Given the center, we can determine its position in the x-y plane. The center of a circle is a point in the x-y plane. Please do the following:
//Java code
public class Point
{
protected double x, y; // coordinates of the Point
//Default constructor
public Point()
{
setPoint( 0, 0 );
}
//Constructor with parameters
public Point(double xValue, double yValue )
{
setPoint(xValue, yValue );
}
// set x and y coordinates of Point
public void setPoint(double xValue, double yValue )
{
x = xValue;
y = yValue;
}
// get x coordinate
public double getX()
{
return x;
}
// get y coordinate
public double getY()
{
return y;
}
// convert point into String representation
public String toString()
{
return "[" + String.format("%.2f", x)
+ ", " + String.format("%.2f", y) + "]";
}
//Method to compare two points
public boolean equals(Point otherPoint)
{
return(x == otherPoint.x &&
y == otherPoint.y);
}
//Method to compare two points
public void makeCopy(Point otherPoint)
{
x = otherPoint.x;
y = otherPoint.y;
}
public Point getCopy()
{
Point temp = new Point();
temp.x = x;
temp.y = y;
return temp;
}
// print method
public void printPoint()
{
System.out.print("[" + String.format("%.2f", x)
+ ", " + String.format("%.2f", y) + "]");
}
} // end class Point
//=====================================
public class Circle {
private double radius;
private Point center;
//Constructor
public Circle(double radius, Point center) {
this.radius = radius;
this.center = center;
}
//Default Constructor
public Circle()
{
radius = 0;
center = new Point();
}
//getters and setters
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public Point getCenter() {
return center;
}
public void setCenter(Point center) {
this.center = center;
}
//Calculate Area
public double getArea()
{
return Math.PI*radius*radius;
}
public double getCircumference()
{
return 2* Math.PI* radius;
}
public double getDistance()
{
return Math.sqrt((center.x*center.x) +(center.y*center.y));
}
@Override
public String toString() {
return "Center: "+center+" radius: "+radius;
}
}
//============================================
public class TestCircle {
public static void main(String[] args)
{
Circle circle = new Circle(10,new Point(50,60));
System.out.println(circle);
System.out.println("Area: "+circle.getArea());
System.out.println("Circumference: "+circle.getCircumference());
System.out.println("Distance from origin: "+circle.getDistance());
System.out.println("Done....");
}
}
//Output

//If you need any help regarding this solution ............ please leave a comment ..... thanks