Reputation: 141
I am trying to implement a self created generic interface "BoundedQueue" with an array as the underlying structure. When I compile my partly complete class "BoundedQueueArray", I get the errors:
3 errors found:
File: C:\Users\Awet\CSC 143-Jinguji_NSCC\Week 6\csc143\data_structures\BoundedQueueArray.java [line: 11]
Error: csc143.data_structures.BoundedQueueArray is not abstract and does not override abstract method insert(java.lang.Object) in csc143.data_structures.BoundedQueue
File: C:\Users\Awet\CSC 143-Jinguji_NSCC\Week 6\csc143\data_structures\BoundedQueueArray.java [line: 20]
Error: generic array creation
File: C:\Users\Awet\CSC 143-Jinguji_NSCC\Week 6\csc143\data_structures\BoundedQueueArray.java [line: 32]
Error: name clash: insert(T) in csc143.data_structures.BoundedQueueArray and insert(T) in csc143.data_structures.BoundedQueue have the same erasure, yet neither overrides the other
Here is the class:
package csc143.data_structures;
public class BoundedQueueArray<T> implements BoundedQueue {
// elements stored in array
private T[] elements;
// the number of elements currently in the queue
private int numElems;
public BoundedQueueArray(int capacity) {
// instantiate and bind to reference
elements = new T[capacity];
numElems = 0;
}
/**
* This method inserts the specified element, unless the
* queue is full.
*
* @param o The element to be inserted.
* @throws FullQueueException If the queue is full.
*/
public void insert(T o) throws FullQueueException {
if(numElems < elements.length) {
elements[numElems] = o;
numElems++;
} else { // queue is full, cannot add element
throw new FullQueueException("Queue is full.");
}
}
/**
* This method returns the element at the front of the
* queue, unless the queue is empty.
*
* @return The element at the front of the queue.
* @throws EmptyQueueException If the queue is empty.
*/
public T front() throws EmptyQueueException {
}
/**
* This method retrieves and removes the element at the front
* of the queue, unless the queue is empty.
*
* @return The element at the front of the queue.
* @throws EmptyQueueException If the queue is empty.
*/
public T remove() throws EmptyQueueException {
if(length() == 0) {
throw new EmptyQueueException("Queue is empty.");
}
}
/**
* This method reports whether or not the queue contains
* element(s).
*
* @return If one or more element exists or not.
*/
public boolean hasMember() {
return length() > 0;
}
/**
* This method reports whether the queue has space to add
* element(s).
*
* @return If space exists or not.
*/
public boolean hasSpace() {
return elements.length - length() > 0;
}
/**
* This method returns the capacity of the queue.
*
* @return The capacity of the queue.
*/
public int capacity() {
return elements.length;
}
/**
* This method returns the current length of the queue.
*
* @return The length of the queue.
*/
public int length() {
return numElems;
}
/**
* This method provides a string representation of the queue.
*
* @return The String representation of the queue.
*/
public String toString() {
}
}
Here is the interface it implements:
package csc143.data_structures;
public interface BoundedQueue<T> {
/**
* This method inserts the specified element, unless the
* queue is full.
*
* @param o The element to be inserted.
* @throws FullQueueException If the queue is full.
*/
public void insert(T o) throws FullQueueException;
/**
* This method returns the element at the front of the
* queue, unless the queue is empty.
*
* @return The element at the front of the queue.
* @throws EmptyQueueException If the queue is empty.
*/
public T front() throws EmptyQueueException;
/**
* This method retrieves and removes the element at the front
* of the queue, unless the queue is empty.
*
* @return The element at the front of the queue.
* @throws EmptyQueueException If the queue is empty.
*/
public T remove() throws EmptyQueueException;
/**
* This method reports whether or not the queue contains
* element(s).
*
* @return If one or more element exists or not.
*/
public boolean hasMember();
/**
* This method reports whether the queue has space to add
* element(s).
*
* @return If space exists or not.
*/
public boolean hasSpace();
/**
* This method returns the capacity of the queue.
*
* @return The capacity of the queue.
*/
public int capacity();
/**
* This method returns the current length of the queue.
*
* @return The length of the queue.
*/
public int length();
/**
* This method provides a string representation of the queue.
*
* @return The String representation of the queue.
*/
public String toString();
}
Upvotes: 2
Views: 333
Reputation: 8640
as i said in my comment, go to have a look to implementation of ArrayList to have some ideas
as every object in java implements Object
, in ArrayList you can see elements are store in Object[]
private transient Object[] elementData;
as you know type of array, you can initialize it without problems
your insert will be exactly same, as whatever T is, it will always inherit Object
getting object out from ArrayList is very simple as well, all what you need to do is cast it to your generic type
public E get(int index) {
RangeCheck(index); //irrevelant for your example
return (E) elementData[index];
}
if i missed anything, go and have a look here
Upvotes: 0
Reputation: 29186
So, you should declare your class like this -
public class BoundedQueueArray<T> implements BoundedQueue<T>
and remove the following array creation code from the constructor -
elements = new T[capacity];
It will be better if you can use a List
(interface, implementation) in place of the array (see Effective Java, Item 25), and stay away from raw types as much as possible (see Effective Java, Item 23).
Example -
private List<T> elements; // convert array to list
and -
elements = new ArrayList<T>(); // create instance like this
Upvotes: 7
Reputation: 10249
private T[] elements;
this line is the problem. generic arrays are not allowed as far as i know.
use a List
instead
Upvotes: 0