The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.
Anyone?
Thanks for actually helping, just out of interest what do you do now? What job do you have?
import java.util.*;
public class VectorStack implements Stack
{
private int i = 0;
private int size;
public VectorStack(int maxSize)
{
vect = new Vector(maxSize);
size = 0;
}
public boolean isEmpty ()
{
return(size == 0);
}
public Object getLast ()
{
if (size ==0)
throw new NoSuchElememntException();
return vect(lastElement());
}
public void clear ()
{
for(int i =0; i < size; i++)
vect(i) = null;
size = 0;
}
public void addLast (Object elem)
{
}
public Object removeLast ()
{
}
public Iterator iterator ()
{
}
}
Stack stack = new VectorStack;
creates a new instance of VectorStack called stack
stack.addLast(new Integer(3));
calls the method addLast from stack (the new instance of VectorStack) and passes in a new integer.
'Implements the stack class' just means creates a new instance of. In this case its called VectorStack. It'll inherit the methods from stack, to which you add your own called AddLast.
Does that make sense?
Ok we get given a driver program, here it is
Stack stack = new VectorStack();
try
{
// stack.getLast(); // Should cause an exception when comment removed.
stack.addLast(new Integer(3));
stack.addLast(new Integer(5));
stack.addLast(new Integer(7));
System.out.print("State of stack: ");
Iterator it = stack.iterator();
while (it.hasNext())
System.out.print(" "+it.next());
System.out.println();
System.out.println(stack.removeLast());
System.out.println(stack.getLast());
System.out.println(stack.removeLast());
}
catch(NoSuchElementException e)
{
System.out.println(e.toString());
}
}
}
and we have to write the vectorstack class which implements the stack class, I think that means I have to write the methods it uses but I'm not exactly sure how you do this when it says implements stack.
Aren't stacks last in, first out tho? ie The top of the stack is the last one put on.
Yep, Stack also inherits methods from Iterator...
What do you want VectorStack to do?
"You must implement the given Stack interface using the the java class java.util.Vector as the basis of the implementation. The implementation should be called
VectorStack.java
Notice that the Stack interface includes an additional method iterator(), specified as:
public Iterator iterator()
This method returns the contents of the stack as an Iterator instance (see java.util.Iterator). The stack contents will be ordered from the bottom of the stack
to the top. "
Maybe you will be able to point me in the right direction, I don't want someone to do it for me just idea on how it works. For example
In the Stack interface there is a method addLast();
Does this mean that in the VectorStack one I should have a method called addLast() but as there is no add last for Vectors in the API I should be using something like add() within the addLast() method?
Hope you understand what I mean.
Stack has got 5 of its own methods - empty(), peek(), pop(), push(Object item) & search(Object o)
plus those it inherites from Vector. All your list seems to be covered either in Stack or Vector.
What are you trying to do?
I need to have the following methods
isEmpty()
getLast()
Clear()
addLast()
removeLast()
and Iterator()
But using vectors I don't think you can use these methods, ie I think you have to use other methods within these ones.
havent got the Java doc with me here, but which methods do you need to overload?
Vectors? they ring vague bells from my uni days, but I haven't looked at C since then.