Skip to content

Commit 8729ae8

Browse files
author
jsquared21
committed
Add Ex 23.03
1 parent ca06a4c commit 8729ae8

10 files changed

+351
-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+
public class Circle
2+
extends GeometricObject {
3+
private double radius;
4+
5+
public Circle() {
6+
}
7+
8+
public Circle(double radius) {
9+
this.radius = radius;
10+
}
11+
12+
public Circle(double radius,
13+
String color, boolean filled) {
14+
this.radius = radius;
15+
setColor(color);
16+
setFilled(filled);
17+
}
18+
19+
/** Return radius */
20+
public double getRadius() {
21+
return radius;
22+
}
23+
24+
/** Set a new radius */
25+
public void setRadius(double radius) {
26+
this.radius = radius;
27+
}
28+
29+
@Override /** Return area */
30+
public double getArea() {
31+
return radius * radius * Math.PI;
32+
}
33+
34+
/** Return diameter */
35+
public double getDiameter() {
36+
return 2 * radius;
37+
}
38+
39+
@Override /** Return perimeter */
40+
public double getPerimeter() {
41+
return 2 * radius * Math.PI;
42+
}
43+
44+
@Override /** Override the toString method in the Object class */
45+
public String toString() {
46+
return super.toString() + ", Circle, Created: "
47+
+ getDateCreated() + ", Radius: " + radius;
48+
}
49+
}
3.78 KB
Binary file not shown.
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*********************************************************************************
2+
* (Generic quick sort) Write the following two generic methods using quick 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 quickSort(E[] list) *
8+
* public static <E> void quickSort(E[] list, *
9+
* Comparator<? super E> comparator) *
10+
*********************************************************************************/
11+
import java.util.Comparator;
12+
13+
public class Exercise_23_03 {
14+
/** Generic quick sort using Comparable */
15+
public static <E extends Comparable<E>> void quickSort(E[] list) {
16+
quickSort(list, 0, list.length - 1);
17+
}
18+
19+
public static <E extends Comparable<E>>
20+
void quickSort(E[] list, int first, int last) {
21+
if (last > first) {
22+
int pivotIndex = partition(list, first, last);
23+
quickSort(list, first, pivotIndex - 1);
24+
quickSort(list, pivotIndex + 1, last);
25+
}
26+
}
27+
28+
/** Partition the array list[first..last] */
29+
public static <E extends Comparable<E>>
30+
int partition(E[] list, int first, int last) {
31+
E pivot = list[first]; // Choose the first element as the pivot
32+
int low = first + 1; // Index for forward search
33+
int high = last; // Index for backward search
34+
35+
while (high > low) {
36+
// Search forward from left
37+
while (low <= high && list[low].compareTo(pivot) <= 0)
38+
low++;
39+
40+
// Search backward from right
41+
while (low <= high && list[high].compareTo(pivot) > 0)
42+
high--;
43+
44+
// Swap two elements in the list
45+
if (high > low) {
46+
E temp = list[high];
47+
list[high] = list[low];
48+
list[low] = temp;
49+
}
50+
}
51+
52+
while (high > first && list[high].compareTo(pivot) >= 0)
53+
high--;
54+
55+
// Swap pivot with list[high]
56+
if (pivot.compareTo(list[high]) > 0) {
57+
list[first] = list[high];
58+
list[high] = pivot;
59+
return high;
60+
}
61+
else {
62+
return first;
63+
}
64+
}
65+
66+
public static <E> void quickSort(E[] list, Comparator<? super E> comparator) {
67+
quickSort(list, 0, list.length - 1, comparator);
68+
}
69+
70+
public static <E> void quickSort(
71+
E[] list, int first, int last, Comparator<? super E> comparator) {
72+
if (last > first) {
73+
int pivotIndex = partition(list, first, last, comparator);
74+
quickSort(list, first, pivotIndex - 1, comparator);
75+
quickSort(list, pivotIndex + 1, last, comparator);
76+
}
77+
}
78+
79+
/** Partition the array list[first.. last] */
80+
public static <E> int partition(
81+
E[] list, int first, int last, Comparator<? super E> comparator) {
82+
E pivot = list[first]; // Choose the first element as the pivot
83+
int low = first + 1; // Index for forward search
84+
int high = last; // Index for backward search
85+
86+
while (high > low) {
87+
// Search forward from left
88+
while (low <= high && comparator.compare(list[low], pivot) <= 0)
89+
low++;
90+
91+
// Search backward from right
92+
while (low <= high && comparator.compare(list[high], pivot) > 0)
93+
high--;
94+
95+
// Swap two elements in the list
96+
if (high > low) {
97+
E temp = list[high];
98+
list[high] = list[low];
99+
list[low] = temp;
100+
}
101+
}
102+
103+
while (high > first && comparator.compare(list[high], pivot) >= 0)
104+
high--;
105+
106+
// Swap pivot with list[high]
107+
if (comparator.compare(pivot, list[high]) > 0) {
108+
list[first] = list[high];
109+
list[high] = pivot;
110+
return high;
111+
}
112+
else {
113+
return first;
114+
}
115+
}
116+
117+
/** A test method */
118+
public static void main(String[] args) {
119+
// Create an Integer array
120+
Integer[] intArray = {2, 3, 2, 5, 6, 1, -2, 3, 14, 12};
121+
122+
// Create a Double array
123+
Double[] doubleArray = {3.4, 1.3, -22.1, 14.8, 6.0, 2.3, 12.2};
124+
125+
// Create a Character array
126+
Character[] charArray = {'a', 'J', 'r'};
127+
128+
// Create a String array
129+
String[] stringArray = {"Tom", "Susan", "Kim"};
130+
131+
// Sort the arrays
132+
quickSort(intArray);
133+
quickSort(doubleArray);
134+
quickSort(charArray);
135+
quickSort(stringArray);
136+
137+
// Display the sorted arrays
138+
printList(intArray);
139+
printList(doubleArray);
140+
printList(charArray);
141+
printList(stringArray);
142+
143+
// Create an array of 10 GeometricObjects
144+
GeometricObject[] list = {new Circle(5), new Rectangle(4, 5),
145+
new Circle(5.5), new Rectangle(2.4, 5), new Circle(0.5),
146+
new Rectangle(4, 65), new Circle(4.5), new Rectangle(4.4, 1),
147+
new Circle(6.5), new Rectangle(4, 5)};
148+
149+
// Invoke quick sort using GeometricObjectComparator
150+
quickSort(list, new GeometricObjectComparator());
151+
152+
// Display the sorted elements
153+
printList(list);
154+
}
155+
156+
/** Print an array elements */
157+
public static void printList(Object[] list) {
158+
for (int i = 0; i < list.length; i++) {
159+
System.out.print(list[i] + " ");
160+
}
161+
System.out.println();
162+
}
163+
164+
/** Print an array of elements */
165+
public static void printList(GeometricObject[] list) {
166+
System.out.print("Sorted elements: ");
167+
for (GeometricObject e: list) {
168+
System.out.printf("%.2f ", e.getArea());
169+
}
170+
System.out.println();
171+
}
172+
}
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+
public abstract class GeometricObject {
2+
private String color = "white";
3+
private boolean filled;
4+
private java.util.Date dateCreated;
5+
6+
/** Construct a default geometric object */
7+
protected GeometricObject() {
8+
dateCreated = new java.util.Date();
9+
}
10+
11+
/** Construct a geometric object with color and filled value */
12+
protected GeometricObject(String color, boolean filled) {
13+
dateCreated = new java.util.Date();
14+
this.color = color;
15+
this.filled = filled;
16+
}
17+
18+
/** Return color */
19+
public String getColor() {
20+
return color;
21+
}
22+
23+
/** Set a new color */
24+
public void setColor(String color) {
25+
this.color = color;
26+
}
27+
28+
/** Return filled. Since filled is boolean,
29+
* the get method is named isFilled */
30+
public boolean isFilled() {
31+
return filled;
32+
}
33+
34+
/** Set a new filled */
35+
public void setFilled(boolean filled) {
36+
this.filled = filled;
37+
}
38+
39+
/** Get dateCreated */
40+
public java.util.Date getDateCreated() {
41+
return dateCreated;
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return "created on " + dateCreated + "\ncolor: " + color +
47+
" and filled: " + filled;
48+
}
49+
50+
/** Abstract method getArea */
51+
public abstract double getArea();
52+
53+
/** Abstract method getPerimeter */
54+
public abstract double getPerimeter();
55+
}
Binary file not shown.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import java.util.Comparator;
2+
3+
public class GeometricObjectComparator
4+
implements Comparator<GeometricObject>, java.io.Serializable {
5+
public int compare(GeometricObject o1, GeometricObject o2) {
6+
double area1 = o1.getArea();
7+
double area2 = o2.getArea();
8+
9+
if (area1 < area2)
10+
return -1;
11+
else if (area1 == area2)
12+
return 0;
13+
else
14+
return 1;
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+
public class Rectangle
2+
extends GeometricObject {
3+
private double width;
4+
private double height;
5+
6+
public Rectangle() {
7+
}
8+
9+
public Rectangle(
10+
double width, double height) {
11+
this.width = width;
12+
this.height = height;
13+
}
14+
15+
public Rectangle(
16+
double width, double height, String color, boolean filled) {
17+
this.width = width;
18+
this.height = height;
19+
setColor(color);
20+
setFilled(filled);
21+
}
22+
23+
/** Return width */
24+
public double getWidth() {
25+
return width;
26+
}
27+
28+
/** Set a new width */
29+
public void setWidth(double width) {
30+
this. width = width;
31+
}
32+
33+
/** Return height */
34+
public double getheight() {
35+
return height;
36+
}
37+
38+
/** Set a new height */
39+
public void setheight(double height) {
40+
this.height = height;
41+
}
42+
43+
@Override /** Return area */
44+
public double getArea() {
45+
return width * height;
46+
}
47+
48+
@Override /** Return perimeter */
49+
public double getPerimeter() {
50+
return 2 * (width * height);
51+
}
52+
53+
@Override /** Override the toString method in the Object class */
54+
public String toString() {
55+
return super.toString() + " Rectangle, Created: "
56+
+ getDateCreated() + ", Width: " + width +
57+
", Height: " + height;
58+
}
59+
}

0 commit comments

Comments
 (0)