Calculating the area of the triangle is a common mathematical operation that can easily be implemented in Java. Implementing such operations improves the programming skills of an individual. Java is a reputable programming language that provides a platform for implementing these computations with ease. In this article, we will implement and discuss various methods for determining the area of a given triangle in Java.
How to Find/Calculate the Triangle Area in Java?
The area of a triangle can be computed by taking the product of base and height and dividing it by half. The length of any side of the triangle is the base while the height is calculated as the perpendicular distance of the base and opposite vertex as shown in the figure below:
Mathematical Formula
The mathematical formula is as follows:
Area of Triangle = 1/2 * (Base * Height)
Implementation in Java
The two double data type variables “base” and “height” are initialized with different values. The area of the triangle is determined by taking the product of base and height with 0.5 (½). This result is stored in the variable “areaTriangle” which is then printed as output on the console:
package areaOfTriangle;
public class areaOfTriangle {
public static void main(String[] args) {
double base = 19.2;
double height = 14.2;
//Two ways of Calculating Triangle
double areaTriangle = 0.5 * ( base * height );
double triangleArea = (base * height ) / 2;
//Printing the area of Triangle using 0.5
System.out.println("Area of triangle is " + areaTriangle); //Printing the area of Triangle using 1/2
System.out.println("Area of triangle is " + triangleArea);}
}
Output
How to Calculate the Equilateral Triangle Area in Java?
Equilateral triangles consist of three sides and each angle is equal in measure. The total of three interior angles will give a total of 180 degrees as shown in the diagram below:
Mathematical Formula
The Mathematical equation for calculating the area of an Equilateral triangle is as follows:
Area of an Equilateral Triangle = 3 / 4 * ( side) * (side)
Implementation in Java
In the following code, a “side” variable of double data type is initialized with the respective value. The variable “b” stores the result which is returned after calculating the square root of 3 and dividing it by 4. Finally, the variable “areaTriangle” stores the product of “b” with the square of the “side” variable. The print statements display the original and the rounded value of the “areaTriangle” variable:
package areaOfTriangle;
public class areaOfTriangle {
public static void main(String[] args) {
double side = 14.5;
double b = Math.sqrt(3) / 4;
//Calculating the area
double areaTriangle = b * ( side * side);
//Printing the area of Triangle
System.out.println("Area of triangle is " + areaTriangle );
//roundoff the area
System.out.println("Area of triangle is after rounding off " + Math.round(areaTriangle));
}
}
Output
How to Calculate the Isosceles Triangle Area in Java?
A triangle that has three sides (lengths) equal in measure is known as an Isosceles triangle. Similarly, the angles facing the two equal sides are also equal in the isosceles triangle. The total of the three interior angles of the isosceles triangles is 180 degrees.
Mathematical Formula
The formula is as follows:
Area of Isosceles Triangle = 1/4 * base * 4(a*a) – (b*b)
Implementation in Java
In this code, we will calculate the area of the isosceles triangle on the values provided by the user. For this purpose, include the Scanner class from the java.util package using the following import statement:
import java.util .Scanner; //import the Scanner class
In the main() method, the Scanner object “input” is created that accepts the input from the keyboard. The print statement displays the message to the user to enter the length of any two sides of the triangle. The value is then stored in the “sides” and “base” variables:
Scanner input = new Scanner (System.in); //Scanner’s object to read input
System.out.println("Enter the length of two equal sides");
double sides = input.nextDouble();
System.out.println("Enter the base length");
double base = input.nextDouble();
The double variable “isoscelesTriangle” calculates the area of the isosceles triangle by implementing the formula. The value is then printed on the console:
double isoscelesTriangle = (base/4) * Math.sqrt((4 * sides* sides) - (base * base));
System.out.println("Area of Isosceles triangle is "+ isoscelesTriangle);
Complete Code & Output
How to Find the Area of the Triangle Using the Heron’s Formula in Java?
Heron’s formula is practiced when the three lengths of the triangle are mentioned. Another popular name for Heron’s formula is “Hero Formula”. To implement the heron’s formula, the measure of the third side of the triangle must not be greater than the sum of the two sides of the triangle. The Mathematical formula is given as follows:
Area of Triangle = s(s-a) *(s-b) * (s-c)
Where “s” is as calculated by:
s= (a + b+ c)/ 2
Implementation in Java
The Scanner class is imported from the java.util package. The object “input” of the Scanner class is created inside the main() method for reading the user’s input. The print statement displays the message to the user to enter the three values for the sides of the triangle. The values provided are then stored in the variable “a”, “b” and “c”. The if-statement checks the condition if the 3rd side of the triangle is less than the total sum of the two sides. The double variable “s” stores the sum of three sides which is divided by 2. By using the Math.sqrt() function, Heron’s formula is implemented to calculate the triangle’s area when the data given contains 3 lengths of the triangle. The else block is executed if the “if” statement is not true.
package areaOfTriangle;
import java.util .Scanner; //importing the scanner class
public class areaOfTriangle {
public static void main(String[] args) // main method to execute the code {
Scanner input = new Scanner (System.in); //creating object of scanner class
//Taking input from the user
System.out.println("Enter the length of three side");
double a = input.nextDouble();
double b = input.nextDouble();
double c= input.nextDouble();
//if the sum of two sides is greater than the third one
if( a+ b > c && a+c > b && b+c > a )
{
double s = (a + b + c)/2;
double areaofTriangle = Math.sqrt(s * ((s - a ) * (s - b ) * ( s- c)));// Calculating the area of triangle using the heron’s formula
System.out.println("Area of Triangle Using the Heron's formula is " + areaofTriangle); //Printing the output
}
else
{
System.out.println("Area of Triangle cannot be calculated");
}
}
}
Output
How to Find the Area of a Triangle Using Trigonometry in Java?
Trigonometry is an essential pillar of Mathematics. We can implement the functions of trigonometry to calculate the area of a triangle in Java using the Math class. The formula for finding the area of a triangle using trigonometry in Java:
Area of Triangle = 1/2 * a * b * sin (Θ)
Implementation in Java
The following statement imports the Scanner class from the “java.util” package. The Scanner class provides different methods for taking input from the user:
import java.util .Scanner;
The instance of the Scanner class “input” invokes the nextDouble() method to read the input from the user. The angle input provided by the user is then converted in the radians by the Math.toRadians():
Scanner input = new Scanner (System.in);
//Taking user’s input
System.out.println("Enter the length for two side");
double a = input.nextDouble();
double b = input.nextDouble();
System.out.println("Enter the angle ");
double c= input.nextDouble();
//Calculating angle in radians
double angle = Math.toRadians(c);
The “areaofTriangle” stores the value that is calculated by taking the product of two sides given by the user with the 0.5 (½). The value is then multiplied by the angle’s value returned by the Math.sin() method. The output is then printed:
/calculate area
double areaofTriangle = 0.5* (a*b) * Math.sin(angle);
System.out.println("Area of Triangle is " + areaofTriangle);
Complete Code & Output
Here, we will sum up these methods for calculating a triangle’s area in Java.
Conclusion
To calculate the area of a triangle, use (base * height) / 2, implement Heron’s formula, or use the Math class methods etc, as shown in this article. There are several formulas available based on the data given for determining the area of a triangle. For instance, using Heron’s formula when three sides are given, trigonometric functions when the two sides and an angle is given, etc. This article provides a practical implementation of calculating the area for different types of triangles.