0

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?

COOLBEANS
  • 729
  • 3
  • 13
  • 31
  • `int` is a primitive type! – donfuxx Mar 25 '14 at 22:03
  • 1
    You want to be able to put any `Object` into your `Queue`, and yet you expect them to be castable to `int`? – Keppil Mar 25 '14 at 22:04
  • It works in Java 7 http://stackoverflow.com/questions/3661413/how-to-cast-an-object-to-an-int-in-java – COOLBEANS Mar 25 '14 at 22:04
  • @keppil This output shows that I added strings,chars, and ints using this code[1]: ["we"]-> [2]: ["we"]-> ["d"]-> [1]: ["d"]-> [2]: ["d"]-> ["15"]-> [3]: ["d"]-> ["15"]-> ["3"]-> [2]: ["15"]-> ["3"]-> [3]: ["15"]-> ["3"]-> ["7"]-> [2]: ["3"]-> ["7"]-> [3]: ["3"]-> ["7"]-> ["20"]-> – COOLBEANS Mar 25 '14 at 22:06
  • Sorry but what is the purpose of data stored in your queue? I am confused with fact that you are storing Strings and trying to cast them later to `int`. – Pshemo Mar 25 '14 at 22:13
  • The reason it works is because you were careful to put int values into your `Queue`. Java `boxes/unboxes` int values into Integers automatically, so you dodged one bullet (in general you can't put primitive types into collections). But if you were to publish your Queue, so that other objects can use it, they may not be as careful as you were in testing. They may put Strings, arrays, or anything other type into the Queue because you explicitly gave them permission to do so. But your code will not work if they do so. – deanosaur Mar 25 '14 at 22:15
  • I'm not sure why, but it did workwith strings, characters , and ints.[1]: ["we"]-> [2]: ["we"]-> ["d"]-> [1]: ["d"]-> [2]: ["d"]-> ["15"]-> [3]: ["d"]-> ["15"]-> ["3"]-> [2]: ["15"]-> ["3"]-> [3]: ["15"]-> ["3"]-> ["7"]-> [2]: ["3"]-> ["7"]-> [3]: ["3"]-> ["7"]-> ["20"]-> – COOLBEANS Mar 25 '14 at 22:22

3 Answers3

3

Using "blind casting" or instanceof is generally VERY BAD approach. Using right types allows your compiler to find bugs before even running the program.

Also, it helps you to easily find, what your instances are used for (imagine, that you see your code after year or two, and you see Queue<Object> myQueue2 = new LinkedQueue<Object>();, how you should know, that it is used for integers?

libik
  • 22,239
  • 9
  • 44
  • 87
1

When you pick the arguments for that class, You are setting a limitation. Since everything in java is an Object (let alone things like primitives), You will be able to store any Type in that array.

Your teacher only wants you to be able to store integers in that array. Since nothing prevents you from doing myqueue.add("Im a string, not an integer");, your teacher docked you those points.

Example:

class Car { }

ArrayList<Car> carlist = new ArrayList<Car>();

If I tried doing carlist.add("Hey");, an error would be thrown, since String does not extend Car. If I had

class Benz extends Car { }

I could do carlist.add(new Benz()); because Benz is a Car.

Every class falls under the hierarchy of Object, so you could put anything in there.

Tip: Some classes like String and Integer are final, so nothing can extend these classes.

Vince
  • 14,470
  • 7
  • 39
  • 84
  • Okay, so my error was conceptual in a way. I thought I was being flexible by allowing more types, when in fact the point is normally to be restrictive in such cases? – COOLBEANS Mar 25 '14 at 22:45
  • Exactly. In some cases, like if you have a `Car` list, you wouldn't want someone to put a `House` in there. – Vince Mar 25 '14 at 22:54
0

You can put in strings in the sense that it compiles, but you will get an exception at runtime when you put in anything other than an Integer and then cast it to int. So you've replaced a compile time error with a runtime error, which is bad.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • Not True though, becuase it outputs chars, strings and ints [1]: ["we"]-> [2]: ["we"]-> ["d"]-> [1]: ["d"]-> [2]: ["d"]-> ["15"]-> [3]: ["d"]-> ["15"]-> ["3"]-> [2]: ["15"]-> ["3"]-> [3]: ["15"]-> ["3"]-> ["7"]-> [2]: ["3"]-> ["7"]-> [3]: ["3"]-> ["7"]-> ["20"]-> – COOLBEANS Mar 25 '14 at 22:17
  • @COOLBEANS Who outputs that? Your code most certainly does not output that. Your code (that is the code you posted) doesn't output anything. What I said is 100% true. – sepp2k Mar 25 '14 at 22:29
  • Sorry, I should have been clearer. My true test method is longer and i didn't post it. When I change some of of the enqueues(int)'s to strings or characters (ex. enqueue("string") or enqueue('c') it does compile. You may need Java 7 though. – COOLBEANS Mar 25 '14 at 22:49
  • 1
    @COOLBEANS It complies, yes. But I'm saying that when the line `total += (int)myQueue2.dequeue();` executes and the thing you take out of the queue is not actually an integer, you'll get an exception. If you're saying that that's not true, I'd like to see the exact code that makes you think so. – sepp2k Mar 25 '14 at 22:51