Hi - I have written the following class which impliments a Stack:
//ImageStack.java
import java.io.*;
import java.util.*;
class ImageStack implements IsaStack {
private Vector myStack;
public ImageStack(){
myStack = new Vector();
}
public void push(Object o){
myStack.add(o);
}
public Object pop(){
return (myStack.remove(myStack.size()-1));
}
public Object peek(){
return (myStack.get(myStack.size()-1));
}
public static void main(String args[]){
System.out.println("Test Stack"
ImageStack stk = new ImageStack() ;
stk.push(new Integer(1));
stk.push(new Integer(4));
System.out.println(stk.peek());
}
}
-----------------------------------
//IsaStack.java
public interface IsaStack{
public void push(Object o);
public Object pop();
public Object peek();
}
-----------------------------------
I understand that a Stack (like a Vector ) can only accept an Object, and thus primitives must be cast as objects using their wrapper classes. However, my question for the day is: How can I modify this class so that the ImageStack so that it becomes a stack for storing objects of type Image only.
Also, how would one clear the stack? I would imagine that one way to do it would be to implement the stack as inheriting from Vector, then you could use the removeAllElements()?
I know this is probably fairly elementary but I am pretty new to OO concepts.
Cheers
//ImageStack.java
import java.io.*;
import java.util.*;
class ImageStack implements IsaStack {
private Vector myStack;
public ImageStack(){
myStack = new Vector();
}
public void push(Object o){
myStack.add(o);
}
public Object pop(){
return (myStack.remove(myStack.size()-1));
}
public Object peek(){
return (myStack.get(myStack.size()-1));
}
public static void main(String args[]){
System.out.println("Test Stack"
ImageStack stk = new ImageStack() ;
stk.push(new Integer(1));
stk.push(new Integer(4));
System.out.println(stk.peek());
}
}
-----------------------------------
//IsaStack.java
public interface IsaStack{
public void push(Object o);
public Object pop();
public Object peek();
}
-----------------------------------
I understand that a Stack (like a Vector ) can only accept an Object, and thus primitives must be cast as objects using their wrapper classes. However, my question for the day is: How can I modify this class so that the ImageStack so that it becomes a stack for storing objects of type Image only.
Also, how would one clear the stack? I would imagine that one way to do it would be to implement the stack as inheriting from Vector, then you could use the removeAllElements()?
I know this is probably fairly elementary but I am pretty new to OO concepts.
Cheers