|
| 1 | +package ch_23; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Comparator; |
| 5 | + |
| 6 | +/** |
| 7 | + * 23.6 (Check order) Write the following overloaded methods that check whether an |
| 8 | + * array is ordered in ascending order or descending order. By default, the method |
| 9 | + * checks ascending order. To check descending order, pass false to the ascending |
| 10 | + * argument in the method. |
| 11 | + * public static boolean ordered(int[] list) |
| 12 | + * public static boolean ordered(int[] list, boolean ascending) |
| 13 | + * public static boolean ordered(double[] list) |
| 14 | + * public static boolean ordered |
| 15 | + * (double[] list, boolean ascending) |
| 16 | + * public static <E extends Comparable<E>> |
| 17 | + * boolean ordered(E[] list) |
| 18 | + * public static <E extends Comparable<E>> boolean ordered |
| 19 | + * (E[] list, boolean ascending) |
| 20 | + * public static <E> boolean ordered(E[] list, Comparator<? super E> comparator) |
| 21 | + * public static <E> boolean ordered(E[] list, Comparator<? super E> comparator, boolean ascending) |
| 22 | + */ |
| 23 | +public class Exercise23_06 { |
| 24 | + /** |
| 25 | + * Test Driver method |
| 26 | + */ |
| 27 | + public static void main(String[] args) { |
| 28 | + /* Test int[] */ |
| 29 | + int[] listAsc = {1, 2, 3, 4, 7, 9, 11, 27}; |
| 30 | + int[] listDesc = {27, 26, 23, 14, 11, 9, 6, 3, 2, 1}; |
| 31 | + System.out.print("listAsc = "); |
| 32 | + System.out.print(Arrays.toString(listAsc)); |
| 33 | + System.out.println(); |
| 34 | + System.out.print("listDesc = "); |
| 35 | + System.out.print(Arrays.toString(listDesc)); |
| 36 | + System.out.println(); |
| 37 | + System.out.println("listAsc, is ascending?: " + ordered(listAsc)); |
| 38 | + System.out.println("listDesc, is descending? " + ordered(listDesc, false)); |
| 39 | + System.out.println("listDesc, is ascending?: " + ordered(listDesc)); |
| 40 | + System.out.println("listAsc, is descending? " + ordered(listAsc, false)); |
| 41 | + /* Test double[] */ |
| 42 | + double[] listAscDoubles = {1.3, 2.1, 3.2, 3.4, 3.7, 3.9, 11.21, 27.54}; |
| 43 | + double[] listDescDoubles = {27.6, 26.1, 23.3, 14.3, 11.54, 3.9, 3.6, 3.3, 3.2, 3.1}; |
| 44 | + System.out.print("listAscDoubles = "); |
| 45 | + System.out.print(Arrays.toString(listAscDoubles)); |
| 46 | + System.out.println(); |
| 47 | + System.out.print("listDescDoubles = "); |
| 48 | + System.out.print(Arrays.toString(listDescDoubles)); |
| 49 | + System.out.println(); |
| 50 | + System.out.println("listAscDoubles, is ascending?: " + ordered(listAscDoubles)); |
| 51 | + System.out.println("listDescDoubles, is descending? " + ordered(listDescDoubles, false)); |
| 52 | + System.out.println("listDescDoubles, is ascending?: " + ordered(listDescDoubles)); |
| 53 | + System.out.println("listAscDoubles, is descending? " + ordered(listAscDoubles, false)); |
| 54 | + /* Test E[] where E implements Comparable */ |
| 55 | + Integer[] listAscInteger = {1, 2, 3, 4, 7, 9, 11, 27}; |
| 56 | + Integer[] listDescInteger = {27, 26, 23, 14, 11, 9, 6, 3, 2, 1}; |
| 57 | + System.out.print("listAscInteger = "); |
| 58 | + System.out.print(Arrays.toString(listAscInteger)); |
| 59 | + System.out.println(); |
| 60 | + System.out.print("listDescInteger = "); |
| 61 | + System.out.print(Arrays.toString(listDescInteger)); |
| 62 | + System.out.println(); |
| 63 | + System.out.println("listAscInteger, is ascending?: " + ordered(listAscInteger)); |
| 64 | + System.out.println("listDescInteger, is descending? " + ordered(listDescInteger, false)); |
| 65 | + System.out.println("listDescInteger, is ascending?: " + ordered(listDescInteger)); |
| 66 | + System.out.println("listAscInteger, is descending? " + ordered(listAscInteger, false)); |
| 67 | + |
| 68 | + /* Test E[] using Comparator */ |
| 69 | + Integer[] listAscWithComparator = {1, 2, 3, 4, 7, 9, 11, 27}; |
| 70 | + Integer[] listDescWithComparator = {27, 26, 23, 14, 11, 9, 6, 3, 2, 1}; |
| 71 | + System.out.print("listAscWithComparator = "); |
| 72 | + System.out.print(Arrays.toString(listAscWithComparator)); |
| 73 | + System.out.println(); |
| 74 | + System.out.print("listDescWithComparator = "); |
| 75 | + System.out.print(Arrays.toString(listDescWithComparator)); |
| 76 | + System.out.println(); |
| 77 | + System.out.println("listAscWithComparator, is ascending?: " + ordered(listAscWithComparator, |
| 78 | + (o1, o2) -> { |
| 79 | + if (o1 > o2) return 1; |
| 80 | + if (o1 < o2) return -1; |
| 81 | + return 0; |
| 82 | + })); |
| 83 | + System.out.println("listDescWithComparator, is descending? " + ordered(listDescWithComparator, (o1, o2) -> { |
| 84 | + if (o1 > o2) return 1; |
| 85 | + if (o1 < o2) return -1; |
| 86 | + return 0; |
| 87 | + }, false)); |
| 88 | + System.out.println("listDescWithComparator, is ascending?: " + ordered(listDescWithComparator, (o1, o2) -> { |
| 89 | + if (o1 > o2) return 1; |
| 90 | + if (o1 < o2) return -1; |
| 91 | + return 0; |
| 92 | + })); |
| 93 | + System.out.println("listAscWithComparator, is descending? " + ordered(listAscWithComparator, (o1, o2) -> { |
| 94 | + if (o1 > o2) return 1; |
| 95 | + if (o1 < o2) return -1; |
| 96 | + return 0; |
| 97 | + }, false)); |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + public static boolean ordered(int[] list) { |
| 102 | + for (int i = 0; i < list.length - 1; i++) { |
| 103 | + if (list[i] > list[i + 1]) return false; |
| 104 | + } |
| 105 | + return true; |
| 106 | + } |
| 107 | + |
| 108 | + public static boolean ordered(int[] list, |
| 109 | + boolean ascending) { |
| 110 | + if (!ascending) { |
| 111 | + for (int i = 0; i < list.length - 1; i++) { |
| 112 | + if (list[i] < list[i + 1]) return false; |
| 113 | + } |
| 114 | + return true; |
| 115 | + } |
| 116 | + for (int i = 0; i < list.length - 1; i++) { |
| 117 | + if (list[i] > list[i + 1]) return false; |
| 118 | + } |
| 119 | + return true; |
| 120 | + } |
| 121 | + |
| 122 | + public static boolean ordered(double[] list) { |
| 123 | + for (int i = 0; i < list.length - 1; i++) { |
| 124 | + if (list[i] > list[i + 1]) return false; |
| 125 | + } |
| 126 | + return true; |
| 127 | + } |
| 128 | + |
| 129 | + public static boolean ordered(double[] list, |
| 130 | + boolean ascending) { |
| 131 | + if (!ascending) { |
| 132 | + for (int i = 0; i < list.length - 1; i++) { |
| 133 | + if (list[i] < list[i + 1]) return false; |
| 134 | + } |
| 135 | + return true; |
| 136 | + } |
| 137 | + for (int i = 0; i < list.length - 1; i++) { |
| 138 | + if (list[i] > list[i + 1]) return false; |
| 139 | + } |
| 140 | + return true; |
| 141 | + } |
| 142 | + |
| 143 | + public static <E extends Comparable<E>> boolean ordered(E[] list) { |
| 144 | + for (int i = 0; i < list.length - 1; i++) { |
| 145 | + if (list[i].compareTo(list[i + 1]) > 0) return false; |
| 146 | + } |
| 147 | + return true; |
| 148 | + } |
| 149 | + |
| 150 | + public static <E extends Comparable<E>> boolean ordered( |
| 151 | + E[] list, |
| 152 | + boolean ascending) { |
| 153 | + if (!ascending) { |
| 154 | + for (int i = 0; i < list.length - 1; i++) { |
| 155 | + if (list[i].compareTo(list[i + 1]) < 0) return false; |
| 156 | + } |
| 157 | + return true; |
| 158 | + } |
| 159 | + |
| 160 | + for (int i = 0; i < list.length - 1; i++) { |
| 161 | + if (list[i].compareTo(list[i + 1]) > 0) return false; |
| 162 | + } |
| 163 | + return true; |
| 164 | + } |
| 165 | + |
| 166 | + public static <E> boolean ordered(E[] list, |
| 167 | + Comparator<? super E> comparator) { |
| 168 | + for (int i = 0; i < list.length - 1; i++) { |
| 169 | + if (comparator.compare(list[i], list[i + 1]) > 0) return false; |
| 170 | + } |
| 171 | + return true; |
| 172 | + } |
| 173 | + |
| 174 | + public static <E> boolean ordered(E[] list, |
| 175 | + Comparator<? super E> comparator, |
| 176 | + boolean ascending) { |
| 177 | + if (!ascending) { |
| 178 | + for (int i = 0; i < list.length - 1; i++) { |
| 179 | + if (comparator.compare(list[i], list[i + 1]) < 0) return false; |
| 180 | + } |
| 181 | + return true; |
| 182 | + } |
| 183 | + |
| 184 | + for (int i = 0; i < list.length - 1; i++) { |
| 185 | + if (comparator.compare(list[i], list[i + 1]) > 0) return false; |
| 186 | + } |
| 187 | + return true; |
| 188 | + } |
| 189 | +} |
0 commit comments