Skip to content

Commit ca06a4c

Browse files
author
jsquared21
committed
Add Ex 23.2
1 parent 7fcde98 commit ca06a4c

File tree

10 files changed

+324
-0
lines changed

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+
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.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+
import java.util.Comparator;
12+
import java.util.Arrays;
13+
14+
public class Exercise_23_02 {
15+
/** Generic merge sort using Comparable */
16+
public static <E extends Comparable<E>> void mergeSort(E[] list) {
17+
if (list.length > 1) {
18+
// Merge sort the first half
19+
E[] firstHalf = (E[])new Comparable[list.length / 2];
20+
System.arraycopy(list, 0 , firstHalf, 0, list.length / 2);
21+
mergeSort(firstHalf);
22+
23+
// Merge sort the second half
24+
int secondHalfLength = list.length - list.length / 2;
25+
E[] secondHalf = (E[])(new Comparable[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+
public static <E extends Comparable<E>> void merge(E[] list1, E[] list2, E[] temp) {
37+
int current1 = 0; // Current index in list1
38+
int current2 = 0; // Current index in list2
39+
int current3 = 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+
public static <E> void mergeSort(E[] list, Comparator<? super E> 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+
public static <E> void merge(E[] list1, E[] list2, E[] temp,
73+
Comparator<? super E> comparator) {
74+
int current1 = 0; // Current index in list1
75+
int current2 = 0; // Current index in list2
76+
int current3 = 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+
public static void main(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 = {new Circle(5), new Rectangle(4, 5),
119+
new Circle(5.5), new Rectangle(2.4, 5), new Circle(0.5),
120+
new Rectangle(4, 65), new Circle(4.5), new Rectangle(4.4, 1),
121+
new Circle(6.5), new Rectangle(4, 5)};
122+
123+
// Invoke merge sort using GeometricObjectComparator
124+
mergeSort(list, new GeometricObjectComparator());
125+
126+
// Display the sorted elements
127+
printList(list);
128+
}
129+
130+
/** Print an array of Objects */
131+
public static void printList(Object[] list) {
132+
for (int i = 0; i < list.length; i ++)
133+
System.out.print(list[i] + " ");
134+
System.out.println();
135+
}
136+
137+
/** Print the sorted elements */
138+
public static void printList(GeometricObject[] list) {
139+
System.out.print("Sorted elements: ");
140+
for (GeometricObject e: 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+
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+
}
707 Bytes
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)