Assignemnt #92

Code


/// Name: Daniel Nguyen
/// Period: 6
/// Program Name: HeronsFormula Program
/// File Name: HeronsFormula.java
/// Date Finished: 9/9/2015

// 1. Yes, they both produce the same output.
// 2. NoFunction version is 50 lines, while other one is 30.
// 3. It's easier to fix in the function one, b/c you only fix it once.
// 4. It was easy in a file that uses a function.
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) );
	}
 
	public static double triangleArea( int a, int b, int c )
	{
		// the code in this method computes the area of a triangle whose sides have lengths a, b, and c
		double s, A;

		s = (a+b+c) / 2;
		A = Math.sqrt( s*(s-a)*(s-b)*(s-c) );

		return A;
		// ^ after computing the area, "return" it
	}
}













    


    

Picture of the output

Assignment 3
BACK HOME NEXT