/* * in this Java program, the user can calculate the area of a rectangle, * and the volumes of a cube, cylinder, and sphere. * The user will be prompt to enter the length and width of a rectangle, * and then choose a shape to calculate the volume of (cube, cylinder, or sphere). */ import java.util.InputMismatchException; import java.util.Scanner; class Rectangle { public static double rectangleArea(double length, double width) { return length * width; } } class Cube { public static double cubeVolume(double side) { return Math.pow(side, 3); } } class Cylinder { public static double cylinderVolume(double radius, double height) { return Math.PI * Math.pow(radius, 2) * height; } } class Sphere { public static double sphereVolume(double radius) { return (4.0 / 3.0) * Math.PI * Math.pow(radius, 3); } } // Main Class public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); try { // User prompt for the length and width System.out.println("Rectangle Area"); System.out.print("Please enter length: "); double length = scanner.nextDouble(); System.out.print("Please enter width: "); double width = scanner.nextDouble(); // making sure that the user put positive numbers otherwise we throw a //warning if (length <= 0 || width <= 0) { System.out.println("WARNING! Length and width must be positive numbers."); } else { double area = Rectangle.rectangleArea(length, width); System.out.println("Rectangle Area: " + area); } // Choose shape for volume calculation System.out.println(""); System.out.println("Volume Calculation"); System.out.println("Choose a shape to calculate volume:"); System.out.println("1. Cube"); System.out.println("2. Cylinder "); System.out.println("3. Sphere "); System.out.print("Enter your choice (1-3): "); int userChoice = scanner.nextInt(); // Let's give the User the choice of which volume shape they want to calculate. switch (userChoice) { case 1: // Cube System.out.print("Enter side length of cube: "); double side = scanner.nextDouble(); if (side <= 0) { System.out.println("WARNING! Side must be a positive number."); } else { double cubeVolume = Cube.cubeVolume(side); System.out.println("Cube Volume: " + cubeVolume); } break; case 2: // Cylinder System.out.print("Enter radius of cylinder: "); double cylinderRadius = scanner.nextDouble(); System.out.print("Enter height of cylinder: "); double cylinderHeight = scanner.nextDouble(); if (cylinderRadius <= 0 || cylinderHeight <= 0) { System.out.println("WARNING! Radius and height must be positive numbers."); } else { double cylinderVolume = Cylinder.cylinderVolume(cylinderRadius, cylinderHeight); System.out.println("Cylinder Volume: " + cylinderVolume); } break; case 3: // Sphere System.out.print("Enter radius of sphere: "); double sphereRadius = scanner.nextDouble(); if (sphereRadius <= 0) { System.out.println("WARNING! Radius must be a positive number."); } else { double sphereVolume = Sphere.sphereVolume(sphereRadius); System.out.println("Sphere Volume: " + sphereVolume); } break; default: System.out.println("Invalid choice."); } } catch (InputMismatchException e) { System.out.println("WARNING! Please enter valid numeric values."); } finally { scanner.close(); } } }