Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class MathProgram {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- String shape;
- double length;
- double width;
- double base;
- double radius;
- double height;
- System.out.println("Please choose a shape:\n 1. Square\n 2. Rectangle\n 3. Triangle\n 4. Circle");
- shape = input.nextLine();
- switch (shape) {
- case "Square":
- System.out.println("Please enter the length/width of your squre: ");
- length = input.nextDouble();
- System.out.println("The area of your square is " + squareArea(length));
- break;
- case "Rectangle":
- System.out.println("Please enter the length of your rectangle: ");
- length = input.nextDouble();
- System.out.println("Please enter the width of your rectangle: ");
- width = input.nextDouble();
- System.out.println("The area of your rectangle is " + rectangleArea(length, width));
- break;
- case "Triangle":
- System.out.println("Please enter the base of your triangle: ");
- base = input.nextDouble();
- System.out.println("Please enter the height of your triangle: ");
- height = input.nextDouble();
- System.out.println("The area of your triangle is " + triangleArea(base, height));
- break;
- case "Circle":
- System.out.println("Please enter the radius of your circle: ");
- radius = input.nextDouble();
- System.out.println("The area of your circle is " + circleArea(radius));
- break;
- }
- }
- public static double squareArea(double length) {
- double area;
- area = Math.pow(length, 2);
- return area;
- }
- public static double rectangleArea (double length, double width) {
- double area;
- area = length * width;
- return area;
- }
- public static double triangleArea(double height, double base) {
- double area;
- area = (height * base) / 2.0;
- return area;
- }
- public static double circleArea(double radius) {
- double area;
- area = (Math.PI * Math.pow(radius, 2));
- return area;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement