Writing a Java program to calculate the area of a triangle is one of the basic programming exercises to develop a coding sense for beginner programmers. Like many mathematical conceptual programs e.g. square root, factorial, or prime number this also serves as a good exercise for beginners. Now, if you remember in maths you might have seen two main ways to calculate the area of a triangle, using vertices and using base and height. In this program, I have created two methods to calculate the area of a triangle using both ways.
In the first method area(Point a Point b, Point c) we expect coordinates of three vertices of the triangle and then we calculate the area of the triangle using the formula (Ax(By -Cy) + Bx(Cy -Ay) + Cx(Ay - By))/2, while in the second method, area(int base, int height) we expect the value of base and height and then we calculate are of the triangle using formula (base * height) / 2.
Area of triangle = (Ax(By -Cy) + Bx(Cy -Ay) + Cx(Ay - By))/2
Here A, B,and C are three vertices of the triangle and x and y represent coordinates.
We'll use the following formula:
Area of triangle = (base * height) / 2
Where the base is the length of the base and height is the height of the triangle as shown in the following diagram:
Now that you know the formulas to calculate the area of a triangle in Java, we'll see the code which implements these formulas. Remember, you can use these formulas to calculate the area of any type of triangle e.g. right-angle triangle, equilateral triangle, etc. These are generic formulas and work for all types of triangles.
Calculating the area of a triangle using 3 points
In this program, you need to write a method that accepts three points, you can create a Point class to encapsulate both X and Y coordinates and then calculate the area of the triangle using the following formula:Area of triangle = (Ax(By -Cy) + Bx(Cy -Ay) + Cx(Ay - By))/2
Here A, B,and C are three vertices of the triangle and x and y represent coordinates.
Calculating area of a triangle using base and height
This one is easier to remember as you might have used this formula lot many times before. In this part of the program, you write a method that expects two integer values to capture base and height and return a float which is the area of a triangle.We'll use the following formula:
Area of triangle = (base * height) / 2
Where the base is the length of the base and height is the height of the triangle as shown in the following diagram:
Now that you know the formulas to calculate the area of a triangle in Java, we'll see the code which implements these formulas. Remember, you can use these formulas to calculate the area of any type of triangle e.g. right-angle triangle, equilateral triangle, etc. These are generic formulas and work for all types of triangles.
Java Program to calculate the area of a triangle
Here is our complete Java program to find the area of a triangle given base and height or points of three vertices. I have created a Point class to represent a point that has both X and Y coordinates and two overloaded area() methods to calculate the area of a triangle.The first area() method expects three parameters, which are points of three vertices and then it return a float value which is the area of a triangle. The second area() method takes base and height and returns a float value which is the area of a triangle.
/* * Java Program to calculate area of triangle using co-ordinates of vertices * or by using base and height. */ public class Main { public static void main(String[] args) { Point A = new Point(13, 34); Point B = new Point(22, 21); Point C = new Point(11, 19); System.out.println("area of triangle using formula 1: " + Triangle.area(A, B, C)); System.out.println("area of triangle using formula 2: " + Triangle.area(3, 5)); } } class Point { int x; int y; public Point(int x, int y) { this.x = x; this.y = y; } } class Triangle { /** * Java method to return area of triangle using vertices as per following * formula area = (Ax(By -Cy) + Bx(Cy -Ay) + Cx(Ay - By))/2 * * @return */ public static float area(Point A, Point B, Point C) { float area = (A.x * (B.y - C.y) + B.x * (C.y - A.y) + C.x * (A.y - B.y)) / 2.0f; return Math.abs(area); } /** * * @param base * @param height * @return */ public static float area(int base, int height) { return (base * height) / 2.0f; } } Output area of triangle using formula 1: 80.5 area of triangle using formula 2: 7.5
That's all about how to calculate the area of a triangle in Java. This is a good exercise to learn to program along with many others which I have shared below. We have learned both ways to calculate the area of a triangle in the program i.e. using 3 points of vertices as well as by using the base and height formula.
Othe Java Programs you may like to practice
You can further read Concrete Mathematics: A Foundation for Computer Science to learn more about how mathematics plays an important role in Computer Science and programming.
Othe Java Programs you may like to practice
- Fibonacci series in Java
- Prime number checking in Java
- String Palindrome Problem
- Number Palindrome Problem
- Armstrong number Checking
- Calculating Factorial in Java
- Reverse String Problem
- Calculating GCD in Java
- Leap year Problem
You can further read Concrete Mathematics: A Foundation for Computer Science to learn more about how mathematics plays an important role in Computer Science and programming.
Useful tool to create binary trees:
ReplyDeletehttps://davidpynes.github.io/Tutorials/Trees/Tree_05/
Nice
ReplyDelete