This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Java ArrayQueues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java ArrayQueues" Watch "Java ArrayQueues" New topic
Author

Java ArrayQueues

lespaul84
Greenhorn

Joined: Mar 24, 2004
Posts: 1
Can someone please help me fill in the missing code, thanks
/**
* class ArrayQueue
*
* @author <yourname>
* @version 1.0
*/
public class ArrayQueue implements Queue
{
// instance variables - replace the example below with your own
private Object[] entries;
private int capacity;
private int head;
private int tail;
private int size;
/**
* Constructors for objects of class ArrayQueue
*/
public ArrayQueue()
{
entries = new Object[10];
this.capacity = 10;
makeEmpty();
}

public ArrayQueue(int capacity)
{
entries = new Object[capacity];
this.capacity = capacity;
makeEmpty();
}

/**
* Method makeEmpty
*
**/
private void makeEmpty()
{
// CODE REQUIRED
}

/**
* Method destroy - sets all queue entries to null
*
**/
public void destroy()
{
makeEmpty();
}

/**
* Method size
*
* @return number of entries in queue
**/
public int size()
{
return size;
}

/**
* Test if the array is logically full.
* @return true if full
*/
public boolean isFull( )
{
// CODE REQUIRED
}

/**
* Test if the array is logically empty.
* @return true if empty
*/
public boolean isEmpty( )
{
// CODE REQUIRED
}
/**
* private method to increment with wraparound.
* @param x index
* @return x+1, or 0, if x is at the end of theArray.
*/
private int increment( int x )
{
// CODE REQUIRED
}

/**
* Add a new entry to the queue.
* @param Object o the item to insert.
*/
public void add( Object o )
{
// CODE REQUIRED
}

/**
* Remove an entry from the queue.
* @return Object - the front queue entry
*/
public Object remove()
{
// CODE REQUIRED

}


/**
* Method iterator
*
* @return Iterator - an ArrayQueueIterator object
**/
public Iterator iterator()
{
return new ArrayQueueIterator();
}

/**
* Nested class ArrayQeueIterator provides an iterator for the ArrayQueue class
*/
protected class ArrayQueueIterator implements Iterator
{
int nextIndex;
// Default constructor
ArrayQueueIterator()
{
nextIndex = head;
}
/**
* method next in nested ArrayQueueIterator class
*
* @return Object - the next entry
**/
public Object next()
{
Object theDataItem = entries[nextIndex];
nextIndex = increment(nextIndex);
return theDataItem;
}

/**
* method hasNext in nested ArrayQueueIterator class
*
* @return true if the end of the queue has not been reached
**/
public boolean hasNext()
{
return nextIndex!=increment(tail);
}
}
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi Les,
Welcome to JavaRanch!
We don't have a whole lot of rules round these parts, but we do have our naming policy, and it appears that you ain't read it. Y'see, it requires that your display name consist of a first and last name, and that the name not be an obvious forgery. So you and yer guitars are wrong on both counts. Please head over here and fix this up, pronto! Thank you kindly, stranger.
Now as to your problem: we don't do people's homework. What we like to do is help you to do it. I'll get you started taking the first little step.
So a queue is a kind of container. You add things to it, and remove them. Imagine just adding one item and then removing it.

... we want to see "1" get printed. Now how might you implement the add() and remove() methods to accomplish this? Show me code, or tell me in words and we'll work on the code together.
But first, go fix your display name!


[Jess in Action][AskingGoodQuestions]
 
 
subject: Java ArrayQueues
 
Similar Threads
Methods that return interfaces
Implementing a class SortedArrayMap that implement Map interface
Questions in relation to a queue.
Implement a data structure that support a sorted Map.