Area of Triangle in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

In Java, determining the area of a triangle is ideal for those new to programming. Java's straightforward syntax and powerful libraries allow for easy implementation of various methods to calculate this area, whether you have two or three sides of the triangle. Beginners can learn through simple Java programs that accept user inputs, while more advanced users can explore static or user-defined methods and constructors. This article delves into these methods, highlighting Java's user-friendly nature for mathematical calculations.

Formulas to find the Area of Triangle

  • Using Base and Height of the Triangle: The area of the triangle can be found if the base and height of the triangle are known using a simple formula:
12BaseHeight\frac{1}{2} * Base * Height
  • Heron's Formula: When all the sides of the three sides of the Triangle are known, We can make use of Heron's formula to calculate the area of the given triangle.
s(sa)(sb)(sc)\sqrt{s(s-a)(s-b)(s-c)}

Here, a, b and, c are the sides of the triangle and s is the semi perimeter (half of the perimeter i.e a+b+c2\frac{a+b+c}{2} ).

Example 1 - Finding Area of Triangle in Java Using the height and base of the triangle

To find the area using the height and base of the triangle, we will make use of the primitive 12BaseHeight\frac{1}{2} * Base * Height formula.

Now we will see how to implement this in Java.

Code:

Output:

Explanation: In the above code, We used the simple 12\frac{1}{2} * BaseHeightBase * Height formula to calculate the area of the triangle.

Example 2 - Finding Area of Triangle in Java using the three sides of the triangle

When three sides of the triangle are given, first we will check whether the triangle exists or not by comparing the sum of two sides of the triangle with the other side of the triangle. Later, The semi-perimeter is calculated and then the area of the triangle is calculated using Heron's formula.

Now we will see how to implement this in Java.

Code:

Output:

  • Output 1:
  • Output 2:

    If the user enters an invalid dimensions.

Explanation:

  • In the above code, the user enters the length of three sides of the triangle.
  • First, we will check if the sum of the any two sides of the triangle is greater than the third side. If the condition is true, We will make use of Heron's formula to find the area of the triangle.

Example 3 - Finding Area of Triangle in Java using Constructor

Constructors are specialized method used to initialize objects. The constructor and the class must have the same name. Constructors are invoked using the new operator.

We will create a constructor that will calculate the area of the triangle using the formula 12\frac{1}{2} * BaseHeightBase * Height. In the main() method, we will invoke/call the constructor while creating a Triangle object. the constructor calculates the area while object creation.

Now we will see how to implement this in Java.

Code:

Output:

Explanation:

  • In the above code, we have created a Triangle class which represents a triangle using its base and height. When an object of Triangle class is created, we find the area of the triangle using the simple formula 12BaseHeight\frac{1}{2} * Base * Height in the constructor call itself.
  • In the main method, using the new operator we invoked the Triangle constructor to find the area of the triangle using its base and height.

Example 4 - Finding Area of Triangle in Java using User-Defined Method

Methods or functions in java are a set of statements grouped to perform an operation. There are mainly 2 types of functions viz, in-built methods and user-defined methods. When a user creates a method, then it is known as a user-defined method.

We will create a static method named areaTriangle() which takes base and height as its parameters and returns the area of the triangle. In the main() method, we will call the areaTriangle method and pass the value of base and height entered by the user.

Now we will see how to implement this in Java.

Code:

Output:

Explanation: In the above code, we created a user-defined method named areaTriangle() which returns a double value. The method takes breadth and height as the parameters and returns the value of 12\frac{1}{2} * BaseHeightBase * Height i.e, area of the triangle.

Example 5 - Finding Area of Triangle in Java using Object-Oriented Programming

Object-oriented programming deals with creating objects rather than writing procedures. Objects contain both data and methods.

We will make use of the object and classes concept in object-oriented programming to find the area of the triangle. First, we will create a class named area with the variables b and h for base and height. A method named void area() is created which calculates the area of the triangle. In the main() method, We will create an instance of the class (object) named tr and we will calculate the area of the triangle.

Now we will see how to implement this in Java.

Code:

Output:

Explanation: In the above code, We created a class named Triangle, and in the main() function, we created an instance of the class aka object to find the area of the triangle using the area() method defined in the class.

Conclusion

  • The area of the triangle can be found using the simple 12BaseHeight\frac{1}{2} * Base * Height formula when the base and height of the triangle are known.
  • When all the sides of the triangle are known, we can make use of the formula s(sa)(sb)(sc)\sqrt{s(s-a)(s-b)(s-c)} (Heron's formula) to find the area of the triangle.
  • The area of the triangle can be found using constructor in Java, user-defined methods, and Object-Oriented Programming.