JAVA - Calculate a circle’s circumference and area CONSOLE: Operation ? The appl
ID: 3676308 • 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 2-Explanation / Answer
Circle.java
package org.students;
import java.text.DecimalFormat;
public class Circle
{
float radius = 0;
static int count = 0;
double area = 0.0;
double circumfrence = 0.0;
DecimalFormat df2 = new DecimalFormat(".##");
public Circle(float radius) {
this.radius = radius;
count++;
}
public int getcount() {
return count;
}
public double getCircumference() {
circumfrence = 2 * Math.PI * radius;
return circumfrence;
}
public double getArea() {
area = Math.PI * radius * radius;
return area;
}
// private String formatNumber(double x)
public String getFormattedCircumference() {
return df2.format(getCircumference());
}
public String getFormattedArea() {
return df2.format(getArea());
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Test.java
package org.students;
import org.students.*;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
float radius = 0;
int z = 0;
String ra[] = new String[100];
String aa[] = new String[100];
String ca[] = new String[100];
String area = null, circumfrence = null;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("Enter radius of the circle:");
radius = sc.nextFloat();
if (radius < 1.0) {
continue;
}
else {
Circle c = new Circle(radius);
area = c.getFormattedArea();
circumfrence = c.getFormattedCircumference();
System.out.println(" ");
System.out.println("area:" + area);
System.out.println("circumfrence:" + circumfrence);
int k = c.getcount();
for (int i = z; i < c.count; i++) {
ra[i] = String.valueOf(radius);
aa[i] = c.getFormattedArea();
ca[i] = c.getFormattedCircumference();
z++;
}
System.out.println(" ");
System.out.print("Do you want to continue(press Y/N):");
char ch = sc.next(".").charAt(0);
if (ch == 'y' || ch == 'Y') {
continue;
} else {
System.out
.println(":: Good Bye ,No of Circle objects created :"
+ c.getcount());
System.out
.print("Do u want to display the summary of the circles(Y/N) :");
char ch1 = sc.next(".").charAt(0);
if (ch1 == 'y' || ch1 == 'Y') {
System.out.println(" ");
for (int i = 0; i < k; i++) {
System.out.println("Circle " + (i + 1) + ":");
System.out.println("Radius of the circle is:"
+ ra[i]);
System.out.println("area:" + aa[i]);
System.out.println("circumfrence:" + ca[i]);
}
} else {
break;
}
}
}
break;
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.