I lost 10% for the following decision (instantiating my que as an Object and not an Integer type) and I'm not sure why? Perhaps someone can see why?
Here I instantiated "myQueue2" as type Object.
Queue<Object> myQueue2 = new LinkedQueue<Object>();
Next I enqueued and dequeued some integers
try {
myQueue2.enqueue(10);
System.out.println(myQueue2);
myQueue2.enqueue(5);
System.out.println(myQueue2);
myQueue2.dequeue();
int total = 0;
while (!myQueue2.isEmpty()) {
total += (int)myQueue2.dequeue();
}
System.out.println("The Queue's remain elements added to: " + total);
} catch (QueueEmptyException ex) {
System.out.println("Stack Empty Error");
}
The problem according to my grader is that I should have should have instantiated my queue as an Integer type. At first they argued that it didn't compile because they weren't using Java 7 and this line is illegal before Java 7:
total += (int)myQueue2.dequeue();
After I explained, they still said I should have instantiated the Queue as an Integer type.
However, my logic is that I can enqueue strings. characters and integers by instantiating it as an Object and then casting it as an (int) in this line: (it just works when I try i try it) total += (int)myQueue2.dequeue();
I thought my approach was more flexible,no? Are their any pros and cons to my choice to use Object here,that I don't fully get?