Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help for comparing two stacks to see if they are equal?  RSS feed

 
Peter Cong
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I want to craete a method class method public static <E> boolean eq( Stack<E> s1, Stack<E> s2 ). to compare two stacks (one stack is Array stack, the other is LinkedList stack for example), the method returns true if and only if s1 and s2 contain the same number of elements and the elements in the same positions are “equals”;
public class TwoStack{

public static <E> boolean eq( Stack<E> xs, Stack<E> ys ) {


throw new UnsupportedOperationException( "Error exception" );


}
}
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter, what do you have so far ? Please understand that the ranch is NotACodeMill and that you should DoYourOwnHomework.
 
Peter Cong
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:Hi Peter, what do you have so far ? Please understand that the ranch is NotACodeMill and that you should DoYourOwnHomework.

Thanks for the quick response, but actually, this is not a homework, I am starting learning Java now and encounter the problem.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks like homework to me.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
post the code so far what you have done. so that we can give you a suggestion.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like "Stack" is a custom interface, since the only Stack in the standard library is java.util.Stack, and that's a class, not an interface. Perhaps you should ask Professor Turcotte what methods his Stack interface declares, as that's not readily apparent from the assignment page. If his interface is sufficiently similar to java.util.Stack, you could just call the equals() method it inherits from Vector. If not, getting a list of the methods you're allowed to use is probably a good first step in figuring out what you can do.
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If, as Mike S says, your Stack is an interface, that makes it easier for you, because "Stack<E>" will cover all types you ever need.

Write down on paper what your definition of equals is: what features of a stack will lead you to believe it is equal or unequal to another stack? And remember your method will have to deal with null values; in all cases you are looking atYou can write yourself an Objects class similar to that planned for Java7, which probably uses the same formula for its equals(Object, Object) method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!