Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi - I have written the following c

Status
Not open for further replies.

Knackers

Technical User
Apr 26, 2001
59
AU
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
 
Hi,

public void push(Image o){
myStack.add((Image) o);
}

Tiago

Regards,
Tiago
 
Thanks for your quick reply Tiago.

I have changed my push() method as you suggest, but the compiler throws this error:

--------------------------------------------

C:\Ben\University\cosc1295\lab4>javac ImageStack.java
ImageStack.java:15: cannot resolve symbol
symbol : class Image
location: class ImageStack
public void push(Image o){
^
ImageStack.java:16: cannot resolve symbol
symbol : class Image
location: class ImageStack
myStack.add((Image) o);
^
2 errors

--------------------------------------------

I assume that this is because it does not as yet know what an Image object is? If so, then do I have to declare / create one at the top of my ImageStack class? DO I need to write and Image Class?

Cheers
 
Hi,

Yes, you may need to write Image class. I don't know exactly what's the purpose of your class, and if you can use some class that already exists.

Tiago

Regards,
Tiago
 
Cheers Tiago

There is no real purpose to my ImageStack class - it is mainly an exercise to demonstrate wrapper classes. SO I guess I need to make a wrapper for my Images so that they are just passed to the Stack as any other Object would be.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top