This project is a simple Java-based Shape Calculator that computes the area, perimeter, and volume (for 3D shapes) of various geometric shapes. The program is interactive and allows users to input dimensions to get corresponding results.
- Supports 2D and 3D shapes.
- Uses Object-Oriented Programming principles such as abstraction, inheritance, and interfaces.
- Includes an interactive console menu for user input.
- Implements an interface for volume calculations.
Defines the base class for all shapes, containing dimensions and methods that child classes override.
dim_one,dim_two,dim_three: Dimensions of the shape.num_sides: Number of sides of the shape.
Shape(): Initializes dimensions to 0.Shape(double dim_one, double dim_two, double dim_three, int num_sides): Initializes shape with given dimensions and number of sides.
abstract double calculateArea(): Abstract method to calculate area.abstract double calculatePerimeter(): Abstract method to calculate perimeter.int getNumSides(): Returns the number of sides.
Represents a triangle with a base and height.
Triangle(double base, double height): Initializes a triangle with 3 sides.
double calculateArea(): Computes area using0.5 * base * height.double calculatePerimeter(): Computes perimeter assuming a right triangle.
Represents a rectangle with length and breadth.
Rectangle(double length, double breadth): Initializes a rectangle with 4 sides.
double calculateArea(): Computes area usinglength * breadth.double calculatePerimeter(): Computes perimeter using2 * (length + breadth).
Represents a cube, implements Volume interface.
Cube(double side): Initializes a cube with 6 faces.
double calculateArea(): Computes total surface area using6 * side².double calculatePerimeter(): Computes perimeter using12 * side.double calculateVolume(): Computes volume usingside³.
Represents an equilateral pyramid, implements Volume interface.
Pyramid(double side, double height): Initializes an equilateral pyramid.
double calculateArea(): Computes total surface area.double calculatePerimeter(): Computes perimeter of the base.double calculateVolume(): Computes volume using1/3 * base area * height.
Represents a sphere, implements Volume interface.
Sphere(double radius): Initializes a sphere.
double calculateArea(): Computes surface area using4 * π * radius².double calculatePerimeter(): Computes circumference using2 * π * radius.double calculateVolume(): Computes volume using(4/3) * π * radius³.
Represents a cylinder, implements Volume interface.
Cylinder(double radius, double height): Initializes a cylinder.
double calculateArea(): Computes surface area using2πr(h + r).double calculatePerimeter(): Computes perimeter.double calculateVolume(): Computes volume usingπr²h.
Defines the method for volume calculation.
double calculateVolume(): Must be implemented by 3D shapes.
Contains the main method and user interaction menu.
- Displays menu for shape selection.
- Takes user input for dimensions.
- Computes and displays area, perimeter, and volume where applicable.
- Allows repeated use until exit is chosen.
- Compile all Java files:
javac Main.java
- Run the
Mainclass:java Main