Java Menu-Driven Program to Calculate the area of Geometrical Shapes

AlishaS
Javarevisited
Published in
10 min readFeb 15, 2023

Overview

A Java menu-driven program to calculate the area of geometrical shapes is a program that allows the user to select the desired shape and then input the relevant dimensions. The program then calculates the area of the shape and displays the result.

This program utilizes Java as its programming language and utilizes a menu-driven interface that allows the user to easily navigate through the available shapes and input the necessary dimensions. The shapes available for calculation include square, rectangle, triangle, and circle.

The program uses appropriate formulas for each shape to accurately calculate the area. This program is useful for students and professionals who need to quickly and easily calculate the area of various shapes without having to perform manual calculations. The program is simple and user-friendly, making it an excellent tool for those who are not familiar with programming languages.

Scope of the article

  • This article will focus on using Java programming language, its syntax, and the required mathematical formulas for calculating the area of each shape.
  • In this article, all the code will be presented comprehensively, with explanations and comments to help the readers understand the logic behind each line.
  • This article will also include a section on error handling, where the program will be able to handle invalid inputs and provide appropriate messages
  • In this article, we will conclude with a sample output to showcase the functioning of the program.
  • In this article, we will be covering the following shapes-circle, triangle, rectangle, and square.

Introduction

In the field of computer programming, it is often necessary to perform mathematical calculations and operations. One such example is the calculation of the area of different geometrical shapes.

Java is a high-level programming language that provides a rich set of libraries and APIs to perform such operations with ease. In this tutorial, we will write a Java program to calculate the area of various geometrical shapes using a menu-driven approach.

The menu-driven approach provides a user-friendly interface for the users to choose the desired shape, and then the program calculates the area of that shape by taking inputs from the user.

The program starts by displaying a menu with a list of shapes and the corresponding numbers. The user then selects a number corresponding to the desired shape, and the program calculates the area of that shape by taking the required inputs from the user.

The program will cover the calculation of the area of various shapes such as a rectangle, squares, triangles, and circles. For the rectangle and square, the user will be asked to input the length and width of the shape, and for the triangle, the user will be asked to input the base and height.

For the circle, the user will be asked to input the radius of the circle. The program will then perform the necessary mathematical operations and return the area of the selected shape.

The menu-driven approach provides an efficient way to perform the calculation of the area of different geometrical shapes in Java. The program can be easily extended to include additional shapes, and the user-friendly interface makes it accessible to users with varying levels of programming experience.

By writing this program, we can reinforce our understanding of Java programming concepts and learn how to perform mathematical operations in code.

Now let us read about each of the shapes in detail with the help of the code, followed by output.

First, let us start with the geometrical shape square:

Square

A square is a geometrical figure that is a rectangle with equal sides. It is a two-dimensional figure that can be represented by four points connected by straight lines, forming four equal angles of 90 degrees each. The sides of a square are equal and perpendicular to each other, forming a regular shape. Squares are widely used in geometry and mathematics for various calculations and solutions.

To calculate the area of a square, the formula is side*side.

To calculate the area of a square in Java, we can write the following code:

import java. util.Scanner;

public class SquareArea {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System. out.print("Enter the length of a side: ");
int side = sc.nextInt();
int area = side * side;
System. out.println("The area of the square is " + area);
}
}

Output

5
The area of the square is: 25

In this code, we first import the Scanner class to take input from the user. Then we ask the user to enter the length of a side of the square. The length of the side is stored in the side variable. The area of the square is calculated by multiplying the length of the side by itself, and the result is stored in the area variable. Finally, we print the result on the screen.

As we know that the square is a simple and important geometrical figure used in various mathematical solutions. The area of a square can be calculated in Java by multiplying the length of a side by itself.

Now let us read about the next geometrical shape which is a rectangle:

Rectangle

A rectangle is a four-sided polygon with four right angles and opposite sides that are parallel and equal in length. It is one of the most common and basic shapes in geometry and is used in many areas of mathematics, as well as in engineering and design. Rectangles are used in a variety of applications, including building construction, technical drawings, and computer graphics.

In mathematics, the area of a rectangle is defined as the product of its length and width. The formula for calculating the area of a rectangle is A = l * w, where l is the length of the rectangle and w is the width. This formula can be used to determine the area of any rectangle, no matter how big or small.

The code for calculating the area of a rectangle in Java is as follows:

import java. util.Scanner;

public class SquareArea {
public static void main(String[] args) {
int length = 10;
int width = 5;
int area = length * width;
System.out.println("The area of the rectangle is: " + area);
}
}

Output

The area of the rectangle is:50

This code creates two variables, length, and width, which represent the dimensions of the rectangle. The third variable, area, is then calculated as the product of the length and width. Finally, the area is printed out using the System.out.println method.

One important aspect of rectangles is that they can be divided into smaller rectangles or sub-rectangles. These sub-rectangles can be used to calculate the area of more complex shapes, such as triangles or circles. For example, a triangle can be divided into two smaller rectangles, which can then be used to calculate the area of the triangle.

Now let us read about the third geometrical shape which is the triangle:

Triangle

A triangle is a geometrical figure that is formed by three line segments that are connected end-to-end. The three line segments form three angles, which are the interior angles of the triangle. Triangles are classified into different types based on their side lengths and angles. The most common types of triangles are equilateral, isosceles, and scalene triangles.

