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...
JAVA FILE PROGRAM Write a contacts database program that presents the user with a menu that...
JAVA FILE PROGRAM 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...
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...
JAVA Write a test program that prompts the user to enter a series of integers and...
JAVA Write a test program that prompts the user to enter a series of integers and displays whether the series contains runLength consecutive same-valued elements. Your program’s main() must prompt the user to enter the input size - i.e., the number of values in the series, the number of consecutive same-valued elements to match, and the sequence of integer values to check. The return value indicates whether at least one run of runLength elements exists in values. Implement the test...
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...
JAVA: Write a program to perform time conversion. The user will select from the following menu:...
JAVA: Write a program to perform time conversion. The user will select from the following menu: Hours to minutes Days to hours Minutes to hours Hours to days. Each choice has a separate method which is called when the user makes the selection. Please see the output below: Hours to minutes. 2. Days to hours. 3. Minutes to hours. 4. Hours to days. Enter your choice: 2 Enter the number of days: 5 There are 120 hours in 5 days.
Write a java program that presents the user with the following menu Linear Search Binary Search...
Write a java program that presents the user with the following menu Linear Search Binary Search The user can choose any of these operations using numbers 1 or 2. Once selected, the operation asks the user for an input file to be searched (the file is provided to you and is named input.txt). If option 1 or 2 is selected, it asks the user for the search string. This is the string that the user wants the program to search...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT