Question: Need some help with Exercise 14-3 Modify the application so it stores circles in an array list 1. Before the while loop, add a statement

Need some help with Exercise 14-3

Modify the application so it stores circles in an array list

1. Before the while loop, add a statement that creates an array list of Circle objects.

2. Within the while loop, add code that adds that Circle object thats created for the specified radius to the array list.

3. After the while loop add another loop that loops through the array list of Circle objects and displays the data for each Circle object on the console. This data should include the radius. To do this, you can move the code that displays the data from the while loop to the second loop.

4. Run the application to make sure it works correctly. If the user enters 100 and 200, the console should look something like this:

Enter radius: 100

Continue? (y/n): y

Enter radius: 200

Continue? (y/n): n

Radius: 100.0

Area: 31415.899999999998

Circumference: 628.318

Diameter: 200.0

Radius: 200.0

Area: 125663.59999999999

Circumference: 1256.636

Diameter: 400.0

However, this should also work if the user enters one radius or if the user enters more than two radiuses.

Existing code is:

Source Package: murach.circle

Main.java:

package murach.circle;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

System.out.println("Welcome to the Circle Calculator");

System.out.println();

Scanner sc = new Scanner(System.in);

String choice = "y";

while (choice.equalsIgnoreCase("y")) {

// get input from user

System.out.print("Enter radius: ");

double radius = Double.parseDouble(sc.nextLine());

// create the Circle object

Circle circle = new Circle(radius);

// format and display output

String message =

"Area: " + circle.getArea() + " " +

"Circumference: " + circle.getCircumference() + " " +

"Diameter: " + circle.getDiameter() + " ";

System.out.println(message);

// see if the user wants to continue5

System.out.print("Continue? (y/n): ");

choice = sc.nextLine();

System.out.println();

}

System.out.println("Bye!");

}

}

Circle.java:

package murach.circle;

public class Circle {

private double radius;

public Circle() {

radius = 0;

}

public Circle(double radius) {

this.radius = radius;

}

public double getRadius() {

return radius;

}

public void setRadius(double radius) {

this.radius = radius;

}

public double getCircumference() {

return 2 * 3.14159 * radius;

}

public double getArea() {

return 3.14159 * radius * radius;

}

public double getDiameter() {

return radius * 2;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!