Calculate area of Triangle in Java
Write a Java program to calculate
Area of Triangle, accept input from User and display output in Console is
one of the frequently asked homework question. This is not a tough
programming exercise but a good exercise for beginners and anyone who has
started working in Java. For calculating area of triangle using formula 1/2(base*height), you need
to use arithmetic operator. By doing this kind of exercise you will understand
how arithmetic operators works in Java, subtle details which affect calculation
like precedence and associativity of operator etc. Fortunately this question
is not very popular on Java
interview like other exercise we have seen e.g. Java
program to print Fibonacci series and Java
program to check for palindrome. Nevertheless its a good Java homework.
How to calculate area of Triangle in Java
In this section we will see complete code example of How to calculate area of Triangle in Java. We are going to use
classic formula 1/2(base*height), where base and height will be
accepted as user
input using java.util.Scanner class. Arithmetic operator used in this
exercise is multiplication and division i.e * and /.
/**
*
* Java program to calculate area of Triangle .
* Formula for calculating area of Triangle is 1/2(base*height)
*
* @author Javin Paul
*/
public class AreaOfTriangle
{
public static void main(String args[]) {
Scanner
scanner = new Scanner(System.in);
System.out.println("Please enter base width of
Triangle ");
float base = scanner.nextFloat();
System.out.println("Please enter height of
Triangle ");
float height = scanner.nextFloat();
//calculating area of Triangle in Java
float area = area(base, height);
System.out.println("Area of Triangle calculated by
Java program is : " + area);
}
public static float area(float base, float height){
return (base* height)/2;
}
}
Output
Please enter base width of Triangle
20
Please enter height of Triangle
30
Area of Traingle calculated by Java program is : 300.0
That's
the whole Java program to calculate area of triangle and Now let me introduce
with one of the most common
mistake Java programmer make while writing this code. If you look closely
to area() method you will find that we have wrapped code into small bracket i.e. (base*
height)/2 . We have done that to increase precedence, anything which is inside
bracket will be executed first. Just change the code, like you write in
notebook e.g. 1/2*base*height and suddenly your calculate area
for triangle become zero, as shown below :
public static
float area(float base, float
height){
return 1/2*base*height;
}
Please enter base width of Triangle
10
Please enter height of Triangle
40
Area of Traingle calculated by Java program is : 0.0
For a beginner in Java, this is
not an easy bug to find or spot on. In order to find the bug you must know how
Java evaluates an arithmetic expression. Here two arithmetic operator * and
/ are used which are right associative i.e. evaluated from right to left and of
same precedence. first expression which is evaluated by Java is 1/2 which is
division of two integer and result of this would be an integer i.e. zero and
not 0.5, which is making whole area zero. This is happening because of both operands of division operator(/) is
integer, if any of them is float or double
then result would be a floating point number. by combining (base*height) in bracket
we ensure that it executed first and produce a floating point number which can
be divided by two. This kind of simple Java
mistakes can only be avoided by learning and writing such kind of program.
That's why programs like How to calculate square root in Java should be in list
of Java learner.
That's all on How to calculate area of Triangle in Java. We have seen
complete code and also analyzed possible but and How arithmetic expression is
evaluate in Java especially multiplication and division operator.
Other Java programming and homework exercise
Nice java programs.
BalasHapusThanks,
https://www.flowerbrackets.com/java-program-to-print-armstrong-number-from-1-to-1000/