• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inheritance & getters

 
Marshal
Posts: 8842
631
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jason for the putting in a lot effort to it. Cow.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm about to go to class & don't have time to give this a thorough look, but there was one place where you identified my EXACT problem:

It does the same thing. Note here that itemName, calories, and cost hold the same values as in the MandMs constructor, but they are entirely different variables. The fact that they have the same name doesn't mean anything.



Why would they be different variables? What happened to the good ol' days when Netbeans would tell me that I didn't need to include the type when using a variable because the variable had already been declared? It was obviously the same variable THEN. Don't answer yet--I haven't finished reading your answer completely!

Thanks for taking so much time to explain this all to me, Jason. And your idea of final & the IDE helping & all that wasn't really bad--I just couldn't figure it out enough even to start trying to do it! I do rely on Netbeans, but usually I eventually understand what it's talking about. AND...it's not always right.
 
Liutauras Vilda
Marshal
Posts: 8842
631
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A bit off topic, but could be important too beside all the given advices already.

1. First about variable names. To choose them right it takes of course a bit of time, but in the final result it could save more than it takes. Likely it won't cause you a problems in small classes, but having good habits from the beginning it is a good habit

"cost", might be a better to choose "costPerUnit" as it reveals better the intention of it.

"theName", you could call it simply "crispyName", rather than use initial version with a comment along that it holds crispy name value. I did notice you wrote: // itemName == "Crispy", calories == 300, cost == .75. I believe because of you were in a hurry. Never ever don't check Strings equality with "==" sign. Use "==" to check equality of primitives OR to check if two reference variables refer to the same object in memory location (it is not checking the contents of the object, that is different).

Didn't manage to decipher class name "MandMs". Maybe because it is not in my native language. But consider the fact that likely you'll need to work in multicultural environment, so always try to choose something simple and clear. Sorry if I misunderstood you here.


2. In your Snack class, think about having set method for the "cost". Might price will change, if you'll be left with excessive amount of snacks near the "best before" date.

3. When you override methods (in this case mean toString method you override), use "@Override" annotation. You can find about the advantage of using it here (<- link), second from the top.

4. Environment where you writing your programs. Consider the possibility to write programs in simple text editor as Notepad++ (if using Windows), TextWrangler (if OS X), vim/emacs (if using Unix like OS; those could be difficult at the beginning). To start with clever IDE's as Eclipse, NetBeans, IntelliJ or any similar, could be overwhelming as you noticed already.
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:
Didn't manage to decipher class name "MandMs". Maybe because it is not in my native language. But consider the fact that likely you'll need to work in multicultural environment, so always try to choose something simple and clear. Sorry if I misunderstood you here.



For the sake of those that haven't heard of them before, M and M's is a brand of confectionery. They are small chocolate disk-like things that are covered in a thin layer of coloured sugar coating and all have the letter M written on them for some reason.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karen Guffey wrote:I do rely on Netbeans, but usually I eventually understand what it's talking about. AND...it's not always right.



I would be surprised if Netbeans makes a mistake. It is possible that it might have the odd bug or two in there, but these will likely be in obscure features because the commonly used ones will be very well tested.

In most cases where you think the i IDE has made a mistake I think it's likely that you have simply misunderstood what it is trying to tell you. Now the compiler warnings can sometimes be a bit cryptic (but are certainly better that in some other languages I have used), and sometimes it takes a bit of experience to realise what the error is. But that's the kind of thing that comes with practice.
 
Marshal
Posts: 79039
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are some IDE warnings which are OTT. For example, Eclipse (bless it!) warns about a resource leak if you open
new Scanner(System.in)
and don't close it. So you need to learn which IDE warnings need to be taken with how large a pinch of salt.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, some IDE solutions for compiler issues aren't always correct. To be fair, they should be treated as little more than hopeful suggestions.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dave--yep, that's the case. It sometimes offers 4 suggestions. Sometimes it offers one, I click on it, Netbeans makes the change...& it's still flagged as incorrect. My theory is that the developer of Netbeans didn't anticipate what some novices might try to do.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jason: I actually think I followed pretty much everything you said. Three questions:

1. Actually, I'm asking for confirmation: when I pass a parameter, am I passing only a type? Is it never a variable with a value?
2. What do you mean by "variables of the widest type"?
3. Arrays are not my forte, but I think what you're saying a getter would do in your example is get all the items in the array that fit the condition. Is that correct?

Thank you very much!
 
