SteakStyles
SteakStyles

Reputation: 87

Homemade Stack Equals method

For my data structures class, we have to create our own Stack data type and the implementation for it as a project. The problem I'm running into is when the professor asked us to implement an equals(Object object) method. Heres what I have so far...

package stack;
import list.*;

public class Stack <E>
implements StackADT<E>//the interface
{
    List <E> values;

    public Stack()
    {
        values = new ArrayList<E>();
    }

    public E push(E value)
    {
        values.add(value);
        return value;
    }

    public E pop()
    {
        return values.remove(values.size()-1);
    }

    public E peek()
    {
        return values.get(values.size()-1);
    }

    /** @return true only if this Stack is empty */
    public boolean isEmpty()
    {
        return (values.size()==0);
    }

    /** Clear this stack, to make it an empty stack */

    public void clear()
    {
        for (int i = 0; i < values.size()-1; i++)
        {
            pop();
        }
    }

    public String toString()
    {
        String result = "[";
        for (int i = 0; i<values.size(); i++)
        { 
            if (i == values.size()-1)
            {
                result = result + values.get(i); 
            }
            else
            {
                result = result + values.get(i) +",";
            }
        }

        result = result + "]";
        return result;
    }

    public boolean equals (Object object)
    {

        if (!(object instanceof StackADT))
        {
            return false;
        }
        StackADT <E> otherStack = new Stack<E>(); 
        for(Object o: object)//heres where i run into trouble
        {
            otherStack.push(o);
        }
        for (int i=0;i<values.size()-1;i++)
        {
            if (!(values.get(i).equals(otherStack.pop())))
            {
                return false;
            }
        }
        return true; 
    }

}

Our Stack is pretty much an ArrayList which we also built in our class. the problem is, I cant add the Object object into a stack because its not something thats iteratable(?able to be iterated over). Is there a better way to do this? I would think a get() would work, since the Stack I create is an ArrayList, but whenever I use get() on otherStack, it can't find the method. I had a temporary solution when I tried casting object as a stack(I hope im using the right terminology). It looked something like this

Stack otherStack = (Stack) object;
            for (int i=0;i<values.size()-1;i++)
        {
            if (!(values.get(i).equals(otherStack.pop())))
            {
                return false;
            }
        }
        return true; 
    }

this seemed to work, but when pop() was called on otherStack, the values in the original list(the one that becomes otherStack) that was passed into the equals() method we're also popped from the original list, leading to an incorrect result. Is there a better way to do this without adding in any other methods? I'm trying to stick as close to the formula set up by my professor as possible, so I dont want to add any extra fields or methods. any and all help is appreciated

Upvotes: 1

Views: 507

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

An equals method is not supposed to create anything, not even a temporary object. Rather than creating a new otherStack, cast the object that you have checked to be StackADT, like this:

// This should be the first line of any equals() implementation:
if (object == this) {
    return true;
}
// You've got this part right: you need to check the other object's type
if (!(object instanceof StackADT)) {
    return false;
}
// Now that you know the type, cast the other object to StackADT<E>
StackADT<E> otherStack = (StackADT<E>)object;
// The next step is to check the sizes:
if (values.size() != otherStack.values.size()) {
    return false;
}
// Finally, go through the individual elements in a loop

In the loop that follows, do not pop the other stack. Do not do anything that can modify it. Simply go through the underlying storage (i.e. values), and check elements one by one.

Don't forget to override hashCode as well: you need to do it every time when you override equals for the object to fulfill the contract specified by java.lang.Object.

Upvotes: 3

Related Questions