In: Computer Science
Write a java class called circle that represents a circle. It
should have following three fields:
int x (x-coordinate), int y (y-coordinate), double radius (radius
of the circle). Your circle object should have following
methods:
public int getRadius ()
public int getX ()
public int getY ()
public double area ()
public double perimeter()
public String toString()
write a client program called CircleClient that creates objects of
the circle class called c1 and c2. Assign values to the fields when
you create these objects using the constructor. Print out these
circle object using toString method.
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Below java classes are created.
circle.java :
//Java class
public class circle {
// fields
private int x;
private int y;
private double radius;
//constructor
public circle(int x,int y,double radius)
{
this.x=x;
this.y=y;
this.radius=radius;
}
// method to return radius
public double getRadius() {
return this.radius;// return
radius
}
// method to return x -coordinate
public int getX() {
return this.x;// return x
}
// method to return y -coordinate
public int getY() {
return this.y;// return y
}
//method to calculate area of circle
public double area ()
{
//return area of circle
return
Math.PI*getRadius()*getRadius();
}
//method to return perimeter
public double perimeter()
{
//return perimeter
return 2*Math.PI*getRadius();
}
//toString() method
public String toString()
{
//return area of circle and
perimeter
return "Area :
"+String.format("%.2f", this.area())+", Perimeter
:"+String.format("%.2f",this.perimeter());
}
}
****************************
CircleClient.java :
//Java class
public class CircleClient {
//entry point of program , main method
public static void main(String[] args) {
//creating object of circle
class
circle c1=new circle (5,6,7);
circle c2=new circle (7,8,9);
System.out.println(c1.toString());//print c1 details
System.out.println(c2.toString());//print c2 details
}
}
======================================================
Output : Compile and Run CircleClient.java to get the screen as shown below
Screen 1 :CircleClient.java
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.