Liutauras Vilda
Marshal
Posts: 8842
631
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karen Guffey wrote:Sometimes it offers one, I click on it, Netbeans makes the change...& it's still flagged as incorrect. My theory is that the developer of Netbeans didn't anticipate what some novices might try to do.


You should understand from compiler errors what is wrong and how to fix it in a first place. IDE's suggestions are meant to quicken those problems solving time (when you understand what the actual problem is and how to solve it), but not solve them in a first place. IDE suggestions likely rely on a short scope solution, but has no way to understand and take your program as a fact to solve all problems.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Liutaurus: I was in the middle of editing my last post to include a message to you, but you're too quick for me!

Can you elaborate on the use of a setter method (which is something I've read about but never even attempted to use) in the Snack class? And give me an example of how to call it in the main method?

Also, I'd be interested in knowing what your native language is...& if you're male or female!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:if you open
new Scanner(System.in)
and don't close it. So you need to learn which IDE warnings need to be taken with how large a pinch of salt.


Hmmm. TBH, that strikes me as a weakness of System.in, not of the general philosophy.

To me, it should be a subclass (or wrapper) to InputStream (Sysin?) whose close() method does nothing (and is flagged accordingly); and for the rare occasions that you DO need to close it, what about a force() method that returns the InputStream it wraps?
Then you could write:
  System.in.force().close();

Just a thought.

Winston
 
Campbell Ritchie
Marshal
Posts: 79039
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:. . .
Hmmm. TBH, that strikes me as a weakness of System.in, not of the general philosophy.
. . .

'Ware tangents!

That is an interesting idea, that the Stream should close itself. Never thought of that before. It sounds good, but after nearly twenty years it would be impossible to implement. Unfortunately if you close a BufferedReader you close all associated resources, too, and that can include System.in.
 
Liutauras Vilda
Marshal
Posts: 8842
631
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karen Guffey wrote:Liutaurus: I was in the middle of editing my last post to include a message to you, but you're too quick for me!

And here are way more quicker than me, so we both have where to improve

Karen Guffey wrote:Can you elaborate on the use of a setter method (which is something I've read about but never even attempted to use) in the Snack class? And give me an example of how to call it in the main method?

Setter so-called method should set the value of variable after class has been instantiated. For instance when you create an object of your Snack class, you create similarly to:
So, at any time you can get their price by calling method "getPrice" on instance variable:
But what happens if price changes? in your case you'd need to create another Snack object as it is the only one way to get new price of it, but in many cases you likely would want to change price or something different for the same instance if circumstances changes (as product get discount for example). So, you'd need setter method to be allowed to do that.
It could look similar to your get, but with no return type:
[addition] Sorry, forgot an example.

Karen Guffey wrote:Also, I'd be interested in knowing what your native language is...& if you're male or female!

It is not a secret. Lithuanian. Male.
 
Ranch Hand
Posts: 115
11
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karen Guffey wrote:Jason: I actually think I followed pretty much everything you said. Three questions:

1. Actually, I'm asking for confirmation: when I pass a parameter, am I passing only a type? Is it never a variable with a value?
2. What do you mean by "variables of the widest type"?
3. Arrays are not my forte, but I think what you're saying a getter would do in your example is get all the items in the array that fit the condition. Is that correct?

Thank you very much!



A variable is just a name for a value. You never pass the variable directly, you just pass the value. It works the same as this:



In the above code, y and x both are names for the value 5. If I were to call a method, it works just the same:




By "widest type" I mean the type the closest to the top of your inheritance hierarchy without losing anything meaningful. For example, ArrayList is a List, which is a Collection, which is an Object. Let's say I have an object that maintains a list internally:



And now let's say I want people to be able to access the books on my book shelf. I should not return an ArrayList; that's an implementation detail. I could return a List, but now I need to think, does it matter that it is a list? Does it need to support random access (indexing with list.get(3)), or is all the matters the fact that it is some collection of books that can be iterated? If the latter, then my getter would look like this:




Of course, going up one more level and returning an Object (which is of course the widest type) is meaningless; I can't do anything with it, really.

Doing this hides the details in my class, and it also means that if I decide later that it'll help performance to use a HashSet instead, I can do that very easily without breaking anything:




And finally, in my example, I was iterating a collection of sweets and doing something depending on how many calories the sweets have. If I didn't have those getters, I wouldn't be able to ask the sweets about their calories (unless I made the private fields public, which is a no-no).
 
reply
    Bookmark Topic Watch Topic
  • New Topic