|
| 1 | +# Representation exposure: Exercises |
| 2 | + |
| 3 | +## String (1) |
| 4 | + |
| 5 | +The following String class is intended to implement an abstraction for representing pieces of text (i.e. sequences of characters) that is *immutable*, |
| 6 | +i.e. a String instance should represent the same piece of text throughout its lifetime. |
| 7 | +However, it has a flaw. |
| 8 | + |
| 9 | +```java |
| 10 | +public class String { |
| 11 | + |
| 12 | + private char[] characters; |
| 13 | + |
| 14 | + public char[] toArray() { return characters; } |
| 15 | + |
| 16 | + public String(char[] characters) { |
| 17 | + this.characters = characters.clone(); |
| 18 | + } |
| 19 | + |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +### Exercise: Exploit the flaw |
| 24 | + |
| 25 | +Complete the test case below that shows that instances of this class are, in fact, mutable: |
| 26 | +```java |
| 27 | +public class StringTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + void testStringIsMutable() { |
| 31 | + // TODO: Add code |
| 32 | + String myString = // TODO: Add code |
| 33 | + assertEquals('H', myString.toArray()[0]); |
| 34 | + // TODO: Add code |
| 35 | + assertEquals('B', myString.toArray()[0]); |
| 36 | + } |
| 37 | + |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +### Exercise: Fix the flaw |
| 42 | + |
| 43 | +Now, update the String class to fix the flaw and check that the exploit now fails. |
| 44 | + |
| 45 | +## String (2) |
| 46 | + |
| 47 | +The following String class is intended to implement an abstraction for representing pieces of text (i.e. sequences of characters) that is *immutable*, |
| 48 | +i.e. a String instance should represent the same piece of text throughout its lifetime. |
| 49 | +However, it has a flaw. |
| 50 | + |
| 51 | +```java |
| 52 | +public class String { |
| 53 | + |
| 54 | + private char[] characters; |
| 55 | + |
| 56 | + public char[] toArray() { return characters.clone(); } |
| 57 | + |
| 58 | + public String(char[] characters) { |
| 59 | + this.characters = characters; |
| 60 | + } |
| 61 | + |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### Exercise: Exploit the flaw |
| 66 | + |
| 67 | +Complete the test case below that shows that instances of this class are, in fact, mutable: |
| 68 | +```java |
| 69 | +public class StringTest { |
| 70 | + |
| 71 | + @Test |
| 72 | + void testStringIsMutable() { |
| 73 | + // TODO: Add code |
| 74 | + String myString = // TODO: Add code |
| 75 | + assertEquals('H', myString.toArray()[0]); |
| 76 | + // TODO: Add code |
| 77 | + assertEquals('B', myString.toArray()[0]); |
| 78 | + } |
| 79 | + |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +### Exercise: Fix the flaw |
| 84 | + |
| 85 | +Now, update the String class to fix the flaw and check that the exploit now fails. |
| 86 | + |
| 87 | +## Matrix (1) |
| 88 | + |
| 89 | +The following Matrix class is intended to implement an abstraction for representing matrices that is *immutable*, |
| 90 | +i.e. a Matrix instance should represent the same matrix throughout its lifetime. |
| 91 | +However, it has a flaw. |
| 92 | + |
| 93 | +```java |
| 94 | +public class Matrix { |
| 95 | + private int nbRows; |
| 96 | + private int nbColumns; |
| 97 | + private double[][] rows; |
| 98 | + |
| 99 | + public double[][] getRows() { |
| 100 | + return rows.clone(); |
| 101 | + } |
| 102 | + |
| 103 | + public Matrix(int nbRows, int nbColumns, double[][] rows) { |
| 104 | + this.nbRows = nbRows; |
| 105 | + this.nbColumns = nbColumns; |
| 106 | + this.rows = new double[nbRows][nbColumns]; |
| 107 | + for (int rowIndex = 0; rowIndex < nbRows; rowIndex++) |
| 108 | + for (int columnIndex; columnIndex < nbColumns; columnIndex++) |
| 109 | + this.rows[rowIndex][columnIndex] = rows[rowIndex][columnIndex]; |
| 110 | + } |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +### Exercise: Exploit the flaw |
| 115 | + |
| 116 | +Complete the test case below that shows that instances of this class are, in fact, mutable: |
| 117 | +```java |
| 118 | +public class MatrixTest { |
| 119 | + |
| 120 | + @Test |
| 121 | + void testMatrixIsMutable() { |
| 122 | + // TODO: Add code |
| 123 | + Matrix myMatrix = // TODO: Add code |
| 124 | + assertEquals(42, myMatrix.getRows()[0][0]); |
| 125 | + // TODO: Add code |
| 126 | + assertEquals(24, myMatrix.getRows()[0][0]); |
| 127 | + } |
| 128 | + |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +### Exercise: Fix the flaw |
| 133 | + |
| 134 | +Now, update the Matrix class to fix the flaw and check that the exploit now fails. |
| 135 | + |
| 136 | +## Matrix (2) |
| 137 | + |
| 138 | +The following Matrix class is intended to implement an abstraction for representing matrices that is *immutable*, |
| 139 | +i.e. a Matrix instance should represent the same matrix throughout its lifetime. |
| 140 | +However, it has a flaw. |
| 141 | + |
| 142 | +```java |
| 143 | +public class Matrix { |
| 144 | + private int nbRows; |
| 145 | + private int nbColumns; |
| 146 | + private double[][] rows; |
| 147 | + |
| 148 | + public double[][] getRows() { |
| 149 | + double[][] result = new double[nbRows][nbColumns]; |
| 150 | + for (int rowIndex = 0; rowIndex < nbRows; rowIndex++) |
| 151 | + for (int columnIndex; columnIndex < nbColumns; columnIndex++) |
| 152 | + result[rowIndex][columnIndex] = rows[rowIndex][columnIndex]; |
| 153 | + return result; |
| 154 | + } |
| 155 | + |
| 156 | + public Matrix(int nbRows, int nbColumns, double[][] rows) { |
| 157 | + this.nbRows = nbRows; |
| 158 | + this.nbColumns = nbColumns; |
| 159 | + this.rows = rows.clone(); |
| 160 | + } |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +### Exercise: Exploit the flaw |
| 165 | + |
| 166 | +Complete the test case below that shows that instances of this class are, in fact, mutable: |
| 167 | +```java |
| 168 | +public class MatrixTest { |
| 169 | + |
| 170 | + @Test |
| 171 | + void testMatrixIsMutable() { |
| 172 | + // TODO: Add code |
| 173 | + Matrix myMatrix = // TODO: Add code |
| 174 | + assertEquals(42, myMatrix.getRows()[0][0]); |
| 175 | + // TODO: Add code |
| 176 | + assertEquals(24, myMatrix.getRows()[0][0]); |
| 177 | + } |
| 178 | + |
| 179 | +} |
| 180 | +``` |
| 181 | + |
| 182 | +### Exercise: Fix the flaw |
| 183 | + |
| 184 | +Now, update the Matrix class to fix the flaw and check that the exploit now fails. |
0 commit comments