In: Computer Science
Language: Java
Question:Using your Circle class (or the one provided below), create a Circle array of size 4 in a driver class using the following statement: Circle circleArr[] = new Circle[4]; Populate the array with four different radiuses and then, using a for loop from 0 to one less then the length of the array, print the area and the diameter of each of the four circles
Circle Class:
import java.text.DecimalFormat;
public class Circle {
DecimalFormat dec = new DecimalFormat("#.##");
private int radius;
public Circle(int radius) {
this.radius = radius;
}
public void setRadius(int r) {
radius = r;
}
public int getRadius() {
return radius;
}
public int getDiameter() {
return radius*radius;
}
public double getArea() {
return Math.PI*radius*radius;
}
public String toString() {
String result = "A circle with
radius " + getRadius() + " has an area of " + dec.format(getArea())
+ " and a diameter of " + getDiameter();
return result;
}
}
import java.text.DecimalFormat; public class Circle { DecimalFormat dec = new DecimalFormat("#.##"); private int radius; public Circle(int radius) { this.radius = radius; } public void setRadius(int r) { radius = r; } public int getRadius() { return radius; } public int getDiameter() { return radius * radius; } public double getArea() { return Math.PI * radius * radius; } public String toString() { String result = "A circle with radius " + getRadius() + " has an area of " + dec.format(getArea()) + " and a diameter of " + getDiameter(); return result; } } class CircleTest { public static void main(String[] args) { Circle circleArr[] = new Circle[4]; circleArr[0] = new Circle(5); circleArr[1] = new Circle(1); circleArr[2] = new Circle(10); circleArr[3] = new Circle(7); for (int i = 0; i < circleArr.length; i++) { System.out.println(circleArr[i]); } } }