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

Commit7fcde98

Browse files
author
jsquared21
committed
Add Ex 23.1
1 parentd662c28 commit7fcde98

10 files changed

+286
-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+
}
2.83 KB
Binary file not shown.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*********************************************************************************
2+
* (Generic bubble sort) Write the following two generic methods using bubble *
3+
* sort. The first method sorts the elements using the Comparable interface and *
4+
* the second uses the Comparator interface. *
5+
* *
6+
* public static <E extends Comparable<E>> *
7+
* void bubbleSort(E[] list) *
8+
* public static <E> void bubbleSort(E[] list, *
9+
* Comparator<? super E> comparator) *
10+
*********************************************************************************/
11+
importjava.util.Comparator;
12+
13+
publicclassExercise_23_01 {
14+
/** Generic bubble sort method using comparable */
15+
publicstatic <EextendsComparable<E>>voidbubbleSort(E[]list) {
16+
booleanneedNextPass =true;
17+
18+
for (intk =1;k <list.length &&needNextPass;k++) {
19+
// Array may be sorted and next pass not needed
20+
needNextPass =false;
21+
for (inti =0;i <list.length -k;i++) {
22+
if (list[i].compareTo(list[i +1]) >0) {
23+
// Swap list[i] with list[i + 1]
24+
Etemp =list[i];
25+
list[i] =list[i +1];
26+
list[i +1] =temp;
27+
28+
needNextPass =true;// Next pass still needed
29+
}
30+
}
31+
}
32+
}
33+
34+
/** Generic bubble sort method using comparator */
35+
publicstatic <E>voidbubbleSort(E[]list,Comparator<?superE>comparator) {
36+
booleanneedNextPass =true;
37+
38+
for (intk =1;k <list.length &&needNextPass;k++) {
39+
// Array may be sorted and next pass not needed
40+
needNextPass =false;
41+
for (inti =0;i <list.length -k;i++) {
42+
if (comparator.compare(list[i],list[i +1]) >0) {
43+
// Swap list[i] with list[i + 1]
44+
Etemp =list[i];
45+
list[i] =list[i +1];
46+
list[i +1] =temp;
47+
48+
needNextPass =true;// Next pass still needed
49+
}
50+
}
51+
}
52+
}
53+
54+
/** A test method */
55+
publicstaticvoidmain(String[]args) {
56+
// Create an Integer array
57+
Integer[]listArray = {2,3,2,5,6,1, -2,3,14,12};
58+
59+
// Carate a Double array
60+
Double[]doubleArray = {3.4,1.3, -22.1,14.8,6.0,2.3,12.2};
61+
62+
// Create a Character array
63+
Character[]charArray = {'a','J','r'};
64+
65+
// Create a String array
66+
String[]stringArray = {"Tom","Susan","Kim"};
67+
68+
// Sort the arrays
69+
bubbleSort(listArray);
70+
bubbleSort(doubleArray);
71+
bubbleSort(charArray);
72+
bubbleSort(stringArray);
73+
74+
printList(listArray);
75+
printList(charArray);
76+
printList(stringArray);
77+
printList(doubleArray);
78+
79+
// Create an array of 10 GeometricObjects
80+
GeometricObject[]list = {newCircle(5),newRectangle(4,5),
81+
newCircle(5.5),newRectangle(2.4,5),newCircle(0.5),
82+
newRectangle(4,65),newCircle(4.5),newRectangle(4.4,1),
83+
newCircle(6.5),newRectangle(4,5)};
84+
85+
// Invoke bubble sort using GeometricObjectComparator
86+
bubbleSort(list,newGeometricObjectComparator());
87+
88+
// Display the sorted elements
89+
printList(list);
90+
}
91+
92+
/** Print an array of objects */
93+
publicstaticvoidprintList(Object[]list) {
94+
for (inti =0;i <list.length;i++)
95+
System.out.print(list[i] +" ");
96+
System.out.println();
97+
}
98+
99+
/** Print the sorted elements */
100+
publicstaticvoidprintList(GeometricObject[]list) {
101+
System.out.print("Sorted elements: ");
102+
for (GeometricObjecte:list) {
103+
System.out.printf("%.2f ",e.getArea());
104+
}
105+
System.out.println();
106+
}
107+
}
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