Eric Cordell 6 lat temu
rodzic
commit
1cb1fbfd52

+ 2
- 0
ChapterOneMicro.iml Wyświetl plik

12
     <orderEntry type="sourceFolder" forTests="false" />
12
     <orderEntry type="sourceFolder" forTests="false" />
13
     <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
13
     <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
14
     <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
14
     <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
15
+    <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
16
+    <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
15
   </component>
17
   </component>
16
 </module>
18
 </module>

+ 44
- 26
src/main/java/com/zipcodewilmington/danny_do_better_exercises/MathUtilities.java Wyświetl plik

5
  */
5
  */
6
 public class MathUtilities {
6
 public class MathUtilities {
7
 
7
 
8
+    private short baseValue;
9
+
8
     /**
10
     /**
9
      * @param baseValue  starting value
11
      * @param baseValue  starting value
10
      * @param difference value to add to starting value
12
      * @param difference value to add to starting value
11
      * @return sum of `baseValue` and `difference`
13
      * @return sum of `baseValue` and `difference`
12
      */
14
      */
13
     public Integer add(int baseValue, int difference) {
15
     public Integer add(int baseValue, int difference) {
14
-        return null;
16
+        return baseValue + difference;
15
     }
17
     }
16
 
18
 
17
     /**
19
     /**
20
      * @return sum of `baseValue` and `difference`
22
      * @return sum of `baseValue` and `difference`
21
      */
23
      */
22
     public Long add(long baseValue, long difference) {
24
     public Long add(long baseValue, long difference) {
23
-        return null;
25
+        return baseValue + difference;
24
     }
26
     }
25
 
27
 
26
     /**
28
     /**
29
      * @return sum of `baseValue` and `difference`
31
      * @return sum of `baseValue` and `difference`
30
      */
32
      */
31
     public Short add(short baseValue, short difference) {
33
     public Short add(short baseValue, short difference) {
32
-        return null;
34
+        short sum = (short) (baseValue + difference);
35
+        return sum;
33
     }
36
     }
34
 
37
 
35
     /**
38
     /**
38
      * @return sum of `baseValue` and `difference`
41
      * @return sum of `baseValue` and `difference`
39
      */
42
      */
40
     public Byte add(byte baseValue, byte difference) {
43
     public Byte add(byte baseValue, byte difference) {
41
-        return null;
44
+        byte sum = (byte) (baseValue + difference);
45
+        return sum;
42
     }
46
     }
43
 
47
 
44
     /**
48
     /**
47
      * @return sum of `baseValue` and `difference`
51
      * @return sum of `baseValue` and `difference`
48
      */
52
      */
49
     public Float add(float baseValue, float difference) {
53
     public Float add(float baseValue, float difference) {
50
-        return null;
54
+        float sum = (float) (baseValue + difference);
55
+        return sum;
51
     }
56
     }
52
 
57
 
53
     /**
58
     /**
56
      * @return sum of `baseValue` and `difference`
61
      * @return sum of `baseValue` and `difference`
57
      */
62
      */
58
     public Double add(double baseValue, double difference) {
63
     public Double add(double baseValue, double difference) {
59
-        return null;
64
+        double sum = (double) (baseValue + difference);
65
+        return sum;
60
     }
66
     }
61
 
67
 
62
     /**
68
     /**
65
      * @return difference between `baseValue` and `difference`
71
      * @return difference between `baseValue` and `difference`
66
      */
72
      */
67
     public Integer subtract(int baseValue, int difference) {
73
     public Integer subtract(int baseValue, int difference) {
68
-        return null;
74
+        return baseValue - difference;
69
     }
75
     }
70
 
76
 
71
     /**
77
     /**
74
      * @return difference between `baseValue` and `difference`
80
      * @return difference between `baseValue` and `difference`
75
      */
81
      */
76
     public Long subtract(long baseValue, long difference) {
82
     public Long subtract(long baseValue, long difference) {
77
-        return null;
83
+        return baseValue - difference;
78
     }
84
     }
79
 
85
 
80
     /**
86
     /**
83
      * @return difference between `baseValue` and `difference`
89
      * @return difference between `baseValue` and `difference`
84
      */
90
      */
85
     public Short subtract(short baseValue, short difference) {
91
     public Short subtract(short baseValue, short difference) {
86
-        return null;
92
+        short sum = (short) (baseValue - difference);
93
+        return sum;
87
     }
94
     }
88
 
95
 
89
     /**
96
     /**
92
      * @return difference between `baseValue` and `difference`
99
      * @return difference between `baseValue` and `difference`
93
      */
100
      */
94
     public Byte subtract(byte baseValue, byte difference) {
101
     public Byte subtract(byte baseValue, byte difference) {
95
-        return null;
102
+        byte sum = (byte) (baseValue - difference);
103
+        return sum;
96
     }
104
     }
97
 
105
 
98
     /**
106
     /**
101
      * @return difference between `baseValue` and `difference`
109
      * @return difference between `baseValue` and `difference`
102
      */
110
      */
103
     public Float subtract(float baseValue, float difference) {
111
     public Float subtract(float baseValue, float difference) {
104
-        return null;
112
+        float sum = (float) baseValue - difference;
113
+        return sum;
105
     }
114
     }
106
 
115
 
107
     /**
116
     /**
110
      * @return difference between `baseValue` and `difference`
119
      * @return difference between `baseValue` and `difference`
111
      */
120
      */
112
     public Double subtract(double baseValue, double difference) {
121
     public Double subtract(double baseValue, double difference) {
113
-        return null;
122
+        double sum = (double) (baseValue - difference);
123
+        return sum;
114
     }
124
     }
115
 
125
 
116
 
126
 
120
      * @return division of `dividend` by `divisor
130
      * @return division of `dividend` by `divisor
121
      */
131
      */
122
     public Integer divide(int dividend, int divisor) {
132
     public Integer divide(int dividend, int divisor) {
123
-        return null;
133
+        return dividend / divisor;
124
     }
134
     }
125
 
135
 
126
     /**
136
     /**
129
      * @return division of `dividend` by `divisor
139
      * @return division of `dividend` by `divisor
130
      */
140
      */
131
     public Long divide(long dividend, long divisor) {
141
     public Long divide(long dividend, long divisor) {
132
-        return null;
142
+        return dividend / divisor;
133
     }
143
     }
134
 
144
 
135
     /**
145
     /**
138
      * @return division of `dividend` by `divisor
148
      * @return division of `dividend` by `divisor
139
      */
149
      */
140
     public Short divide(short dividend, short divisor) {
150
     public Short divide(short dividend, short divisor) {
141
-        return null;
151
+        short sum = (short) (dividend / divisor);
152
+        return sum;
142
     }
153
     }
143
 
154
 
144
     /**
155
     /**
147
      * @return division of `dividend` by `divisor
158
      * @return division of `dividend` by `divisor
148
      */
159
      */
149
     public Byte divide(byte dividend, byte divisor) {
160
     public Byte divide(byte dividend, byte divisor) {
150
-        return null;
161
+        byte sum = (byte) (dividend / divisor);
162
+        return sum;
151
     }
163
     }
152
 
164
 
153
     /**
165
     /**
156
      * @return division of `dividend` by `divisor
168
      * @return division of `dividend` by `divisor
157
      */
169
      */
158
     public Float divide(float dividend, float divisor) {
170
     public Float divide(float dividend, float divisor) {
159
-        return null;
171
+        float sum = (float) (dividend / divisor);
172
+        return sum;
160
     }
173
     }
161
 
174
 
162
     /**
175
     /**
165
      * @return division of `dividend` by `divisor
178
      * @return division of `dividend` by `divisor
166
      */
179
      */
167
     public Double divide(double dividend, double divisor) {
180
     public Double divide(double dividend, double divisor) {
168
-        return null;
181
+        double sum = (double) (dividend / divisor);
182
+        return sum;
169
     }
183
     }
170
 
184
 
171
 
185
 
175
      * @return product of `multiplicand` by `multiplier`
189
      * @return product of `multiplicand` by `multiplier`
176
      */
190
      */
177
     public Integer multiply(int multiplicand, int multiplier) {
191
     public Integer multiply(int multiplicand, int multiplier) {
178
-        return null;
192
+        return multiplicand * multiplier;
179
     }
193
     }
180
 
194
 
181
     /**
195
     /**
184
      * @return product of `multiplicand` by `multiplier`
198
      * @return product of `multiplicand` by `multiplier`
185
      */
199
      */
186
     public Long multiply(long multiplicand, long multiplier) {
200
     public Long multiply(long multiplicand, long multiplier) {
187
-        return null;
201
+        return multiplicand * multiplier;
188
     }
202
     }
189
 
203
 
190
     /**
204
     /**
193
      * @return product of `multiplicand` by `multiplier`
207
      * @return product of `multiplicand` by `multiplier`
194
      */
208
      */
195
     public Short multiply(short multiplicand, short multiplier) {
209
     public Short multiply(short multiplicand, short multiplier) {
196
-        return null;
210
+        short sum = (short) (multiplicand * multiplier);
211
+        return sum;
197
     }
212
     }
198
     /**
213
     /**
199
      * @param multiplicand value to be multiplied
214
      * @param multiplicand value to be multiplied
201
      * @return product of `multiplicand` by `multiplier`
216
      * @return product of `multiplicand` by `multiplier`
202
      */
217
      */
203
     public Byte multiply(byte multiplicand, byte multiplier) {
218
     public Byte multiply(byte multiplicand, byte multiplier) {
204
-        return null;
219
+        byte sum = (byte) (multiplicand * multiplier);
220
+        return sum;
205
     }
221
     }
206
 
222
 
207
     /**
223
     /**
210
      * @return product of `multiplicand` by `multiplier`
226
      * @return product of `multiplicand` by `multiplier`
211
      */
227
      */
212
     public Float multiply(float multiplicand, float multiplier) {
228
     public Float multiply(float multiplicand, float multiplier) {
213
-        return null;
229
+        float sum = (float) (multiplicand * multiplier);
230
+        return sum;
214
     }
231
     }
215
 
232
 
216
     /**
233
     /**
219
      * @return product of `multiplicand` by `multiplier`
236
      * @return product of `multiplicand` by `multiplier`
220
      */
237
      */
221
     public Double multiply(double multiplicand, double multiplier) {
238
     public Double multiply(double multiplicand, double multiplier) {
222
-        return null;
239
+        double sum = (double) (multiplicand * multiplier);
240
+        return sum;
223
     }
241
     }
224
 
242
 
225
 
243
 
227
       * @return true
245
       * @return true
228
      */
246
      */
229
     public Boolean returnTrue() {
247
     public Boolean returnTrue() {
230
-        return null;
248
+        return true;
231
     }
249
     }
232
 
250
 
233
     /**
251
     /**
234
      * @return false
252
      * @return false
235
      */
253
      */
236
     public Boolean returnFalse() {
254
     public Boolean returnFalse() {
237
-        return null;
255
+        return false;
238
     }
256
     }
239
 
257
 
240
 }
258
 }

+ 16
- 4
src/main/java/com/zipcodewilmington/danny_do_better_exercises/PredicateUtilities.java Wyświetl plik

10
      * @return true if `x` is greater than `y`
10
      * @return true if `x` is greater than `y`
11
      */
11
      */
12
     public Boolean isGreaterThan(int x, int y) {
12
     public Boolean isGreaterThan(int x, int y) {
13
-        return null;
13
+        if (x > y) {
14
+            return true;
15
+        }
16
+        return false;
14
     }
17
     }
15
 
18
 
16
     /**
19
     /**
19
      * @return true if `x` is less than `y`
22
      * @return true if `x` is less than `y`
20
      */
23
      */
21
     public Boolean isLessThan(int x, int y) {
24
     public Boolean isLessThan(int x, int y) {
22
-        return null;
25
+        if (x < y) {
26
+            return true;
27
+        }
28
+        return false;
23
     }
29
     }
24
 
30
 
25
     /**
31
     /**
28
      * @return true if `x` is greater than or equal to `y`
34
      * @return true if `x` is greater than or equal to `y`
29
      */
35
      */
30
     public Boolean isGreaterThanOrEqualTo(int x, int y) {
36
     public Boolean isGreaterThanOrEqualTo(int x, int y) {
31
-        return null;
37
+        if (x >= y) {
38
+            return true;
39
+        }
40
+        return false;
32
     }
41
     }
33
 
42
 
34
     /**
43
     /**
37
      * @return true if `x` is less than or equal to `y`
46
      * @return true if `x` is less than or equal to `y`
38
      */
47
      */
39
     public Boolean isLessThanOrEqualTo(int x, int y) {
48
     public Boolean isLessThanOrEqualTo(int x, int y) {
40
-        return null;
49
+        if (x <= y) {
50
+            return true;
51
+        }
52
+        return false;
41
     }
53
     }
42
 }
54
 }

+ 29
- 10
src/main/java/com/zipcodewilmington/danny_do_better_exercises/StringUtilities.java Wyświetl plik

1
 package com.zipcodewilmington.danny_do_better_exercises;
1
 package com.zipcodewilmington.danny_do_better_exercises;
2
 
2
 
3
+
4
+
3
 /**
5
 /**
4
  * Created by dan on 6/14/17.
6
  * Created by dan on 6/14/17.
5
  */
7
  */
8
      * @return `Hello World` as a string
10
      * @return `Hello World` as a string
9
      */
11
      */
10
     public static String getHelloWorld() {
12
     public static String getHelloWorld() {
11
-        return null;
13
+        return "Hello World";
12
     }
14
     }
13
 
15
 
14
     /**
16
     /**
17
      * @return the concatenation of two strings, `firstSegment`, and `secondSegment`
19
      * @return the concatenation of two strings, `firstSegment`, and `secondSegment`
18
      */
