// Helene Martin, CSE 142 // Calculates the hypotenuse of a triangle. public class Triangle { public static void main(String[] args) { double hyp = round2(hypotenuse(4, 7)); System.out.println("Hypotenuse of triangle with sides 4 and 7: " + hyp); System.out.println("Hypotenuse of triangle with sides 12 and 18: " + round2(hypotenuse(12, 18))); // NOTE: although the line of code below compiles, it doesn't do anything // because hypotenuse returns a value that we're not using in any way. // In general, you should never call a method that returns without doing // something with the returned value. hypotenuse(5, 6); } // Computes triangle hypotenuse length given its side lengths. public static double hypotenuse(int a, int b) { double c = Math.sqrt(a * a + b * b); return c; } // Rounds the given value to two digits after the decimal point. public static double round2(double value) { // 46.3463332321 => 4635 => 46.35 double result = Math.round(value * 100) / 100.0; return result; } }