Stacks
Hi,
I have to Implement a class stack using a reference-based list. I am creating my own class Stack but I'm a little confused. For example, I want to create my own methods for the stack operations but have found to examples:
The first:
publicinterface StackADT<T>
The second:
publicclass StackClass<T>implements StackADT<T>
Both seem to describe the same methods: initializeStack, isEmptyStack, isFullStack, etc.
How do I start?
Thanks for helping...
[693 byte] By [
aiki985a] at [2007-10-3 8:30:09]

Yes, that's how interfaces work. Read your textbook, or the online java tutorial, about interfaces and their relation to classes. http://java.sun.com/docs/books/tutorial/java/IandI/index.html http://java.sun.com/docs/books/tutorial/
The tutorial says the same thing as my book does, which is what I posted...no examples to follow. I created a small example here...if I can figure out how to do just one ADT method, I can run with it from there. Here is what I have so far using just one method. I'm receiving multiple errors here...how can I fix this?
public class StackADT {
public interface StackADT<T>{
public void initializeStack();
//Method to initialize the stack to an empty state.
//Postcondition: The stack is initialized.
}
public class StackClass<T> implements StackADT<T>{
private int maxStackSize;//variable to store the maximum stack size
private int stackTop;//variable to point to the top of the stack
private T[] list;//array of reference variables
public void initialize Stack(){
for (int i = 0; i < stackTop; i++)
list[i] = null;
stackTop = 0;
}//end initializeStack
}
}
Can anyone help out here?Thanksaiki985
> The first:
> > public interface StackADT<T>
>
>
> The second:
> > public class StackClass<T> implements StackADT<T>
>
>
What if I create a separate class called StackClass and then create another called StackADT?
Will that work?
No, the whole point is that you have an interface and an implementation (a class). You don't create a class just to hold an implementation.The "ADT" almost certainly stands for "abstract data type". You don't create a StackADT class.Did you even read the tutorial?
> No, the whole point is that you have an interface and
> an implementation (a class). You don't create a
> class just to hold an implementation.
>
> The "ADT" almost certainly stands for "abstract data
> type". You don't create a StackADT class.
>
> Did you even read the tutorial?
Yes I read the tutorial, but I need a complete example. Tell me if I'm getting closer to understanding this:
(1) I need to create an interface that defines the abstract data type methods, for example:
public interface StackADT<T>{
In the above, I place all the generic methods. QUESTION: Where do I save my code? Save in a Class?
(2) I then create a class to hold all my actual methods and constructors, for example:
public class StackClass<T> implements StackADT<T>{
It's in this code I define my instance variables, instance methods and constructors?
Please let me know if I'm making any headway understanding this...
Thanks
aiki985
> (1) I need to create an interface that defines the
> abstract data type methods, for example:
> > public interface StackADT<T>{
>
> In the above, I place all the generic methods.
> QUESTION: Where do I save my code? Save in a
> Class?
You put them in a java source code file, like "StackADT.java". You can then compile that source code like you can for any other source code.
> (2) I then create a class to hold all my actual
> methods and constructors, for example:
> > public class StackClass<T> implements StackADT<T>{
>
> It's in this code I define my instance variables,
> instance methods and constructors?
Yes.
I don't know if this helps...but in a well-designed framework or library, you can look at just the interface docs and get a sense of the general relationship between the parts. Then you can look at the class docs, and see how they're specially used.
You get it; you're just anxious that maybe you don't get it. Relax. In a hundred years we'll all be dead anyway.
You can create the stack with or without an interface definition, just having a class works as well.
The primary reason to have an interface is if
- there may be more than one implementation (like List, which comes as ArrayList and LinkedList, and has room for more implementations later.)
- the interface has fewer public methods than the implementation, so it is quicker to learn and use. In this case the interface is there primarily for programmer usability.
Your code example is not very far off, but you can't nest a 'StackADT' interface inside a 'StackADT' class the way you did.
The interface and the class are separate entities, and will normally be stored in different files, like this:
// save as StackADT.java
public interface StackADT<T>{
// specifying method
public void initializeStack();
}
// save as StackClass.java
public class StackClass<T> implements StackADT<T>{
// implementing method
public void initialize Stack(){
// implementation
}
}
Thanks all, I think I'm making progress ... : ) it feels good but don't know if it makes up for the pain!! ha ha