• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Method help: how does this work?

 
Greenhorn
Posts: 7
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm still confused how methods work. I am studying Java through Head First Java, a book that experienced Java developers suggests I begin my Java experience. I'm just in the Chapter 2 explaining methods when I came to this problem. Of course I tried my best to solve it but I got a wrong answer. So when I go to the solution page of the book, I got confused.

Here is what confuses me(I'll add comment on the part where I got confused):


The confusing part is the boolean thing and the methods inside DrumKit class. How was it able to work even though it is not inside in any of the 2 methods. Ex. I figured that topHat should be inside playTopHat() method to work and the same with snare and playSnare().
 
Marshal
Posts: 79699
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't got my copy with me; please tell us what the answers were and which you got right or wrong.
 
Arnel Colar
Greenhorn
Posts: 7
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

My first post was the given solution in that book. This is my answer:
My first attempt:


My second attempt was correct but:


what confuses me is the solution in that book.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, it looks like the line 19, the part that confuses you, is something for you to play with. I do not have this book, but from what I can gather from the program, try commenting out the part that confuses you '//' and then run the program and see what happens.

another thing you can try is to change the 'if' statement in line 22 to false instead of commenting out line 19. Go back and forth a couple of times and see what the changes are. Ask yourself what you think the changes will be in the program before you run it, that sort of thing.

Good luck!
 
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Arnel

Can you a bit a liitle more specific what the confusion is about.In the solution provided by book the instance varriable snare is being called with a reference of DrumKit which is correct.I could not get what is your confusion regarding it.....
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arnel Colar wrote:
The confusing part is the boolean thing and the methods inside DrumKit class. How was it able to work even though it is not inside in any of the 2 methods. Ex. I figured that topHat should be inside playTopHat() method to work and the same with snare and playSnare().



I'm going to try to cut down on the back-and-forth postings by making some guesses; I will try to let you know what I'm guessing.

By "the boolean thing" I'm guessing that you mean line 20 in your originally posted program: "d.snare = false;"

That is an assignment statement to the boolean variable associated with d, an object of type DrumKit. DrumKit's definition makes its variables "topHat" and "snare" "package protected", which means that another class in the same package can access those variables directly with the object.variable syntax, and use them just like they were variables declared within its own scope.

Then I'm guessing that you are confused because the methods in DrumKit are able to access the variables, even though the variables are not inside the methods. That is normal for OO languages, including Java -- the variables are declared within the class DrumKit and outside of any method, so they are "instance variables". They come into existence when the object is instantiated, and live as long as some part of the program has a reference to that object. They can be accessed by instance methods (though not static methods) within the class, and will retain the last values they were given, even if that's outside the invocation of that method.

Does that answer at least some of your questions?

rc
 
Arnel Colar
Greenhorn
Posts: 7
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ralph. That explains some parts but why is it that when I remove d.snare = false i get this output:


and when I bring back d.snare = false I get this output:


I'm sorry if my questions are a bit confusing to some. I'm a Java beginner and also new in forums. I'll be more specific with my questions next time.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm afraid the short answer is "Because you told it to."

Let's step through your main program as originally posted; numbers refer to lines:

18 create an instance of DrumKit in d
19 playSnare, which will output "bang bang ba-bang"
20 set the snare variable within d to false
21 playTopHat, which will output "ding ding da-ding"
23 if the snare variable with d is true, playSnare again.

so only if the snare variable is true will playSnare be executed on line 23. With line 20 setting that value to false, then the test on line 23 will fail, and the code to output the 3rd line is not executed.

Now, if you don't set the value of a variable, it is a good rule not to trust what its value will be when the program runs. In this case, it appears that the Java runtime (or random chance, take your pick) have set the snare variable to true "by default", i.e., it has the value true unless you set it something else. If you take out line 20, then the variable is true, and you get your third line of output.

Welcome to programming! Those of us that do this for a living step through code like this ALL THE TIME, *much* more often than we write code...

rc
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic