Created
May 4, 2013 06:21
-
-
Save jimmy087/5516454 to your computer and use it in GitHub Desktop.
Print Circle Array and total area of the circle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TotalArea { | |
| public static void main(String[] args) { | |
| //Declare circleArray | |
| Circle3[] circleArray = createCircleArray(); | |
| //Print circleArray and total areas of the circles | |
| printCircleArray(circleArray); | |
| } | |
| public static Circle3[] createCircleArray() { | |
| Circle3[] circleArray = new Circle3[5]; | |
| for (int i = 0; i < circleArray.length; i++) { | |
| circleArray[i] = new Circle3(Math.random() * 100); | |
| } | |
| return circleArray; | |
| } | |
| public static void printCircleArray(Circle3[] circleArray) { | |
| System.out.printf("%-30s%-15s\n", "Radius", "Area"); | |
| for (int i = 0; i < circleArray.length; i++) { | |
| System.out.printf("%-30f%-15f\n", circleArray[i].getRadius(), circleArray[i].getArea()); | |
| } | |
| System.out.println("-----------------------------------------------------"); | |
| // Create and display the result | |
| System.out.printf("%-30s%-15f\n", "The total area of circles is", sum(circleArray)); | |
| } | |
| public static double sum(Circle3[] circleArray) { | |
| // Initialize sum | |
| double sum = 0; | |
| // Add areas to sum | |
| for (int i = 0; i < circleArray.length; i++) { | |
| sum += circleArray[i].getArea(); | |
| } | |
| return sum; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment