Program 1 Write a class called Circle which has two constructors. The first cons
ID: 3912589 • Letter: P
Question
Program 1 Write a class called Circle which has two constructors. The first constructor (default constructor) does not take any parameter and supplies default values for the coordinates and the radius. The second constructor takes three doubles as parameters corresponding to the X and Y coordinates and the radius. The class must include these methods: public double circumference) returns the circumference of the circle. public double area) returns the area of the circle . public void setRadíus(double r) is called in the constructor and checks the radius against a maximum·lf the radius is greater than the maximurn, setRadius resets it to the maximum (using the ternary conditional operator). You may set your own maximum value public void printAttributes) prints the coordinates, the radius, the circumference, and the area. public boolean isinside(double x, double y) return true if a point represented in the parameters falls inside the circle, false otherwise. . public void move(int x, inty) moves the origin by a specified amount.Explanation / Answer
public class circle {
double x,y,radius,circum,area; //Declare coordinates,radius,circumfarence,area
double defx=0,defy=0,defr=25,pi=(22/7),maxr=100; //Default coordinates,radius,maxradius and pi value
public circle()
{
x=defx; //Assigning the default values to coordinates and radius
y=defy;
radius=defr;
setRadius(radius);
}
public circle(double x,double y,double z)
{
this.x=x; //Assigning the argument values coordinates and radius
this.y=y;
this.radius=z;
setRadius(radius);
}
public double circumfarence()
{
circum=(2*pi*radius); //Formula for circumference
return circum;
}
public double area()
{
area=(pi*radius*radius); //Formula for area
return area;
}
public void setRadius(double r)
{
int result = r>maxr? 1: 0; //Teritiary operator for checking radius greater than maxradius
if(result==1)
{
r=maxr; //Assign radius to maxr
}
}
public void printAttributes()
{ //Printing all the attributes
System.out.println("The coordinates are ("+x+","+y+")");
System.out.println("The radius is "+radius);
System.out.println("The circumfarence is "+circum);
System.out.println("The area is "+area);
}
public boolean isInside(double x, double y)
{
double d= Math.sqrt(Math.pow((this.x-x), 2)+Math.pow((this.y-y),2)); //Distance between center and point
if(d<radius)
return true; //point is inside
else
return false; //point is outside
}
public void move (int x, int y)
{
this.x=this.x+x; // Adding x coordinate with given x
this.y=this.y+y; // Adding y coordinate with given y
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.