-
Queue Stack Question
I have Created a Queue stack with exceptions as seen below. In the main method of the Queue stack is where i test the queue. I am having problems with testing the 3 methods enter(), leave() and peek().
the enter method should add an element to the queue
leave() should retrieve and remove the first element in the queue
peek() should retrieve the first element in the queue
I am using java 5.0 (With Generics)
stack.java
import java.util.ArrayList;
public class Queue <E> {
E typedObject;
int maxsize;
ArrayList list;
public Queue(int size){
list = new ArrayList(size);
}
@SuppressWarnings("unchecked")
public void enter (E element) throws FullException {
if (full())
throw new FullException("The stack is full");
list.add(element);
}
public E leave() throws EmptyException {
if (empty())
throw new EmptyException("The stack is empty");
E apple = (E)list.remove(0);
return apple;
}
public E peek() throws EmptyException {
if (empty())
throw new EmptyException("The stack is empty");
E orange = (E)list.get(0);
return orange;
}
public boolean empty(){
return list.isEmpty();
}
public boolean full(){
return list.size()==maxsize;
}
public static void main(String args[]) {
// initialize the stack
Queue<String> newStack = new Queue<String>(4);
System.out.println("Queue stack initialized size == 4");
// check stacks empty
if (newStack.empty())
System.out.println("Queue stack is empty");
else
System.err.println("Problem with queue stack");
// add some objects
try {
newStack.enter(new String("First object"));
System.out.println("First object added");
newStack.enter(new String("Second object"));
System.out.println("Second object added");
newStack.enter(new String("Third object"));
System.out.println("Third object added");
newStack.enter(new String("Fourth object"));
//newStack.push(new Integer(4));
} catch (FullException fse) {
System.err.println("Problem with queue stack");
System.err.println(fse.getMessage());
}
// check stacks full
if (newStack.full())
System.out.println("Queue stack is full");
else
System.err.println("Problem with queue stack");
// try peek and pop
try {
if (((String) newStack.peek()).equals("Third object"))
System.out.println("Successful peek at third object");
if (((String) newStack.leave()).equals("Third object"))
System.out.println("Successful leave of third object");
if (((String) newStack.leave()).equals("Second object"))
System.out.println("Successful leave of second object");
if (((String) newStack.leave()).equals("First object"))
System.out.println("Successful leave of first object");
newStack.leave();
} catch (EmptyException ese) {
System.err.println("Problem with queue stack");
System.err.println(ese.getMessage());
}
// check stacks empty
if (newStack.empty())
System.out.println("Queue stack is empty");
else
System.err.println("Problem with queue stack");
}
}
public class StackException extends Exception {
public StackException() {
super();
// TODO Auto-generated constructor stub
}
public StackException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public StackException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public StackException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
public class FullException extends StackException {
public FullException() {
super();
// TODO Auto-generated constructor stub
}
public FullException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public FullException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public FullException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
public class EmptyException extends StackException {
public EmptyException() {
super();
// TODO Auto-generated constructor stub
}
public EmptyException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public EmptyException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public EmptyException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
Last edited by trixma; 08-08-2005 at 03:29 AM.
-
What are the problems? It'd be helpful if you'd capture and copy to your posting all error messages that you get.
I assume that the shown program doesn't do what you want it to do.
What does the program do?
What do you want to change about the way it works?
-
I had difficulty with your message because you speak of "queue" and "stack" with what appears to be such ready inter-changeability.
You have data members "typedObject" and "maxSize" but I don't see that your code initializes these data members - and it only uses maxSize once that I could see, but never typedObject.
If you want your Queue to be typed, your ArrayList will have to be typed. Therefore, you need to have ArrayList<E> as your untyped Collection, and use ArrayList<E> list = new ArrayList<E>(maxSize) as your constructor. Then, when you create your string-typed Queue you'll be storing your stings in a string-typed ArrayList.
Last edited by nspils; 08-08-2005 at 09:13 PM.
-
The working queue class
import java.util.ArrayList;
public class Queue <E> {
E typedObject;
int maxsize;
ArrayList list;
public Queue(int size){
list = new ArrayList(size);
}
//add an element to the queue
@SuppressWarnings("unchecked")
public void enter (E element) throws FullException {
if (full())
throw new FullException("The stack is full");
list.add(element);
}
//retrieve and remove the first element in the queue
public E leave() throws EmptyException {
if (empty())
throw new EmptyException("The stack is empty");
E a = (E)list.remove(0);
return a;
}
//retrieve the first element in the queue
public E peek() throws EmptyException {
if (empty())
throw new EmptyException("The stack is empty");
E b = (E)list.get(0);
return b;
}
public boolean empty(){
return list.isEmpty();
}
public boolean full(){
return list.size()==maxsize;
}
public static void main(String args[]) {
// initialize the stack
Queue<String> newStack = new Queue<String>(4);
System.out.println("Queue stack initialized size == 4");
// check stacks empty
if (newStack.empty())
System.out.println("Queue stack is empty");
else
System.err.println("Problem with queue stack");
// add some objects
try {
newStack.enter(new String("First object"));
System.out.println("First object added");
newStack.enter(new String("Second object"));
System.out.println("Second object added");
newStack.enter(new String("Third object"));
System.out.println("Third object added");
newStack.enter(new String("Fourth object"));
//newStack.push(new Integer(4));
} catch (FullException fse) {
System.err.println("Problem with queue stack");
System.err.println(fse.getMessage());
}
// check stacks full
if (newStack.full())
System.out.println("Queue stack is full");
else
System.err.println("Problem with queue stack");
// try peek and pop
try {
if (((String) newStack.peek()).equals("Third object"))
System.out.println("Successful peek at third object");
if (((String) newStack.leave()).equals("Third object"))
System.out.println("Successful leave of third object");
if (((String) newStack.leave()).equals("Second object"))
System.out.println("Successful leave of second object");
if (((String) newStack.leave()).equals("First object"))
System.out.println("Successful leave of first object");
newStack.leave();
} catch (EmptyException ese) {
System.err.println("Problem with queue stack");
System.err.println(ese.getMessage());
}
// check stacks empty
if (newStack.empty())
System.out.println("Queue stack is empty");
else
System.err.println("Problem with queue stack");
}
}
-
Why do you have to do all the casting of elements being returned by your stack? Isn't this what the "generic" structure is all about - being able to trust that the only objects which will be stored in the stack are of the indicated datatype so that you don't have to cast when the objects are returned or accessed from the data structure?
Similar Threads
-
By Agent_Smith in forum C++
Replies: 8
Last Post: 04-21-2005, 07:04 PM
-
Replies: 2
Last Post: 01-15-2003, 01:46 PM
-
By ASP learner in forum ASP.NET
Replies: 5
Last Post: 10-08-2002, 07:17 PM
-
Replies: 0
Last Post: 06-12-2002, 02:46 PM
-
Replies: 3
Last Post: 03-11-2001, 08:04 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|