Using the attached Circle class as a reference (feel free to use this class!), p
ID: 3837937 • Letter: U
Question
Using the attached Circle class as a reference (feel free to use this class!), pleae write an application that performs the following:
Step 1: create a Circle array that holds 1000 Circle objects
Step 2: using a for loop, initialize the Circle array - create 1000 Circle objects and store each Circle object in the array.
(Hint : use the index of the array as the argument for the Circle constructor)
Step 3. Create a method that accepts A Circle array (see step 2) , and returns the sum of all Circle objects in the array.
Step 4. Create a method that accepts a Circle array (see step 2) and returns the average area of all Circle objects in the array.
Step 5. Call the methods in steps 3 and 4 to display the values returned by those methods.
Explanation / Answer
Hi friend, you have not posted Circle class, so i have implemented both Circle and CircleTest class.
############
/**
* @author pravesh
*
*/
public class Circle {
// instance variables
private double radius;
private static final double PI = 3.14159;
// constructors
public Circle() {
radius = 0;
}
/**
* @param radius
*/
public Circle(double radius) {
this.radius = radius;
}
// getters and setters
/**
* @return radius
*/
public double getRadius() {
return radius;
}
/**
* @param radius
*/
public void setRadius(double radius) {
this.radius = radius;
}
/**
* @return area
*/
public double getArea(){
return PI*radius*radius;
}
/**
* @return diameter
*/
public double getDiameter(){
return 2*radius;
}
/**
* @return perimeter
*/
public double getCircumference(){
return 2*PI*radius;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Radius: "+radius;
}
}
################
import java.util.Scanner;
public class CircleTest {
public static double getSumCircle(Circle[] circles){
double sum = 0;
for(int i=0; i<circles.length; i++)
sum = sum + circles[i].getRadius();
return sum;
}
public static double getAvgAreaCircle(Circle[] circles){
double sum = 0;
for(int i=0; i<circles.length; i++)
sum = sum + circles[i].getRadius();
return sum/circles.length;
}
public static void main(String[] args) {
// creating an array
Circle circles[] = new Circle[100];
Scanner sc = new Scanner(System.in);
for(int i=0; i<circles.length; i++){
System.out.print("Enter radius of circle "+(i+1)+":");
double r = sc.nextDouble();
circles[i] = new Circle(r);
}
sc.close();
System.out.println("Sum of circle radius: "+getSumCircle(circles));
System.out.println("Average of circle: "+getAvgAreaCircle(circles));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.