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!!
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...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1 to 10 then subtract the second integer from the first integer. Note, if the second integer is greater than the first integer, swap the two integers before making the subtraction.Then prompt user for the answer after the subtraction. If the answer is correct, display “Correct”otherwise display “Wrong”.You will need to do this in a loop FIVE times and keep a count of how many...
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
every code should be in java Program 1: Write a while loop that sums the integers...
every code should be in java Program 1: Write a while loop that sums the integers from 1 to 10, excluding 3 and 6. Print the sum. [Hint: Use logical operators] Program 2: Write a for loop that attempts to display the numbers from 1 to 10, but terminates when the control variable reaches the value 6. Program 3: Write a do...while loop that prints the integers from 10 to 0, inclusive.
Write a program that ask the user for three integers. Use two functions to determine the...
Write a program that ask the user for three integers. Use two functions to determine the largest and smallest number of the three. One function will determine the largest number, and the other function will determine the smallest number. (6 points) In c++ using functions.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT