Skip to content

Commit 2df4690

Browse files
committed
Added exception for parameters
1 parent ef10bb9 commit 2df4690

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/main/java/com/thealgorithms/maths/HeronsFormula.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ public class HeronsFormula {
1414
* @author satyabarghav
1515
*/
1616
public static double Herons(double a, double b, double c) {
17-
double s = (a + b + c) / 2.0;
18-
double area = 0;
19-
area = Math.sqrt((s) * (s - a) * (s - b) * (s - c));
20-
return area;
17+
if(!(a + b > c && b + c > a && c + a > b)){
18+
throw new IllegalArgumentException("Triangle can't be formed with the given side lengths");
19+
}else{
20+
double s = (a + b + c) / 2.0;
21+
double area = 0;
22+
area = Math.sqrt((s) * (s - a) * (s - b) * (s - c));
23+
return area;
24+
}
2125
}
2226
}

0 commit comments

Comments
 (0)