Here is our sample solution for the FixedSizeQueue class:
public class FixedSizeQueue
{
private int maxItems; // maximum number of items in the queue
private int numberOfItems; // current number of items in the queue
private int[] queue; // the queue items
// Constructor - create a queue of specified capacity
public FixedSizeQueue(int capacity)
{
maxItems = capacity;
numberOfItems = 0;
queue = new int[capacity];
}
// add a new item to the end of the queue
// ** modify this method to protect against adding too many items
public boolean add(int newItem)
{
if (isFull())
{
return false;
}
else
{
queue[numberOfItems] = newItem;
numberOfItems++;
return true;
}
}
// returns true if the number of items in the queue is at its maximum
private boolean isFull()
{
return(numberOfItems >= maxItems);
}
// return contents of queue in string format
public String toString()
{
String queueString = "";
for (int i = 0; i < numberOfItems; i++)
{
queueString += (queue[i] + " ");
}
return queueString;
}
}
Initially the unchanged program should not compile.