View All

- Post Graduate Certificate in Product Management from Duke CE
- Human Resource Analytics Course from IIM-K
- Global Master Certificate in Integrated Supply Chain Management
- CSM® Certification Training
- CSPO® Certification Training
- PMP® Certification Training
- SAFe® 6.0 Product Owner Product Manager (POPM) Certification

- Case Study using Tableau, Python and SQL
- Introduction to Tableau
- Data Science in E-commerce
- Introduction to Natural Language Processing
- Linear Algebra for Analysis
- Advanced SQL: Functions and Formulas
- Introduction to Database Design with MySQL
- Unsupervised Learning: Clustering
- Linear Regression - Step by Step Guide

- Introduction to Modern Public Relations
- Creating Consumer Value through Effective Marketing
- Build Successful Celebrity Partnerships & Campaigns
- Influencer Marketing from a Brand's Perspective
- Introduction to Advertising
- Getting Influencer Marketing Right
- Complete Guide to Content Marketing
- Ultimate Guide to Email Marketing
- Basics of Search Engine Optimization

- How to be a Successful Business Analyst
- Introduction to Design Thinking
- Investing in the Modern Era
- Introduction to International Business Environment
- Introduction to Entrepreneurship
- Digital Innovations in the Financial System
- Introduction to FinTech
- Introduction to HR Analytics
- Introduction to Research Methodology

Step by Step Java Tutorial Concepts - From Beginner to Pro

Tutorial Playlist

186. # Area of Triangle in Java

Content

- Introduction
- Overview
- Formulas To Find the Area of a Triangle
- Example 1 – Finding the Area of a Triangle in Java Using the Height and Base of the Triangle
- Example 2 – Finding the Area of Triangle in Java Using the Three Sides of the Triangle
- Example 3 - Finding the Area of a Triangle in Java Using Constructor

Java is an object-oriented programming language developers widely use for developing software applications.

Java offers platform independence, vast libraries, and frameworks, making it a popular choice among developers, especially for building robust, scalable, and portable applications for web, enterprise, mobile, and embedded systems.

You can easily compute the area of triangle in Java. This could be done using formulas such as ** area = (base * height) / 2**. For example, you can write a Java program that accepts user input for the base and height values and computes the triangle's area with these values' help.

In this tutorial, we’ll discuss several methods of finding the area of a triangle in Java. Keep reading to know more.

If we know the base (** b**) and the height (

**Area = (b * h) / 2**

If we know the lengths of all three sides (** a**,

**Area = sqrt(s * (s - a) * (s - b) * (s - c))**

where **s = (a + b + c) / 2**

If we know the coordinates of three vertices (** x1, y1), (x2, y2), (x3, y3**), we can use the shoelace formula:

**Area = 0.5 * |(x1y2 + x2y3 + x3y1) - (x2y1 + x3y2 + x1y3)|**

Here is an example of calculating the area of a triangle using the height and base of the triangle (base x height) in Java:

import java.util.Scanner;

public class upGradTutorials {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

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

double base = scanner.nextDouble();

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

double height = scanner.nextDouble();

double area = (base * height) / 2;

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

}

}

The program begins by importing the ** Scanner** class from

Inside the ** main** method, an instance of the

Similarly, the program prompts the user to enter the height of the triangle, reads the input value as a ** double**, and assigns it to the

Finally, the program displays the calculated area of the triangle by printing the concatenation of the string "** The area of the triangle is:** " with the value of the

Here is an example of calculating the area of a triangle using the lengths of its three sides (Heron's formula) in Java:

import java.util.Scanner;

public class upGradTutorials {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the length of side a:");

double a = scanner.nextDouble();

System.out.println("Enter the length of side b:");

double b = scanner.nextDouble();

System.out.println("Enter the length of side c:");

double c = scanner.nextDouble();

double s = (a + b + c) / 2;

double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));

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

}

}

In the above example, the program prompts the user to enter the lengths of the triangle's three sides. It reads these values as ** double** using the

Next, it calculates the semi-perimeter of the triangle (** s**) using the formula

Finally, the program displays the calculated area of the triangle by printing the concatenation of the string "** The area of the triangle is:** " with the value of the area variable.

Here is an example of calculating the area of a triangle in Java using a constructor:

import java.util.Scanner;

public class Triangle {

private double base;

private double height;

private double area;

public Triangle (double base, double height) {

this.base = base;

this.height = height;

}

public void calculateArea() {

area = (base * height) / 2;

}

public double getArea() {

return area;

}

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

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

double base = scanner.nextDouble();

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

double height = scanner.nextDouble();

Triangle triangle = new Triangle(base, height);

triangle.calculateArea();

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

}

}

This example defines a ** Triangle** class with

We also have two methods within the ** Triangle** class:

In the ** main** method, we prompt the user to enter the base and height of the triangle and read those values using the

Next, we call the ** calculateArea()** method on the

import java.util.Scanner;

public class upGradTutorials {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

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

double base = scanner.nextDouble();

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

double height = scanner.nextDouble();

double area = calculateTriangleArea(base, height);

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

}

public static double calculateTriangleArea(double base, double height) {

return (base * height) / 2;

}

}

In this program, we define a class called ** TriangleAreaCalculator**. The

The program then calls the ** calculateTriangleArea** method, passing the base and height as arguments. The

Finally, the calculated area is stored in the ** area** variable and displayed to the user using the

Here is an example of calculating the area of a triangle in Java using object-oriented programming:

import java.util.Scanner;

public class Triangle {

private double sideA;

private double sideB;

private double sideC;

public Triangle(double a, double b, double c) {

sideA = a;

sideB = b;

sideC = c;

}

public double calculateArea() {

double s = (sideA + sideB + sideC) / 2;

return Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC));

}

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the length of side A:");

double sideA = scanner.nextDouble();

System.out.println("Enter the length of side B:");

double sideB = scanner.nextDouble();

System.out.println("Enter the length of side C:");

double sideC = scanner.nextDouble();

Triangle triangle = new Triangle(sideA, sideB, sideC);

double area = triangle.calculateArea();

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

}

}

In the above program, we define a ** Triangle** class representing a

The ** Triangle** class has a constructor that takes

Additionally, the ** Triangle** class has a

In the ** main** method, we prompt the user to enter the lengths of the triangle's sides (

Next, we call the ** calculateArea() **method on the

The numerous ways of calculating the area of a triangle in Java have been discussed in this tutorial. You must enroll in a certified course if you want to use Java for more advanced functions. You could look for software development courses by upGrad. The courses on upGrad are well-designed for professionals and ensure you develop a holistic understanding of the subject. Enquire today to know more.

**What data types should I use for the base and height values?**

The base and height values can be represented using the double data type in Java, which can be decimal numbers.

**What is the constructor of the area of a triangle in Java?**

In Java, the constructor of the area of a triangle would typically be defined within a Triangle class and would take the base and height values as parameters. It would initialize the instance variables of the Triangle object with the provided values.

**Can I use the Math class in Java to calculate the area of a triangle?**

The Math class in Java provides various mathematical functions. However, you cannot use it directly to calculate a triangle's area. You will need to implement the formula manually.

Take the Free Java Quiz & See Your Rank!

PAVAN VADAPALLI

popular

Join 10M+ Learners & Transform Your Career

Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.

Talk to our experts. We’re available 24/7.

Indian Nationals

1800 210 2020

Foreign Nationals

+918045604032

upGrad facilitates program delivery and is not a college/university in itself. Credits and credentials are awarded by the university. Please refer relevant terms and conditions before applying.

Past record is no guarantee of future job prospects.