// Zorah Fung, CSE 142 // Some examples of using and writing methods that return public class ReturnExamples { public static void main(String[] args) { // We need to store the value returned by hypotenuse() // into a variable so we can use it later double result = hypotenuse(3.0, 4.0); double result2 = hypotenuse(2.8, 5.6); // Now we can use the variables result and result2 which store the values // returned by the method call. System.out.println("The first hypotenuse is: " + result); System.out.println("The second hypotenuse is: " + result2); double rounded = round2(.1289); System.out.println(rounded); } // Given two sides of a triangle, returns the length of the hypotenuse public static double hypotenuse(double side1, double side2) { double hyp = Math.sqrt(side1 * side1 + side2 * side2); return hyp; } // Given a real number, returns the number rounded to two places after the decimal place public static double round2(double number) { // Example: // number => .1289 // number * 100 => 12.89 // Math.round(12.89) => 13 // 13 / 100.0 => .13 return Math.round(number * 100) / 100.0; } }