// 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 minimumDatingAge() // into a variable so we can use it later int result = minDatingAge(18); int result2 = minDatingAge(21); // Now we can use the variables result and result2 which store the values // returned by the method call. System.out.println("The first minimum age is: " + result); System.out.println("The second minimum age is: " + result2); double rounded = round2(.1289); System.out.println(rounded); } // Given an age, returns the minimum age appropriate to date public static int minDatingAge(int age) { int minAge = age / 2 + 7; return minAge; } // Given an age, returns the maximum age appropriate to date public static int maxDatingAge(int age) { return (age - 7) * 2; // Can return an expression directly } // 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; } }