Im trying to work on some of the Queue methods, get some practice with them in a little bit so I chose a handful from the list on Oracle's documentation and gave them a shot. Everything in my code seems to be working smoothly except for this one hiccup that I have not been able to get over. I'm pretty new at programming and I am in college for it, but I am still learning the basics. Here is what I have for my code:
import java.util.NoSuchElementException;
public class Queue<E> {
private Object[] queue;
private int size = 0;
private int top;
private int bottom;
public Queue() {
queue = new Object[10];
}
public Queue(int capacity) {
queue = new Object[capacity];
}
@SuppressWarnings("unchecked")
public E elm() {
if (size == 0) {
throw new NoSuchElementException();
}
return (E) queue[top];
}
public boolean add(E elm) {
if (elm == null) {
throw new NullPointerException();
}
if (size == queue.length) {
int newCapacity = (int)Math.ceil(queue.length + 1.5);
Object[] newArr = new Object[newCapacity];
for (int i = 0; i < queue.length; i++) {
newArr[i] = queue[i];
}
}
if (bottom == -1) {
top = 0;
bottom = 0;
queue[bottom]= elm;
} else {
bottom = (bottom +1) % queue.length;
}
queue[bottom] = elm;
size++;
return true;
}
public boolean isEmpty() {
return size == 0;
}
@SuppressWarnings("unchecked")
public E remove() {
if (size == queue.length) {
throw new NoSuchElementException();
}
E elm = (E) queue[top];
top = (top +1) % queue.length;
size--;
if (size == 0) {
bottom = -1;
}
return elm;
}
public void clear() {
return;
}
public int size() {
for (int i = 0; i < queue.length; i++) {
size += 1;
}
return size;
}
boolean contains(Object o) {
if (Queue.contains(o)) {
return true;
} else {
return false;
}
}
}//end of file
the problem lies in the very last block of code. I keep getting the message in Eclipse, telling me "Cannot make a static reference to the non-static method contains(Object) from the type Queue" and it is suggesting I change contains() to static. I have tried this to no avail. When I do change it to static, I only get a repeating error upon running it that says "Exception in thread "main" java.lang.StackOverflowError" and gives the location of the error repeatedly. I am not sure what it is that I am doing wrong here? I'll put my driver below; I was using it to test some of my code but it doesnt have a whole lot in it.
public class Driver {
public static void main(String[] args) {
Queue<Integer> a1 = new Queue<>();
a1.add(10);
a1.add(79);
System.out.println(a1.size());
System.out.println(a1.contains(10));
}
}