Answer:
The java program is as follows.
import java.util.Scanner;
import java.lang.*;
public class Area
{
//variables to hold the values
static double a, b, c;
static double s;
static double area;
public static void main(String[] args) {
//scanner class object created
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first side: ");
a=sc.nextInt();
System.out.print("Enter the second side: ");
b=sc.nextInt();
System.out.print("Enter the third side: ");
c=sc.nextInt();
s=(a+b+c)/2;
//function of Math class used
area = Math.sqrt( s*(s-a)*(s-b)*(s-c) );
//result displayed with 3 decimal places
System.out.printf("The area of the triangle is %.3f", area);
}
}
OUTPUT
Enter the first side: 1
Enter the second side: 1
Enter the third side: 1
The area of the triangle is 0.433
Step-by-step explanation:
1. The variables to declare the three sides of the triangle, the semi-perimeter and the area of the triangle are declared with double datatype. All the variables are declared at class level and hence, declared with keyword, static.
2. Inside main(), an object of the Scanner class is created.
Scanner sc = new Scanner(System.in);
3. Using the Scanner class object, user input is taken for all the three sides of the triangle.
4. Following this, the semi-perimeter is computed as shown.
s=(a+b+c)/2;
5. The area of the triangle is computed using the given formula which is implemented as shown.
area = Math.sqrt( s*(s-a)*(s-b)*(s-c) );
6. The sqrt() method of the Math class is used while computing the area of the triangle.
7. The area is displayed with three decimals. This is done using the printf() method as shown.
System.out.printf("The area of the triangle is %.3f", area);
8. The program is saved with the same name as the name of the class having the main() method.
9. The class having the main() method is always public.