belongs to collection: Method/Function programming exercises in Java
Write a Java method to calculate the area of a triangle
In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..
import java.util.Scanner; public class Javaexcercise { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Input Side-1: "); double side1 = in.nextDouble(); System.out.print("Input Side-2: "); double side2 = in.nextDouble(); System.out.print("Input Side-3: "); double side3 = in.nextDouble(); System.out.println( isValid(side1, side2,side3) ? "The area of the triangle is " + areaoftriangle(side1, side2, side3) : "Invalid triangle" ); } public static boolean isValid(double side1, double side2, double side3) { if( side1 + side2 > side3 && side2 + side3 > side1 && side1 + side3 > side2) return true; else return false; } public static double areaoftriangle(double side1, double side2, double side3) { double area = 0; double s = (side1 + side2 + side3)/2; area = Math.sqrt(s*(s - side1)*(s - side2)*(s - side3)); return area; } }
Result:
Input Side-1: 10
Input Side-2: 12
Input Side-3: 13
The area of the triangle is 56.99506557588999
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..
Result:
Input Side-1: 10
Input Side-2: 12
Input Side-3: 13
The area of the triangle is 56.99506557588999
need an explanation for this answer? contact us directly to get an explanation for this answer