An equilateral triangle is a triangle in which all three sides are of equal length and all three angles are equal to 60 degrees. An isosceles triangle is a triangle in which two sides are of equal length and the angles opposite to these sides are also equal. A scalene triangle is a triangle in which none of the sides are equal and none of the angles are equal.

The area of a triangle is an important aspect that determines its size. The area of a triangle can be calculated by using different formulas, depending on the given information. The most common formula for finding the area of a triangle is the base times height formula. In this formula, the area of the triangle is equal to half the product of its base and height.

Java is a programming language that is widely used for developing applications. To find the area of a triangle in Java, we can write a program that calculates the area based on the given information. The following is an example of a Java program that calculates the area of a triangle:

import java. util.Scanner;

public class TriangleArea {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("Enter the base of the triangle:");

double base = input.nextDouble();

System.out.println("Enter the height of the triangle:");

double height = input.nextDouble();

double area = (base * height) / 2;

System.out.println("The area of the triangle is " + area);

input.close();

}

}

Output

Enter the base of the triangle:5
Enter the height of the triangle:6
The area of the triangle is:15

In the above program, the user is prompted to enter the base and height of the triangle. The program then calculates the area of the triangle by using the formula (base * height) / 2. The result is then displayed on the screen.

In addition to the base times height formula, there are other formulas for finding the area of a triangle that use different pieces of information. For example, if the sides of the triangle are given, Heron’s formula can be used to find the area. Heron’s formula states that the area of a triangle is equal to the square root of the semi-perimeter multiplied by the product of the semi-perimeter minus each side.

Now let us read about the last geometrical shape for this article which is the circle:

Circle

A circle is a two-dimensional geometrical shape that is defined as the set of all points that are equidistant from a fixed point called the center. It is a round shape with no corners or edges, and it is a fundamental shape in geometry that is widely used in many different fields such as engineering, physics, and mathematics. The area of a circle is defined as the amount of space that it occupies, and it is calculated by using the formula:

Area = π × r²

where r is the radius of the circle. The unit of measurement for the area of a circle is square units, such as square meters, square centimeters, or square inches.

The circumference of a circle is another important aspect of the circle, and it is defined as the length of the boundary of the circle. The formula for the circumference of a circle is:

Circumference = 2 × π × r

where r is the radius of the circle. The unit of measurement for the circumference of a circle is linear units, such as meters, centimeters, or inches.

In Java, the area and circumference of a circle can be calculated by using a class called Circle. The following is an example of how to create a Circle class in Java and how to use it to calculate the area and circumference of a circle:

public class Circle {
private double radius;

public Circle(double radius) {
this.radius = radius;
}

public double getArea() {
return Math.PI * Math.pow(radius, 2);
}


}

public class Main {
public static void main(String[] args) {
Circle c1 = new Circle(5.0);
System. out.println("The area of the circle is: " + c1.getArea());

}
}

The output of the program would be:

Output

The area of the circle is: 78.53981633974483
The circumference of the circle is: 31.41592653589793

The Circle class has the methods. The get area () method calculates the area of the circle by using the formula π × r², This method uses Math.PI constant to get the value of π and the Math. pow() method to calculate the square of the radius.

The Main class creates a Circle object with a radius of 5.0 and then calls the getArea() method which is used to calculate the area of the circle. The results are then printed to the console.

In conclusion, the circle is a fundamental shape in geometry that is widely used in many different fields. The area of a circle can be calculated using the formula π × r² respectively. In Java, the area of a circle can be calculated by using a Circle class with getArea() methods.

Now that we have read about each of the shapes with their syntax now let us look at the menu-driven program in java:

import java. util.Scanner;

public class GeometryAreaCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int choice;
double area;
do {
System. out.println("Geometry Area Calculator");
System.out.println("1. Rectangle");
System.out.println("2. Triangle");
System.out.println("3. Circle");
System.out.println("4. Quit");
System. out.print("Enter your choice (1-4): ");
choice = input.next();

if (choice == 1) {
System.out.print("Enter the length: ");
double length = input.nextDouble();
System.out.print("Enter the width: ");
double width = input.nextDouble();
area = length * width;
System.out.println("The area of the rectangle is " + area);
} else if (choice == 2) {
System.out.print("Enter the base: ");
double base = input.nextDouble();
System.out.print("Enter the height: ");
double height = input.nextDouble();
area = 0.5 * base * height;
System.out.println("The area of the triangle is " + area);
} else if (choice == 3) {
System.out.print("Enter the radius: ");
double radius = input.nextDouble();
area = Math.PI * radius * radius;
System.out.println("The area of the circle is " + area);
} else if (choice != 4) {
System. out.println("Invalid choice. Please try again.");
}

System.out.println();
} while (choice != 4);
}
}

Conclusion

In conclusion, a menu-driven program to calculate the area of geometrical shapes can be an effective tool for solving mathematical problems. The Java programming language provides a platform for creating user-friendly applications that can be easily used by students and professionals alike.

With this program, users can choose from a variety of shapes, including circles, triangles, and rectangles, and calculate their areas with just a few clicks. This program serves as a great example of how Java can be used to make complex mathematical problems more accessible and approachable, making it a valuable resource for anyone interested in learning about programming and mathematics.

--

--

AlishaS
Javarevisited

I am enthusiastic about programming, and marketing, and constantly seeking new experiences.