0

I am working on implementing a custom Deque interface. Everything is working except the equals methods. Whenever I submit my code to an auto-checker, it fails one of the tests for equality and outputs: "ArrayDeques and LinkedListDeques with the same elements should be equal." I cannot figure out what I have done wrong.

These are the equals methods within my LinkedListDeque and ArrayDeque classes, respectively (part of a package that includes my custom deque interface).

    /** Returns whether the parameter o is equal to the Deque.
     * o is considered equal if it is a Deque and if it contains
     * the same contents (as governed by the generic T's equals method) in the same order. */
 
@Override
    public boolean equals(Object o) {
        if (o == null) {
            return false;
        }
        if (o == this) {
            return true;
        }
        if (!(o instanceof Deque)) {
            return false;
        }

        if (o instanceof LinkedListDeque) {
            LinkedListDeque<T> other = (LinkedListDeque<T>) o;
            if (this.size() != other.size()) {
                return false;
            }
            Iterator<T> dequeIt = this.iterator();
            Iterator<T> otherIt = other.iterator();
            while (dequeIt.hasNext()) {
                if (!dequeIt.next().equals(otherIt.next())) {
                    return false;
                }
            }
            return true;
        } else if (o instanceof ArrayDeque) {
            ArrayDeque<T> other = (ArrayDeque<T>) o;
            if (this.size() != other.size()) {
                return false;
            }
            Iterator<T> thisIt = iterator();
            Iterator<T> otherIt = other.iterator();
            while (thisIt.hasNext()) {
                if (!thisIt.next().equals(otherIt.next())) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }
}

   @Override
    public boolean equals(Object o) {
        if (o == null) {
            return false;
        }
        if (this == o) {
            return true;
        }
        if (!(o instanceof Deque)) {
            return false;
        }
        if (o instanceof ArrayDeque) {
            ArrayDeque<T> other = (ArrayDeque<T>) o;
            if (other.size() != this.size) {
                return false;
            }
            Iterator<T> thisIt = iterator();
            Iterator<T> otherIterator = other.iterator();
            while (thisIt.hasNext()) {
                if (!thisIt.next().equals(otherIterator.next())) {
                    return false;
                }
            }
            return true;
        } else if (o instanceof LinkedListDeque) {
            LinkedListDeque<T> other = (LinkedListDeque<T>) o;
            if (other.size() != this.size) {
                return false;
            }
            Iterator<T> thisIt = iterator();
            Iterator<T> otherIterator = other.iterator();
            while (thisIt.hasNext()) {
                if (!thisIt.next().equals(otherIterator.next())) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }


It passes my unit tests: 
 @Test
    public void randomizedInsertionAndDeletion(){
        ArrayDeque<Integer> deque = new ArrayDeque<>();
        LinkedListDeque<Integer> deque2 = new LinkedListDeque<>();
        int N = 5000;
        for (int i =0; i<N; i++) {
            int operationNumber = StdRandom.uniform(0, 4);
            int randValue = StdRandom.uniform(0, 100);
            if (operationNumber == 0) {
                deque.addFirst(randValue);
                deque2.addFirst(randValue);
                assertEquals((Integer) randValue, deque.removeFirst());
                assertEquals((Integer) randValue, deque2.removeFirst());
            } else if (operationNumber == 1) {
                deque.addLast(randValue);
                deque2.addLast(randValue);
                assertEquals((Integer) randValue, deque.removeLast());
                assertEquals((Integer) randValue, deque2.removeLast());
            } else if (operationNumber == 2) {
                assertEquals(deque.get(0), deque.removeFirst());
                assertEquals(deque2.get(0), deque.removeFirst());
            } else if (operationNumber == 3) {
                assertEquals(deque.removeLast(), deque.get(deque.size()));
            } else if (operationNumber == 4) {
                boolean actual1 = deque.equals(deque2);
                boolean actual2 = deque2.equals(deque);
                assertEquals(true, actual1);
                assertEquals(true, actual2);
            }
        }
    }

    @Test
    public void equals() {
        ArrayDeque<Boolean> deque = new ArrayDeque<>();
        deque.addLast(true);
        deque.addFirst(false);
        deque.addFirst(false);
        deque.addFirst(true);

        LinkedListDeque<Boolean> deque2 = new LinkedListDeque<>();
        deque2.addLast(true);
        deque2.addFirst(false);
        deque2.addFirst(false);
        deque2.addFirst(false);

        assertEquals(false, deque2.equals(deque));
        assertEquals(false, deque.equals(deque2));
    }

Thanks for any guidance.

rdk7
  • 3
  • 2

0 Answers0