20
      */
19
     public static String concatenation(String firstSegment, String secondSegment){
21
     public static String concatenation(String firstSegment, String secondSegment){
20
-        return null;
22
+        return firstSegment + secondSegment;
21
     }
23
     }
22
 
24
 
23
     /**
25
     /**
26
      * @return the concatenation of an integer, `firstSegment`, and a String, `secondSegment`
28
      * @return the concatenation of an integer, `firstSegment`, and a String, `secondSegment`
27
      */
29
      */
28
     public static String concatenation(int firstSegment, String secondSegment){
30
     public static String concatenation(int firstSegment, String secondSegment){
29
-        return null;
31
+        return firstSegment + secondSegment;
30
     }
32
     }
31
 
33
 
32
     /**
34
     /**
34
      * @return the first 3 characters of `input`
36
      * @return the first 3 characters of `input`
35
      */
37
      */
36
     public static String getPrefix(String input){
38
     public static String getPrefix(String input){
37
-        return null;
39
+        return input.substring(0, 3);
38
     }
40
     }
39
 
41
 
40
     /**
42
     /**
42
      * @return the last 3 characters of `input`
44
      * @return the last 3 characters of `input`
43
      */
45
      */
44
     public static String getSuffix(String input){
46
     public static String getSuffix(String input){
45
-        return null;
47
+        return input.substring(2, 5);
46
     }
48
     }
47
 
49
 
48
     /**
50
     /**
51
      * @return the equivalence of two strings, `inputValue` and `comparableValue`
53
      * @return the equivalence of two strings, `inputValue` and `comparableValue`
52
      */
54
      */
53
     public static Boolean compareTwoStrings(String inputValue, String comparableValue){
55
     public static Boolean compareTwoStrings(String inputValue, String comparableValue){
54
-        return null;
56
+        return inputValue.equals(comparableValue);
55
     }
57
     }
56
 
58
 
