Untitled

 avatar
unknown
plain_text
2 years ago
4.7 kB
2
Indexable
import java.util.ArrayList;
import java.util.Scanner;

public class ShapeCalculator {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);	//user to get input from keyboard
		
		String useAgain = "";	//variable to determine whether to use the calculator again
		
		do{
			int choice = displayMenu(scanner);
			if(choice == 1) {		//Square
				System.out.println();
				System.out.println("SQUARE:");
				System.out.print("Square - Side Length: ");
				int squareSideLength = scanner.nextInt(); scanner.nextLine();
				int squareArea = squareSideLength * squareSideLength;
				
				System.out.println();
				System.out.println("The area of a square with");
				System.out.println("\tside length: " + squareSideLength);
				System.out.println("is " + squareArea);			
			} else if(choice == 2) { 	//Rectangle
				System.out.println();
				System.out.println("RECTANGLE:");
				System.out.print("Rectangle - Side Length 1: ");
				int rectSideLength1 = scanner.nextInt(); scanner.nextLine();
				System.out.print("Rectangle - Side Length 2: ");
				int rectSideLength2 = scanner.nextInt(); scanner.nextLine();
				int rectArea = rectSideLength1 * rectSideLength2;
				
				System.out.println();
				System.out.println("The area of a rectangle with");
				System.out.println("\tside length 1: " + rectSideLength1);
				System.out.println("\tside length 2: " + rectSideLength2);
				System.out.println("is " + rectArea);		
			}  else if(choice == 3) { 	//Triangle
				System.out.println();
				System.out.println("TRIANGLE:");
				System.out.print("Triangle - Base Length: ");
				int triBaseLength = scanner.nextInt(); scanner.nextLine();
				System.out.print("Triangle - Height: ");
				int triHeight = scanner.nextInt(); scanner.nextLine();
				double triArea = 0.5 * triBaseLength * triHeight;
				
				System.out.println();
				System.out.println("The area of a triangle with");
				System.out.println("\tbase length: " + triBaseLength);
				System.out.println("\theight: " + triHeight);
				System.out.println("is " + triArea);		
			} else if(choice == 4) { 	//Circle
				System.out.println();
				System.out.println("CIRCLE:");
				System.out.print("Circle - Radius: ");
				int circleRadius = scanner.nextInt(); scanner.nextLine();
				double circleArea = Math.PI * circleRadius * circleRadius;
				
				System.out.println();
				System.out.println("The area of a circle with");
				System.out.println("\tradius: " + circleRadius);
				System.out.printf("is %.2f\n", circleArea);		
			} else if(choice == 5) { 	//Trapezoid
				System.out.println();
				System.out.println("TRAPEZOID:");
				System.out.print("Trapezoid - Base 1: ");
				int trapezoidBase1 = scanner.nextInt(); scanner.nextLine();
				System.out.print("Trapezoid - Base 2: ");
				int trapezoidBase2 = scanner.nextInt(); scanner.nextLine();
				System.out.print("Trapezoid - Height: ");
				int trapezoidHeight = scanner.nextInt(); scanner.nextLine();
				double trapezoidArea = 0.5 * (trapezoidBase1 + trapezoidBase2) * trapezoidHeight;
				
				System.out.println();
				System.out.println("The area of a trapezoid with");
				System.out.println("\tbase length 1: " + trapezoidBase1);
				System.out.println("\tbase length 2: " + trapezoidBase2);
				System.out.println("\theight: " + trapezoidHeight);
				System.out.printf("is %.2f\n", trapezoidArea);		
			}
			
			//Checks with the user if he wants to use the calculator again
			//Continue asking user until he provides a valid answer - Y/N
			System.out.println();
			do {
				System.out.print("Would you like to use another equation? (Y/N): ");
				useAgain = scanner.nextLine();
			} while(!useAgain.equalsIgnoreCase("y") && !useAgain.equalsIgnoreCase("n"));
		} while (useAgain.equalsIgnoreCase("y"));
		
		System.out.println("\nThank you for using the Area Equation Calculator!");
	}
	
	/**
	 * Display the menu option to the user
	 * And ask for the equation the user wants to use
	 * @param scanner
	 * @return equation user will use
	 */
	public static int displayMenu(Scanner scanner){
		ArrayList<String> shapes = new ArrayList<String>();
		shapes.add("Square");
		shapes.add("Rectangle");
		shapes.add("Triangle");
		shapes.add("Circle");
		shapes.add("Trapezoid");
		
		System.out.println();
		System.out.println("AREA EQUATIONS: ");
		System.out.println();			
		for(int i=0; i<shapes.size(); i++) {
			System.out.println((i+1) + ". " + shapes.get(i));
		}
		System.out.println();
		System.out.print("Which equation would you like to use? (1-5): ");
		int equation = scanner.nextInt();
		scanner.nextLine();
		return equation;
	}

}
Editor is loading...