Ask Question
24 September, 01:46

Imagine that you have access to a class named MyCircle that has void setRadius (double r) and double getRadius () methods. Write a static method that accepts a MyCircle array. The method should accomplish two goals: it should return the average of the radius's for all MyCircles that have a positive radius, and for any MyCircles that have a negative radius it should set the radius to 0.

+3
Answers (1)
  1. 24 September, 02:02
    0
    Code for the method described in the question in java:

    public static double averageRadius (MyCircle[] myCircles) {

    double sum = 0;

    for (MyCircle myCircle: myCircles) {

    if (myCircle. getRadius () < 0) myCircle. setRadius (0);

    sum + = myCircle. getRadius ();

    }

    return sum / myCircles. length;

    }

    And the complete program:

    import java. util. Random;

    public class MyCircle {

    private double radius;

    public double getRadius () {

    return radius;

    }

    public void setRadius (double radius) {

    this. radius = radius;

    }

    public static double averageRadius (MyCircle[] myCircles) {

    double sum = 0;

    for (MyCircle myCircle: myCircles) {

    if (myCircle. getRadius () < 0) myCircle. setRadius (0);

    sum + = myCircle. getRadius ();

    }

    return sum / myCircles. length;

    }

    public static void main (String[] args) {

    Random random = new Random ();

    int N = 10;

    MyCircle[] myCircles = new MyCircle[N];

    for (int i = 0; i < myCircles. length; i++) {

    myCircles[i] = new MyCircle ();

    myCircles[i]. setRadius (random. nextInt (100));

    System. out. printf ("Created MyCircle %d with radius %.2f / n", i, myCircles[i]. getRadius ());

    }

    System. out. printf ("/nAverage radius of %d circles is %.2f / n", N, MyCircle. averageRadius (myCircles));

    }

    }

    The output was:

    Created MyCircle 0 with radius 76.00

    Created MyCircle 1 with radius 86.00

    Created MyCircle 2 with radius 38.00

    Created MyCircle 3 with radius 4.00

    Created MyCircle 4 with radius 8.00

    Created MyCircle 5 with radius 39.00

    Created MyCircle 6 with radius 77.00

    Created MyCircle 7 with radius 78.00

    Created MyCircle 8 with radius 39.00

    Created MyCircle 9 with radius 46.00

    Average radius of 10 circles is 49.10
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Imagine that you have access to a class named MyCircle that has void setRadius (double r) and double getRadius () methods. Write a static ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers