In: Computer Science
1: (Passing Objects to Methods) From the following UML Class
Diagram, define the Circle class in
Java.
Circle
Circle
Radius: double
Circle()
Circle(newRadius: double)
getArea(): double
setRadius(newRadius: double): void
getRadius(): double
After creating the Circle class, you should test this class from
the main() method by passing objects of this
class to a method “ public static void printAreas(Circle c, int
times)”
You should display the area of the circle object 5 times with
different radii.
Explanation:I have completed the code have also shown the output, please find the images attached with the answer.Please upvote if you liked my answer and comment if you need any modification or explanation
//code
public class Circle {
private double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius *
radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public static void printAreas(Circle c, int times)
{
for (int i = 0; i < times; i++)
{
System.out.println(c.getArea());
}
}
public static void main(String[] args) {
Circle c1 = new Circle(2.5);
Circle.printAreas(c1, 1);
Circle c2 = new Circle(1.5);
Circle.printAreas(c2, 1);
Circle c3 = new Circle(2.0);
Circle.printAreas(c3, 1);
Circle c4 = new Circle(1.8);
Circle.printAreas(c4, 1);
Circle c5 = new Circle(1.0);
Circle.printAreas(c5, 1);
}
}
Output: