Advertisement
binibiningtinamoran

Geometry

Nov 6th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Geometry {
  4.     public static void main(String[] args) {
  5.         Scanner in = new Scanner(System.in);
  6.  
  7.         int choice;
  8.  
  9.         do {
  10.             displayMenu(); // Call display menu
  11.             System.out.print("Enter choice (1-3): ");
  12.             choice = in.nextInt();
  13.             selectOption(choice, in);
  14.         } while ((choice < 1) || (choice > 3));
  15.         System.out.println("Thanks for using the Geometry Calculator - Goodbye!");
  16.     }
  17.  
  18.     /**
  19.      * This will display the welcome message and your choice for the
  20.      * three options (circle, triangle, or rectangle).
  21.      */
  22.     public static void displayMenu() {
  23.         System.out.println("Welcome to the Geometry Calculator Application");
  24.         System.out.println("1. Calculate the Area of a Circle");
  25.         System.out.println("2. Calculate the Area of a Rectangle");
  26.         System.out.println("3. Calculate the Area of a Triangle");
  27.     }
  28.  
  29.     /**
  30.      * This will prompt the user to put in the circle's radius,
  31.      * then calculate the area.
  32.      *
  33.      * @param s Scanner object
  34.      * @return the area
  35.      */
  36.     public static double getCircleArea(Scanner s) {
  37.         System.out.print("What is the circle's radius? ");
  38.         double radius = s.nextDouble();
  39.         while (radius <=0) {
  40.             System.out.println("Invalid radius entered.");
  41.             System.out.print("What is the circle's radius? ");
  42.             radius = s.nextDouble();
  43.         }
  44.         return Math.PI * Math.pow(radius, 2);
  45.     }
  46.  
  47.     /**
  48.      * This will prompt the user to put in the rectangle's length
  49.      * and width. Afterwards calculating the area.
  50.      *
  51.      * @param s Scanner object
  52.      * @return the area
  53.      */
  54.     public static double getRectangleArea(Scanner s) {
  55.         System.out.print("What is the rectangle's length? ");
  56.         double length = s.nextDouble();
  57.         while (length <= 0) {
  58.             System.out.println("Invalid length!");
  59.             System.out.print("What is the rectangle's length? ");
  60.             length = s.nextDouble();
  61.         }
  62.  
  63.         System.out.print("What is the rectangle's width? ");
  64.         double width = s.nextDouble();
  65.         while (width <= 0) {
  66.             System.out.println("Invalid width!");
  67.             System.out.print("What is the rectangle's width? ");
  68.             width = s.nextDouble();
  69.         }
  70.  
  71.         return length * width;
  72.     }
  73.  
  74.     /**
  75.      * This will prompt the user to put in the triangle's
  76.      * base and height, then calculating the area.
  77.      *
  78.      * @param s Scanner object
  79.      * @return the area
  80.      */
  81.     public static double getTriangleArea(Scanner s) {
  82.         System.out.print("What is the triangle's base? ");
  83.         double base = s.nextDouble();
  84.         while (base <= 0) {
  85.             System.out.println("Invalid base!");
  86.             System.out.print("What is the rectangle's base? ");
  87.             base = s.nextDouble();
  88.         }
  89.         System.out.print("What is the triangle's height? ");
  90.         double height = s.nextDouble();
  91.         while (height <= 0) {
  92.             System.out.println("Invalid height!");
  93.             System.out.print("What is the rectangle's height? ");
  94.             height = s.nextDouble();
  95.         }
  96.         return (base * height) / 2;
  97.     }
  98.  
  99.     /**
  100.      * This will prompt the user to put in the triangle's
  101.      * base and height, then calculating the area.
  102.      *
  103.      */
  104.     public static void printArea(double someArea) {
  105.  
  106.         System.out.printf("The area is %.2f\n", someArea);
  107.  
  108.     }
  109.  
  110.     /**
  111.      * This will input the user's choice.
  112.      *
  113.      * @param choice the value of the option
  114.      * @param s Scanner object
  115.      */
  116.     public static void selectOption(int choice, Scanner s) {
  117.         if (choice == 1) {
  118.             double areaOfCircle = getCircleArea(s);
  119.             printArea(areaOfCircle);
  120.         } else if (choice == 2) {
  121.             double areaOfRectangle = getRectangleArea(s); // assign the return value of the method
  122.             printArea(areaOfRectangle); // then use that return value as parameter to another method
  123.         } else if (choice == 3) {
  124.             double areaOfTriangle = getTriangleArea(s);
  125.             printArea(areaOfTriangle);
  126.         } else {
  127.             System.out.println("Invalid choice made!");
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement