• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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" );


}
}
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This looks like homework to me.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
post the code so far what you have done. so that we can give you a suggestion.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic