In: Computer Science
Problem 3.2 (Please download Point.java to complete this problem)
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:
Here is the Point.Java program:
// class Point 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
// class Point
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
class Circle
{
private double radius;
private Point p;
public Circle(Point p, double radius)
{
this.p = p;
this.radius = radius;
}
public void setRadius(double radius)
{
this.radius = radius;
}
public double getRadius()
{
return radius;
}
public double getArea()
{
return 3.14*radius*radius;
}
public double getCircumference()
{
return 2*3.14*radius;
}
public void printCircle()
{
System.out.print("Center :");
p.printPoint();
System.out.println(" Radius : "+radius +" Area : "+getArea() +"
Circumference : "+getCircumference());
}
}
class Test
{
public static void main (String[] args)
{
Point center = new Point(5.5,
4.7);
Circle c = new Circle(center,
10.00);
c.printCircle();
}
}
Output:
Center :[5.50, 4.70] Radius : 10.0 Area : 314.0 Circumference : 62.800000000000004
Do ask if any doubt. Please up-vote.