Browse Source

complete assignment

Kibret Tecle 6 years ago
parent
commit
115bd1fb07

+ 2
- 0
ChapterOneMicro.iml View File

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>

+ 54
- 27
src/main/java/com/zipcodewilmington/danny_do_better_exercises/MathUtilities.java View File

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

+ 17
- 6
src/main/java/com/zipcodewilmington/danny_do_better_exercises/PredicateUtilities.java View File

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

+ 29
- 10
src/main/java/com/zipcodewilmington/danny_do_better_exercises/StringUtilities.java View File

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

+ 1
- 1
src/main/java/com/zipcodewilmington/danny_do_better_exercises/ZipcodeRocks.java View File

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
 }

+ 3
- 3
src/test/java/com/zipcodewilmington/danny_do_better_exercises/TestMathUtilities.java View File

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 = primativeTypes.add(baseValue, addedValue);
42
         // : Then
42
         // : Then
146
         // : When
146
         // : When
147
         float actualFloat = primativeTypes.subtract(baseValue,difference);
147
         float actualFloat = primativeTypes.subtract(baseValue,difference);
148
         // : Then
148
         // : Then
149
-        assertEquals(expectedFloat,actualFloat, 0);
149
+        assertEquals(expectedFloat,actualFloat, 0.003);
150
     }
150
     }
151
     @Test
151
     @Test
152
     public void testSubtractions5() {
152
     public void testSubtractions5() {
295
     public void testMultiplication3() {
295
     public void testMultiplication3() {
296
         // : Given
296
         // : Given
297
         byte multiplicand = 16;
297
         byte multiplicand = 16;
298
-        byte multiplier = 14;
298
+        byte multiplier = 4;
299
         byte expectedByte = 64;
299
         byte expectedByte = 64;
300
         // : When
300
         // : When
301
         byte actualByte = primativeTypes.multiply(multiplicand, multiplier);
301
         byte actualByte = primativeTypes.multiply(multiplicand, multiplier);

+ 4
- 4
src/test/java/com/zipcodewilmington/danny_do_better_exercises/TestPredicateUtilities.java View File

27
     public void testGreaterThanFalse(){
27
     public void testGreaterThanFalse(){
28
         // : Given
28
         // : Given
29
         int greaterValue = 350;
29
         int greaterValue = 350;
30
-        int lesserValue = 350;
30
+        int lesserValue = 349;
31
 
31
 
32
         // : When
32
         // : When
33
         boolean outcome = math.isGreaterThan(greaterValue, lesserValue);
33
         boolean outcome = math.isGreaterThan(greaterValue, lesserValue);
62
         boolean outcome = math.isLessThan(greaterValue, lesserValue);
62
         boolean outcome = math.isLessThan(greaterValue, lesserValue);
63
 
63
 
64
         // : Then
64
         // : Then
65
-        assertTrue(outcome);
65
+        assertFalse(outcome);
66
     }
66
     }
67
 
67
 
68
 
68
 
109
     @Test
109
     @Test
110
     public void testGreaterOrEqual2(){
110
     public void testGreaterOrEqual2(){
111
         // : Given
111
         // : Given
112
-        int greaterValue = 8;
113
-        int lesserValue = 15;
112
+        int greaterValue = 15;
113
+        int lesserValue = 8;
114
 
114
 
115
         // : When
115
         // : When
116
         boolean outcome = math.isGreaterThanOrEqualTo(greaterValue, lesserValue);
116
         boolean outcome = math.isGreaterThanOrEqualTo(greaterValue, lesserValue);

+ 3
- 2
src/test/java/com/zipcodewilmington/danny_do_better_exercises/TestStringUtilities.java View File

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 = "olleH";
60
+        String expected = "Hel";
60
 
61
 
61
         // : When
62
         // : When
62
         String actual = StringUtilities.getPrefix(input);
63
         String actual = StringUtilities.getPrefix(input);
154
         String expected = "Wilmington";
155
         String expected = "Wilmington";
155
 
156
 
156
         // : When
157
         // : When
157
-        String actual = StringUtilities.getFirstWord(input);
158
+        String actual = StringUtilities.getSecondWord(input);
158
 
159
 
159
         // : Then
160
         // : Then
160
         assertEquals(expected, actual);
161
         assertEquals(expected, actual);

BIN
target/classes/com/zipcodewilmington/danny_do_better_exercises/MathUtilities.class View File


BIN
target/classes/com/zipcodewilmington/danny_do_better_exercises/PredicateUtilities.class View File


BIN
target/classes/com/zipcodewilmington/danny_do_better_exercises/StringUtilities.class View File


BIN
target/classes/com/zipcodewilmington/danny_do_better_exercises/ZipcodeRocks.class View File


BIN
target/test-classes/com/zipcodewilmington/danny_do_better_exercises/TestMathUtilities.class View File


BIN
target/test-classes/com/zipcodewilmington/danny_do_better_exercises/TestPredicateUtilities.class View File


BIN
target/test-classes/com/zipcodewilmington/danny_do_better_exercises/TestStringUtilities.class View File