1
+ package ch_20 ;
2
+
3
+ import java .util .Arrays ;
4
+ import java .util .Comparator ;
5
+
6
+ /**
7
+ * 20.21 (Use Comparator) Write the following generic method using selection sort
8
+ * and a comparator.
9
+ * public static <E> void selectionSort(E[] list, Comparator<? super E> comparator)
10
+ * <p>
11
+ * Write a test program that creates an array of 10 GeometricObjects and
12
+ * invokes this method using the GeometricObjectComparator introduced in
13
+ * Listing 20.4 to sort the elements. Display the sorted elements. Use the following statement to create the array.
14
+ * GeometricObject[] list = {new Circle(5), new Rectangle(4, 5),
15
+ * new Circle(5.5), new Rectangle(2.4, 5), new Circle(0.5),
16
+ * new Rectangle(4, 65), new Circle(4.5), new Rectangle(4.4, 1),
17
+ * new Circle(6.5), new Rectangle(4, 5)};
18
+ */
19
+ public class Exercise20_21 {
20
+ public static void main (String [] args ) {
21
+ GeometricObject [] list = {new Circle (5 ), new Rectangle (4 , 5 ),
22
+ new Circle (5.5 ), new Rectangle (2.4 , 5 ), new Circle (0.5 ),
23
+ new Rectangle (4 , 65 ), new Circle (4.5 ), new Rectangle (4.4 , 1 ),
24
+ new Circle (6.5 ), new Rectangle (4 , 5 )};
25
+
26
+ selectionSort (list , new GeometricObjectComparator ());
27
+ for (int i = 0 ; i < list .length ; i ++) {
28
+ System .out .println ("Area of GeometricObject: list[" + i + "] = " + list [i ].getArea ());
29
+ }
30
+ }
31
+
32
+ public static <E > void selectionSort (E [] list ,
33
+ Comparator <? super E > comparator ) {
34
+ for (int i = 0 ; i < list .length - 1 ; i ++) {
35
+ E currentMin = list [i ];
36
+ int currentMinIndex = i ;
37
+
38
+ for (int j = i + 1 ; j < list .length ; j ++) {
39
+ if (comparator .compare (currentMin , list [j ]) > 0 ) { // currentMin is greater than list at j
40
+ currentMin = list [j ];
41
+ currentMinIndex = j ;
42
+ }
43
+ }
44
+ if (currentMinIndex != i ) {
45
+ list [currentMinIndex ] = list [i ];
46
+ list [i ] = currentMin ;
47
+ }
48
+ }
49
+ }
50
+ }
51
+
52
+
53
+ class GeometricObjectComparator
54
+ implements Comparator <GeometricObject >, java .io .Serializable {
55
+ public int compare (GeometricObject o1 , GeometricObject o2 ) {
56
+ double area1 = o1 .getArea ();
57
+ double area2 = o2 .getArea ();
58
+ if (area1 < area2 )
59
+ return -1 ;
60
+ else if (area1 == area2 )
61
+ return 0 ;
62
+ else
63
+ return 1 ;
64
+ }
65
+ }
66
+
67
+
68
+ class Circle extends GeometricObject {
69
+
70
+ private double radius ;
71
+
72
+ public Circle () {
73
+
74
+ }
75
+
76
+ public Circle (double radius ) {
77
+
78
+ this .radius = radius ;
79
+
80
+ }
81
+
82
+ public Circle (double radius , String color ,
83
+ boolean filled ) {
84
+
85
+ this .radius = radius ;
86
+ setColor (color );
87
+ setFilled (filled );
88
+
89
+ }
90
+
91
+ public double getRadius () {
92
+ return radius ;
93
+ }
94
+
95
+ public void setRadius (double radius ) {
96
+ this .radius = radius ;
97
+ }
98
+
99
+ @ Override
100
+ public double getArea () {
101
+ return radius * radius * Math .PI ;
102
+ }
103
+
104
+ @ Override
105
+ public double getPerimeter () {
106
+ return 2 * radius * Math .PI ;
107
+ }
108
+
109
+ @ Override
110
+ public String toString () {
111
+ return super .toString () + "\n Radius: " + getRadius () + "\n Area: "
112
+ + getArea () + "\n Perimeter: " + getPerimeter ();
113
+ }
114
+
115
+ }
116
+
117
+ class Rectangle extends GeometricObject {
118
+
119
+ private double width ;
120
+ private double height ;
121
+
122
+ public Rectangle () {
123
+ width = 1 ;
124
+ height = 1 ;
125
+ }
126
+
127
+ public Rectangle (double newWidth , double newHeight ) {
128
+
129
+ width = newWidth ;
130
+ height = newHeight ;
131
+ }
132
+
133
+ public double getWidth () {
134
+ return width ;
135
+ }
136
+
137
+ public void setWidth (double width ) {
138
+ this .width = width ;
139
+ }
140
+
141
+ public double getHeight () {
142
+ return height ;
143
+ }
144
+
145
+ public void setHeight (double height ) {
146
+ this .height = height ;
147
+ }
148
+
149
+ @ Override
150
+ public double getArea () {
151
+ double area = width * height ;
152
+ return area ;
153
+ }
154
+
155
+ @ Override
156
+ public double getPerimeter () {
157
+
158
+ return (2 * width ) + (2 * height );
159
+ }
160
+
161
+ @ Override
162
+ public String toString () {
163
+ return super .toString () + "\n Width: " + width + "\n Height: " + height
164
+ + "\n Area: " + getArea () + "\n Perimeter: " + getPerimeter ();
165
+ }
166
+
167
+
168
+ }
169
+
170
+ abstract class GeometricObject {
171
+ private String color = "white" ;
172
+ private boolean filled ;
173
+ private java .util .Date dateCreated ;
174
+
175
+ /**
176
+ * Construct a default geometric object
177
+ */
178
+ protected GeometricObject () {
179
+ dateCreated = new java .util .Date ();
180
+ }
181
+
182
+ /**
183
+ * Construct a geometric object with color and filled value
184
+ */
185
+ protected GeometricObject (String color , boolean filled ) {
186
+ dateCreated = new java .util .Date ();
187
+ this .color = color ;
188
+ this .filled = filled ;
189
+ }
190
+
191
+ /**
192
+ * Return color
193
+ */
194
+ public String getColor () {
195
+ return color ;
196
+ }
197
+
198
+ /**
199
+ * Set a new color
200
+ */
201
+ public void setColor (String color ) {
202
+ this .color = color ;
203
+ }
204
+
205
+ /**
206
+ * Return filled. Since filled is boolean,
207
+ * the get method is named isFilled
208
+ */
209
+ public boolean isFilled () {
210
+ return filled ;
211
+ }
212
+
213
+ /**
214
+ * Set a new filled
215
+ */
216
+ public void setFilled (boolean filled ) {
217
+ this .filled = filled ;
218
+ }
219
+
220
+ /**
221
+ * Get dateCreated
222
+ */
223
+ public java .util .Date getDateCreated () {
224
+ return dateCreated ;
225
+ }
226
+
227
+ @ Override
228
+ public String toString () {
229
+ return "created on " + dateCreated + "\n color: " + color +
230
+ " and filled: " + filled ;
231
+ }
232
+
233
+ /**
234
+ * Abstract method getArea
235
+ */
236
+ public abstract double getArea ();
237
+
238
+ /**
239
+ * Abstract method getPerimeter
240
+ */
241
+ public abstract double getPerimeter ();
242
+ }
0 commit comments