Assignment #92 and Heron's Formula
Code
/// Name: Ali Kurland
/// Period: 6
/// Program Name: Heron's Formula
/// File Name: HeronsFormula.java
/// Date Finished: 11/16/2015
/// 1. The two programs produce exactly the same output.
/// 2. HeronsFormulaNoFunction.java is 50 lines long, whereas HeronsFormula.java is only 30 lines long.
/// 3. It was much easier to fix the file that didn't use a function because the bug only had to be fixed in one place.
public class HeronsFormula
{
public static void main( String[] args )
{
double a;
a = triangleArea(3, 3, 3);
System.out.println("A triangle with sides 3,3,3 has an area of " + a );
a = triangleArea(3, 4, 5);
System.out.println("A triangle with sides 3,4,5 has an area of " + a );
a = triangleArea(7, 8, 9);
System.out.println("A triangle with sides 7,8,9 has an area of " + a );
System.out.println("A triangle with sides 5,12,13 has an area of " + triangleArea(5, 12, 13) );
System.out.println("A triangle with sides 10,9,11 has an area of " + triangleArea(10, 9, 11) );
System.out.println("A triangle with sides 8,15,17 has an area of " + triangleArea(8, 15, 17) );
System.out.println("A triangle with sides 9,9,9 has an area of " + triangleArea(9, 9, 9) );
/// 4. The test for a triangle with sides 9,9,9 was not difficult to add to the file that used a function.
}
public static double triangleArea( int a, int b, int c )
{
// the code in this function computes the area of a triangle whose sides have lengths a, b, and c
double s, A;
s = (a+b+c) / 2.0;
A = Math.sqrt( s*(s-a)*(s-b)*(s-c) );
return A;
// ^ after computing the area, "return" it
}
}
Picture of the output