Advertisement
IanO-B

Untitled

Mar 5th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. public class Main {
  2.  
  3. public static void main(String[] args) {
  4. double a;
  5.  
  6. a = triangleArea(3, 3, 3);
  7. System.out.println("A triangle with sides 3,3,3 has an area of " + a );
  8.  
  9. a = triangleArea(3, 4, 5);
  10. System.out.println("A triangle with sides 3,4,5 has an area of " + a );
  11.  
  12. a = triangleArea(7, 8, 9);
  13. System.out.println("A triangle with sides 7,8,9 has an area of " + a );
  14.  
  15. System.out.println("A triangle with sides 5,12,13 has an area of " + triangleArea(5, 12, 13) );
  16. System.out.println("A triangle with sides 10,9,11 has an area of " + triangleArea(10, 9, 11) );
  17. System.out.println("A triangle with sides 8,15,17 has an area of " + triangleArea(8, 15, 17) );
  18. System.out.println("A triangle with sides 9,9,9 has an area of " + triangleArea(9, 9, 9));
  19. //It is easier to add a new call to the code with the formula, instead of having to manually re-do all the calculations in code.
  20. }
  21.  
  22. public static double triangleArea( int a, int b, int c )
  23. {
  24.  
  25. double s, A;
  26.  
  27. s = (a+b+c) / 2.0;
  28. A = Math.sqrt( s*(s-a)*(s-b)*(s-c) );
  29.  
  30. return A;
  31.  
  32. //Both the code with the formula and the one without the formula give the same results
  33. //No Formula has 38 lines of code, Formula has 15 lines of code
  34. //It's easier to alter the calculations when the code is in a formula format, due to it needing to be changed only once instead of every time you need to use the formula.
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement