Christopher Laurenzano wrote:I tried your first suggestion (changed 46-49)
It worked?
Thanks. Could you explain the logic in your solution regarding the 'toString' method? I'm unclear as to what it does. (I haven't read about "this" yet and don't want to put it into my code; It might confuse me
I was wondering if you could also explain a little further why I couldn't use my original way of coding the method -- I'm unclear maybe as to what 'void' acutally mean/does.
Thanks again.
I need a snack.... this is gonna be a long story....
....
... ok
1. toString Overriding - All objects in
Java (even the ones you create) are subclasses of Object. This allows us to utilize some of the most basic and useful methods for every object we create, including equals(), getClass(), and toString() (there are others, check the API). toString() is what you want if you want a String representation of the Object.... go look at what it does in the API. Open another tab. I'll wait.......
....ok. So to make this information useful in your application you can (and typically should) override it. This eliminates the need for typing a billion println statements when you want to output, say, the objects name, album title, year it was made, et cetera. It makes sure that every time the object is printed to the screen it's the same and eliminates excess typing. To override it, all you need to do is create a method in your class with the same return type (String), name (toString) and parameters (none).
2. "this" as Campbell said, is used to identify that you are using "this" instance of the object. I don't really know why it made my application work correctly (or at this point whther it did or not). I use it to reiterate in my coding (to myself, my professor, my classmates, and others) that I am referring to THIS instance of the object and not some other instance.
3. The reasoning for the mishap in your program.... so campbell said that the "this" syntax wasn't going to work. There was nothing wrong with the method, logically or synctactically, so I looked back at where you called it. It was inside the System.out.println() method, as a parameter. That method takes a String as a parameter, not a void (we'll get to that), so it wasn't going to work. So, the call to the method had to be changed (with a toString() override and use of a new method, or the use of the printCD() method.... or whatever its name was). The easiest way was to just use the method you had already created, so we took it out of the println() and it worked.
4. void - we say it all the time as newbies. Do we know what it means? Not until someone tells us. It means the method should not return a value. That's it. That method signiture is very important, it has the access modifier, the return type, the name, and the parameters expected. That is, if you have a return type and don't return anything, the compiler is going to get mad. You need to return something with any return type other than void. If you don't think the method should return anything, it's a void. Otherwise, you figure out what it should return (like the getters all have a return type that matches what they are created to "get"). You do not have to do anything with the return value if you create one. It just matters that it's there.
I suppose the return type also means that the method can be used in the place wherever something of that type is expected:
Alright....
.... did we cover everything?