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