Commit 7c2016d34e11e55ed6165e48839612f0a6c99ea3

Authored by Adrian Chirita
1 parent 8662c8e4

MyList @override metoder er nå implementert.

Showing 2 changed files with 126 additions and 96 deletions   Show diff stats
src/oblig_2/MyList.java
1 1 package oblig_2;
2   -// MyList from 11th edition
  2 +
3 3  
4 4 import java.util.Collection;
5 5  
6 6 public interface MyList<E> extends Collection<E> {
7   - /** Add a new element at the specified index in this list */
8   - public void add(int index, E e);
9   -
10   - /** Return the element from this list at the specified index */
11   - public E get(int index);
12   -
13   - /** Return the index of the first matching element in this list.
14   - * Return -1 if no match. */
15   - public int indexOf(Object e);
16 7  
17   - /** Return the index of the last matching element in this list
18   - * Return -1 if no match. */
19   - public int lastIndexOf(E e);
  8 + public void add(int index, E e);
  9 +
  10 + public E get(int index);
20 11  
21   - /** Remove the element at the specified position in this list
22   - * Shift any subsequent elements to the left.
23   - * Return the element that was removed from the list. */
24   - public E remove(int index);
  12 + public int indexOf(Object e);
  13 +
  14 + public int lastIndexOf(E e);
25 15  
26   - /** Replace the element at the specified position in this list
27   - * with the specified element and returns the new set. */
28   - public E set(int index, E e);
  16 + public E remove(int index);
29 17  
30   - @Override /** Add a new element at the end of this list */
31   - public default boolean add(E e) {
32   - add(size(), e);
33   - return true;
34   - }
35   -
36   - @Override /** Return true if this list contains no elements */
37   - public default boolean isEmpty() {
38   - return size() == 0;
39   - }
40   -
41   - @Override /** Remove the first occurrence of the element e
42   - * from this list. Shift any subsequent elements to the left.
43   - * Return true if the element is removed. */
44   - public default boolean remove(Object e) {
45   - if (indexOf(e) >= 0) {
46   - remove(indexOf(e));
47   - return true;
48   - }
49   - else
50   - return false;
51   - }
52   -
53   - @Override
54   - public default boolean containsAll(Collection<?> c) {
55   - // Left as an exercise
56   - return true;
57   - }
58   -
59   - @Override
60   - public default boolean addAll(Collection<? extends E> c) {
61   - // Left as an exercise
62   - return true;
63   - }
64   -
65   - @Override
66   - public default boolean removeAll(Collection<?> c) {
67   - // Left as an exercise
68   - return true;
69   - }
70   -
71   - @Override
72   - public default boolean retainAll(Collection<?> c) {
73   - // Left as an exercise
74   - return true;
75   - }
76   -
77   - @Override
78   - public default Object[] toArray() {
79   - // Left as an exercise
80   - return null;
81   - }
82   -
83   - @Override
84   - public default <T> T[] toArray(T[] array) {
85   - // Left as an exercise
86   - return null;
87   - }
88   -}
  18 + public E set(int index, E e);
  19 +
  20 + public default boolean add(E e) {
  21 + add(size(), e);
  22 + return true;
  23 + }
  24 + @Override
  25 + public default boolean isEmpty() {
  26 + return size() == 0;
  27 + }
  28 + @Override
  29 + public default boolean remove(Object e) {
  30 + if (indexOf(e) >= 0) {
  31 + remove(indexOf(e));
  32 + return true;
  33 + }
  34 + else
  35 + return false;
  36 + }
  37 + @Override
  38 + public default boolean containsAll(Collection<?> c) {
  39 + for(Object o : c)
  40 + if (!this.contains(o)) return false;
  41 + return true;
  42 + }
  43 + @Override
  44 + public default boolean addAll(Collection<? extends E> c) {
  45 + if (c == null) throw new NullPointerException("Input collection is null");
  46 + int aChangeWasMade = size();
  47 + for(E e: c) {
  48 + try {
  49 + this.add(e);
  50 + }
  51 + catch(ClassCastException ex) {
  52 + ex.printStackTrace();
  53 + }
  54 + }
  55 + return (aChangeWasMade == size()? false:true);
  56 + }
  57 + @Override
  58 + public default boolean removeAll(Collection<?> c) {
  59 + int aChangeWasMade = size();
  60 + for (Object o: c) {
  61 + if (this.contains(o)) {
  62 + remove(o);
  63 + }
  64 + }
  65 + return (aChangeWasMade == size() ? false:true);
  66 + }
  67 + @Override
  68 + public default boolean retainAll(Collection<?> c) {
  69 + if (c == null) throw new NullPointerException("Input collection is null");
  70 + int aChangeWasMade = size();
  71 + for(Object o: this)
  72 + if (!c.contains(o)) {
  73 + remove(o);
  74 + }
  75 + return (aChangeWasMade == size() ? false:true);
  76 + }
  77 + @Override
  78 + public default Object[] toArray() {
  79 + Object[] result = new Object[size()];
  80 + int index = 0;
  81 + java.util.Iterator<E> myListIterator = this.iterator();
  82 + while(myListIterator.hasNext()) {
  83 + result[index++] = myListIterator.next();
  84 + }
  85 + return result;
  86 + }
  87 + @Override
  88 + public default <T> T[] toArray(T[] array) {
  89 + if (array == null) throw new NullPointerException();
  90 +
  91 + if (array.length - size() >= 0) {
  92 + int index = 0;
  93 + java.util.Iterator<E> myListIterator = this.iterator();
  94 + while (myListIterator.hasNext()) {
  95 + try {
  96 + array[index++] = (T)myListIterator.next();
  97 + }
  98 + catch(ClassCastException e) {
  99 + throw new ArrayStoreException();
  100 + }
  101 + }
  102 + while (index < array.length)
  103 + array[index++] = null;
  104 + return array;
  105 + }
  106 + else {
  107 + T[] newArray = (T[])(new Object[size()]);
  108 + int index = 0;
  109 + java.util.Iterator<E> myListIterator = this.iterator();
  110 + while(myListIterator.hasNext()) {
  111 + try {
  112 + newArray[index++] = (T)myListIterator.next();
  113 + }
  114 + catch(ClassCastException e) {
  115 + throw new ArrayStoreException();
  116 + }
  117 + }
  118 + return newArray;
  119 + }
  120 + }
  121 +}
89 122 \ No newline at end of file
... ...
src/test/TwoWayLinkedListTest.java
... ... @@ -49,23 +49,23 @@ public class TwoWayLinkedListTest {
49 49 @Test
50 50 public void add() {
51 51 int n = 5;
52   - for (int i=0;i<=n;i++) data.add(i,"test " + i);
53   - assertEquals(n+1, data.size());
54   - assertEquals("test "+n, data.getLast());
  52 + for (int i = 1; i <= n; i++) data.addLast("test"+i);
  53 + assertEquals(n, data.size());
  54 + assertEquals("test"+n, data.getLast());
55 55 }
56 56 @Test
57 57 public void removeFirst() {
58   - data.add("Kitkat");
59   - data.add("Pie");
60   - data.add("Oreo");
  58 + data.addLast("Kitkat");
  59 + data.addLast("Pie");
  60 + data.addLast("Oreo");
61 61 assertEquals("Kitkat",data.removeFirst());
62 62 assertNotEquals("Kitkat",data.getFirst());
63 63 }
64 64 @Test
65 65 public void removeLast() {
66   - data.add("Kitkat");
67   - data.add("Pie");
68   - data.add("Oreo");
  66 + data.addLast("Kitkat");
  67 + data.addLast("Pie");
  68 + data.addLast("Oreo");
69 69 assertEquals("Oreo",data.removeLast());
70 70 assertNotEquals("Oreo",data.getLast());
71 71 }
... ... @@ -88,16 +88,13 @@ public class TwoWayLinkedListTest {
88 88 }
89 89 @Test
90 90 public void clear() {
91   - data.add(0,"Vitkat");
92   - data.add(1,"Kitkat");
93   - data.add(2,"Pie");
94   - data.add(3,"Oreo");
  91 + data.add("Vitkat");
  92 + data.add("Kitkat");
  93 + data.add("Pie");
  94 + data.add("Oreo");
95 95 assertEquals("Pie",data.get(2));
96 96 data.clear();
97   - assertEquals(null,data.get(0));
98   - assertNotEquals("Kitkat",data.get(1));
99   - assertEquals(null,data.get(2));
100   - assertEquals(null,data.get(3));
  97 + assertEquals(null,data.getFirst());
101 98 }
102 99 @Test
103 100 public void clearList() {
... ...