Write an object oriented program which can calculate the area of a circle with a
ID: 639051 • Letter: W
Question
Write an object oriented program which can calculate the area of a circle with a given radius value.
Description: Your program will have two classes.
Class 1: Circle Class
This class has a field named radius and two methods: (1) setRadius method sets the value of radius and (2) area method calculates and returns the area of the circle corresponding to the radius value.
Class 2: CircleDemo Class
This class has the main method which creates two circle objects:
Soccer Field with radius 100.
Cricket Field with radius 150.
The main function prints the area of Soccer Field and Cricket Field.
Explanation / Answer
//Circle.java that extends the GeometricObject
//The method compareTo that returns the greater of the two
//circle objects
public class Circle
{
//private filed for radius
private double radius;
//defualt constrctor
public Circle()
{
radius=0;
}
//The method setRadius accepts the radius of type double
public void setRadius(double radius)
{
this.radius=radius;
}
//The getArea metod returns the area of the circle
//Forumula area=PI * radius*radius
public double getArea()
{
return Math.PI*radius*radius;
}
@Override
public String toString()
{
return "Circle :Radius "+radius;
}
}
--------------------------------------------------------
/*The java class that tests the circle class
* with soccer field radius and circket stadium
* radius and print its areas
* */
//CircleDemo.java
public class CircleDemo
{
public static void main(String[] args)
{
//create an instance of Circle class
//and set the radius using setRadius method
Circle soccerField=new Circle();
soccerField.setRadius(150);
System.out.println(soccerField.toString());
System.out.println("Area : "+soccerField.getArea());
//create an instance of Circle class
//and set the radius using setRadius method
Circle cricketField=new Circle();
cricketField.setRadius(100);
System.out.println(cricketField.toString());
System.out.println("Area : "+cricketField.getArea());
}
}
------------------------
Sample output:
Circle :Radius 150.0
Area : 70685.83470577035
Circle :Radius 100.0
Area : 31415.926535897932
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.