• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Java ArrayQueues

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
machines help you to do more, but experience less. Experience this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic