So we have to use the supplied methods and variables without adding any and no matter what I try I can not get it working. I am extremely new to queues so might be missing something small. Here is my code:
/**
* Int Queue
*/
public class Queue{
/** Max num elements*/
private int numElements;
/** Array to save elements **/
private int elements[];
/** Indice to last element */
private int last;
/** Constructor to init the state object */
Queue(int numElements){
this.numElements = numElements;
this.elements = new int[numElements];
this.last = -1;
}
/** Is empty the queue? */
public boolean isEmpty(){
return (last == -1);
}
/** Is full the queue */
public boolean isFull(){
return (numElements() == this.numElements);
}
/** Insert an element in the queue */
public void enqueue(int element) {
last++;
elements[last]= element;
}
/** Extract the element in the queue.
* There isn't control error */
public int dequeue() {
int elem = elements[0];
for (int x = 0; x < last; x++){
elements[x] = elements [x + 1];
}
last --;
return elem;
}
/** Returns the number of elements in the queue */
public int numElements(){
int number = 0;
for (int x = 0; x < this.last; x++) {
number++;
}
return number;
}
/** Print the elements in the queue*/
public void print(){
System.out.println("\nElements: ");
for(int b:elements){
System.out.println(b);
}
}
public static void main(String args[]){
// test the class
System.out.println("Test the queue class");
Queue que = new Queue(4);
que.enqueue(1);
que.enqueue(4);
que.enqueue(5);
que.print();
que.dequeue();
que.dequeue();
que.print();
} // main
} // Queue
and it outputs:
Test the queue class
Elements:
0
1
4
5
Elements:
4
5
5
5
I am not ale to get it to print the correct options. First it should print 1 4 5 (not the zero) and then it should print 1 and nothing else. All help is apreciated thank you!