Consider a class that represents a circle. An object of this class will have the
ID: 3675099 • Letter: C
Question
Consider a class that represents a circle. An object of this class will have the following attributes:
• Color
• Radius
and the following methods:
• display() which will print the color and the radius
• computeArea() to compute the area of the circle
• computeCircumference() to compute the circumference of the circle Implement this class in Java.
Now download the attached class that tests your implementation. When you run the provided class, you should see the following output: I am a circle my color is green my radius is 10.0 circle area: 314.159226 circle circumference: 62.8318452
1 package lab7;
2
3 public class CircleDriver {
4
5 public static void main(String[] args) {
6
7 Circle aCircle = new Circle();
8
9 aCircle.setColor("green");
10 aCircle.setRadius(10);
11 aCircle.display();
12
13 Double circleArea = aCircle.computeArea();
14 Double circumference = aCircle.computeCircumference();
15
16 System.out.println("circle area: " + circleArea);
17 System.out.println("circle circumference: " + circumference);
18 System.out.println();
19 }2021 }
Explanation / Answer
//Circle Class
public class Circle {
private String color;
private double radius;
private static double PI = 3.14;
//default constructor
public Circle(){
color="";
radius=0;
}
//parameterized constructor
public Circle(String color, double radius) {
this.color = color;
this.radius = radius;
}
//getters and setters
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
//aread computation
public double computeArea(){
return PI*radius*radius;
}
//circumference computation
public double computeCircumference(){
return 2*PI*radius;
}
@Override
public String toString() {
return " I am a circle my color is "+getColor()+" my radius is "+getRadius()
+ " circle area: "+computeArea()+" circle circumference: "+computeCircumference();
}
}
//CircleDriver class
public class CircleDriver {
public static void main(String[] args) {
Circle aCircle = new Circle();
aCircle.setColor("green");
aCircle.setRadius(12);
aCircle.display();
Double circleArea = aCircle.computeArea();
Double circumference = aCircle.computeCircumference();
System.out.println("circle area: " + circleArea);
System.out.println("circle circumference: " + circumference);
System.out.println();
}
}
/*
Output:
I am a circle my color is green my radius is 12.0 circle area: 452.15999999999997 circle circumference: 75.36
circle area: 452.15999999999997
circle circumference: 75.36
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.