II. Answer part A, B, and C. Remember that 0 is not a positive value!
2a)
Return true if at least one parameter value is positive, and false otherwise.
atLeastOnePositive(1, 0, 3) → true
atLeastOnePositive(-10, 0, -4) → false
atLeastOnePositive(3, -2, -4) → true
2b)
Return true if exactly one parameter value is positive, and
false otherwise.
exactlyOnePositive(1, 0, 3) → false
exactlyOnePositive(-10, 0, -4) → false
exactlyOnePositive(3, -2, -4) → true
2c)
Return the middle of three integer values. This is the value
such that it is less than or equal one value, and greater than or
equal the other.
midOfThree(1, 2, 3) → 2
midOfThree(200, 100, 300) → 200
midOfThree(33, 11, 22) → 22
// 2a) public static boolean atLeastOnePositive(int n1, int n2, int n3) { return n1 > 0 || n2 > 0 || n3 > 0; } // 2b) public static boolean exactlyOnePositive(int n1, int n2, int n3) { int count = 0; if (n1 > 0) ++count; if (n2 > 0) ++count; if (n3 > 0) ++count; return count == 1; } // 2c) public static int midOfThree(int n1, int n2, int n3) { int min = n1, max = n1; if (n2 > max) max = n2; if (n3 > max) max = n3; if (n2 < min) min = n2; if (n3 < min) min = n3; return (n1 + n2 + n3) - (min + max); }
Get Answers For Free
Most questions answered within 1 hours.