Welcome to the Ranch as a more active participant.
Adrian Meneses wrote:. . . I'm new on this forum!
Of course, we don't know about you until you ask a question.
From time to time, I come here for information about Java.
I thought you used push pop and peek; you obviously have been given a different naming convention.
. . . I'm learning about stacks (ADT) with Interfaces and I understand how they work with the basic methods push(), pop(), and top().
At the very simplest, your class has variables declared as type T, which you would know about if you wrote the class yourself. If you downloaded the class from elsewhere, like this class, which comes with the standard Java® installation, you wouldn't see where the Ts are.
What I'm trying to understand is how the parameterized type works. . . .
So we have the newer Java5 version (2004‑2009) with generics.Now, the casts have gone, but lines 6 and 9 won't compile any more. Generics means you no longer have Lists of different types. The book will have to go in another List. The following will compile and run happily in Java5:-Different Lists for the different types. So yes, if you want to put Books into your Stacks, you need a separate object declared as Stack<Book>. The post‑2009 version of the code (Java7/8/9) is slightly different, the only difference being on lines 1 and 2:-I suggest you have a look at the Java™ Tutorials which has at least two sections about generics: 1 2.
C++ had generics and Eiffel has generics; why do we have to put up with such unreliable code?
Campbell Ritchie wrote:
So, you have a Stack<T>. You declare it as a Stack<String>, and every time you try to put a String into that class, the compiler is quite happy.
Knute Snortum wrote:Why is the class Book parameterized? What would T be for a Book? Do you use T in the Book Class? What does BookInterface look like? Why do you need to implement it in MyStack?
Knute Snortum wrote:You don't need parameterization to do what you're describing, just three instances of MyStack. If you wanted to -- and it might be a good idea -- you could parameterize MyStack so that it could hold Books or Clocks or Chickens or whatever, but you don't need to do that to have a stack of English, Spanish, and Other language Books.
Adrian Meneses wrote:
I didn't know about that data type. I read about it and it might be useful for some applications. Are enum types only Strings?
If you can remember how to find a wayback machine, find the version of the Java™ Tutorials about enumerated types from the year 2005. Even the current version tells you that Java® enumerated types are much more powerful than their counterparts in some other languages. You can give elements of an enum fields and methods, though you should probably regard them as immutable and make sure never to try to change their state (and give them private constructors). Anywhere you can use a limited number of distinct objects, think enum. They are very useful things.Since Strings are immutable, there is no need to take defensive copies but traps are probably advisable to stop them eating the cheese off your motherboard.
Adrian Meneses wrote:. . . I didn't know about that data type. I read about it and it might be useful for some applications. . . .
Junilu Lacar wrote:A couple of other things:
1. Your push() logic looks faulty to me.
Seems like you would get an ArrayIndexOutOfBoundsException on the very first call to push() a Book unto a new StackNever mind, I see you did the increment and use of the index in two different statements. That's not how it's normally coded though. You wouldn't initialize the index of a new stack to -1 but 0. Then do the post increment at the same time you're accessing your stack array.
2. You declared the myBookStack variable as Stack not StackInterface. What's the point of declaring and implementing StackInterface if you're not going to use that abstraction?
Campbell Ritchie wrote:Two more things:
1: Before popping something it will be necessary to null out the location in the array, otherwise you are risking a hard reference to an object no longer required. 2: As an alternative to throwing an exception when the array is full, it is possible to copy the entire contents into a larger array.