Skip to content

Commit b2b7541

Browse files
committed
Chapter 19 Completed
1 parent 60bdbea commit b2b7541

11 files changed

+524
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ ____
6464
16</strong></a> - JavaFx UI Controls and Multimedia</li><br>
6565
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/ch_17"><strong>Chapter 17</strong></a> - Binary I/O (Complete)</li><br>
6666
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/ch_18"><strong>Chapter 18</strong></a> - Recursion</li><br>
67-
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/ch_19"><strong>Chapter 19</strong></a> - Generics</li><br>
67+
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/ch_19"><strong>Chapter 19</strong></a> - Generics (Complete)</li><br>
6868
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/ch_20"><strong>Chapter 20</strong></a> - Lists, Stacks, Queues, and Priority Queues</li><br>
6969
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/ch_21"><strong>Chapter 21</strong></a> - Sets and Maps</li><br>
7070
<li><!--<a href="https://github.com/HarryDulaney/java-programming-daniel-liang-10th/tree/master/">--><strong>Chapter 22</strong><!--</a>--> - Developing Efficient Algorithms</li><br>

ch_18/Exercise18_25.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static void displayPermutation(String s1, String s2) {
4343
if (s2.length() > 0) {
4444
for (int i = 0; i < s2.length(); i++) {
4545
String shuffle1 = s1 + s2.charAt(i);
46-
String shuffle2 = s2.substring(0, i) + s1.substring(i + 1);
46+
String shuffle2 = s2.substring(0, i) + s2.substring(i + 1);
4747
displayPermutation(shuffle1, shuffle2);
4848
}
4949
} else {

ch_19/Exercise19_06.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package ch_19;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 19.6 (Maximum element in a two-dimensional array) Write a generic method that
7+
* returns the maximum element in a two-dimensional array.
8+
* public static <E extends Comparable<E>> E max(E[][] list)
9+
*/
10+
public class Exercise19_06 {
11+
public static void main(String[] args) {
12+
Double[][] values = new Double[][]{
13+
{100.1, 100.3, 555.12},
14+
{100.0, 100.8, 100.6},
15+
{65656.22, 1112.34, 99.9}
16+
17+
};
18+
System.out.println("Generic Max method returned: " + max(values) + ". When passed this two-dimensional Array " +
19+
"of values: ");
20+
for (Double[] valArray: values) {
21+
System.out.println(Arrays.toString(valArray));
22+
}
23+
}
24+
25+
public static <E extends Comparable<E>> E max(E[][] list) {
26+
E maxValue = list[0][0];
27+
for (int i = 0; i < list.length; i++) {
28+
for (int j = 0; j < list[i].length; j++) {
29+
if (list[i][j].compareTo(maxValue) > 0) {
30+
maxValue = list[i][j];
31+
}
32+
}
33+
}
34+
return maxValue;
35+
}
36+
37+
}

ch_19/Exercise19_07.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package ch_19;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 19.7 (Generic binary search) Implement the following method using binary search.
7+
* public static <E extends Comparable<E>> int binarySearch(E[] list, E key)
8+
*/
9+
public class Exercise19_07 {
10+
public static void main(String[] args) {
11+
Double searchKey = 111.11;
12+
Double[] values = new Double[]{4400.1, 1203.3, 3100.0, 18700.8, 4100.6, 111.11, 2222.21, 4529.21};
13+
14+
System.out.println("Generic Binary Search method found the search key value: " + searchKey + " at index: " + binarySearch(values,
15+
searchKey) + " in the above array.");
16+
17+
}
18+
19+
public static <E extends Comparable<E>> int binarySearch(E[] list, E key) {
20+
System.out.println("Sorting the array to ensure binarySearch is supported: ");
21+
Arrays.sort(list);
22+
System.out.println(Arrays.toString(list));
23+
int lowIdx = 0; // First element in the array
24+
int highIdx = list.length - 1; // Last element in the array
25+
while (highIdx >= lowIdx) {
26+
int midIdx = (lowIdx + highIdx) / 2;
27+
int compare = key.compareTo(list[midIdx]); // Compare key to value at midIdx
28+
if (compare < 0) { // Key is less than list[midIdx]
29+
highIdx = midIdx - 1; // Value can only be to the left of list[midIdx]
30+
} else if (compare == 0) { // Key equals list[midIdx]
31+
return midIdx; // Return the index of matched value
32+
} else { // Key is greater than list[midIdx]
33+
lowIdx = midIdx + 1; // Value can only be to the right of list[midIdx]
34+
}
35+
}
36+
return -1; // No matching value found in list
37+
}
38+
}

ch_19/Exercise19_08.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package ch_19;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
6+
/**
7+
* 19.8 (Shuffle ArrayList) Write the following method that shuffles an ArrayList:
8+
* public static <E> void shuffle(ArrayList<E> list)
9+
*/
10+
public class Exercise19_08 {
11+
public static void main(String[] args) {
12+
//Test generic method with Double values
13+
ArrayList<Double> doubleList =
14+
new ArrayList<>(Arrays.asList(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8));
15+
//Test generic method with Integer values
16+
ArrayList<Integer> integerList =
17+
new ArrayList<>(Arrays.asList(11, 22, 33, 44, 55, 66, 77, 88));
18+
19+
System.out.println("Before shuffling the ArrayList of Doubles is: " + doubleList);
20+
shuffle(doubleList);
21+
System.out.println("After performing Generic shuffle: " + doubleList);
22+
23+
System.out.println("Before shuffling the ArrayList of Integers is: " + integerList);
24+
shuffle(integerList);
25+
System.out.println("After performing Generic shuffle: " + integerList);
26+
}
27+
28+
public static <E> void shuffle(ArrayList<E> list) {
29+
for (int i = 0; i < list.size() && !list.isEmpty(); i++) { // Loop through ArrayList
30+
int randomIdx = (int) (Math.random() * (list.size() - 1));
31+
// Swap random values
32+
E value = list.get(i); // Get value in array at index i
33+
E temp = list.get(randomIdx); // Get value at random index
34+
list.set(randomIdx, value); // Set value to randomIdx
35+
list.set(i, temp); // Set value from randomIdx to index i
36+
}
37+
}
38+
}

ch_19/Exercise19_09.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ch_19;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
6+
/**
7+
* 19.9 (Sort ArrayList) Write the following method that sorts an ArrayList:
8+
* public static <E extends Comparable<E>> void sort(ArrayList<E> list)
9+
*/
10+
public class Exercise19_09 {
11+
public static void main(String[] args) {
12+
//Test generic method with Double values
13+
ArrayList<Double> doubleList =
14+
new ArrayList<>(Arrays.asList(5.0, 7.0, 1.0, 9.0, 10.0, 6.0, 2.0, 4.0, 3.0, 8.0));
15+
//Test generic method with Integer values
16+
ArrayList<Integer> integerList =
17+
new ArrayList<>(Arrays.asList(55, 99, 66, 33, 100, 11, 77, 44, 88, 22));
18+
19+
System.out.println("Before sorting the ArrayList of Doubles is: " + doubleList);
20+
sort(doubleList);
21+
System.out.println("After performing Generic sort: " + doubleList);
22+
23+
System.out.println("Before sorting the ArrayList of Integers is: " + integerList);
24+
sort(integerList);
25+
System.out.println("After performing Generic sort: " + integerList);
26+
}
27+
28+
public static <E extends Comparable<E>> void sort(ArrayList<E> list) {
29+
boolean loop = true;
30+
while (loop) {
31+
loop = false;
32+
for (int i = 0; i < list.size() - 1; i++) {
33+
if (list.get(i).compareTo(list.get(i + 1)) > 0) {
34+
loop = true;
35+
E temp = list.get(i);
36+
list.set(i, list.get(i + 1));
37+
list.set(i + 1, temp);
38+
}
39+
}
40+
}
41+
}
42+
}

ch_19/Exercise19_10.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package ch_19;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
6+
/**
7+
* 19.10 (Largest element in ArrayList)
8+
* Write the following method that returns the largest
9+
* element in an ArrayList:
10+
* <p>
11+
* public static <E extends Comparable<E>> E max(ArrayList<E> list)
12+
*/
13+
public class Exercise19_10 {
14+
public static void main(String[] args) {
15+
//Test generic method with Double values
16+
ArrayList<Double> doubleList =
17+
new ArrayList<>(Arrays.asList(4400.0, 2.2, 6.6, 4400.1, 4.4, 7.7, 1.1, 8.8, 3.3, 5.5, 1203.3, 4400.2,
18+
4400.6, 55.2));
19+
//Test generic method with Integer values
20+
ArrayList<Integer> integerList =
21+
new ArrayList<>(Arrays.asList(55, 66, 33, 11, 77, 44, 88, 22));
22+
23+
System.out.println("The max(ArrayList<E> list) method with a list Doubles returned: " + max(doubleList));
24+
System.out.println("The max(ArrayList<E> list) method with a list of Integers returned: " + max(integerList));
25+
26+
}
27+
28+
public static <E extends Comparable<E>> E max(ArrayList<E> list) {
29+
if (!list.isEmpty()) {
30+
E maxValue = list.get(0); // Initialize Max
31+
for (int i = 1; i < list.size(); i++) {
32+
E nextValue = list.get(i); // Initialize next value in list
33+
if (maxValue.compareTo(nextValue) < 0) { // If maxValue is less than nextValue compareTo() returns -1;
34+
maxValue = list.get(i); // Set larger value to maxValue
35+
}
36+
}
37+
return maxValue;
38+
}
39+
throw new IllegalArgumentException("public static <E extends Comparable<E>> E max(ArrayList<E> list) must be " +
40+
"passed a NON-empty list.");
41+
}
42+
}
43+
44+
45+

ch_19/exercise19_11/Complex.java

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package ch_19.exercise19_11;
2+
3+
/**
4+
* The Complex.class from Programming Exercise 13.17
5+
*/
6+
public class Complex extends Number implements Cloneable {
7+
8+
private double a;
9+
private double b;
10+
private static double i = Math.sqrt(-1);
11+
12+
public Complex() {
13+
this(0);
14+
}
15+
16+
public Complex(double a) {
17+
this(a, 0);
18+
}
19+
20+
public Complex(double a, double b) {
21+
this.a = a;
22+
this.b = b;
23+
}
24+
25+
/**
26+
* Example addition: (a + bi) + (c + di) = (a + c) + (b + d)i
27+
*
28+
* @param complex Complex number to add
29+
* @return the addition result
30+
*/
31+
public double add(Complex complex) {
32+
return (this.a + complex.a) + (this.b + complex.b) * i;
33+
}
34+
35+
/**
36+
* Subtract example:
37+
* a + bi - (c + di) = (a - c) + (b - d)i
38+
*
39+
* @param complex
40+
* @return
41+
*/
42+
public double subtract(Complex complex) {
43+
return (this.a - complex.a) + (this.b - complex.b) * i;
44+
45+
}
46+
47+
/**
48+
* (a + bi)*(c + di) = (ac - bd) + (bc + ad)i
49+
*
50+
* @param complex
51+
* @return
52+
*/
53+
public double multiply(Complex complex) {
54+
return (this.a * complex.a - this.b * complex.b) + ((this.b * complex.a + this.a * complex.b) * i);
55+
}
56+
57+
/**
58+
* (a + bi)/(c + di) = (ac + bd)/(c2 + d2) + (bc - ad)i/(c2 + d2)
59+
*
60+
* @param complex
61+
* @return
62+
*/
63+
public double divide(Complex complex) {
64+
return (this.a * complex.a + this.b * complex.b) / (Math.pow(complex.a, 2) + Math.pow(complex.b, 2)) + ((this.b * complex.a - this.a * complex.b) * i) / (Math.pow(complex.a, 2) + Math.pow(complex.b, 2));
65+
}
66+
67+
public double abs(Complex complex) {
68+
return Math.sqrt(Math.pow(this.a, 2) + Math.pow(this.b, 2));
69+
}
70+
71+
public double getRealPart() {
72+
return a;
73+
}
74+
75+
public double getImaginaryPart() {
76+
return b * i;
77+
}
78+
79+
@Override
80+
public Complex clone() throws CloneNotSupportedException {
81+
return (Complex) super.clone();
82+
83+
}
84+
85+
@Override
86+
public int intValue() {
87+
return (int) Math.round(a + b * i);
88+
}
89+
90+
@Override
91+
public long longValue() {
92+
return Math.round(a + b * i);
93+
}
94+
95+
@Override
96+
public float floatValue() {
97+
return (float) (a + b * i);
98+
}
99+
100+
@Override
101+
public double doubleValue() {
102+
return a + b * i;
103+
}
104+
105+
@Override
106+
public String toString() {
107+
return "( " + a + " + " + b + "(" + i + ")" + " )";
108+
}
109+
}

0 commit comments

Comments
 (0)