Skip to content

Commit 15dc023

Browse files
committed
Refactored Chapter 9
1 parent 1de0b05 commit 15dc023

18 files changed

+635
-415
lines changed

src/Lessons/IntWrapper.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package Lessons;
2+
3+
/* Used in ShallowDeep lesson */
4+
public class IntWrapper {
5+
int value;
6+
7+
public IntWrapper(int v){
8+
value = v;
9+
}
10+
11+
@Override
12+
public String toString(){
13+
return String.valueOf(value);
14+
}
15+
}

src/Lessons/ShallowDeep.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package Lessons;
2+
3+
import java.util.ArrayList;
4+
5+
/* This is a lesson on how Integer differs from other objects when it comes to copying values
6+
*
7+
* - .clone() on an ArrayList<> gives you a new ArrayList. If it's an ArrayList of Integers
8+
* then you get your own deep copy of Integers. If it's an ArrayList of other objects then
9+
* your ArrayList contains references to the original objects.
10+
*/
11+
public class ShallowDeep {
12+
public static void main (String [] args){
13+
/***********/
14+
/* Integer */
15+
/***********/
16+
System.out.println("************* Integer **************");
17+
ArrayList<Integer> array1 = new ArrayList<>();
18+
ArrayList<Integer> array2 = new ArrayList<>();
19+
20+
Integer a = 2;
21+
array1.add(a);
22+
array2.add(a);
23+
printArrays(array1, array2);
24+
25+
a = 88; // changed value of a, but neither array is affected.
26+
printArrays(array1, array2);
27+
28+
array1.set(0, 99); // changed entry in 1st array. 2nd array remains unaffected.
29+
printArrays(array1, array2);
30+
31+
array2 = (ArrayList<Integer>) array1.clone(); // cloned 1st array into 2nd array
32+
printArrays(array1, array2);
33+
34+
array1.set(0, 700); // changed value of element in 1st array, 2nd array remains unaffected
35+
printArrays(array1, array2);
36+
37+
/**************/
38+
/* IntWrapper */
39+
/**************/
40+
System.out.println("************* IntWrapper **************");
41+
ArrayList<IntWrapper> array3 = new ArrayList<>();
42+
ArrayList<IntWrapper> array4 = new ArrayList<>();
43+
44+
IntWrapper four = new IntWrapper(4);
45+
array3.add(four);
46+
array4.add(four);
47+
printArrays2(array3, array4);
48+
49+
four.value = 55; // changed value in 1 array, and it changes in other array too
50+
printArrays2(array3, array4);
51+
52+
array3.get(0).value = 99; // changed value in 1st array, changes in 2nd array also
53+
printArrays2(array3, array4);
54+
55+
/* .clone() */
56+
ArrayList<IntWrapper> array5 = new ArrayList<>();
57+
ArrayList<IntWrapper> array6 = new ArrayList<>();
58+
59+
IntWrapper six = new IntWrapper(6);
60+
IntWrapper seven = new IntWrapper(7);
61+
array5.add(six);
62+
array5.add(seven);
63+
printArrays2(array5, array6);
64+
65+
array6.addAll((ArrayList<IntWrapper>) array5.clone()); //cloned 1st array into 2nd array
66+
printArrays2(array5, array6);
67+
68+
array6.get(0).value = 99; // changed value in 1st array, and it changes in 2nd array as well
69+
printArrays2(array5, array6);
70+
}
71+
72+
private static void printArrays(ArrayList<Integer> array1, ArrayList<Integer> array2){
73+
System.out.println("(1st) array = " + array1);
74+
System.out.println("(2nd) array = " + array2 + "\n");
75+
}
76+
77+
private static void printArrays2(ArrayList<IntWrapper> array1, ArrayList<IntWrapper> array2){
78+
System.out.println("(1st) array = " + array1);
79+
System.out.println("(2nd) array = " + array2 + "\n");
80+
}
81+
}

src/chapter9/Box.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,49 @@
11
package chapter9;
2+
23
/* Used in 9.10 */
34
public class Box {
45
int width;
56
int height;
67
int depth;
78

8-
/* Constructor */
9-
Box(int w, int h, int d){
9+
/* Constructors */
10+
Box (int w, int h, int d){
1011
width = w;
1112
height = h;
1213
depth = d;
1314
}
1415

16+
Box (Box other){
17+
width = other.width;
18+
height = other.height;
19+
depth = other.depth;
20+
}
21+
22+
/* A box can be placed on 1) an empty platform (that's what the "other == null" is for), or 2) a bigger box (in every dimension) */
1523
public boolean canPlaceAbove(Box other){
16-
return (other == null) ||(width < other.width && height < other.height && depth < other.depth);
17-
// the (other == null) is so the bottom-most box can be placed on anything
24+
return (other == null) || ( width < other.width && height < other.height && depth < other.depth);
25+
}
26+
27+
/* Book didn't have these 2 functions. I think they're needed so Boxes can be hashed correctly */
28+
29+
@Override
30+
public boolean equals(Object other){
31+
if (other == this)
32+
return true;
33+
if (other == null || !(other instanceof Box))
34+
return false;
35+
36+
Box otherBox = (Box) other;
37+
return width == otherBox.width && height == otherBox.height && depth == otherBox.depth;
38+
}
39+
40+
@Override
41+
public int hashCode(){
42+
return 2 * width + 3 * height + 5 * depth;
43+
}
44+
45+
@Override
46+
public String toString() {
47+
return "Box (" + width + "," + height + "," + depth + ")";
1848
}
1949
}

0 commit comments

Comments
 (0)