In: Computer Science
java code
Write a program that gives the user a menu of six choices (use integers) to select from. The choices are circle, triangle, cone, cylinder, sphere, and quit. (The formulas are given below.) Once the figure is calculated, an informative message should be printed and the user shown the menu again, so that another choice can be made. The formulas are:
this is what i have so far
public static void main(String[] args) {
}
public static void menu( String input) {
if
}
public static void circle( double raduis) {
double area =
3.14*raduis*raduis;
System.out.println("the area of
cirle is :"+ area);
}
public static void triangle( double base, double
height) {
double area =
1/2*base*height;
System.out.println("the area of the
triangle is :"+ area);
}
public static void cone( double raduis, double height
) {
double volume = 1/3*3.14 *
raduis *raduis* height;
System.out.println("the volume of
the cone is :"+ volume);
}
public static void cylinder(double raduis, double
height) {
double volume = 3.14*
raduis*raduis* height;
System.out.println("the volume of
the cylinder is :"+ volume);
}
public static void sphere(double raduis) {
double volume = 4/3*3.14 *
raduis *raduis*raduis ;
System.out.println("the volume of
the sphere is :"+ volume);
}
}
Following is the code written in java and it's result. Program uses switch case which call the corresponding function to print the area, until user hits 6 which is assigned to quit.
import java.util.Scanner;
public class Main {
private static void area_circle(Scanner scanner) {
System.out.print("Enter radius:");
double radius = scanner.nextDouble();
System.out.println("Area of circle is: " + 3.14*radius*radius);
}
private static void area_triangle(Scanner scanner) {
System.out.print("Enter base:");
double base = scanner.nextDouble();
System.out.print("Enter height:");
double height = scanner.nextDouble();
System.out.println("Area of triangle is: " + 0.5*base*height);
}
private static void area_cone(Scanner scanner) {
System.out.print("Enter radius:");
double radius = scanner.nextDouble();
System.out.print("Enter height:");
double height = scanner.nextDouble();
System.out.println("Area of cone is: " + 0.33*3.14*radius*radius*height);
}
private static void area_cylinder(Scanner scanner) {
System.out.print("Enter radius:");
double radius = scanner.nextDouble();
System.out.print("Enter height:");
double height = scanner.nextDouble();
System.out.println("Area of cylinder is: " + 3.14*radius*radius*height);
}
private static void area_sphere(Scanner scanner) {
System.out.print("Enter radius:");
double radius = scanner.nextDouble();
System.out.println("Area of sphere is " + 1.33*3.14*radius*radius*radius);
}
public static void main(String[] args) {
Scanner get_choice = new Scanner(System.in);
System.out.println("Enter choice to print area 1.Circle 2.Triangle 3.Cone 4.Cylinder 5.Sphere 6.Quit: ");
int number = get_choice.nextInt();
while (number!=6) {
switch (number) {
case 1:
area_circle(get_choice);
break;
case 2:
area_triangle(get_choice);
break;
case 3:
area_cone(get_choice);
break;
case 4:
area_cylinder(get_choice);
break;
case 5:
area_sphere(get_choice);
break;
case 6:
break;
}
System.out.println("Enter choice to print area 1.Circle 2.Triangle 3.Cone 4.Cylinder 5.Sphere 6.Quit: ");
number = get_choice.nextInt();
}
}
}