Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitca06a4c

Browse files
author
jsquared21
committed
Add Ex 23.2
1 parent7fcde98 commitca06a4c

10 files changed

+324
-0
lines changed
1.2 KB
Binary file not shown.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
publicclassCircle
2+
extendsGeometricObject {
3+
privatedoubleradius;
4+
5+
publicCircle() {
6+
}
7+
8+
publicCircle(doubleradius) {
9+
this.radius =radius;
10+
}
11+
12+
publicCircle(doubleradius,
13+
Stringcolor,booleanfilled) {
14+
this.radius =radius;
15+
setColor(color);
16+
setFilled(filled);
17+
}
18+
19+
/** Return radius */
20+
publicdoublegetRadius() {
21+
returnradius;
22+
}
23+
24+
/** Set a new radius */
25+
publicvoidsetRadius(doubleradius) {
26+
this.radius =radius;
27+
}
28+
29+
@Override/** Return area */
30+
publicdoublegetArea() {
31+
returnradius *radius *Math.PI;
32+
}
33+
34+
/** Return diameter */
35+
publicdoublegetDiameter() {
36+
return2 *radius;
37+
}
38+
39+
@Override/** Return perimeter */
40+
publicdoublegetPerimeter() {
41+
return2 *radius *Math.PI;
42+
}
43+
44+
@Override/** Override the toString method in the Object class */
45+
publicStringtoString() {
46+
returnsuper.toString() +", Circle, Created: "
47+
+getDateCreated() +", Radius: " +radius;
48+
}
49+
}
3.71 KB
Binary file not shown.
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*********************************************************************************
2+
* (Generic merge sort) Write the following two generic methods using merge sort. *
3+
* The first method sorts the elements using the Comparable interface and the *
4+
* second uses the Comparator interface. *
5+
* *
6+
* public static <E extends Comparable<E>> *
7+
* void mergeSort(E[] list) *
8+
* public static <E> void mergeSort(E[] list, *
9+
* Comparator<? super E> comparator) *
10+
*********************************************************************************/
11+
importjava.util.Comparator;
12+
importjava.util.Arrays;
13+
14+
publicclassExercise_23_02 {
15+
/** Generic merge sort using Comparable */
16+
publicstatic <EextendsComparable<E>>voidmergeSort(E[]list) {
17+
if (list.length >1) {
18+
// Merge sort the first half
19+
E[]firstHalf = (E[])newComparable[list.length /2];
20+
System.arraycopy(list,0 ,firstHalf,0,list.length /2);
21+
mergeSort(firstHalf);
22+
23+
// Merge sort the second half
24+
intsecondHalfLength =list.length -list.length /2;
25+
E[]secondHalf = (E[])(newComparable[secondHalfLength]);
26+
System.arraycopy(list,list.length /2,
27+
secondHalf,0,secondHalfLength);
28+
mergeSort(secondHalf);
29+
30+
// Merge firstHalf with secondHalf into list
31+
merge(firstHalf,secondHalf,list);
32+
}
33+
}
34+
35+
/** Merge two sorted lists */
36+
publicstatic <EextendsComparable<E>>voidmerge(E[]list1,E[]list2,E[]temp) {
37+
intcurrent1 =0;// Current index in list1
38+
intcurrent2 =0;// Current index in list2
39+
intcurrent3 =0;// Current index in temp
40+
41+
while (current1 <list1.length &&current2 <list2.length) {
42+
if (list1[current1].compareTo(list2[current2]) <0)
43+
temp[current3++] =list1[current1++];
44+
else
45+
temp[current3++] =list2[current2++];
46+
}
47+
48+
while (current1 <list1.length)
49+
temp[current3++] =list1[current1++];
50+
51+
while (current2 <list2.length)
52+
temp[current3++] =list2[current2++];
53+
}
54+
55+
publicstatic <E>voidmergeSort(E[]list,Comparator<?superE>comparator) {
56+
/** Generic mergeSort using Comparator */
57+
if (list.length >1) {
58+
// Merge sort the first half
59+
E[]firstHalf =Arrays.copyOf(list,list.length /2);
60+
mergeSort(firstHalf,comparator);
61+
62+
// Merge sort the second half
63+
E[]secondHalf =Arrays.copyOfRange(list,list.length /2,list.length);
64+
mergeSort(secondHalf,comparator);
65+
66+
// Merge firstHalf with secondHalf into list
67+
merge(firstHalf,secondHalf,list,comparator);
68+
}
69+
}
70+
71+
/** Merge two sorted lists */
72+
publicstatic <E>voidmerge(E[]list1,E[]list2,E[]temp,
73+
Comparator<?superE>comparator) {
74+
intcurrent1 =0;// Current index in list1
75+
intcurrent2 =0;// Current index in list2
76+
intcurrent3 =0;// Current index in temp
77+
78+
while (current1 <list1.length &&current2 <list2.length) {
79+
if (comparator.compare(list1[current1],list2[current2]) <0)
80+
temp[current3++] =list1[current1++];
81+
else
82+
temp[current3++] =list2[current2++];
83+
}
84+
85+
while (current1 <list1.length)
86+
temp[current3++] =list1[current1++];
87+
88+
while (current2 <list2.length)
89+
temp[current3++] =list2[current2++];
90+
}
91+
92+
/** A test method */
93+
publicstaticvoidmain(String[]args) {
94+
Integer[]intArray = {2,3,2,5,6,1, -2,3,14,12};
95+
96+
// Create a Double array
97+
Double[]doubleArray = {3.4,1.3, -22.1,14.8,6.0,2.3,12.2};
98+
99+
// Create a Character array
100+
Character[]charArray = {'a','J','r'};
101+
102+
// Create a String array
103+
String[]stringArray = {"Tom","Susan","Kim"};
104+
105+
// Sort the arrays
106+
mergeSort(intArray);
107+
mergeSort(doubleArray);
108+
mergeSort(charArray);
109+
mergeSort(stringArray);
110+
111+
// Display the arrays
112+
printList(intArray);
113+
printList(charArray);
114+
printList(stringArray);
115+
printList(doubleArray);
116+
117+
// Create an array of 10 GeometricObjects
118+
GeometricObject[]list = {newCircle(5),newRectangle(4,5),
119+
newCircle(5.5),newRectangle(2.4,5),newCircle(0.5),
120+
newRectangle(4,65),newCircle(4.5),newRectangle(4.4,1),
121+
newCircle(6.5),newRectangle(4,5)};
122+
123+
// Invoke merge sort using GeometricObjectComparator
124+
mergeSort(list,newGeometricObjectComparator());
125+
126+
// Display the sorted elements
127+
printList(list);
128+
}
129+
130+
/** Print an array of Objects */
131+
publicstaticvoidprintList(Object[]list) {
132+
for (inti =0;i <list.length;i ++)
133+
System.out.print(list[i] +" ");
134+
System.out.println();
135+
}
136+
137+
/** Print the sorted elements */
138+
publicstaticvoidprintList(GeometricObject[]list) {
139+
System.out.print("Sorted elements: ");
140+
for (GeometricObjecte:list) {
141+
System.out.printf("%.2f ",e.getArea());
142+
}
143+
System.out.println();
144+
}
145+
}
1.25 KB
Binary file not shown.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
publicabstractclassGeometricObject {
2+
privateStringcolor ="white";
3+
privatebooleanfilled;
4+
privatejava.util.DatedateCreated;
5+
6+
/** Construct a default geometric object */
7+
protectedGeometricObject() {
8+
dateCreated =newjava.util.Date();
9+
}
10+
11+
/** Construct a geometric object with color and filled value */
12+
protectedGeometricObject(Stringcolor,booleanfilled) {
13+
dateCreated =newjava.util.Date();
14+
this.color =color;
15+
this.filled =filled;
16+
}
17+
18+
/** Return color */
19+
publicStringgetColor() {
20+
returncolor;
21+
}
22+
23+
/** Set a new color */
24+
publicvoidsetColor(Stringcolor) {
25+
this.color =color;
26+
}
27+
28+
/** Return filled. Since filled is boolean,
29+
* the get method is named isFilled */
30+
publicbooleanisFilled() {
31+
returnfilled;
32+
}
33+
34+
/** Set a new filled */
35+
publicvoidsetFilled(booleanfilled) {
36+
this.filled =filled;
37+
}
38+
39+
/** Get dateCreated */
40+
publicjava.util.DategetDateCreated() {
41+
returndateCreated;
42+
}
43+
44+
@Override
45+
publicStringtoString() {
46+
return"created on " +dateCreated +"\ncolor: " +color +
47+
" and filled: " +filled;
48+
}
49+
50+
/** Abstract method getArea */
51+
publicabstractdoublegetArea();
52+
53+
/** Abstract method getPerimeter */
54+
publicabstractdoublegetPerimeter();
55+
}
Binary file not shown.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
importjava.util.Comparator;
2+
3+
publicclassGeometricObjectComparator
4+
implementsComparator<GeometricObject>,java.io.Serializable {
5+
publicintcompare(GeometricObjecto1,GeometricObjecto2) {
6+
doublearea1 =o1.getArea();
7+
doublearea2 =o2.getArea();
8+
9+
if (area1 <area2)
10+
return -1;
11+
elseif (area1 ==area2)
12+
return0;
13+
else
14+
return1;
15+
}
16+
}
1.31 KB
Binary file not shown.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
publicclassRectangle
2+
extendsGeometricObject {
3+
privatedoublewidth;
4+
privatedoubleheight;
5+
6+
publicRectangle() {
7+
}
8+
9+
publicRectangle(
10+
doublewidth,doubleheight) {
11+
this.width =width;
12+
this.height =height;
13+
}
14+
15+
publicRectangle(
16+
doublewidth,doubleheight,Stringcolor,booleanfilled) {
17+
this.width =width;
18+
this.height =height;
19+
setColor(color);
20+
setFilled(filled);
21+
}
22+
23+
/** Return width */
24+
publicdoublegetWidth() {
25+
returnwidth;
26+
}
27+
28+
/** Set a new width */
29+
publicvoidsetWidth(doublewidth) {
30+
this.width =width;
31+
}
32+
33+
/** Return height */
34+
publicdoublegetheight() {
35+
returnheight;
36+
}
37+
38+
/** Set a new height */
39+
publicvoidsetheight(doubleheight) {
40+
this.height =height;
41+
}
42+
43+
@Override/** Return area */
44+
publicdoublegetArea() {
45+
returnwidth *height;
46+
}
47+
48+
@Override/** Return perimeter */
49+
publicdoublegetPerimeter() {
50+
return2 * (width *height);
51+
}
52+
53+
@Override/** Override the toString method in the Object class */
54+
publicStringtoString() {
55+
returnsuper.toString() +" Rectangle, Created: "
56+
+getDateCreated() +", Width: " +width +
57+
", Height: " +height;
58+
}
59+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp