Forums Register Login

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

+Pie Number of slices to send: Send
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" );


}
}
+Pie Number of slices to send: Send
Hi Peter, what do you have so far ? Please understand that the ranch is NotACodeMill and that you should DoYourOwnHomework.
+Pie Number of slices to send: Send
 

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.
1
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
post the code so far what you have done. so that we can give you a suggestion.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
Yes, my master! Here is the tiny ad you asked for:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4197 times.
Similar Threads
a question about Stack
Stack
Stack
Stack
Kai Notes - http://www.jdiscuss.com/
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 18:24:38.