• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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.
 
Rancher
Posts: 4327
59
  • 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: 76873
366
  • 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.
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic