diff21 Given an int n, return the absolute difference between n and 21, except return double the absolute difference if n is over 21. diff21(19) 2 → diff21(10) 11 → diff21(21) 0 → public int diff21(int n) { if (n <= 21) { return Math.abs(21 - n); } return Math.abs((n - 21) * 2); } parrotTrouble We have a loud talking parrot. The "hour" parameter is the current hour time in the range 0..23. We are in trouble if the parrot is talking and the hour is before 7 or after 20. Return true if we are in trouble. parrotTrouble(true, 6) true → parrotTrouble(true, 7) false → parrotTrouble(false, 6) false → public boolean parrotTrouble(boolean talking, int hour) { return (talking && (hour < 7 || hour > 20)); // Need extra parenthesis around the || clause // since && binds more tightly than || // && is like arithmetic *, || is like arithmetic + } makes10 Given 2 ints, a and b, return true if one if them is 10 or if their sum is 10. makes10(9, 10) true → makes10(9, 9) false → makes10(1, 9) true → public boolean makes10(int a, int b) { return (a == 10 || b == 10 || a + b == 10); } posNeg Given 2 int values, return true if one is negative and one is positive. Except if the parameter "negative" is true, then return true only if both are negative. posNeg(1, -1, false) true → posNeg(-1, 1, false) true → posNeg(-4, -5, true) true →
diff21 Given an int n, return the absolute difference between n and 21, except return double the absolute difference if n is over 21. diff21(19) → 2 diff21(10) → 11 diff21(21) → 0 public int diff21(int n) { if (n <= 21) { return Math.abs(21 - n); } return Math.abs((n - 21) * 2); } parrotTrouble We have a loud talking parrot. The "hour" parameter is the current hour time in the range 0..23. We are in trouble if the parrot is talking and the hour is before 7 or after 20. Return true if we are in trouble. parrotTrouble(true, 6) → true parrotTrouble(true, 7) → false parrotTrouble(false, 6) → false public boolean parrotTrouble(boolean talking, int hour) { return (talking && (hour < 7 || hour > 20)); // Need extra parenthesis around the || clause // since && binds more tightly than || // && is like arithmetic *, || is like arithmetic + } makes10 Given 2 ints, a and b, return true if one if them is 10 or if their sum is 10. makes10(9, 10) → true makes10(9, 9) → false makes10(1, 9) → true public boolean makes10(int a, int b) { return (a == 10 || b == 10 || a + b == 10); } posNeg Given 2 int values, return true if one is negative and one is positive. Except if the parameter "negative" is true, then return true only if both are negative. posNeg(1, -1, false) → true posNeg(-1, 1, false) → true posNeg(-4, -5, true) → true