In: Computer Science
I. Answer part A, B, and C
1a)
Return true if the given int parameter is both positive and less than 10, and false otherwise. Remember that 0 is not positive!
positiveLessThan10(10) → false
positiveLessThan10(9) → true
positiveLessThan10(11) → false
1b)
Return true if the parameters are in ascending order, from left to right. Two values that are equal are considered to be in ascending order. Remember, you can just type return true or return false to return true or false. Both true and false are reserved words in Java. isAscending(1, 2, 3) → true isAscending(1, 2, 2) → true isAscending(3, 2, 1) → false |
1c)
Return true if the parameters are in ascending order, from left to right, or in descending order from left to right. Two values that are equal are considered to be in either ascending or descending order. isAscendingOrDescending(1, 2, 3) → true isAscendingOrDescending(1, 2, 2) → true isAscendingOrDescending(3, 2, 1) → true |
public class Methods { // A) public static boolean positiveLessThan10(int n) { return n > 0 && n < 10; } // B) public static boolean isAscending(int n1, int n2, int n3) { return n1 <= n2 && n2 <= n3; } // C) public static boolean isAscendingOrDescending(int n1, int n2, int n3) { return (n1 <= n2 && n2 <= n3) || (n1 >= n2 && n2 >= n3); } public static void main(String[] args) { System.out.println(positiveLessThan10(10)); System.out.println(positiveLessThan10(9)); System.out.println(positiveLessThan10(11)); System.out.println(isAscending(1, 2, 3)); System.out.println(isAscending(1, 2, 2)); System.out.println(isAscending(3, 2, 1)); System.out.println(isAscendingOrDescending(1, 2, 3)); System.out.println(isAscendingOrDescending(1, 2, 2)); System.out.println(isAscendingOrDescending(3, 2, 1)); } }