The area of a circle can be calculated using the formula: PI r 2 The volume of a
ID: 3550693 • Letter: T
Question
The area of a circle can be calculated using the formula: PI r2 The volume of a sphere can be calculated using the formula: (4/3) PIr 3
PI = 3.141124 or can be generated using the Math.PI method.
r = the radius of a circle or sphere (distance between center and the perimeter)
Accept input for any number of entries (use a loop) for the radius of a circle or sphere. Then, calculate the area of that circle and output the result to the screen. Note: Your program will accept input and then output a result in each iteration of the loop.
Your program will have at least six methods:
1. The main method that outputs an introduction to the program.
2. A method that prompts for and accepts input from the user.
3. A method that calculates the area of the circle using the input from step 2.
4. A method that calculates the area of a sphere using input from step 2.
5. A method that outputs the result of both calculations.
a. No output will exceed three decimal places.
6. A method that thanks and says goodbye to the user just before the program terminates.
7. As always, a program header, proper indentation and spacing, and adequate code comments are expect
Your program will end when the user enters a 0 for the radius of the circle.
Here is a sample run of the assigned program:
Welcome to the Round Object Calculator
This program will calculate the area of a circle
or the volume of a sphere.
The calculations will be based on the user input radius.
Enter C for circle or S for sphere: S
Thank you. What is the radius of the sphere (in inches): 45.9
The volume of a sphere with a radius of 45.9 inches is: 405066.816 cubic inches.
Do you want to calculate another round object (Y/N)? Y
Enter C for circle or S for sphere: C
Thank you. What is the radius of the circle (in inches): 15.86
The area of a circle with a radius of 15.86 inches is: 790.235 inches.
Do you want to calculate another round object (Y/N)? N
Thank you for using the Round Object Calculator. Goodbye.
Welcome to the Round Object Calculator
This program will calculate the area of a circle
or the volume of a sphere.
The calculations will be based on the user input radius.
Enter C for circle or S for sphere: S
Thank you. What is the radius of the sphere (in inches): 45.9
The volume of a sphere with a radius of 45.9 inches is: 405066.816 cubic inches.
Do you want to calculate another round object (Y/N)? Y
Enter C for circle or S for sphere: C
Thank you. What is the radius of the circle (in inches): 15.86
The area of a circle with a radius of 15.86 inches is: 790.235 inches.
Do you want to calculate another round object (Y/N)? N
Thank you for using the Round Object Calculator. Goodbye.
Explanation / Answer
package menu;
import java.io.*;
import java.lang.*;
import java.util.*;
import java.text.*;
public class Menu
{
public static String s;
public static char c;
static Scanner inpu = new Scanner ( System.in );
static public void welcome()
{
System.out.println("Welcome to the Round Object Calculator");
System.out.println(" This program will calculate the area of a circle");
System.out.println("or the volume of a sphere.");
System.out.println("The calculations will be based on the user input radius. ");
}
static public void end() throws IOException
{
System.out.println("Thank you for using the Round Object Calculator. Goodbye. ");
}
static public void out(double x,double y,int a) throws IOException
{
DecimalFormat f = new DecimalFormat("##.000"); // this will helps you to always keeps in two decimal places
if(a==1)
System.out.println("The area of a circle with a radius of "+x +"inches is:" + f.format(y)+"square inches.");
else
System.out.println("The volume of a sphere with a radius of "+x +"inches is:" + f.format(y)+"cubic inches.");
}
static public void cir(double x)throws IOException
{
double PI = 3.141124;
double y = x * x * PI;
out(x,y,1);
}
static public void sph(double x)throws IOException
{
double PI = 3.141124;
double y = x * x * x * PI * 4;
y = y/3.0;
out(x,y,2);
}
static public void input()throws IOException
{
System.out.println("Enter C for circle or S for sphere: S");
s = null;
c = (char)System.in.read();
double x;
if(c == 'C' || c=='c')
{
System.out.println("Thank you. What is the radius of the circle (in inches):");
s = inpu.next();
x = Double.parseDouble(s);
cir(x);
}
else if(c == 'S' || c=='s')
{
System.out.println("Thank you. What is the radius of the sphere (in inches):");
s = inpu.next();
x = Double.parseDouble(s);
sph(x);
}
}
public static void main(String[] args) throws IOException
{
welcome();
input();
System.out.println("Do you want to calculate another round object (Y/N)? ");
c ='y';
while(true)
{
c = (char) System.in.read();
if(c=='N' || c=='n')
break;
input();
}
end();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.