Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 14-3 Work with an array list of Circle objects In this exercise, you’ll

ID: 3589192 • Letter: E

Question

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

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

Review the application

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.

Modify the application so it stores circles in an array list

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 that’s 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

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

THE FOLLOWING CODE HAS ALREADY BEEN GIVEN:

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

AND

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;
}
}

Explanation / Answer

package murach.circle;

import java.util.ArrayList;

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";

int i=0;

ArrayList<Circle> circleList = new ArrayList<Circle>();

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);

circleList.add(circle);

// format and display output

  

// see if the user wants to continue5

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

choice = sc.nextLine();

System.out.println();

}

  

while(i<circleList.size()) {

String message =

"Area: " + circleList.get(i).getArea() + " " +

"Circumference: " + circleList.get(i).getCircumference() + " " +

"Diameter: " + circleList.get(i).getDiameter() + " ";

System.out.println(message);

i++;

}

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;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote