Answer to Question #8544 in Java | JSP | JSF for sonya

Question #8544
// Qs. Write java program that contains the following 2 classes.
(i) Circle class that contains a radius field and getArea method that calculates its area.Give it a constructor where u pass in the radius.
(ii) TestCircle class which creates an array of 100 circles, each with a a random radius and prints out the sum of the areas , the biggest & smallest areas



class Circle {

private float radius;
public Circle(float r)
{
this.radius=r;
}
public float getArea()
{
return (float)(Math.PI*radius*radius);
}
}

class circle7
{
public static void main(String[] args)
{
Circle circles[]= new Circle[100];
for(int i=0; i<circles.length; i++)
{
circles[i]= new Circle((float)(50*Math.random()));
showAreas(circles);
System.out.println("the sum is"+ areaSum(circles));
System.out.println("the minimum is"+ minArea(circles));
System.out.println("the maximum is"+ maxArea(circles));

}
public static float areaSum(Circle c[])
{
float sum= 0;
for(int i=0; i<c.length; i++)
{
sum+= c[i].getArea();
}
return sum;


}

public static float minArea(Circle c[])
{
float min= c[0].getArea();
for(int i=1; i<c.length; i++)
{
if(c[i].getgetArea()<min)
min= c[i].getArea();
}
return min;

}




public static float maxArea(Circle c[])
{
float max= c[0].getArea();
for(int i=1; i<c.length; i++)
{
if(c[i].getgetArea()>max)
max= c[i].getArea();
}
return max;

}
public static void showAreas(Circle c[])
{
for(int i=0; i<c.length; i++)
{
System.out.println(c[i].getArea());
}


}

}


// Can someone plz find the errors in the program? Cuz the compiler is giving 8 errors.
1
Expert's answer
2012-04-19T07:52:40-0400
package testcircle;
/**
*
* @author admin
*/
public class Circle {
private double radius;
public Circle(double r){
this.radius = r;
}
public Circle(){
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double gerArea(){
return Math.PI*Math.pow(radius, 2);
}
}
import java.util.Random;
/**
*
* @author admin
*/
public class TestCircle {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Random rand = new Random();
double big = 0;
double small = 100000;
double sum = 0;
for(int i=0; i<100; i++){
Circle circle = new Circle(rand.nextDouble()*10);
if(small>=circle.gerArea())
small = circle.gerArea();
if(big<circle.gerArea())
big = circle.gerArea();
sum += circle.gerArea();
System.out.println((i+1)+ ": area = "+circle.gerArea());
}
System.out.println("Smallest area: "+small);
System.out.println("Biggest area: "+big);
System.out.println("Sum of areas: "+sum);
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS