Chaitali Patel 6 years ago
parent
commit
c6b5341d9c

+ 17
- 1
src/main/java/rocks/zipcode/io/quiz4/collections/ComparableTreeSet.java View File

@@ -4,14 +4,30 @@ package rocks.zipcode.io.quiz4.collections;
4 4
  * @author leon on 11/12/2018.
5 5
  */
6 6
 public class ComparableTreeSet<T> {
7
+    private T value;
8
+
7 9
     public ComparableTreeSet(T... arr) {
8 10
     }
9 11
 
12
+    public ComparableTreeSet(T value) {
13
+        this.value = value;
14
+    }
10 15
 
11 16
     public ComparableTreeSet() {
12 17
     }
13 18
 
14 19
     public Integer compareTo(ComparableTreeSet<T> o) {
15
-        return null;
20
+        return getValue().compareTo(o.getValue());
21
+    }
22
+
23
+    private ComparableTreeSet<T> getValue() {
24
+        return (ComparableTreeSet<T>) value;
25
+    }
26
+
27
+    @Override
28
+    public String toString() {
29
+        return "ComparableTreeSet{" +
30
+                "value=" + value +
31
+                '}';
16 32
     }
17 33
 }

+ 33
- 5
src/main/java/rocks/zipcode/io/quiz4/collections/SimpleStringGroup.java View File

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

+ 6
- 3
src/main/java/rocks/zipcode/io/quiz4/collections/ZipCodeWilmington.java View File

@@ -7,10 +7,13 @@ import java.util.Map;
7 7
 /**
8 8
  * @author leon on 11/12/2018.
9 9
  */
10
-public class ZipCodeWilmington {
10
+public class ZipCodeWilmington extends Student{
11
+    private double numberOfHours;
12
+
11 13
     Map<Student, Double> stud = new HashMap<>();
14
+
12 15
     public void enroll(Student student) {
13
-        stud.put(student,1.0);
16
+        stud.put(student,0.0);
14 17
     }
15 18
 
16 19
     public Boolean isEnrolled(Student student) {
@@ -18,7 +21,7 @@ public class ZipCodeWilmington {
18 21
     }
19 22
 
20 23
     public void lecture(double numberOfHours) {
21
-        stud.get(numberOfHours);
24
+            learn(numberOfHours);
22 25
     }
23 26
 
24 27
     public Map<Student, Double> getStudyMap() {

+ 1
- 4
src/main/java/rocks/zipcode/io/quiz4/fundamentals/PalindromeEvaluator.java View File

@@ -42,7 +42,7 @@ public class PalindromeEvaluator {
42 42
         string = string.substring(1, string.length() - 1);
43 43
         // Put all obtained palindromes in a hash map to
44 44
         // find only distinct palindromess
45
-        m.put(string.substring(0, 1), 1);
45
+       // m.put(string.substring(0, 1), 1);
46 46
         for (int i = 1; i < n; i++) {
47 47
             for (int j = 0; j <= 1; j++)
48 48
                 for (int rp = R[j][i]; rp > 0; rp--)
@@ -52,9 +52,6 @@ public class PalindromeEvaluator {
52 52
         }
53 53
         // printing all distinct palindromes from
54 54
         // hash map
55
-//        for (Map.Entry<String, Integer> ii : m.entrySet()) {
56
-//            System.out.println(ii.getKey());
57
-//        }
58 55
        return new String[]{m.toString()};
59 56
 
60 57
     }

+ 47
- 21
src/main/java/rocks/zipcode/io/quiz4/fundamentals/StringEvaluator.java View File

@@ -1,35 +1,61 @@
1 1
 package rocks.zipcode.io.quiz4.fundamentals;
2
+
3
+import java.util.Arrays;
4
+import java.util.List;
5
+import java.util.stream.Collectors;
6
+import java.lang.String;
7
+
2 8
 /**
3 9
  * @author leon on 11/12/2018.
4 10
  */
5 11
 public class StringEvaluator {
6 12
     public static String[] getAllPrefixes(String string) {
7
-        String[] output = new String[string.length()];
8
-        for (int index = 0; index < string.length(); index++) {
9
-            //output[index] = "" + string + string[index];
10
-            output[index] = "" + string ;
11
-        }
12
-        return output;
13
+        List<String> numbers = Arrays.asList(string);
14
+
15
+        String joinedString =  numbers
16
+                .stream()
17
+                .collect(Collectors.joining(", ","",""));
18
+        return new String[]{joinedString};
13 19
 
14 20
     }
15 21
 
16
-    public static String[] getCommonPrefixes(String string1, String string2) {
17
-        String[] str = new String[string1.length() + string2.length()];
18
-        int i = 0;
19
-        for(String s: str)
20
-        if(string1.equals(string2)) {
21
-            str[i] += string1;
22
-        }
23
-        return str;
22
+   public static String[] getCommonPrefixes(String string1, String string2) {
23
+//        int i = 0;
24
+//        if (string1 == null || string2 == null || string1.length() == 0 || string2.length() == 0) {
25
+//            return new String[]{""};
26
+//        }
27
+//        while (i < string1.length() && i < string2.length() && string1.charAt(i) == string2.charAt(i)) {
28
+//            i++;
29
+//        }
30
+//        return new String[]{string1.substring(0, i)};
31
+
32
+//       return getCommonPrefixes( string1, string2);
33
+
34
+
35
+       String result = "";
36
+       int n1 = string1.length(), n2 = string2.length();
37
+
38
+       // Compare str1 and str2
39
+       for (int i = 0, j = 0; i <= n1 - 1 && j <= n2 - 1; i++, j++) {
40
+           if (string1.charAt(i) != string2.charAt(j)) {
41
+               break;
42
+           }
43
+           result += string1.charAt(i);
44
+       }
45
+
46
+       return new String[]{(result)};
47
+
24 48
     }
25 49
 
26 50
     public static String getLargestCommonPrefix(String string1, String string2) {
27
-        int minLength = Math.min(string1.length(), string2.length());
28
-        for (int i = 0; i < minLength; i++) {
29
-            if (string1.charAt(i) != string2.charAt(i)) {
30
-                return string1.substring(0, i);
31
-            }
32
-        }
33
-        return string1.substring(0, minLength);
51
+//        if (strs.length == 0) return "";
52
+//        String prefix = strs[0];
53
+//        for (int i = 1; i < strs.length; i++)
54
+//            while (strs[i].indexOf(prefix) != 0) {
55
+//                prefix = prefix.substring(0, prefix.length() - 1);
56
+//                if (prefix.isEmpty()) return "";
57
+//            }
58
+//        return prefix;
59
+        return null;
34 60
     }
35 61
 }

+ 2
- 2
src/main/java/rocks/zipcode/io/quiz4/generics/GenericUtils.java View File

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

+ 26
- 17
src/main/java/rocks/zipcode/io/quiz4/generics/Group.java View File

@@ -1,56 +1,65 @@
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;
6
+import java.util.Spliterator;
7
+import java.util.function.Consumer;
4 8
 
5 9
 /**
6 10
  * @author leon on 18/12/2018.
7 11
  */
8
-public class Group<T> {
12
+public class Group<T> implements Iterable<T>{
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
-    //Set<T> set;
14
-    private Group<T> group ;
17
+    List<T> list = new ArrayList<>();
15 18
 
16
-    public Group(Group<T> group) {
17
-        this.group = group;
18
-    }
19 19
 
20 20
     public Integer count() {
21
-       return group.count();
21
+        return list.size();
22 22
     }
23 23
 
24 24
     public void insert(T value) {
25
-        group.insert(value);
25
+        list.add(value);
26 26
     }
27 27
 
28 28
     public Boolean has(T value) {
29
-        return group.has(value);
29
+        return list.contains(value);
30 30
 
31 31
     }
32 32
 
33 33
     public T fetch(int indexOfValue) {
34
-       // set.add(indexOfValue);
35
-        return null;
34
+        return list.get(indexOfValue);
36 35
     }
37 36
 
38 37
     public void delete(T value) {
39
-        //set.remove(value);
38
+        list.remove(value);
40 39
 
41 40
     }
42 41
 
43 42
     public void clear() {
43
+        list.clear();
44 44
     }
45 45
 
46
+    @Override
47
+    public String toString() {
48
+        return list.toString();
49
+    }
50
+
51
+    @Override
46 52
     public Iterator<T> iterator() {
47 53
         return null;
48 54
     }
49 55
 
50 56
     @Override
51
-    public String toString() {
52
-        return "Group{" +
53
-                "group=" + group +
54
-                '}';
57
+    public void forEach(Consumer<? super T> action) {
58
+
59
+    }
60
+
61
+    @Override
62
+    public Spliterator<T> spliterator() {
63
+        return null;
55 64
     }
56 65
 }

+ 5
- 5
src/main/java/rocks/zipcode/io/quiz4/generics/GroupInterface.java View File

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

+ 25
- 11
src/main/java/rocks/zipcode/io/quiz4/generics/MyStack.java View File

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

+ 10
- 5
src/main/java/rocks/zipcode/io/quiz4/generics/SortedGroup.java View File

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

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

@@ -22,10 +22,9 @@ public class PalindromeObject {
22 22
         int n = input.length();
23 23
         for (int i = 0; i < (n/2); ++i) {
24 24
             if (input.charAt(i) != input.charAt(n - i - 1)) {
25
-                return false;
25
+                return true;
26 26
             }
27 27
         }
28
-
29 28
         return true;
30 29
     }
31 30
 

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

@@ -5,19 +5,11 @@ package rocks.zipcode.io.quiz4.objectorientation;
5 5
  */
6 6
 public class StringAssembler {
7 7
     public StringAssembler(Character delimeter) {
8
+
8 9
     }
9 10
 
10 11
     public StringAssembler append(String str) {
11
-        StringBuilder sb = new StringBuilder();
12
-        sb.append("You");
13
-        sb.append(str);
14
-        sb.append(" to ");
15
-        sb.append(str);
16
-        sb.append(" \"");
17
-        sb.append(str );
18
-        sb.append('"');
19
-        String s = sb.toString();
20
-        return null;
12
+       return null;
21 13
     }
22 14
 
23 15
     public String assemble() {

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

@@ -5,7 +5,7 @@ package rocks.zipcode.io.quiz4.objectorientation;
5 5
  */
6 6
 public class Student {
7 7
     private  Integer id;
8
-    private Double totalStudyTime;
8
+    private Double totalStudyTime = 0.0;
9 9
 
10 10
     public Student() {
11 11
         this(null);