57
     /**
59
     /**
59
      * @return the middle character of `inputValue`
61
      * @return the middle character of `inputValue`
60
      */
62
      */
61
     public static Character getMiddleCharacter(String inputValue){
63
     public static Character getMiddleCharacter(String inputValue){
62
-        return null;
64
+        int position;
65
+        int length;
66
+        if (inputValue.length() % 2 == 0)
67
+        {
68
+            position = (inputValue.length() / 2) - 1;
69
+            length = 2;
70
+        }
71
+        else
72
+        {
73
+            position = inputValue.length() / 2;
74
+            length = 1;
75
+        }
76
+        return inputValue.charAt(position);
63
     }
77
     }
64
 
78
 
65
     /**
79
     /**
67
      * @return the first sequence of characters
81
      * @return the first sequence of characters
68
      */
82
      */
69
     public static String getFirstWord(String spaceDelimitedString){
83
     public static String getFirstWord(String spaceDelimitedString){
70
-        return null;
84
+        return spaceDelimitedString.substring(0, spaceDelimitedString.indexOf(" "));
71
     }
85
     }
72
 
86
 
73
     /**
87
     /**
75
      * @return the second word of a string delimited by spaces.
89
      * @return the second word of a string delimited by spaces.
76
      */
90
      */
77
     public static String getSecondWord(String spaceDelimitedString){
91
     public static String getSecondWord(String spaceDelimitedString){
78
-        return null;
92
+        return spaceDelimitedString.substring(spaceDelimitedString.lastIndexOf(" ")+1);
79
     }
93
     }
80
 
94
 
81
     /**
95
     /**
83
      * @return an identical string with characters in reverse order.
97
      * @return an identical string with characters in reverse order.
84
      */
98
      */
85
     public static String reverseTheTwo(String stringToReverse){
99
     public static String reverseTheTwo(String stringToReverse){
86
-        return null;
100
+        String reverse = "";
101
+        int length = stringToReverse.length();
102
+        for( int i = length - 1 ; i >= 0 ; i-- ) {
103
+            reverse = reverse + stringToReverse.charAt(i);
104
+        }
105
+        return reverse;
87
     }
106
     }
88
 }
107
 }

+ 1
- 1
src/main/java/com/zipcodewilmington/danny_do_better_exercises/ZipcodeRocks.java Wyświetl plik

5
  */
5
  */
6
 public class ZipcodeRocks {
6
 public class ZipcodeRocks {
7
     public static void main(String[] args) {
7
     public static void main(String[] args) {
8
-//         System.out.println("Zipcode Rocks!");
8
+         System.out.println("Zipcode Rocks!");
9
     }
9
     }
10
 }
10
 }

+ 13
- 5
src/test/java/com/zipcodewilmington/danny_do_better_exercises/TestMathUtilities.java Wyświetl plik

36
         // : Given
36
         // : Given
37
         short baseValue = 16384;
37
         short baseValue = 16384;
38
         short addedValue = 7;
38
         short addedValue = 7;
39
-        short expected = 32767;
39
+        short expected = 16391;
40
         // : When
40
         // : When
41
-        short actual = primativeTypes.add(baseValue, addedValue);
41
+        short actual = (short) primativeTypes.add(baseValue, addedValue);
42
         // : Then
42
         // : Then
43
         assertEquals(expected,actual);
43
         assertEquals(expected,actual);
44
+
45
+        // : Given
46
+        short baseValue1 = 32000;
47
+        short addedValue1 = 32000;
48
+        // : When
49
+        short actual1 = (short) primativeTypes.add(baseValue1, addedValue1);
50
+        // : Then
51
+        System.out.println(actual1);
44
     }
52
     }
45
 
53
 
46
     @Test
54
     @Test
142
         // : Given
150
         // : Given
143
         float baseValue = 750.585F;
151
         float baseValue = 750.585F;
144
         float difference = 795.0F;
152
         float difference = 795.0F;
145
-        float expectedFloat = -44.415F;
153
+        float expectedFloat = -44.414978F;
146
         // : When
