Question
How can I modify the code to calculate the areas of circles with radius = 1, 10, 100 Only one .java file. public class TotalArea
How can I modify the code to calculate the areas of circles with radius = 1, 10, 100 Only one .java file.
public class TotalArea {
public static void main(String[] args) {
CircleWithPrivateDataFields[] circleArray;
circleArray = createCircleArray();
printCircleArray(circleArray);
}
public static CircleWithPrivateDataFields[] createCircleArray() {
CircleWithPrivateDataFields[] circleArray = new CircleWithPrivateDataFields[5];
for (int i = 0; i < circleArray.length; i++) {
circleArray[i] = new CircleWithPrivateDataFields(Math.random() * 100);
}
return circleArray;
}
public static void printCircleArray(CircleWithPrivateDataFields[] circleArray) {
System.out.printf("%-30s%-15s ", "Radius", "Area");
for (int i = 0; i < circleArray.length; i++)
{ System.out.printf("%-30f%-15f ", circleArray[i].getRadius(), circleArray[i].getArea());
}
System.out.println("-----------------------------------------");
System.out.printf("%-30s%-15f ", "The total areas of circles is", sum(circleArray));
}
public static double sum(CircleWithPrivateDataFields[] circleArray) {
double sum = 0;
for (int i = 0; i < circleArray.length; i++)
sum += circleArray[i].getArea();
return sum;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started