Browse Source

poking around

Jennifer Chao 5 years ago
parent
commit
3e1995b6fc

+ 5
- 2
src/main/java/rocks/zipcode/quiz5/fundamentals/Calculator.java View File

@@ -1,5 +1,7 @@
1 1
 package rocks.zipcode.quiz5.fundamentals;
2 2
 
3
+import java.math.BigDecimal;
4
+
3 5
 /**
4 6
  * @author leon on 21/12/2018.
5 7
  */
@@ -27,8 +29,9 @@ public class Calculator {
27 29
     }
28 30
 
29 31
     public static Double add(Double value1, Double value2) {
30
-        return value1 + value2;
31
-        // return (Math.floor(value1 + value2) * 100) / 100;
32
+        String twoDecimals = String.format("%.2f", value1 + value2);
33
+
34
+        return Double.parseDouble(twoDecimals);
32 35
     }
33 36
 
34 37
     public static Double subtract(Double value1, Double value2) {

+ 57
- 7
src/main/java/rocks/zipcode/quiz5/fundamentals/StringUtils.java View File

@@ -1,34 +1,84 @@
1 1
 package rocks.zipcode.quiz5.fundamentals;
2 2
 
3
+import java.util.*;
4
+
3 5
 /**
4 6
  * @author leon on 21/12/2018.
5 7
  */
6 8
 public class StringUtils {
7 9
     public static Character getMiddleCharacter(String string) {
8
-        return null;
10
+        return string.charAt(getMiddleIndex(string));
11
+    }
12
+
13
+    public static Integer getMiddleIndex(String string) {
14
+        return string.length() / 2;
9 15
     }
10 16
 
11 17
     public static String capitalizeMiddleCharacter(String str) {
12
-        return null;
18
+        StringBuilder builder = new StringBuilder(str);
19
+        builder.replace(getMiddleIndex(str), getMiddleIndex(str) + 1, getMiddleCharacter(str).toString().toUpperCase());
20
+
21
+        return builder.toString();
13 22
     }
14 23
 
15 24
     public static String lowerCaseMiddleCharacter(String str) {
16
-        return null;
25
+        StringBuilder builder = new StringBuilder(str);
26
+        builder.replace(getMiddleIndex(str), getMiddleIndex(str) + 1, getMiddleCharacter(str).toString().toLowerCase());
27
+
28
+        return builder.toString();
17 29
     }
18 30
 
19 31
     public static Boolean isIsogram(String str) {
20
-        return null;
32
+        List<Character> characters = new ArrayList<>();
33
+
34
+        for (int i = 0; i < str.length(); i++) {
35
+            if (!characters.contains(str.charAt(i))) {
36
+                characters.add(str.charAt(i));
37
+            } else {
38
+                return false;
39
+            }
40
+        }
41
+
42
+        return true;
21 43
     }
22 44
 
23 45
     public static Boolean hasDuplicateConsecutiveCharacters(String str) {
24
-        return null;
46
+        for (int i = 1; i < str.length(); i++) {
47
+            if (str.charAt(i) == str.charAt(i - 1)) {
48
+                return true;
49
+            }
50
+        }
51
+
52
+        return false;
25 53
     }
26 54
 
27 55
     public static String removeConsecutiveDuplicateCharacters(String str) {
28
-        return null;
56
+        Set<String> duplicateChars = new HashSet<>();
57
+
58
+        for (int i = 1; i < str.length(); i++) {
59
+            if (str.charAt(i) == str.charAt(i - 1)) {
60
+                duplicateChars.add(str.substring(i, i + 1));
61
+            }
62
+        }
63
+
64
+        for (String character : duplicateChars) {
65
+            str = str.replaceAll(character, "");
66
+        }
67
+
68
+        return str;
29 69
     }
30 70
 
31 71
     public static String invertCasing(String str) {
32
-        return null;
72
+        StringBuilder builder = new StringBuilder();
73
+
74
+        for (int i = 0; i < str.length(); i++) {
75
+            if (Character.isUpperCase(str.charAt(i))) {
76
+                builder.append(Character.toLowerCase(str.charAt(i)));
77
+            } else {
78
+                builder.append(Character.toUpperCase(str.charAt(i)));
79
+            }
80
+        }
81
+
82
+        return builder.toString();
33 83
     }
34 84
 }