Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Geometry {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int choice;
- do {
- displayMenu(); // Call display menu
- System.out.print("Enter choice (1-3): ");
- choice = in.nextInt();
- selectOption(choice, in);
- } while ((choice < 1) || (choice > 3));
- System.out.println("Thanks for using the Geometry Calculator - Goodbye!");
- }
- /**
- * This will display the welcome message and your choice for the
- * three options (circle, triangle, or rectangle).
- */
- public static void displayMenu() {
- System.out.println("Welcome to the Geometry Calculator Application");
- System.out.println("1. Calculate the Area of a Circle");
- System.out.println("2. Calculate the Area of a Rectangle");
- System.out.println("3. Calculate the Area of a Triangle");
- }
- /**
- * This will prompt the user to put in the circle's radius,
- * then calculate the area.
- *
- * @param s Scanner object
- * @return the area
- */
- public static double getCircleArea(Scanner s) {
- System.out.print("What is the circle's radius? ");
- double radius = s.nextDouble();
- while (radius <=0) {
- System.out.println("Invalid radius entered.");
- System.out.print("What is the circle's radius? ");
- radius = s.nextDouble();
- }
- return Math.PI * Math.pow(radius, 2);
- }
- /**
- * This will prompt the user to put in the rectangle's length
- * and width. Afterwards calculating the area.
- *
- * @param s Scanner object
- * @return the area
- */
- public static double getRectangleArea(Scanner s) {
- System.out.print("What is the rectangle's length? ");
- double length = s.nextDouble();
- while (length <= 0) {
- System.out.println("Invalid length!");
- System.out.print("What is the rectangle's length? ");
- length = s.nextDouble();
- }
- System.out.print("What is the rectangle's width? ");
- double width = s.nextDouble();
- while (width <= 0) {
- System.out.println("Invalid width!");
- System.out.print("What is the rectangle's width? ");
- width = s.nextDouble();
- }
- return length * width;
- }
- /**
- * This will prompt the user to put in the triangle's
- * base and height, then calculating the area.
- *
- * @param s Scanner object
- * @return the area
- */
- public static double getTriangleArea(Scanner s) {
- System.out.print("What is the triangle's base? ");
- double base = s.nextDouble();
- while (base <= 0) {
- System.out.println("Invalid base!");
- System.out.print("What is the rectangle's base? ");
- base = s.nextDouble();
- }
- System.out.print("What is the triangle's height? ");
- double height = s.nextDouble();
- while (height <= 0) {
- System.out.println("Invalid height!");
- System.out.print("What is the rectangle's height? ");
- height = s.nextDouble();
- }
- return (base * height) / 2;
- }
- /**
- * This will prompt the user to put in the triangle's
- * base and height, then calculating the area.
- *
- */
- public static void printArea(double someArea) {
- System.out.printf("The area is %.2f\n", someArea);
- }
- /**
- * This will input the user's choice.
- *
- * @param choice the value of the option
- * @param s Scanner object
- */
- public static void selectOption(int choice, Scanner s) {
- if (choice == 1) {
- double areaOfCircle = getCircleArea(s);
- printArea(areaOfCircle);
- } else if (choice == 2) {
- double areaOfRectangle = getRectangleArea(s); // assign the return value of the method
- printArea(areaOfRectangle); // then use that return value as parameter to another method
- } else if (choice == 3) {
- double areaOfTriangle = getTriangleArea(s);
- printArea(areaOfTriangle);
- } else {
- System.out.println("Invalid choice made!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement