Queue Stack Question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Queue Stack Question

Threaded View

  1. #1
    Join Date
    Feb 2005
    Posts
    55

    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.

Similar Threads

  1. deleting stack objects?
    By Agent_Smith in forum C++
    Replies: 8
    Last Post: 04-21-2005, 07:04 PM
  2. Replies: 2
    Last Post: 01-15-2003, 01:46 PM
  3. Display one record at a time!
    By ASP learner in forum ASP.NET
    Replies: 5
    Last Post: 10-08-2002, 07:17 PM
  4. DotNet and MSMQ question
    By zack in forum .NET
    Replies: 0
    Last Post: 06-12-2002, 02:46 PM
  5. 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center