Simran Bhutani 6 gadus atpakaļ
vecāks
revīzija
b307ffd42b

+ 20
- 4
src/main/java/rocks/zipcode/io/quiz4/collections/ComparableTreeSet.java Parādīt failu

@@ -1,17 +1,33 @@
1 1
 package rocks.zipcode.io.quiz4.collections;
2 2
 
3
+import java.util.Arrays;
4
+import java.util.TreeSet;
5
+
3 6
 /**
4 7
  * @author leon on 11/12/2018.
5 8
  */
6
-public class ComparableTreeSet<_> {
7
-    public ComparableTreeSet(_... arr) {
9
+public class ComparableTreeSet<T> extends TreeSet implements Comparable {
10
+
11
+
12
+    public ComparableTreeSet(T... arr) {
13
+        for(T elem: arr)
14
+
15
+        this.add(elem);
8 16
     }
9 17
 
10 18
 
11 19
     public ComparableTreeSet() {
12 20
     }
13 21
 
14
-    public Integer compareTo(ComparableTreeSet<_> o) {
15
-        return null;
22
+
23
+    @Override
24
+    public int compareTo(Object o) {
25
+
26
+        String st1=this.toString();
27
+        String st2= o.toString();
28
+
29
+        return st1.compareTo(st2);
16 30
     }
31
+
32
+
17 33
 }

+ 37
- 5
src/main/java/rocks/zipcode/io/quiz4/collections/SimpleStringGroup.java Parādīt failu

@@ -1,31 +1,63 @@
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
+    List<String> list= new ArrayList<>();
15
+
7 16
     public SimpleStringGroup() {
8
-        throw new UnsupportedOperationException("Method not yet implemented");
17
+      //  throw new UnsupportedOperationException("Method not yet implemented");
9 18
     }
10 19
 
11 20
     public Integer count() {
12
-        return null;
21
+        return list.size();
13 22
     }
14 23
 
15 24
     public void insert(String string) {
25
+        list.add(string);
16 26
     }
17 27
 
18 28
     public Boolean has(String valueToInsert) {
19
-        return null;
29
+
30
+        return list.contains(valueToInsert);
20 31
     }
21 32
 
22 33
     public String fetch(int indexOfValue) {
23
-        return null;
34
+
35
+
36
+        return list.get(indexOfValue);
24 37
     }
25 38
 
26 39
     public void delete(String valueToInsert) {
40
+        list.remove(valueToInsert);
27 41
     }
28 42
 
29 43
     public void clear() {
44
+
45
+        list.clear();
46
+    }
47
+
48
+    @Override
49
+    public Iterator iterator() {
50
+
51
+        return null;
52
+    }
53
+
54
+    @Override
55
+    public void forEach(Consumer action) {
56
+
57
+    }
58
+
59
+    @Override
60
+    public Spliterator spliterator() {
61
+        return null;
30 62
     }
31 63
 }

+ 16
- 3
src/main/java/rocks/zipcode/io/quiz4/collections/ZipCodeWilmington.java Parādīt failu

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

+ 27
- 10
src/main/java/rocks/zipcode/io/quiz4/fundamentals/PalindromeEvaluator.java Parādīt failu

@@ -1,28 +1,45 @@
1 1
 package rocks.zipcode.io.quiz4.fundamentals;
2 2
 
3
+import java.util.Collections;
4
+import java.util.HashSet;
5
+import java.util.Set;
6
+
3 7
 /**
4 8
  * @author leon on 18/12/2018.
5 9
  */
6 10
 public class PalindromeEvaluator {
7 11
     public static String[] getAllPalindromes(String string) {
8
-        return null;
12
+
13
+        Set<CharSequence> out = new HashSet<CharSequence>();
14
+        int length = string.length();
15
+        for (int i = 1; i <= length; i++) {
16
+            for (int j = i - 1, k = i; j >= 0 && k < length; j--, k++) {
17
+                if (string.charAt(j) == string.charAt(k)) {
18
+                    out.add(string.subSequence(j, k + 1));
19
+                } else {
20
+                    break;
21
+                }
22
+            }
23
+        }
24
+        return out.toArray(new String[0]);
25
+
26
+
9 27
     }
10 28
 
29
+
30
+
11 31
     public static Boolean isPalindrome(String string) {
12 32
 
13
-        char[] newString= new char[string.length()];
14
-        int i1 = 0;
15
-        int i2 = newString.length - 1;
16
-        while (i2 > i1) {
17
-            if (newString[i1] != newString[i2]) {
33
+
34
+        for(int i=0;i<=string.length()/2;i++)
35
+            if(string.charAt(i)!=string.charAt(string.length()-1-i))
18 36
                 return false;
19
-            }
20
-            ++i1;
21
-            --i2;
22
-        }
23 37
         return true;
24 38
 
25 39
 
40
+
41
+
42
+
26 43
     }
27 44
 
28 45
     public static String reverseString(String string) {

+ 31
- 2
src/main/java/rocks/zipcode/io/quiz4/fundamentals/StringEvaluator.java Parādīt failu

@@ -1,18 +1,47 @@
1 1
 package rocks.zipcode.io.quiz4.fundamentals;
2 2
 
3
+import com.sun.xml.internal.ws.util.StringUtils;
4
+
3 5
 /**
4 6
  * @author leon on 11/12/2018.
5 7
  */
6 8
 public class StringEvaluator {
7 9
     public static String[] getAllPrefixes(String string) {
10
+
8 11
         return null;
9 12
     }
10 13
 
11 14
     public static String[] getCommonPrefixes(String string1, String string2) {
12
-        return null;
15
+
16
+          return null;
17
+
13 18
     }
14 19
 
15 20
     public static String getLargestCommonPrefix(String string1, String string2) {
16
-        return null;
21
+
22
+
23
+        int minStrLen = string1.length();
24
+        if ( minStrLen > string2.length()){
25
+            minStrLen = string2.length();
26
+        }
27
+
28
+        StringBuilder output = new StringBuilder();
29
+        for(int i=0; i<minStrLen; i++){
30
+            if ( string1.charAt(i) ==  string2.charAt(i)){
31
+                output.append(string1.charAt(i));
32
+            }else{
33
+                break;
34
+            }
35
+        }
36
+       return (output.toString());
37
+
38
+//        int minLength = Math.min(string1.length(), string2.length());
39
+//        for (int i = 0; i < minLength; i++) {
40
+//            if (string1.charAt(i) != string2.charAt(i)) {
41
+//                return string1.substring(0, i);
42
+//            }
43
+//        }
44
+//        return string1.substring(0, minLength);
45
+
17 46
     }
18 47
 }

+ 2
- 2
src/main/java/rocks/zipcode/io/quiz4/generics/GenericUtils.java Parādīt failu

@@ -8,11 +8,11 @@ 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 <T extends Comparable> Iterable<Iterable<T>> powerSet(Set<T> originalSet) {
12 12
         return null;
13 13
     }
14 14
 
15
-    public static <_ extends Comparable> Iterable<Iterable<_>> powerSet(_... originalSet) {
15
+    public static <T extends Comparable> Iterable<Iterable<T>> powerSet(T... originalSet) {
16 16
         return powerSet(new TreeSet<>(Arrays.asList(originalSet)));
17 17
     }
18 18
 }

+ 28
- 11
src/main/java/rocks/zipcode/io/quiz4/generics/Group.java Parādīt failu

@@ -1,37 +1,54 @@
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<T>  implements GroupInterface<T> {
11
+
12
+    List<T> list= new ArrayList<>();
9 13
     public Group() {
10
-        throw new UnsupportedOperationException("Method not yet implemented");
14
+       // throw new UnsupportedOperationException("Method not yet implemented");
11 15
     }
12 16
 
13 17
     public Integer count() {
14
-        return null;
18
+        return list.size();
15 19
     }
16 20
 
17
-    public void insert(_ value) {
21
+    public void insert(T value) {
22
+        list.add(value);
18 23
     }
19 24
 
20
-    public Boolean has(_ value) {
21
-        return null;
25
+    public Boolean has(T value) {
26
+        return list.contains(value);
22 27
     }
23 28
 
24
-    public _ fetch(int indexOfValue) {
25
-        return null;
29
+    public T fetch(int indexOfValue) {
30
+        return list.get(indexOfValue);
26 31
     }
27 32
 
28
-    public void delete(_ value) {
33
+    public void delete(T value) {
34
+        list.remove(value);
29 35
     }
30 36
 
31 37
     public void clear() {
38
+        list.clear();
39
+    }
40
+
41
+    public Iterator<T> iterator() {
42
+
43
+        Iterator<T> iterator= list.iterator();
44
+        while (iterator.hasNext()) {
45
+            iterator.next();
46
+        }
47
+        return iterator;
32 48
     }
33 49
 
34
-    public Iterator<_> iterator() {
35
-        return null;
50
+    @Override
51
+    public String toString() {
52
+        return String.valueOf(list);
36 53
     }
37 54
 }

+ 5
- 5
src/main/java/rocks/zipcode/io/quiz4/generics/GroupInterface.java Parādīt failu

@@ -3,11 +3,11 @@ package rocks.zipcode.io.quiz4.generics;
3 3
 /**
4 4
  * @author leon on 18/12/2018.
5 5
  */
6
-public interface GroupInterface<_> extends Iterable<_> {
6
+public interface GroupInterface<T> extends Iterable<T> {
7 7
     Integer count();
8
-    Boolean has(_ valueToInsert);
9
-    _ fetch(int indexOfValue);
10
-    void insert(_ string);
11
-    void delete(_ valueToInsert);
8
+    Boolean has(T valueToInsert);
9
+    T fetch(int indexOfValue);
10
+    void insert(T string);
11
+    void delete(T valueToInsert);
12 12
     void clear();
13 13
 }

+ 31
- 5
src/main/java/rocks/zipcode/io/quiz4/generics/MyStack.java Parādīt failu

@@ -1,25 +1,51 @@
1 1
 package rocks.zipcode.io.quiz4.generics;
2 2
 
3
+import java.util.*;
4
+import java.util.function.Consumer;
5
+
3 6
 /**
4 7
  * @author leon on 11/12/2018.
5 8
  */
6
-public class MyStack<SomeType> {
9
+public class MyStack<SomeType> implements Iterable<SomeType> {
10
+
11
+    Stack stack = new Stack();
12
+
7 13
     public MyStack() {
8
-        throw new UnsupportedOperationException("Method not yet implemented");
14
+       // throw new UnsupportedOperationException("Method not yet implemented");
9 15
     }
10 16
 
11 17
     public Boolean isEmpty() {
12
-        return null;
18
+
19
+        return stack.isEmpty();
13 20
     }
14 21
 
15 22
     public void push(SomeType i) {
23
+        stack.addElement(i);
16 24
     }
17 25
 
18
-    public SomeType peek() {
19
-        throw new UnsupportedOperationException("Method not yet implemented");
26
+    public SomeType peek() throws EmptyStackException{
27
+
28
+       // throw new UnsupportedOperationException("Method not yet implemented");
29
+        return (SomeType) stack.peek();
20 30
     }
21 31
 
22 32
     public SomeType pop() {
33
+      return (SomeType) stack.pop();
34
+
35
+    }
36
+
37
+    @Override
38
+    public Iterator<SomeType> iterator() {
39
+        return null;
40
+    }
41
+
42
+    @Override
43
+    public void forEach(Consumer<? super SomeType> action) {
44
+
45
+    }
46
+
47
+    @Override
48
+    public Spliterator<SomeType> spliterator() {
23 49
         return null;
24 50
     }
25 51
 }

+ 18
- 5
src/main/java/rocks/zipcode/io/quiz4/generics/SortedGroup.java Parādīt failu

@@ -1,18 +1,31 @@
1 1
 package rocks.zipcode.io.quiz4.generics;
2 2
 
3
+import java.util.*;
4
+
3 5
 /**
4 6
  * @author leon on 18/12/2018.
5 7
  */
6
-public class SortedGroup<_> extends Group<_> {
8
+public class SortedGroup<T> extends Group<T> {
9
+
10
+
7 11
     @Override
8
-    public void insert(_ value) {
12
+    public void insert(T value) {
13
+
14
+        list.add(value);
15
+        Collections.sort(list,null);
9 16
     }
10 17
 
11 18
     @Override
12
-    public void delete(_ value) {
19
+    public void delete(T value) {
20
+       list.remove(value);
21
+        Collections.sort(list,null);
13 22
     }
14 23
 
15
-    public Integer indexOf(_ firstValue) {
16
-        return null;
24
+    public Integer indexOf(T firstValue) {
25
+
26
+        Collections.sort(list,null);
27
+       return list.indexOf(firstValue);
28
+
29
+
17 30
     }
18 31
 }

+ 7
- 2
src/main/java/rocks/zipcode/io/quiz4/objectorientation/PalindromeObject.java Parādīt failu

@@ -1,10 +1,15 @@
1 1
 package rocks.zipcode.io.quiz4.objectorientation;
2 2
 
3
+import rocks.zipcode.io.quiz4.fundamentals.PalindromeEvaluator;
4
+
3 5
 /**
4 6
  * @author leon on 18/12/2018.
5 7
  */
6 8
 public class PalindromeObject {
9
+    private final String input;
10
+
7 11
     public PalindromeObject(String input) {
12
+        this.input= input;
8 13
     }
9 14
 
10 15
     public String[] getAllPalindromes(){
@@ -12,10 +17,10 @@ public class PalindromeObject {
12 17
     }
13 18
 
14 19
     public Boolean isPalindrome(){
15
-        return null;
20
+        return PalindromeEvaluator.isPalindrome(input);
16 21
     }
17 22
 
18 23
     public String reverseString(){
19
-        return null;
24
+        return PalindromeEvaluator.reverseString(input);
20 25
     }
21 26
 }

+ 13
- 2
src/main/java/rocks/zipcode/io/quiz4/objectorientation/StringAssembler.java Parādīt failu

@@ -1,17 +1,28 @@
1 1
 package rocks.zipcode.io.quiz4.objectorientation;
2 2
 
3
+import java.util.ArrayList;
4
+import java.util.List;
5
+
3 6
 /**
4 7
  * @author leon on 11/12/2018.
5 8
  */
6 9
 public class StringAssembler {
10
+
11
+    List<String> list= new ArrayList<>();
12
+    private  String delimeter;
13
+
7 14
     public StringAssembler(Character delimeter) {
15
+        this.delimeter= ""+ delimeter;
8 16
     }
9 17
 
10 18
     public StringAssembler append(String str) {
11
-        return null;
19
+        list.add(str);
20
+        return this;
21
+
12 22
     }
13 23
 
14 24
     public String assemble() {
15
-        return null;
25
+
26
+        return String.join(delimeter, list);
16 27
     }
17 28
 }

+ 10
- 4
src/main/java/rocks/zipcode/io/quiz4/objectorientation/StringEvaluatorObject.java Parādīt failu

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

+ 9
- 2
src/main/java/rocks/zipcode/io/quiz4/objectorientation/Student.java Parādīt failu

@@ -4,17 +4,24 @@ package rocks.zipcode.io.quiz4.objectorientation;
4 4
  * @author leon on 11/12/2018.
5 5
  */
6 6
 public class Student {
7
+    private  Integer id;
8
+    private Double totalStudyTime=0.0;
9
+
7 10
     public Student() {
8
-        this(null);
11
+
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
+        totalStudyTime += amountOfHours;
20
+
15 21
     }
16 22
 
17 23
     public Double getTotalStudyTime() {
18
-        return null;
24
+
25
+        return totalStudyTime;
19 26
     }
20 27
 }