Kyle
Kyle

Reputation: 3

Method to check if both stacks are equal

Given the following class definition for ArrayStack:

Public class ArrayStack<T> implements Stack {

    T[] stack;
    int topIndex = -1;

Write a method equals(Stack other) in class ArrayStack that takes a Stack as parameter and returns true if both stacks are equal, and false otherwise.

    public boolean equals(Stack<T> other) {

Code for ArrayStack.java

    import java.util.Arrays;
    import java.util.EmptyStackException;

    public class ArrayStack<T> implements Stacks<T> {

      T[] stack;
      int topIndex = -1;
      private final static int DEFCAP = 100;

      public ArrayStack(int maxSize) {
        stack = (T[]) new Object[maxSize];
      }

      public ArrayStack() {
        this(DEFCAP);
      }

      @Override
      public void push(T element) {
        if (topIndex == stack.length - 1) {
          enlarge();
        }
        topIndex++;
        stack[topIndex] = element;
      }

      @Override
      public T pop() {
        return stack[topIndex--];
      }

      @Override
      public boolean isEmpty() {
        return topIndex == -1;
      }

      @Override
      public  T peak() {
        if (!isEmpty()) {
          return stack[topIndex];
        } else {
          throw new EmptyStackException();
        }
      }

      private void enlarge() {
        stack = Arrays.copyOf(stack, stack.length + DEFCAP);
      }
    }

My attempt: Im seriously pissed at how bad my attempt was but i'm just too closed at the moment and i can't think properly. Need your help please in thinking this question!

public boolean equals(Stack<T> other) {
    if(! other.isEmpty() ) {
        for(int i=0; i < stack.length; i++) {
            if(stack[i].equals(Other.stack[i]) ) {
                return true;
            }
        }
    }

    return false;
}

Thank you!

Upvotes: 0

Views: 6225

Answers (3)

Shubham Chopra
Shubham Chopra

Reputation: 1737

You can use String.valueOf() method in java,this method converts different types of values into string.

If s and t are two stack objects

String a = String.valueOf(s);
String b= String.valueOf(t);

return a.equals(b); // will return true if stacks are equal 

This method will also take care if any of the stack is null.

Upvotes: 0

Kevin DiTraglia
Kevin DiTraglia

Reputation: 26078

public boolean equals(Stack<T> other) {
    //If they point to the same object return true
    if (stack == other) return true;
    //Check for nulls
    if (stack == null || other == null) return false;
    //If the stacks are not the same length, then they won't be equal, easy first test case
    if (stack.length != other.size()) return false;

    for(int i=0; i < stack.length; i++) {
           //Step through each item in both stacks, if any don't match return false
           if(!stack[i].equals(other.stack[i]) ) {
                  return false;
           }
    }

    //Haven't returned yet, they must be equal
    return true;
}

Upvotes: 4

Shamim Hafiz - MSFT
Shamim Hafiz - MSFT

Reputation: 22114

Your equals implementation is obvioiusly wrong. You are returning true if just one element matches. This means, the only time you will get false is, when all entries mismatch one to one.

What you are looking for is to return false, if any corresponding elements do not match and return true at the end of running loop.

You must also return false, if the size of the two stacks are different.

Upvotes: 0

Related Questions