Write a class encapsulating the concept of a circle, assuming a circle has the f
ID: 3632667 • Letter: W
Question
Write a class encapsulating the concept of a circle, assuming a circle has the following attributes:
two doubles representing the x and y coordinates of the center of the circle
a double representing the radius of the circle
Include the following methods:
two constructors
accessor methods
mutator methods
toString and equals
methods returning the perimeter (2 * B * radius) and area (B * radius2) of the circle
Write a client class to test all the methods in your class
The no-argument constructor should initialize the circle to be a unit circle with center at the origin.
The explicit-value constructor ( the second one in the UML diagram ) should use the setRadius
method to initialize the radius.
The setRadius method will validate the newRadius and set the radius of the circle to newRadius if
newRadius >= 0, othersies it will set the radius of the circle to 0.
The toString method should return a String that is in the format shown in the example below.
This is my code so far, I'm pretty sure it's incorrect.
public class Circle {
main(String args) {
private double x;
private double y;
private double r;
private double B=Math.PI;
}
public Circle(double x, double y, double r) {
x=getCenterX;
y=getCenterY;
}
public double getCenterX() {
return x;
}
public double getCenterY() {
return y;
}
public double getR() {
return r;
}
}
Explanation / Answer
class Circle
{
private double x;
private double y;
private double r;
private double B=Math.PI;
public Circle(double getCenterX, double getCenterY, double radi)
{
x=getCenterX;
y=getCenterY;
r=radi;
}
public Circle(double rd)
{
setRadius(rd);
}
public void setRadius(double rd)
{
if(rd>=0)
r=rd;
else
r=0;
}
public double getCenterX()
{
return x;
}
public double getCenterY()
{
return y;
}
public double getR() {
return r;
}
public double getArea()
{
return B*r*r;
}
public double getPerimeter()
{
return 2*B*r;
}
public String toString()
{
return "Circle Point("+x+","+y+") Radius:"+r+" Area:"+getArea()+" Perimeter:"+getPerimeter();
}
}
public class TestCircle {
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
Circle c1=new Circle(4.2,7.5,6.5);
System.out.println(c1);
}
Circle
Point(4.2,7.5)
Radius:6.5
Area:132.73228961416876
Perimeter:40.840704496667314
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.