Browse Source

Refactored Math

Kthomas 6 years ago
parent
commit
a77345dde1

+ 28
- 72
src/main/java/com/zipcodewilmington/danny_do_better_exercises/MathUtilities.java View File

1
 package com.zipcodewilmington.danny_do_better_exercises;
1
 package com.zipcodewilmington.danny_do_better_exercises;
2
 
2
 
3
 /**
3
 /**
4
- * Created by dan on 6/14/17.
4
+ * Refactored
5
  */
5
  */
6
 public class MathUtilities {
6
 public class MathUtilities {
7
 
7
 
8
-
9
     public Integer add(int baseValue, int difference) {
8
     public Integer add(int baseValue, int difference) {
10
-        int add = baseValue + difference;
11
-        return add;
9
+        return baseValue + difference;
12
     }
10
     }
13
 
11
 
14
     public Long add(long baseValue, long difference) {
12
     public Long add(long baseValue, long difference) {
15
-        long add = baseValue + difference;
16
-        return add;
13
+        return baseValue + difference;
17
     }
14
     }
18
 
15
 
19
     public Short add(short baseValue, short difference) {
16
     public Short add(short baseValue, short difference) {
20
         int sum = baseValue + difference;
17
         int sum = baseValue + difference;
21
-        short shortSum = (short) sum;
22
-        return shortSum;
18
+        return (short) sum;
23
     }
19
     }
24
 
20
 
25
     public Byte add(byte baseValue, byte difference) {
21
     public Byte add(byte baseValue, byte difference) {
26
         int sum = baseValue + difference;
22
         int sum = baseValue + difference;
27
-        byte byteSum = (byte) sum;
28
-        return byteSum;
23
+        return (byte) sum;
29
     }
24
     }
30
 
25
 
31
     public Float add(float baseValue, float difference) {
26
     public Float add(float baseValue, float difference) {
32
-       float add = baseValue + difference;
33
-        return add;
27
+        return baseValue + difference;
34
     }
28
     }
35
 
29
 
36
     public Double add(double baseValue, double difference) {
30
     public Double add(double baseValue, double difference) {
37
-        double add = baseValue + difference;
38
-        return add;
31
+        return baseValue + difference;
39
     }
32
     }
40
 
33
 
41
     public Integer subtract(int baseValue, int difference) {
34
     public Integer subtract(int baseValue, int difference) {
42
-        int subtract = baseValue - difference;
43
-        return subtract;
35
+        return baseValue - difference;
44
     }
36
     }
45
 
37
 
46
     public Long subtract(long baseValue, long difference) {
38
     public Long subtract(long baseValue, long difference) {
47
-        long subtract = baseValue - difference;
48
-        return subtract;
39
+        return baseValue - difference;
49
     }
40
     }
50
 
41
 
51
     public Short subtract(short baseValue, short difference) {
42
     public Short subtract(short baseValue, short difference) {
52
         int sum = baseValue - difference;
43
         int sum = baseValue - difference;
53
-        short shortSum = (short) sum;
54
-        return shortSum;
55
-
44
+        return (short) sum;
56
     }
45
     }
57
 
46
 
58
     public Byte subtract(byte baseValue, byte difference) {
47
     public Byte subtract(byte baseValue, byte difference) {
59
         int sum = baseValue - difference;
48
         int sum = baseValue - difference;
60
-        byte byteSum = (byte) sum;
61
-        return byteSum;
62
-
49
+        return (byte) sum;
63
     }
50
     }
64
 
51
 
65
     public Float subtract(float baseValue, float difference) {
52
     public Float subtract(float baseValue, float difference) {
66
-        float subtract = baseValue - difference;
67
-        return subtract;
53
+        return baseValue - difference;
68
     }
54
     }
69
 
55
 
70
     public Double subtract(double baseValue, double difference) {
56
     public Double subtract(double baseValue, double difference) {
71
-        double subtract = baseValue - difference;
72
-        return subtract;
57
+        return baseValue - difference;
73
     }
58
     }
74
 
59
 
75
     public Integer divide(int dividend, int divisor) {
60
     public Integer divide(int dividend, int divisor) {
76
-        int divide = dividend / divisor;
77
-        return divide;
61
+        return dividend / divisor;
78
     }
62
     }
79
 
63
 
80
     public Long divide(long dividend, long divisor) {
64
     public Long divide(long dividend, long divisor) {
81
-        long divide = dividend / divisor;
82
-        return divide;
83
-
65
+        return dividend / divisor;
84
     }
66
     }
85
 
67
 
86
     public Short divide(short dividend, short divisor) {
68
     public Short divide(short dividend, short divisor) {
87
         int sum = dividend / divisor;
69
         int sum = dividend / divisor;
88
-        short shortSum = (short) sum;
89
-        return shortSum;
90
-
70
+        return (short) sum;
91
     }
71
     }
92
 
72
 
93
     public Byte divide(byte dividend, byte divisor) {
73
     public Byte divide(byte dividend, byte divisor) {
94
         int sum = dividend / divisor;
74
         int sum = dividend / divisor;
95
-        byte byteSum = (byte) sum;
96
-        return byteSum;
97
-
75
+        return (byte) sum;
98
     }
76
     }
99
 
77
 
100
     public Float divide(float dividend, float divisor) {
78
     public Float divide(float dividend, float divisor) {
101
-        float divide = dividend / divisor;
102
-        return divide;
103
-
79
+        return dividend / divisor;
104
     }
80
     }
105
 
81
 
106
     public Double divide(double dividend, double divisor) {
82
     public Double divide(double dividend, double divisor) {
107
-        double divide = dividend / divisor;
108
-        return divide;
109
-
83
+        return dividend / divisor;
110
     }
84
     }
111
 
85
 
112
     public Integer multiply(int multiplicand, int multiplier) {
86
     public Integer multiply(int multiplicand, int multiplier) {
113
-        int multiply = multiplicand * multiplier;
114
-        return multiply;
87
+        return multiplicand * multiplier;
115
     }
88
     }
116
 
89
 
117
     public Long multiply(long multiplicand, long multiplier) {
90
     public Long multiply(long multiplicand, long multiplier) {
118
-        long multiply = multiplicand * multiplier;
119
-        return multiply;
91
+        return multiplicand * multiplier;
120
     }
92
     }
121
 
93
 
122
     public Short multiply(short multiplicand, short multiplier) {
94
     public Short multiply(short multiplicand, short multiplier) {
123
         int sum = multiplicand * multiplier;
95
         int sum = multiplicand * multiplier;
124
-        short shortSum = (short) sum;
125
-        return shortSum;
96
+        return (short) sum;
126
     }
97
     }
127
 
98
 
128
     public Byte multiply(byte multiplicand, byte multiplier) {
99
     public Byte multiply(byte multiplicand, byte multiplier) {
129
         int sum = multiplicand * multiplier;
100
         int sum = multiplicand * multiplier;
130
-        byte byteSum = (byte) sum;
131
-        return byteSum;
101
+        return (byte) sum;
132
     }
102
     }
133
 
103
 
134
     public Float multiply(float multiplicand, float multiplier) {
104
     public Float multiply(float multiplicand, float multiplier) {
135
-        float multiply = multiplicand * multiplier;
136
-        return multiply;
105
+        return multiplicand * multiplier;
137
     }
106
     }
138
 
107
 
139
     public Double multiply(double multiplicand, double multiplier) {
108
     public Double multiply(double multiplicand, double multiplier) {
140
-        double multiply = multiplicand * multiplier;
141
-        return multiply;
142
-
109
+        return multiplicand * multiplier;
143
     }
110
     }
144
 
111
 
145
     public Boolean returnTrue() {
112
     public Boolean returnTrue() {
146
-        if (20 < 100) {
147
-            return true;
148
-        }
149
-        else {
150
-            return false;
151
-        }
113
+        return true;
152
     }
114
     }
153
 
115
 
154
     public Boolean returnFalse() {
116
     public Boolean returnFalse() {
155
-        if (69 >= 9000) {
156
-            return true;
157
-        } else {
158
-            return false;
159
-        }
160
-
117
+        return false;
161
     }
118
     }
162
-
163
-}
119
+}

+ 16
- 22
src/main/java/com/zipcodewilmington/danny_do_better_exercises/StringUtilities.java View File

35
         return inputValue.equals(comparableValue) ;
35
         return inputValue.equals(comparableValue) ;
36
     }
36
     }
37
 
37
 
38
-    /**
39
-     * @param inputValue the value input from user
40
-     * @return the middle character of `inputValue`
41
-     */
42
     public static Character getMiddleCharacter(String inputValue){
38
     public static Character getMiddleCharacter(String inputValue){
43
-
44
-        return input;
39
+        int length = inputValue.length();
40
+        int middleIndex = length / 2;
41
+        if (middleIndex % 2 == 0){
42
+            middleIndex = middleIndex - 1;
43
+        }
44
+        char middleCharacter = inputValue.charAt(middleIndex);
45
+        return middleCharacter;
45
     }
46
     }
46
 
47
 
47
-    /**
48
-     * @param spaceDelimitedString a string, representative of a sentence, containing spaces
49
-     * @return the first sequence of characters
50
-     */
51
-    public static String getFirstWord(String spaceDelimitedString){
52
-        return null;
48
+    public static String getFirstWord(String spaceDelimitedString) {
49
+        String[] firstWord = spaceDelimitedString.split(" ");
50
+        return firstWord[0];
53
     }
51
     }
54
 
52
 
55
-    /**
56
-     * @param spaceDelimitedString a string delimited by spaces
57
-     * @return the second word of a string delimited by spaces.
58
-     */
59
     public static String getSecondWord(String spaceDelimitedString){
53
     public static String getSecondWord(String spaceDelimitedString){
60
-        return null;
54
+        String[]secondWord = spaceDelimitedString.split( " ");
55
+        return secondWord[1];
61
     }
56
     }
62
 
57
 
63
-    /**
64
-     * @param stringToReverse
65
-     * @return an identical string with characters in reverse order.
66
-     */
67
     public static String reverseTheTwo(String stringToReverse){
58
     public static String reverseTheTwo(String stringToReverse){
68
-        return null;
59
+
60
+        String reverse = new StringBuffer(stringToReverse).reverse().toString();
61
+
62
+        return reverse;
69
     }
63
     }
70
 }
64
 }

+ 2
- 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
+
9
+        System.out.println("Zipcode Rocks!");
9
     }
10
     }
10
 }
11
 }

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

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/StringUtilities.class View File


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