binibiningtinamoran

GeometricShapes

Oct 29th, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.65 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class GeometryShapes {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner scan = new Scanner(System.in);
  8.         String yesOrNo;
  9.  
  10.         do {
  11.             System.out.println("\nChoose a shape: ");
  12.             System.out.println(" Circle\n" +
  13.                     " Rectangle\n" +
  14.                     " Triangle\n" +
  15.                     " Trapezoid");
  16.  
  17.             System.out.print("Enter Your Choice: ");
  18.             String shapeChoice = scan.next();
  19.  
  20.             if (!(shapeChoice.equalsIgnoreCase("circle")
  21.                     || shapeChoice.equalsIgnoreCase("rectangle")
  22.                     || shapeChoice.equalsIgnoreCase("triangle")
  23.                     || shapeChoice.equalsIgnoreCase("trapezoid"))) {
  24.                 System.out.println("Invalid shape chosen.");
  25.             } else { // if correct shape was chosen...
  26.                 System.out.print("Solve for area or volume? ");
  27.                 String choice = scan.next();
  28.  
  29.                 if (choice.equalsIgnoreCase("volume")) {
  30.                     System.out.println("Invalid choice made. " +
  31.                             "Cannot calculate the volume of a 2D shape.");
  32.                 } else if (choice.equalsIgnoreCase("area")) {
  33.  
  34.                     double area;
  35.                     switch (shapeChoice.toLowerCase()) {
  36.                         case "circle":
  37.                             System.out.print("What is the radius: ");
  38.                             double r = Double.parseDouble(scan.next());
  39.                             area = Math.PI * Math.pow(r, 3);
  40.                             break;
  41.  
  42.                         case "rectangle":
  43.                             System.out.print("What is the length: ");
  44.                             double length = Double.parseDouble(scan.next());
  45.                             System.out.println("What is the width: ");
  46.                             double width = Double.parseDouble(scan.next());
  47.                             area = length * width;
  48.                             break;
  49.  
  50.                         case "triangle":
  51.                             System.out.print("What is the base: ");
  52.                             double base = Double.parseDouble(scan.next());
  53.                             System.out.print("What is the height: ");
  54.                             double height = Double.parseDouble(scan.next());
  55.                             area = (1.0 / 2.0) * (base * height);
  56.                             break;
  57.  
  58.                         case "trapezoid":
  59.                             System.out.print("What is b1: ");
  60.                             double b1 = Double.parseDouble(scan.next());
  61.                             System.out.print("What is b2: ");
  62.                             double b2 = Double.parseDouble(scan.next());
  63.                             System.out.print("What is the height: ");
  64.                             double heightOfTrapezoid = Double.parseDouble(scan.next());
  65.                             area = (1.0 / 2.0) * ((b1 + b2) * heightOfTrapezoid);
  66.                             break;
  67.                         default:
  68.                             area = 0.0;
  69.                     } // end switch
  70.                     System.out.printf("Area is: %,.2f\n", area);
  71.                 } else { // if neither area nor volume was chosen...
  72.                     System.out.println("Invalid choice made. Choose either area or volume only.");
  73.                 } // end inner else
  74.             } // end outer else
  75.  
  76.             System.out.print("\nWould you to continue (y or n): ");
  77.             yesOrNo = scan.next().toLowerCase();
  78.         } while (yesOrNo.charAt(0) == 'y');
  79.  
  80.     } // end main()
  81. } // end class
Advertisement
Add Comment
Please, Sign In to add comment