Instead of creating an instance for interface reference use your class' reference. VectorQueue<Integer> myVector = new VectorQueue<Integer>();
Why can't I implement my Vector methods on this instance of VectorQueue?
-
18-10-2022 - |
Question
The class VectorQueue
here is a queue data structure implementing vector methods. In my test class at the bottom I can successfully compile code such as " myVector.enqueue(20); " where myVector is an instance of the VectorQueue
class. It's actual type is Queue
- see here
Queue<Integer> myVector = new VectorQueue<Integer>();
My question is this: Is there no way to use the vector methods on the instance "myVector" once it has been actualized as type Queue? I've tried the following:
((Vector<T>) myVector).insertAtRank(2,5);
I also tried making my main class VectorQueue implement Queue and Vector interfaces, but it didn't help. I wondered to myself also, why can't something have multiple actualized types?
package W7;
public class VectorQueue<T> implements Queue<T> {
public Vector<T> myVector = new ArrayVector<T>();
public void enqueue(T e) {
myVector.insertAtRank(myVector.size(), e);
}
public T front() {
return myVector.elemAtRank(0);
}
public T dequeue() {
return myVector.removeAtRank(0);
}
public boolean isEmpty() {
return myVector.isEmpty();
}
public int size() {
return myVector.size();
}
public String toString(){
return myVector.toString();
}
public static void main(String[] args) throws VectorFullException,
RankOutOfBoundsException {
Queue<Integer> myVector = new VectorQueue<Integer>();
try {
myVector.enqueue(10);
System.out.println(myVector);
myVector.enqueue(5);
System.out.println(myVector);
myVector.dequeue();
System.out.println(myVector);
myVector.enqueue(15);
System.out.println(myVector);
myVector.enqueue(3);
System.out.println(myVector);
myVector.dequeue();
System.out.println(myVector);
myVector.enqueue(7);
System.out.println(myVector);
myVector.dequeue();
System.out.println(myVector);
myVector.enqueue(20);
System.out.println(myVector);
((Vector<T>) myVector).insertAtRank(2,5);
} catch (VectorFullException RankOutOfBoundsException) {
System.out.println("Error");
}
int total = 0;
while(! myVector.isEmpty()){
total += myVector.dequeue();
}
System.out.println("The total size of the contents: " + total);
}
}
No correct solution
OTHER TIPS
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow