Question: I need help on Java ArrayList. I got ArrayList is a private message This is the question Write a Circle class that has the following

I need help on Java ArrayList. I got ArrayList is a private message

This is the question

Write a Circle class that has the following fields: radius (double data type) PI (a final double initialized with the value 3.14159

The class should have the following methods: Constructor: Accepts the radius of the circle as an argument Constructor: A no-arg constructor that sets the radius field to 0.0 setRadius: A mutator method for the radius field getRadius: An accessor method for the radius field getArea: Returns the area of the circle, which is calculated as: area = PI * radius * radius getDiameter: Returns the diameter of the circle, which is calculated as: diameter = radius * 2 getCircumference: Returns the circumference of the circle, which is calculated as: circumference = 2 * PI * radius

Note: Review avoiding stale data in Chapter 6 before creating the calculation methods.

Write a program that creates Circle objects. The program should ask the user for the circle's radius. Validate the input. Do not accept a negative number for the radius. Store your objects in a container that will automatically expand as objects are added. The program should continue allowing the user to input until the user indicates to stop.

Display the circle's area, diameter, and circumference.

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

here is my code

2 3 4 public class Circle 5 { 6 private double radius; 7 private final double PI=3.14159; 8 9 public Circle() 10 { 11 radius=0.0; 12 } 13 public Circle(double radius) 14 { 15 this.radius=radius; 16 } 17 public void setRadius(double radius) 18 { 19 this.radius=radius; 20 } 21 public double getRadius() 22 { 23 return radius; 24 } 25 public double getArea() 26 { 27 double area=PI*radius*radius; 28 return area; 29 } 30 public double getDiameter() 31 { 32 double diameter=radius*2; 33 return radius; 34 } 35 public double getCircumference() 36 { 37 double circumference=2*radius*PI; 38 return circumference; 39 } 40 }

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

2 import java.util.*; 3 4 5 public class CircleDemo { 6 7 8 public static void main(String[] args) { 9 Scanner keyboard = new Scanner(System.in); 10 double radius; 11 String keepGoing; 12 ArrayListcircleList=new ArrayList(); 13 // loop to prompt the user for input 14 do{ 15 System.out.println("Enter the circle radius: "); 16 radius = keyboard.nextDouble(); 17 while(radius<0)// validat input 18 { 19 System.out.println("Input is invalid. Pleas enter non-negative value:"); 20 radius=keyboard.nextDouble(); 21 22 } 23 // cerat the circle object add it to our ArrayList 24 circleList.add(new Circle(radius)); 25 26 System.out.println("Do you want to add another radius"); 27 28 keepGoing=keyboard.nextLine().toUpperCase(); 29 System.out.print(" "); 30 } 31 while(keepGoing.charAt(0)=='y'); 32 // iterate through our ArraList of Circle and output the result 33 34 for(int i=0;i

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!