In: Computer Science
8) Create the following program using Java.
Circle calculation using methods
Create scanner
declare double variable radius = -999
declare character choice
create do while loop
inside of do loop write:
System.out.println();
System.out.println("*** CIRCLE CALCULATIONS ***");
System.out.println();
System.out.println("1. Enter the radius of the circle");
System.out.println("2. Display the area of the circle");
System.out.println("3. Display the circumference of the
circle");
System.out.println("4. Quit");
System.out.println();
System.out.println("Enter a number from 1 - 4");
System.out.println();
Declare choice character and relate to scanner
declare switch (choice)
case 1 radius which is equal to option1 method
case 2 call option2(radius)
case 3 call option3(radius)
case 4 quit
default
prompt the user to enter numbers from 1 - 4
close bracket
while condition choice !='4'
create method static double option1 gets the user to enter the
radius of the circle
declare myradius variable double
declare a scanner
prompt the user to enter the radius of circle and relate to
variable above with scaner
return the variable
create method static void option2 with parameter double
radiusin
check condition id radiusin is equal to -999 display the message in
console no radius is entered
else
declare variable double area
area is equal to 3.14 * radiusin * radiusin
display in console the area
create method static void option3 with parameter double
radiusin
check condition id radiusin is equal to -999 display the message in
console no radius is entered
else
declare variable double circumference
circumference is equal to 2 * 3.14 * radiusin
display in console the circumference
package learning;
import java.util.*;
public class Main{
static double option1() {
double myradius;
Scanner input = new Scanner(System.in);
System.out.print("Enter the radius of the circle : ");
myradius = input.nextDouble();
return myradius;
}
static void option2(double radiusin) {
if(radiusin == -999) {
System.out.println("No radius entered!!");
return;
}
double area = 3.14 * radiusin * radiusin;
System.out.println("Area : " + area);
}
static void option3(double radiusin) {
if(radiusin == -999) {
System.out.println("No radius entered!!");
return;
}
double circumference;
circumference = 2 * 3.14 * radiusin;
System.out.println("Circumference : " + circumference );
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
double radius = -999;
char choice;
while(true) {
System.out.println();
System.out.println("*** CIRCLE CALCULATIONS ***");
System.out.println();
System.out.println("1. Enter the radius of the circle");
System.out.println("2. Display the area of the circle");
System.out.println("3. Display the circumference of the circle");
System.out.println("4. Quit");
System.out.println();
System.out.println("Enter a number from 1 - 4");
System.out.println();
choice = input.next().charAt(0);
switch(choice) {
case '1':
radius = option1();
break;
case '2':
option2(radius);
break;
case '3':
option3(radius);
break;
default:
return;
}
}
}
}
OUTPUT: