Okay, so I’ve written my own stack class, not using the built in one. The stack class holds a List of Entry objects and the size of the stack ( both instance fields). In the stack class i have a push method which adds an Entry object onto the stack. When added the size of stack should increment by one.
This works fine I’ve tested it and it works. I should probably also explain what my Entry class contain. The Entry class basically has a constructor which takes a number and holds this.
So for the Stack class my push method looks like this:
public class Stack{
private List<Entry> entries = new ArrayList<Entry>();
private int size;
public void push(Entry entry)
{
if(entry == null)
String message = "The Parameter given was illegal(null)";
throw new IllegalArgumentException(message);
}
entries.add(entry) // adds the entry object to the stack (or entries List)
size ++ // increments the size by 1.
}
Again, this works perfectly fine, im able to add Entry objects to my Stack and the size increments as it should. Now, we’re required to make a new class called NumberStack which basically hides the detail and calls the push method in the stack class from that class. I’ll show you how my NumberStack class looks like:
public class NumberStack {
private Stack numStack = new Stack();
public void push(final float i) {
numStack.push(new Entry(i));
}
So, basically it does the same as the push method in the Stack class, but hides the details. Now THIS doesn’t work! Nothing gets added to the Stack (entries List) when i call this method. If i try to push two numbers via my NumberStack push method. it should call the push method in the Stack class and add the Entry objects. But nothing at all happens. Again, if I simply push directly in my Stack class, it works perfectly fine.
Why doesn’t the push method in my NumberStack class work? It has no effect, nothings gets added to the Stack.
EDIT:
I tried testing it:
public static void main(String[] args){
System.out.println("Debugging...");
NumStack numStack = new NumStack();
Stack stack = new Stack();
System.out.println("Size before pushing from NumStack = " + stack.size());
numStack.push(5);
System.out.println("Size after pushing from NumStack method push = " + stack.size());
stack.push(new Entry(5));
System.out.println("Size after pushing directly from Stack = " + stack.size());
}
The above returned:
Size before pushing from NumStack = 0
Size after pushing from NumStack method push = 0
Size after pushing directly from Stack = 1
Any help much appreciated
Thanks.
Inside the NumberStack class you create a Stack object called numStack.When
numStack.push(5);is called it goes to the NumberStack class push method and pushed into the stack object called numStack.So instance variable size is incremented in that object. But in testing you are creating another instance of Stack class called stack. But instance variable size is still 0 in that object.But inside the main method you print the size from stack instance.That is why you get answer 0.