7 Revize

Autor SHA1 Zpráva Datum
  De'Jon Johnson 44baf6fad0 update před 6 roky
  De'Jon Johnson c487024818 testtteerrrrrssszzzzz před 6 roky
  Leon f6d67906de update před 6 roky
  De'Jon Johnson 080759034d pre-merge commit před 6 roky
  Leon a2c2d3be0d update před 6 roky
  Leon 4ae486eca3 updated genericutils před 6 roky
  Leon c15ab321e7 created an additional oop class & tests před 6 roky

+ 27
- 5
src/main/java/rocks/zipcode/io/quiz4/collections/ComparableTreeSet.java Zobrazit soubor

@@ -1,17 +1,39 @@
1 1
 package rocks.zipcode.io.quiz4.collections;
2 2
 
3
+import java.util.TreeSet;
4
+
3 5
 /**
4 6
  * @author leon on 11/12/2018.
5 7
  */
6
-public class ComparableTreeSet<_> {
7
-    public ComparableTreeSet(_... arr) {
8
-    }
8
+public class ComparableTreeSet<Element> implements Comparable<Element> {
9
+    private TreeSet<Element> treeset = new TreeSet<Element>();
10
+
11
+    public ComparableTreeSet(Element... arr) {
12
+
13
+        for(Element itemAdded : arr){
14
+            treeset.add(itemAdded);
15
+        }
9 16
 
17
+    }
18
+    public int size(){
19
+        return treeset.size();
20
+    }
10 21
 
11 22
     public ComparableTreeSet() {
12 23
     }
13 24
 
14
-    public Integer compareTo(ComparableTreeSet<_> o) {
15
-        return null;
25
+    public Integer compareTo(ComparableTreeSet<Element> o) {
26
+        if (treeset.size() > o.size())
27
+          return 1;
28
+         else if (treeset.size() < o.size())
29
+           return -1;
30
+        else
31
+        return 0;
32
+        }
33
+
34
+
35
+    @Override
36
+    public int compareTo(Element o) {
37
+        return 0;
16 38
     }
17 39
 }

+ 34
- 5
src/main/java/rocks/zipcode/io/quiz4/collections/SimpleStringGroup.java Zobrazit soubor

@@ -1,31 +1,60 @@
1 1
 package rocks.zipcode.io.quiz4.collections;
2 2
 
3
+import java.util.ArrayList;
4
+import java.util.Iterator;
5
+import java.util.List;
6
+import java.util.Spliterator;
7
+import java.util.function.Consumer;
8
+
3 9
 /**
4 10
  * @author leon on 11/12/2018.
5 11
  */
6
-public class SimpleStringGroup {
12
+public class SimpleStringGroup implements Iterable {
13
+
14
+    private List<String> list = new ArrayList<String>();
15
+
16
+
7 17
     public SimpleStringGroup() {
8
-        throw new UnsupportedOperationException("Method not yet implemented");
18
+//        throw new UnsupportedOperationException("Method not yet implemented");
9 19
     }
10 20
 
11 21
     public Integer count() {
12
-        return null;
22
+        return list.size();
13 23
     }
14 24
 
15 25
     public void insert(String string) {
26
+        list.add(string);
16 27
     }
17 28
 
18 29
     public Boolean has(String valueToInsert) {
19
-        return null;
30
+        return list.contains(valueToInsert);
20 31
     }
21 32
 
22 33
     public String fetch(int indexOfValue) {
23
-        return null;
34
+        return list.get(indexOfValue);
24 35
     }
25 36
 
26 37
     public void delete(String valueToInsert) {
38
+        list.remove(valueToInsert);
27 39
     }
28 40
 
29 41
     public void clear() {
42
+        list.clear();
43
+    }
44
+
45
+    @Override
46
+    public Iterator iterator() {
47
+        return list.iterator();
48
+    }
49
+    @Override
50
+public void forEach(Consumer action) {
51
+        list.forEach(action);
52
+        }
53
+  @Override
54
+public Spliterator spliterator() {
55
+     return list.spliterator();
56
+
30 57
     }
58
+
59
+
31 60
 }

+ 25
- 4
src/main/java/rocks/zipcode/io/quiz4/collections/ZipCodeWilmington.java Zobrazit soubor

@@ -2,23 +2,44 @@ package rocks.zipcode.io.quiz4.collections;
2 2
 
3 3
 import rocks.zipcode.io.quiz4.objectorientation.Student;
4 4
 
5
+import java.util.ArrayList;
6
+import java.util.HashMap;
7
+import java.util.List;
5 8
 import java.util.Map;
6 9
 
7 10
 /**
8 11
  * @author leon on 11/12/2018.
9 12
  */
10 13
 public class ZipCodeWilmington {
14
+
15
+    private List<Student> students = new ArrayList<>();
16
+
17
+    public ZipCodeWilmington() {
18
+          this.students = new ArrayList<>();
19
+         }
20
+
11 21
     public void enroll(Student student) {
22
+        this.students.add(student);
12 23
     }
13 24
 
14 25
     public Boolean isEnrolled(Student student) {
15
-        return null;
26
+
27
+        return students.contains(student);
16 28
     }
17 29
 
18 30
     public void lecture(double numberOfHours) {
31
+        for (Student dork : this.students) {
32
+            dork.learn(numberOfHours);
33
+        }
19 34
     }
20 35
 
21
-    public Map<Student, Double> getStudyMap() {
22
-        return null;
36
+        public Map<Student, Double> getStudyMap () {
37
+
38
+            Map<Student, Double> map = new HashMap<>();
39
+            for (Student dork : this.students) {
40
+                map.put(dork, dork.getTotalStudyTime());
41
+            }
42
+            return map;
43
+        }
23 44
     }
24
-}
45
+

+ 22
- 3
src/main/java/rocks/zipcode/io/quiz4/fundamentals/PalindromeEvaluator.java Zobrazit soubor

@@ -1,18 +1,37 @@
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 {
10
+
11
+
7 12
     public static String[] getAllPalindromes(String string) {
8
-        return null;
13
+        List<String> result = new ArrayList<>();
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]);
9 22
     }
10 23
 
11 24
     public static Boolean isPalindrome(String string) {
12
-        return null;
25
+
26
+        return string.equals(reverseString(string));
13 27
     }
14 28
 
15 29
     public static String reverseString(String string) {
16
-        return null;
30
+
31
+        String newString = "";
32
+        for (int i = string.length() - 1; i >= 0 ; i--) {
33
+          newString += string.charAt(i);
34
+           }
35
+     return newString;
17 36
     }
18 37
 }

+ 27
- 3
src/main/java/rocks/zipcode/io/quiz4/fundamentals/StringEvaluator.java Zobrazit soubor

@@ -1,18 +1,42 @@
1 1
 package rocks.zipcode.io.quiz4.fundamentals;
2 2
 
3
+import java.util.ArrayList;
4
+import java.util.Arrays;
5
+import java.util.List;
6
+
3 7
 /**
4 8
  * @author leon on 11/12/2018.
5 9
  */
6 10
 public class StringEvaluator {
7 11
     public static String[] getAllPrefixes(String string) {
8
-        return null;
12
+
13
+        ArrayList<String> list = new ArrayList<>();
14
+       for (int i = 0; i < string.length(); i++) {
15
+             for (int j = string.length(); j > i; j--) {
16
+                 if (!list.contains(string.substring(i, j))) {
17
+                    list.add(string.substring(i, j));
18
+                     }
19
+              }
20
+           }
21
+       return list.toArray(new String[list.size()]);
9 22
     }
10 23
 
11 24
     public static String[] getCommonPrefixes(String string1, String string2) {
12
-        return null;
25
+        List<String> result = new ArrayList<String>(Arrays.asList(getAllPrefixes(string1)));
26
+       result.retainAll(Arrays.asList(getAllPrefixes(string2)));
27
+       return result.toArray(new String[0]);
28
+
29
+
13 30
     }
14 31
 
15 32
     public static String getLargestCommonPrefix(String string1, String string2) {
16
-        return null;
33
+        String[] newArray = getCommonPrefixes(string1,string2);
34
+        String str = "";
35
+         for (String s : newArray) {
36
+            if (s.length() > str.length()) {
37
+               str = s;
38
+                }
39
+           }
40
+      return str;
17 41
     }
18 42
 }

+ 3
- 2
src/main/java/rocks/zipcode/io/quiz4/generics/GenericUtils.java Zobrazit soubor

@@ -8,11 +8,12 @@ import java.util.TreeSet;
8 8
  * @author leon on 11/12/2018.
9 9
  */
10 10
 public class GenericUtils {
11
-    public static <_ extends Comparable> Iterable<Iterable<_>> powerSet(Set<_> originalSet) {
11
+    public static <_ extends Comparable> Iterable<? extends Iterable<_>> powerSet(Set<_> originalSet) {
12 12
         return null;
13 13
     }
14 14
 
15
-    public static <_ extends Comparable> Iterable<Iterable<_>> powerSet(_... originalSet) {
15
+    public static <_ extends Comparable> Iterable<? extends Iterable<_>> powerSet(_... originalSet) {
16 16
         return powerSet(new TreeSet<>(Arrays.asList(originalSet)));
17 17
     }
18 18
 }
19
+

+ 14
- 6
src/main/java/rocks/zipcode/io/quiz4/generics/Group.java Zobrazit soubor

@@ -1,37 +1,45 @@
1 1
 package rocks.zipcode.io.quiz4.generics;
2 2
 
3
+import java.util.ArrayList;
3 4
 import java.util.Iterator;
5
+import java.util.List;
4 6
 
5 7
 /**
6 8
  * @author leon on 18/12/2018.
7 9
  */
8
-public class Group<_> {
10
+public class Group<_> implements Iterable<_>, GroupInterface<_> {
11
+    List<_> result = new ArrayList<>();
9 12
     public Group() {
10
-        throw new UnsupportedOperationException("Method not yet implemented");
13
+//        throw new UnsupportedOperationException("Method not yet implemented");
11 14
     }
12 15
 
16
+
13 17
     public Integer count() {
14
-        return null;
18
+        return result.size();
15 19
     }
16 20
 
17 21
     public void insert(_ value) {
22
+        result.add(value);
18 23
     }
19 24
 
20 25
     public Boolean has(_ value) {
21
-        return null;
26
+        return result.contains(value);
22 27
     }
23 28
 
24 29
     public _ fetch(int indexOfValue) {
25
-        return null;
30
+        return result.get(indexOfValue);
26 31
     }
27 32
 
28 33
     public void delete(_ value) {
34
+        result.remove(value);
29 35
     }
30 36
 
31 37
     public void clear() {
38
+        result.clear();
32 39
     }
33 40
 
34 41
     public Iterator<_> iterator() {
35
-        return null;
42
+        Iterator<_> list = result.iterator();
43
+        return list;
36 44
     }
37 45
 }

+ 23
- 3
src/main/java/rocks/zipcode/io/quiz4/objectorientation/PalindromeObject.java Zobrazit soubor

@@ -1,21 +1,41 @@
1 1
 package rocks.zipcode.io.quiz4.objectorientation;
2 2
 
3
+import rocks.zipcode.io.quiz4.fundamentals.PalindromeEvaluator;
4
+
5
+import java.util.HashSet;
6
+import java.util.Set;
7
+
3 8
 /**
4 9
  * @author leon on 18/12/2018.
5 10
  */
6 11
 public class PalindromeObject {
12
+
13
+    private String input;
7 14
     public PalindromeObject(String input) {
15
+        this.input = input;
8 16
     }
9 17
 
10 18
     public String[] getAllPalindromes(){
11
-        return null;
19
+
20
+        Set<String> list = new HashSet<>();
21
+        for (int i = 0; i < this.input.length(); i++){
22
+            for (int j = i + 1; j <= this.input.length(); j++){
23
+               if(PalindromeEvaluator.isPalindrome(this.input.substring(i, j))){
24
+                    list.add(this.input.substring(i, j));
25
+                    }
26
+                }
27
+           }
28
+
29
+       return list.toArray(new String[0]);
12 30
     }
13 31
 
14 32
     public Boolean isPalindrome(){
15
-        return null;
33
+
34
+        return (reverseString().equals(this.input));
16 35
     }
17 36
 
18 37
     public String reverseString(){
19
-        return null;
38
+
39
+        return new StringBuilder(this.input).reverse().toString();
20 40
     }
21 41
 }

+ 10
- 2
src/main/java/rocks/zipcode/io/quiz4/objectorientation/StringAssembler.java Zobrazit soubor

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

+ 20
- 3
src/main/java/rocks/zipcode/io/quiz4/objectorientation/StringEvaluatorObject.java Zobrazit soubor

@@ -1,21 +1,38 @@
1 1
 package rocks.zipcode.io.quiz4.objectorientation;
2 2
 
3
+import rocks.zipcode.io.quiz4.fundamentals.StringEvaluator;
4
+
5
+import java.util.HashSet;
6
+import java.util.Set;
7
+
3 8
 /**
4 9
  * @author leon on 19/12/2018.
5 10
  */
6 11
 public class StringEvaluatorObject {
12
+    private String str;
13
+
7 14
     public StringEvaluatorObject(String str) {
15
+        this.str = str;
8 16
     }
9 17
 
10 18
     public String[] getAllPrefixes() {
11
-        return null;
19
+
20
+        Set<String> results = new HashSet<>();
21
+        for (int i = 0; i < str.length(); i++){
22
+             for (int j = i + 1; j <= str.length(); j++){
23
+                 results.add(str.substring(i, j));
24
+                }
25
+          }
26
+      return results.toArray(new String[0]);
12 27
     }
13 28
 
14 29
     public String[] getCommonPrefixes(String secondInput) {
15
-        return null;
30
+
31
+        return StringEvaluator.getCommonPrefixes(str, secondInput);
16 32
     }
17 33
 
18 34
     public String getLargestCommonPrefix(String secondInput) {
19
-        return null;
35
+
36
+        return StringEvaluator.getLargestCommonPrefix(str, secondInput);
20 37
     }
21 38
 }

+ 8
- 2
src/main/java/rocks/zipcode/io/quiz4/objectorientation/Student.java Zobrazit soubor

@@ -4,17 +4,23 @@ package rocks.zipcode.io.quiz4.objectorientation;
4 4
  * @author leon on 11/12/2018.
5 5
  */
6 6
 public class Student {
7
+    private int id;
8
+    private double studyLength;
7 9
     public Student() {
8
-        this(null);
10
+
11
+        this(0);
9 12
     }
10 13
 
11 14
     public Student(Integer id) {
15
+        this.id = id;
12 16
     }
13 17
 
14 18
     public void learn(Double amountOfHours) {
19
+        studyLength += amountOfHours;
15 20
     }
16 21
 
17 22
     public Double getTotalStudyTime() {
18
-        return null;
23
+
24
+        return studyLength;
19 25
     }
20 26
 }

+ 1
- 1
src/test/java/rocks/zipcode/io/quiz4/fundamentals/palindromeevaluator/PalindromeEvaluatorIsPalindromeTestNegative.java Zobrazit soubor

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

+ 1
- 1
src/test/java/rocks/zipcode/io/quiz4/objectorientation/palindromeobject/PalindromeObjectIsPalindromeTestNegative.java Zobrazit soubor

@@ -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
 }

+ 0
- 54
src/test/java/rocks/zipcode/io/quiz4/utils/MyTestClass.java Zobrazit soubor

@@ -1,54 +0,0 @@
1
-package rocks.zipcode.io.quiz4.utils;
2
-
3
-import rocks.zipcode.io.quiz4.collections.ComparableTreeSet;
4
-
5
-import java.util.*;
6
-
7
-/**
8
- * @author leon on 18/12/2018.
9
- */
10
-public class MyTestClass {
11
-
12
-    public static <E> Set<Set<E>> findPowerSet(Set<E> set) {
13
-        Set<Set<E>> ret = new TreeSet<>();
14
-        ret.add(set);
15
-        if (set.isEmpty()) {
16
-            return ret;
17
-        }
18
-        Iterator<E> it = set.iterator();
19
-        while (it.hasNext()) {
20
-            Set<E> tmp = (Set)new ComparableTreeSet(set);   //create a copy of current set
21
-            tmp.remove(it.next());              //remove current element from copy set
22
-            ret.add(tmp);                       //add the remaining set to result
23
-            ret.addAll(findPowerSet(tmp));      //recursively find subsets of copy set
24
-        }
25
-        return ret;
26
-    }
27
-
28
-    public static void main(String[] args) {
29
-        Set<Character> set = (Set)new ComparableTreeSet<Character>();
30
-        set.add('x');
31
-        set.add('y');
32
-        set.add('z');
33
-        set.add('t');
34
-        System.out.println("Input set");
35
-        printSet(set);
36
-        System.out.println("\nsub sets");
37
-        findPowerSet(set).stream().forEach(MyTestClass::printSet);
38
-    }
39
-
40
-
41
-    public static <E> void printSet(E... val) {
42
-        printSet(new TreeSet<>(Arrays.asList(val)));
43
-    }
44
-
45
-    public static <E> void printSet(Set<E> set) {
46
-        StringBuilder sb = new StringBuilder(set.size() == 0 ? "{}\n" : "{");
47
-        Iterator<E> it = set.iterator();
48
-        while (it.hasNext()) {
49
-            sb.append(it.next().toString())
50
-                    .append(it.hasNext() ? ", " : "}\n");
51
-        }
52
-        System.out.print(sb.toString());
53
-    }
54
-}