Browse Source

Done with fundamentals and oo

Lauren Green 6 years ago
parent
commit
5d66746a8f

+ 14
- 3
src/main/java/rocks/zipcode/io/quiz4/fundamentals/PalindromeEvaluator.java View File

@@ -1,23 +1,34 @@
1 1
 package rocks.zipcode.io.quiz4.fundamentals;
2 2
 
3
+import java.util.ArrayList;
4
+import java.util.List;
5
+
3 6
 /**
4 7
  * @author leon on 18/12/2018.
5 8
  */
6 9
 public class PalindromeEvaluator {
7 10
     public static String[] getAllPalindromes(String string) {
8 11
 
9
-        return null;
12
+        List<String> result = new ArrayList<>();
13
+
14
+        for (int i = 0; i < string.length(); i++) {
15
+            for (int j = i + 1; j <= string.length(); j++) {
16
+                if(isPalindrome(string.substring(i,j)) && !result.contains(string.substring(i,j))) {
17
+                    result.add(string.substring(i, j));
18
+                }
19
+            }
20
+        }
21
+        return result.toArray(new String[0]);
10 22
     }
11 23
 
12 24
     public static Boolean isPalindrome(String string) {
13 25
 
14
-        return string.equalsIgnoreCase(reverseString(string));
26
+        return string.equals(reverseString(string));
15 27
     }
16 28
 
17 29
     public static String reverseString(String string) {
18 30
 
19 31
         String result = "";
20
-        string.toLowerCase();
21 32
 
22 33
         for (int i = (string.length() - 1); i >= 0; i--) {
23 34
             result = result + string.charAt(i);

+ 41
- 3
src/main/java/rocks/zipcode/io/quiz4/fundamentals/StringEvaluator.java View File

@@ -1,18 +1,56 @@
1 1
 package rocks.zipcode.io.quiz4.fundamentals;
2 2
 
3
+import rocks.zipcode.io.quiz4.objectorientation.StringEvaluatorObject;
4
+
5
+import java.util.ArrayList;
6
+import java.util.List;
7
+
3 8
 /**
4 9
  * @author leon on 11/12/2018.
5 10
  */
6 11
 public class StringEvaluator {
7 12
     public static String[] getAllPrefixes(String string) {
8
-        return null;
13
+
14
+        List<String> result = new ArrayList<>();
15
+
16
+        for (int i = 0; i < string.length(); i++) {
17
+            for (int j = i + 1; j <= string.length(); j++) {
18
+                if(!result.contains(string.substring(i,j))) {
19
+                    result.add(string.substring(i, j));
20
+                }
21
+            }
22
+        }
23
+
24
+        return result.toArray(new String[0]);
9 25
     }
10 26
 
11 27
     public static String[] getCommonPrefixes(String string1, String string2) {
12
-        return null;
28
+
29
+        String[] thesePrefixes = getAllPrefixes(string1);
30
+        String[] thosePrefixes = getAllPrefixes(string2);
31
+        List<String> commonPrefixes = new ArrayList<>();
32
+
33
+        for(String s: thesePrefixes) {
34
+            for(String s2: thosePrefixes) {
35
+                if(s.equals(s2)) {
36
+                    commonPrefixes.add(s);
37
+                }
38
+            }
39
+        }
40
+
41
+        return commonPrefixes.toArray(new String[0]);
13 42
     }
14 43
 
15 44
     public static String getLargestCommonPrefix(String string1, String string2) {
16
-        return null;
45
+
46
+        String[] commonPrefixes = getCommonPrefixes(string1, string2);
47
+        String longest = "";
48
+
49
+        for(String s: commonPrefixes) {
50
+            if(s.length() > longest.length()) {
51
+                longest = s;
52
+            }
53
+        }
54
+        return longest;
17 55
     }
18 56
 }

+ 14
- 1
src/main/java/rocks/zipcode/io/quiz4/objectorientation/PalindromeObject.java View File

@@ -2,6 +2,9 @@ package rocks.zipcode.io.quiz4.objectorientation;
2 2
 
3 3
 import rocks.zipcode.io.quiz4.fundamentals.PalindromeEvaluator;
4 4
 
5
+import java.util.ArrayList;
6
+import java.util.List;
7
+
5 8
 /**
6 9
  * @author leon on 18/12/2018.
7 10
  */
@@ -14,7 +17,17 @@ public class PalindromeObject {
14 17
     }
15 18
 
16 19
     public String[] getAllPalindromes(){
17
-        return null;
20
+
21
+        List<String> result = new ArrayList<>();
22
+
23
+        for (int i = 0; i < this.input.length(); i++) {
24
+            for (int j = i + 1; j <= this.input.length(); j++) {
25
+                if(PalindromeEvaluator.isPalindrome(this.input.substring(i,j)) && !result.contains(this.input.substring(i,j))) {
26
+                    result.add(this.input.substring(i, j));
27
+                }
28
+            }
29
+        }
30
+        return result.toArray(new String[0]);
18 31
     }
19 32
 
20 33
     public Boolean isPalindrome(){

+ 11
- 2
src/main/java/rocks/zipcode/io/quiz4/objectorientation/StringAssembler.java View File

@@ -4,14 +4,23 @@ package rocks.zipcode.io.quiz4.objectorientation;
4 4
  * @author leon on 11/12/2018.
5 5
  */
6 6
 public class StringAssembler {
7
+
8
+    private StringBuilder str;
9
+    private char delimeter;
10
+
7 11
     public StringAssembler(Character delimeter) {
12
+        this.str = new StringBuilder();
13
+        this.delimeter = delimeter;
8 14
     }
9 15
 
10 16
     public StringAssembler append(String str) {
11
-        return null;
17
+        this.str.append(str);
18
+        this.str.append(this.delimeter);
19
+        return this;
12 20
     }
13 21
 
14 22
     public String assemble() {
15
-        return null;
23
+
24
+        return this.str.deleteCharAt(this.str.length() - 1).toString();
16 25
     }
17 26
 }

+ 17
- 3
src/main/java/rocks/zipcode/io/quiz4/objectorientation/StringEvaluatorObject.java View File

@@ -1,21 +1,35 @@
1 1
 package rocks.zipcode.io.quiz4.objectorientation;
2 2
 
3
+import rocks.zipcode.io.quiz4.fundamentals.StringEvaluator;
4
+
5
+import java.util.ArrayList;
6
+import java.util.List;
7
+
3 8
 /**
4 9
  * @author leon on 19/12/2018.
5 10
  */
6 11
 public class StringEvaluatorObject {
12
+
13
+    private String str;
14
+
7 15
     public StringEvaluatorObject(String str) {
16
+        this.str = str;
8 17
     }
9 18
 
10 19
     public String[] getAllPrefixes() {
11
-        return null;
20
+
21
+        return StringEvaluator.getAllPrefixes(this.str);
22
+
12 23
     }
13 24
 
14 25
     public String[] getCommonPrefixes(String secondInput) {
15
-        return null;
26
+
27
+        return StringEvaluator.getCommonPrefixes(this.str, secondInput);
28
+
16 29
     }
17 30
 
18 31
     public String getLargestCommonPrefix(String secondInput) {
19
-        return null;
32
+
33
+        return StringEvaluator.getLargestCommonPrefix(this.str, secondInput);
20 34
     }
21 35
 }

+ 1
- 1
src/test/java/rocks/zipcode/io/quiz4/objectorientation/palindromeobject/PalindromeObjectIsPalindromeTestNegative.java View File

@@ -25,6 +25,6 @@ public class PalindromeObjectIsPalindromeTestNegative {
25 25
     }
26 26
 
27 27
     public void test(String input) {
28
-        Assert.assertTrue(new PalindromeObject(input).isPalindrome());
28
+        Assert.assertFalse(new PalindromeObject(input).isPalindrome());
29 29
     }
30 30
 }