Browse Source

up to string utils

mpierse 5 years ago
parent
commit
19b55d1943

+ 3
- 3
src/main/java/rocks/zipcode/quiz5/collections/Food.java View File

@@ -7,9 +7,9 @@ import java.util.*;
7 7
 /**
8 8
  * @author leon on 27/12/2018.
9 9
  */
10
-public class Food <SpiceType extends Class<? extends Spice>> {
10
+public class Food  {
11 11
 
12
-    Map<Spice, Integer> spiceCount = new HashMap<>();
12
+    Map<Spice, Integer> spiceCount = new LinkedHashMap<>();
13 13
 
14 14
     public List<Spice> getAllSpices() {
15 15
         Set<Spice> spicekeys = spiceCount.keySet();
@@ -18,7 +18,7 @@ public class Food <SpiceType extends Class<? extends Spice>> {
18 18
         return result;
19 19
     }
20 20
 
21
-    public  Map<SpiceType, Integer> getSpiceCount() {
21
+    public <SpiceType extends Class<? extends Spice>> Map<SpiceType, Integer> getSpiceCount() {
22 22
         return null;
23 23
     }
24 24
 

+ 17
- 7
src/main/java/rocks/zipcode/quiz5/fundamentals/Calculator.java View File

@@ -1,35 +1,45 @@
1 1
 package rocks.zipcode.quiz5.fundamentals;
2 2
 
3
+import java.util.ArrayList;
4
+
3 5
 /**
4 6
  * @author leon on 21/12/2018.
5 7
  */
6 8
 public class Calculator {
7 9
     public static Double squareRoot(Double value) {
8
-        return null;
10
+        return Math.sqrt(value);
9 11
     }
10 12
 
11 13
     public static Double square(Double value) {
12
-        return null;
14
+        return value*value;
13 15
     }
14 16
 
15 17
     public static Double[] squareRoots(Double... value) {
16
-        return null;
18
+        ArrayList<Double> result = new ArrayList<>();
19
+        for (Double dbl : value) {
20
+            result.add(squareRoot(dbl));
21
+        }
22
+        return result.toArray(new Double[0]);
17 23
     }
18 24
 
19 25
     public static Double[] squares(Double... values) {
20
-        return null;
26
+        ArrayList<Double> result = new ArrayList<>();
27
+        for (Double dbl : values) {
28
+            result.add(square(dbl));
29
+        }
30
+        return result.toArray(new Double[0]);
21 31
     }
22 32
 
23 33
     public static Double add(Double value1, Double value2) {
24
-        return null;
34
+        return value1 + value2;
25 35
     }
26 36
 
27 37
     public static Double subtract(Double value1, Double value2) {
28
-        return null;
38
+        return value1-value2;
29 39
     }
30 40
 
31 41
 
32 42
     public static Double divide(Double divisor, Double dividend) {
33
-        return null;
43
+        return divisor/dividend;
34 44
     }
35 45
 }

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

@@ -5,30 +5,74 @@ package rocks.zipcode.quiz5.fundamentals;
5 5
  */
6 6
 public class StringUtils {
7 7
     public static Character getMiddleCharacter(String string) {
8
-        return null;
8
+        return string.charAt(string.length() / 2);
9 9
     }
10 10
 
11 11
     public static String capitalizeMiddleCharacter(String str) {
12
-        return null;
12
+        int midIndex = str.length() / 2;
13
+        return str.substring(0, midIndex) + str.substring(midIndex, midIndex + 1).toUpperCase() + str.substring(midIndex + 1);
13 14
     }
14 15
 
15 16
     public static String lowerCaseMiddleCharacter(String str) {
16
-        return null;
17
+        int midIndex = str.length() / 2;
18
+        return str.substring(0, midIndex) + str.substring(midIndex, midIndex + 1).toLowerCase() + str.substring(midIndex + 1);
17 19
     }
18 20
 
19 21
     public static Boolean isIsogram(String str) {
20
-        return null;
22
+        char[] stringChars = str.toCharArray();
23
+        for (int i = 0; i < str.length(); i++) {
24
+            for (int j = 0; j < str.length(); j++) {
25
+                if (i != j) {
26
+                    if (stringChars[i] == stringChars[j]) {
27
+                        return false;
28
+                    }
29
+                }
30
+            }
31
+        }
32
+        return true;
21 33
     }
22 34
 
23 35
     public static Boolean hasDuplicateConsecutiveCharacters(String str) {
24
-        return null;
36
+        char[] stringChars = str.toCharArray();
37
+        for (int i = 1; i < str.length(); i++) {
38
+            if (stringChars[i] == stringChars[i - 1]) {
39
+                return true;
40
+            }
41
+        }
42
+        return false;
25 43
     }
26 44
 
27 45
     public static String removeConsecutiveDuplicateCharacters(String str) {
28
-        return null;
46
+        String result = "";
47
+        char[] stringChars = str.toCharArray();
48
+        for (int i = 1; i < str.length(); i++) {
49
+            if (stringChars[i] == stringChars[i - 1]) {
50
+                stringChars[i] = ' ';
51
+                stringChars[i - 1] = ' ';
52
+            }
53
+        }
54
+        for (char letter : stringChars) {
55
+            if (letter != ' ') {
56
+                result += letter;
57
+            }
58
+        }
59
+        return result;
29 60
     }
30 61
 
31 62
     public static String invertCasing(String str) {
32
-        return null;
63
+        char[] stringChars = str.toCharArray();
64
+        String result = "";
65
+        for (int i = 0; i < str.length(); i++) {
66
+            if (stringChars[i]>96 && stringChars[i]<123) {
67
+                stringChars[i] = (char) (stringChars[i] - 32);
68
+            }else if (stringChars[i]>64 && stringChars[i]<91){
69
+                stringChars[i] = (char) (stringChars[i] + 32);
70
+            }
71
+        }
72
+        for (char letter : stringChars) {
73
+            result += letter;
74
+        }
75
+        return result;
33 76
     }
77
+
34 78
 }