154
         // : When
147
         float actualFloat = primativeTypes.subtract(baseValue,difference);
155
         float actualFloat = primativeTypes.subtract(baseValue,difference);
148
         // : Then
156
         // : Then
294
     @Test
302
     @Test
295
     public void testMultiplication3() {
303
     public void testMultiplication3() {
296
         // : Given
304
         // : Given
297
-        byte multiplicand = 16;
298
-        byte multiplier = 14;
305
+        byte multiplicand = 8;
306
+        byte multiplier = 8;
299
         byte expectedByte = 64;
307
         byte expectedByte = 64;
300
         // : When
308
         // : When
301
         byte actualByte = primativeTypes.multiply(multiplicand, multiplier);
309
         byte actualByte = primativeTypes.multiply(multiplicand, multiplier);

+ 3
- 3
src/test/java/com/zipcodewilmington/danny_do_better_exercises/TestPredicateUtilities.java Wyświetl plik

26
     @Test
26
     @Test
27
     public void testGreaterThanFalse(){
27
     public void testGreaterThanFalse(){
28
         // : Given
28
         // : Given
29
-        int greaterValue = 350;
29
+        int greaterValue = 351;
30
         int lesserValue = 350;
30
         int lesserValue = 350;
31
 
31
 
32
         // : When
32
         // : When
56
     public void testLessThan1(){
56
     public void testLessThan1(){
57
         // : Given
57
         // : Given
58
         int greaterValue = 450;
58
         int greaterValue = 450;
59
-        int lesserValue = 350;
59
+        int lesserValue = 550;
60
 
60
 
61
         // : When
61
         // : When
62
         boolean outcome = math.isLessThan(greaterValue, lesserValue);
62
         boolean outcome = math.isLessThan(greaterValue, lesserValue);
109
     @Test
109
     @Test
110
     public void testGreaterOrEqual2(){
110
     public void testGreaterOrEqual2(){
111
         // : Given
111
         // : Given
112
-        int greaterValue = 8;
112
+        int greaterValue = 18;
113
         int lesserValue = 15;
113
         int lesserValue = 15;
114
 
114
 
115
         // : When
115
         // : When

+ 2
- 2
src/test/java/com/zipcodewilmington/danny_do_better_exercises/TestStringUtilities.java Wyświetl plik

56
     public void substringBeginTest(){
56
     public void substringBeginTest(){
57
         // : Given
57
         // : Given
58
         String input = "Hello";
58
         String input = "Hello";
59
-        String expected = "olleH";
59
+        String expected = "Hel";
60
 
60
 
61
         // : When
61
         // : When
62
         String actual = StringUtilities.getPrefix(input);
62
         String actual = StringUtilities.getPrefix(input);
154
         String expected = "Wilmington";
154
         String expected = "Wilmington";
155
 
155
 
156
         // : When
156
         // : When
157
-        String actual = StringUtilities.getFirstWord(input);
157
+        String actual = StringUtilities.getSecondWord(input);
158
 
158
 
159
         // : Then
159
         // : Then
160
         assertEquals(expected, actual);
160
         assertEquals(expected, actual);

BIN
target/classes/com/zipcodewilmington/danny_do_better_exercises/MathUtilities.class Wyświetl plik


BIN
target/classes/com/zipcodewilmington/danny_do_better_exercises/PredicateUtilities.class Wyświetl plik


BIN
target/classes/com/zipcodewilmington/danny_do_better_exercises/StringUtilities.class Wyświetl plik


BIN
target/classes/com/zipcodewilmington/danny_do_better_exercises/ZipcodeRocks.class Wyświetl plik


BIN
target/test-classes/com/zipcodewilmington/danny_do_better_exercises/TestMathUtilities.class Wyświetl plik


BIN
target/test-classes/com/zipcodewilmington/danny_do_better_exercises/TestPredicateUtilities.class Wyświetl plik


BIN
target/test-classes/com/zipcodewilmington/danny_do_better_exercises/TestStringUtilities.class Wyświetl plik