JAVA - Calculate a circle’s circumference and area Console Operation ? The appli
ID: 3671902 • Letter: J
Question
JAVA - Calculate a circle’s circumference and area
Console
Operation
? The application prompts the user to enter the radius of a circle.
? If the user enters invalid data, the application displays an appropriate error message and prompts the user again until the user enters valid data. (Invalid – not double data or double values less than 1.0)
? When the user enters a valid radius, the application calculates and displays the circumference and area of the circle to the nearest 2 decimal places.
? The application prompts the user to continue.
? When the user chooses not to continue, the application displays a goodbye message that indicates the number of Circle objects that were created by the application.
? The application then asks the user if they want a summary of the circles created – if yes, a list of the circles is displayed.
Specifications
? Create a class named Circle to store the data about this circle. This class should contain these constructors and methods: (maybe more if needed) public Circle(double radius) public double getCircumference() public String getFormattedCircumference() public double getArea() public String getFormattedArea() private String formatNumber(double x)
? The formulas for calculating circumference and area are: circumference = 2 * pi * radius area = pi * radius2
? For the value of pi, use the PI constant of the java.lang.Math class.
Welcome to the Circle Creator Enter radius: 3 Circumference: 18.85 Area: 28.27 Continue? (y/n): y Enter radius: 6 Circumference: 37.7 Area: 113.1 Continue? (y/n): n Goodbye. You created 2 Circle object(s). Do you want to display a summary of the Circles? (y/n): y Circle 1 - Radius 3 Circumference 18.85 Area 28.27 Circle 2Explanation / Answer
program:
import java.io.*;
import java.lang.Math;
import java.util.Scanner;
public class CircleTest {
public static void main(String[] args)throws IOException
{double radius;
String str="y";
Scanner scan=new Scanner(System.in);
Circle[] c=new Circle[10];
int i=0;
System.out.println("Welcome to circle creator");
while(str.equalsIgnoreCase("y"))
{
System.out.print("Enter radius:");
radius=scan.nextDouble();
c[i]=new Circle(radius);
System.out.printf("Circumference:%.2f",c[i].getCircumference());
System.out.printf("Area:%.2f",c[i].getArea());
System.out.print("continue? y/n:");
str=scan.next();
i++;
}
System.out.println("GoodBye. you created "+i+"Circle object(s)");
System.out.print("Do you want to display the summary of the circle? y/n:");
str=scan.next();
if(str.equalsIgnoreCase("y"))
{
for(int j=0;j<i;j++)
{
System.out.printf(" Circle %d radius %.2f Circumference %.2f Area %.2f",(j+1),c[j].radius,c[j].getCircumference(),c[j].getArea());
}
}
}
}
class Circle
{
double radius;
Circle(double r)
{
radius=r;
}
double getCircumference()
{
return(2*Math.PI*radius);
}
double getArea()
{
return(Math.PI*radius*radius);
}
}
output:
run:
Welcome to circle creator
Enter radius:13
Circumference:81.68Area:530.93continue? y/n:y
Enter radius:56
Circumference:351.86Area:9852.03continue? y/n:n
GoodBye. you created 2Circle object(s)
Do you want to display the summary of the circle? y/n:y
Circle 1 radius 13.00 Circumference 81.68 Area 530.93
Circle 2 radius 56.00 Circumference 351.86 Area 9852.03BUILD SUCCESSFUL (total time: 22 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.