Question: A java question. Write a class CircleProcessor which manages an array of Circles . You are given a Circle class. (P.S. the Circles and the
A java question. Write a class CircleProcessor which manages an array of Circles. You are given a Circle class. (P.S. the Circles and the Circle are different)
The constructor of the CircleProcessor takes an array of Circles. The array must contain at least one element
Provide these methods
1. An averageArea method that returns the average area of the Circles in the array. Only divide one time
2. A swapMaxAndMin method which swaps the largest and smallest elements in the array. Only use one loop
3. A toString method which returns the string representation of the underlying array using Arrays.toString
The CircleProcessorTester is also given:
--------------------------------------------------------------------------------------------------------------------------------
Circle.java
public class Circle { private double radius; /** * Constructs a Circle with the given radius * @param theRadius the radius of this Circle */ public Circle(double theRadius) { radius = theRadius; } /** * Gets the area of this circle * @return the area of this circle */ public double area() { return radius * radius * Math.PI; } /** * Sets a new radius for this Circle * @param newRadius the new radius of this Circle */ public void setRadius(int newRadius) { radius = newRadius; } /** * Gets the radius of this Circle. * @return the radius */ public double getRadius() { return radius; } public String toString() { String s = "Circle[radius=" + radius + "]"; return s; } } CircleProcessorTester.java
public class CircleProcessorTester { public static void main(String[] args) { Circle[] circles = { new Circle(10.0), new Circle(100.0), new Circle(50), new Circle(1.0) }; CircleProcessor processor = new CircleProcessor(circles); System.out.printf("Aveage: %.2f ", processor.averageArea()); System.out.println("Expected: 9896.80"); processor.swapMaxAndMin(); System.out.println(processor.toString()); System.out.println("[Circle[radius=10.0], Circle[radius=1.0], Circle[radius=50.0], Circle[radius=100.0]]"); Circle[] circles2 = { new Circle(10.0), new Circle(100.0), new Circle(50) }; CircleProcessor processor2 = new CircleProcessor(circles2); System.out.printf("Aveage: %.2f ", processor2.averageArea()); System.out.println("Expected: 13194.69"); processor2.swapMaxAndMin(); System.out.println(processor2.toString()); System.out.println("[Circle[radius=100.0], Circle[radius=10.0], Circle[radius=50.0]]"); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
