Building a Plane Shapes Calculator in Java: A Step-by-Step Guide

 


Plane Shapes Calculator in Java: A Comprehensive Guide

In this blog post, we'll explore a Java program that can help you calculate various properties of different plane shapes. Whether you're a student looking to practice geometry or a professional looking for a quick way to compute the area and perimeter of common shapes, this program can be an excellent tool.

What is the Plane Shapes Calculator?

The Plane Shapes Calculator is a Java program that provides users with the ability to calculate key geometric properties for a range of two-dimensional shapes. The program allows the user to select a shape, input necessary parameters (such as radius, side length, or height), and then it computes the area and perimeter of the chosen shape.

Features of the Program:

  1. User-friendly Interface: The program displays a simple menu that allows the user to choose which shape they want to calculate.
  2. Multiple Shapes: It supports a wide range of shapes, including circles, squares, rectangles, triangles, rhombuses, and more.
  3. Real-time Calculation: The program instantly computes and displays the area and perimeter once the necessary values are input.

How It Works:

The program uses a do-while loop to repeatedly present the user with a list of shapes. The user can then select a shape, input the required parameters, and view the results. This continues until the user chooses the option to exit.

Below is the complete code for the Plane Shapes Calculator program:

import java.util.Scanner;

class PlaneShapesCalculator {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;

do {
System.out.println("\nPlane Shapes Calculator");
System.out.println("1. Circle");
System.out.println("2. Square");
System.out.println("3. Rectangle");
System.out.println("4. Triangle");
System.out.println("5. Equilateral Triangle");
System.out.println("6. Rhombus");
System.out.println("7. Parallelogram");
System.out.println("8. Trapezoid");
System.out.println("9. Regular Pentagon");
System.out.println("10. Regular Hexagon");
System.out.println("11. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();

switch (choice) {
case 1:
calculateCircle(scanner);
break;
case 2:
calculateSquare(scanner);
break;
case 3:
calculateRectangle(scanner);
break;
case 4:
calculateTriangle(scanner);
break;
case 5:
calculateEquilateralTriangle(scanner);
break;
case 6:
calculateRhombus(scanner);
break;
case 7:
calculateParallelogram(scanner);
break;
case 8:
calculateTrapezoid(scanner);
break;
case 9:
calculateRegularPentagon(scanner);
break;
case 10:
calculateRegularHexagon(scanner);
break;
case 11:
System.out.println("Exiting the program. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 11);

scanner.close();
}

private static void calculateCircle(Scanner scanner) {
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();

double area = Math.PI * radius * radius;
double circumference = 2 * Math.PI * radius;

System.out.println("Area of the circle: " + area);
System.out.println("Circumference of the circle: " + circumference);
}

private static void calculateSquare(Scanner scanner) {
System.out.print("Enter the side length of the square: ");
double side = scanner.nextDouble();

double area = side * side;
double perimeter = 4 * side;

System.out.println("Area of the square: " + area);
System.out.println("Perimeter of the square: " + perimeter);
}

private static void calculateRectangle(Scanner scanner) {
System.out.print("Enter the length of the rectangle: ");
double length = scanner.nextDouble();
System.out.print("Enter the width of the rectangle: ");
double width = scanner.nextDouble();

double area = length * width;
double perimeter = 2 * (length + width);

System.out.println("Area of the rectangle: " + area);
System.out.println("Perimeter of the rectangle: " + perimeter);
}

private static void calculateTriangle(Scanner scanner) {
System.out.print("Enter the base of the triangle: ");
double base = scanner.nextDouble();
System.out.print("Enter the height of the triangle: ");
double height = scanner.nextDouble();
System.out.print("Enter the first side of the triangle: ");
double side1 = scanner.nextDouble();
System.out.print("Enter the second side of the triangle: ");
double side2 = scanner.nextDouble();
System.out.print("Enter the third side of the triangle: ");
double side3 = scanner.nextDouble();

double area = 0.5 * base * height;
double perimeter = side1 + side2 + side3;

System.out.println("Area of the triangle: " + area);
System.out.println("Perimeter of the triangle: " + perimeter);
}

private static void calculateEquilateralTriangle(Scanner scanner) {
System.out.print("Enter the side length of the equilateral triangle: ");
double side = scanner.nextDouble();

double area = (Math.sqrt(3) / 4) * side * side;
double perimeter = 3 * side;

System.out.println("Area of the equilateral triangle: " + area);
System.out.println("Perimeter of the equilateral triangle: " + perimeter);
}

private static void calculateRhombus(Scanner scanner) {
System.out.print("Enter the length of a side of the rhombus: ");
double side = scanner.nextDouble();
System.out.print("Enter the height of the rhombus: ");
double height = scanner.nextDouble();

double area = side * height;
double perimeter = 4 * side;

System.out.println("Area of the rhombus: " + area);
System.out.println("Perimeter of the rhombus: " + perimeter);
}

private static void calculateParallelogram(Scanner scanner) {
System.out.print("Enter the base of the parallelogram: ");
double base = scanner.nextDouble();
System.out.print("Enter the height of the parallelogram: ");
double height = scanner.nextDouble();
System.out.print("Enter the side length of the parallelogram: ");
double side = scanner.nextDouble();

double area = base * height;
double perimeter = 2 * (base + side);

System.out.println("Area of the parallelogram: " + area);
System.out.println("Perimeter of the parallelogram: " + perimeter);
}

private static void calculateTrapezoid(Scanner scanner) {
System.out.print("Enter the length of the first base of the trapezoid: ");
double base1 = scanner.nextDouble();
System.out.print("Enter the length of the second base of the trapezoid: ");
double base2 = scanner.nextDouble();
System.out.print("Enter the height of the trapezoid: ");
double height = scanner.nextDouble();
System.out.print("Enter the length of the first side of the trapezoid: ");
double side1 = scanner.nextDouble();
System.out.print("Enter the length of the second side of the trapezoid: ");
double side2 = scanner.nextDouble();

double area = 0.5 * (base1 + base2) * height;
double perimeter = base1 + base2 + side1 + side2;

System.out.println("Area of the trapezoid: " + area);
System.out.println("Perimeter of the trapezoid: " + perimeter);
}

private static void calculateRegularPentagon(Scanner scanner) {
System.out.print("Enter the side length of the regular pentagon: ");
double side = scanner.nextDouble();

double area = (0.25 * Math.sqrt(5 * (5 + 2 * Math.sqrt(5)))) * side * side;
double perimeter = 5 * side;

System.out.println("Area of the regular pentagon: " + area);
System.out.println("Perimeter of the regular pentagon: " + perimeter);
}

private static void calculateRegularHexagon(Scanner scanner) {
System.out.print("Enter the side length of the regular hexagon: ");
double side = scanner.nextDouble();

double area = (3 * Math.sqrt(3) / 2) * side * side;
double perimeter = 6 * side;

System.out.println("Area of the regular hexagon: " + area);
System.out.println("Perimeter of the regular hexagon: " + perimeter);
}
}

Breakdown of the Code:

  1. User Interaction: The program asks the user to select a shape from a list and enter relevant measurements.
  2. Shape Calculations: Based on the shape selected, the corresponding method calculates the area and perimeter using mathematical formulas.
  3. Exit Option: The program will continue running until the user selects the "Exit" option (choice 11).

Conclusion:

This Java program is a practical solution for anyone needing quick and easy access to geometric properties for common plane shapes. Whether you're building a similar application or simply learning more about geometric calculations, this code serves as an excellent resource. Happy coding!

Comments

Popular posts from this blog

A Journey Through the Milestones of Computing and Programming Languages

Exploring Software Development Paradigms: A Guide to Modern Approaches Introduction