Question

In: Computer Science

java code Write a program that gives the user a menu of six choices (use integers)...

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:

  • Area of circle: a = 3.14 * radius * radius
  • Area of triangle: a = ½ base * height
  • Volume of a cone: v = 1/3 * 3.14 * radius * radius * height
  • Volume of a cylinder: v = 3.14 * radius * radius * height
  • Volume of a sphere: v = 4/3 * 3.14 * radius * radius * radius

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);
      
   }
  

}

Solutions

Expert Solution

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();
        }
    }
}


Related Solutions

Write a program names EncryptDecrypt.java that has the following menu choices: Print menu and allow user...
Write a program names EncryptDecrypt.java that has the following menu choices: Print menu and allow user to choose options. The program must have a file dialogue box for text file. Output should be based on user choices. Read in a file Print the file to the console Encrypt the file and write it to the console Write out the encrypted file to a text file Clear the data in memory Read in an encrypted file Decrypt the file Write out...
Write a java program that will ask the user to enter integers (use loops) until -100...
Write a java program that will ask the user to enter integers (use loops) until -100 is entered. The program will find and display the greatest, the smallest, the sum and the average of the entered numbers. also write a separate driver class which will be used to run and display the greatest, the smallest, the sum and the average of the entered numbers. Thanks!!
in java Write a contacts database program that presents the user with a menu that allows...
in java Write a contacts database program that presents the user with a menu that allows the user to select between the following options: Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...
LANGUAGE: C Only using <stdio.h> & <stdlib.h> Write a program that gives the user a menu...
LANGUAGE: C Only using <stdio.h> & <stdlib.h> Write a program that gives the user a menu to choose from – 1. Convert temperature input from the user in degrees Fahrenheit to degrees Celsius 2. Convert temperature input from the user in degrees Celsius to degrees Fahrenheit 3. Quit. Formulae you will need: C = (5 / 9) * (F-32) and F = (9/5) * C + 32 1. Use functions to accomplish 1 and 2 above. 2. Use at least...
Write a java program that asks the user for a number n and gives them the...
Write a java program that asks the user for a number n and gives them the possibility to choose between computing the sum and computing the product of 1,…,n. Example of running this program: Enter an integer number n: __7________ Enter Sum or Product: __Sum__________________________________ Program output: Sum of 1 ... 7 Sum or Product: Sum Sum = 28 Now second sample of second execution Enter an integer number n: __5__________________________________ Enter Sum or Product: __Product__________________________________ Program output:  Product of 1...
Write a Java program that prompts a user for 10 integers. When completed it outputs the...
Write a Java program that prompts a user for 10 integers. When completed it outputs the highest number, the lowest number, the number of odd number, and the average of the numbers
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
I need to make this into a Java Code: Write a program that prompts the user...
I need to make this into a Java Code: Write a program that prompts the user for a double value representing a radius. You will use the radius to calculate: circleCircumference = 2πr circleArea = πr2 sphereArea = 4πr2 sphereVolume = 43πr3 You must use π as defined in the java.lang.Math class. Your prompt to the user to enter the number of days must be: Enter radius: Your output must be of the format: Circle Circumference = circleCircumference Circle Area...
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Important! Consider which control structures will work best for which aspect of the assignment. For example, which would be the best to use for a menu?...
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Your code must contain at least one of all of the following control types: nested for() loops a while() or a do-while() loop a switch() statement...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT