Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class GeometryShapes {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String yesOrNo;
- do {
- System.out.println("\nChoose a shape: ");
- System.out.println(" Circle\n" +
- " Rectangle\n" +
- " Triangle\n" +
- " Trapezoid");
- System.out.print("Enter Your Choice: ");
- String shapeChoice = scan.next();
- if (!(shapeChoice.equalsIgnoreCase("circle")
- || shapeChoice.equalsIgnoreCase("rectangle")
- || shapeChoice.equalsIgnoreCase("triangle")
- || shapeChoice.equalsIgnoreCase("trapezoid"))) {
- System.out.println("Invalid shape chosen.");
- } else { // if correct shape was chosen...
- System.out.print("Solve for area or volume? ");
- String choice = scan.next();
- if (choice.equalsIgnoreCase("volume")) {
- System.out.println("Invalid choice made. " +
- "Cannot calculate the volume of a 2D shape.");
- } else if (choice.equalsIgnoreCase("area")) {
- double area;
- switch (shapeChoice.toLowerCase()) {
- case "circle":
- System.out.print("What is the radius: ");
- double r = Double.parseDouble(scan.next());
- area = Math.PI * Math.pow(r, 3);
- break;
- case "rectangle":
- System.out.print("What is the length: ");
- double length = Double.parseDouble(scan.next());
- System.out.println("What is the width: ");
- double width = Double.parseDouble(scan.next());
- area = length * width;
- break;
- case "triangle":
- System.out.print("What is the base: ");
- double base = Double.parseDouble(scan.next());
- System.out.print("What is the height: ");
- double height = Double.parseDouble(scan.next());
- area = (1.0 / 2.0) * (base * height);
- break;
- case "trapezoid":
- System.out.print("What is b1: ");
- double b1 = Double.parseDouble(scan.next());
- System.out.print("What is b2: ");
- double b2 = Double.parseDouble(scan.next());
- System.out.print("What is the height: ");
- double heightOfTrapezoid = Double.parseDouble(scan.next());
- area = (1.0 / 2.0) * ((b1 + b2) * heightOfTrapezoid);
- break;
- default:
- area = 0.0;
- } // end switch
- System.out.printf("Area is: %,.2f\n", area);
- } else { // if neither area nor volume was chosen...
- System.out.println("Invalid choice made. Choose either area or volume only.");
- } // end inner else
- } // end outer else
- System.out.print("\nWould you to continue (y or n): ");
- yesOrNo = scan.next().toLowerCase();
- } while (yesOrNo.charAt(0) == 'y');
- } // end main()
- } // end class
Advertisement
Add Comment
Please, Sign In to add comment