Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise 14-3 Work with an array list of Circle objects In this exercise, youll modify the Circle Calculator so it works with two or more

Exercise 14-3 Work with an array list of Circle objects

In this exercise, youll modify the Circle Calculator so it works with two or more circles.

1. Open the project named ch14_ex3_Circle in the extra_ex_starts directory.

2. Open the Main class and review its code. Note that it only creates one Circle object at a time.

3. Run the application to make sure it works correctly.

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

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

6. 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.

7. 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

----------------------------------------------------------------------------------

Code:

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!"); } }

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago