Browse Source

pre-merge commit

Connor Dunnigan 5 years ago
parent
commit
a5d721428b

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

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

+ 34
- 8
src/main/java/rocks/zipcode/quiz5/fundamentals/StringUtils.java View File

@@ -1,34 +1,60 @@
1 1
 package rocks.zipcode.quiz5.fundamentals;
2 2
 
3
+import java.util.ArrayList;
4
+import java.util.Arrays;
5
+import java.util.HashSet;
6
+import java.util.Set;
7
+
3 8
 /**
4 9
  * @author leon on 21/12/2018.
5 10
  */
6 11
 public class StringUtils {
7
-    public static Character getMiddleCharacter(String string) {
8
-        return null;
12
+    public static Character getMiddleCharacter(String string){
13
+        return string.charAt(string.length()/2);
9 14
     }
10 15
 
11 16
     public static String capitalizeMiddleCharacter(String str) {
12
-        return null;
17
+        StringBuilder sb = new StringBuilder(str);
18
+        sb.setCharAt(str.length() / 2, Character.toUpperCase(getMiddleCharacter(str)));
19
+        return sb.toString();
13 20
     }
14 21
 
15 22
     public static String lowerCaseMiddleCharacter(String str) {
16
-        return null;
23
+        StringBuilder sb = new StringBuilder(str);
24
+        sb.setCharAt(str.length() / 2, Character.toLowerCase(getMiddleCharacter(str)));
25
+        return sb.toString();
17 26
     }
18 27
 
19 28
     public static Boolean isIsogram(String str) {
20
-        return null;
29
+        String[] arr = str.split("");
30
+        Set<String> set = new HashSet<>(Arrays.asList(arr));
31
+        return (str.length() == set.size());
21 32
     }
22 33
 
23 34
     public static Boolean hasDuplicateConsecutiveCharacters(String str) {
24
-        return null;
35
+        char[] charArr = str.toCharArray();
36
+        for(int i = 0; i < charArr.length - 1; i++)
37
+            if(charArr[i]==charArr[i+1])
38
+                return true;
39
+        return false;
25 40
     }
26 41
 
27 42
     public static String removeConsecutiveDuplicateCharacters(String str) {
28
-        return null;
43
+        Set<Integer> set = new HashSet<>();
44
+        char[] array = str.toCharArray();
45
+        StringBuilder sb = new StringBuilder(str);
46
+        for(int i = 1; i < array.length; i++)
47
+            if(array[i-1] == array[i]) {
48
+                set.add(i - 1);
49
+                set.add(i);
50
+            }
51
+        int numDeleted = 0;
52
+        for(Integer num : set)
53
+            sb.deleteCharAt(num - numDeleted++);
54
+        return sb.toString();
29 55
     }
30 56
 
31 57
     public static String invertCasing(String str) {
32
-        return null;
58
+        return org.apache.commons.lang3.StringUtils.swapCase(str);
33 59
     }
34 60
 }