aspose file tools*
The moose likes Beginning Java and the fly likes Method help: how does this work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method help: how does this work?" Watch "Method help: how does this work?" New topic
Author

Method help: how does this work?

Arnel Colar
Greenhorn

Joined: Apr 14, 2011
Posts: 7

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().


http://magicduel.com/register/register.php?refererID=217409
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Apr 14, 2011
Posts: 7

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.
Jason Koonce
Ranch Hand

Joined: Oct 30, 2010
Posts: 36
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!


Jason K. (AC)
jishnu dasgupta
Ranch Hand

Joined: Mar 11, 2011
Posts: 103

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.....


If debugging is the process of removing bugs, then programming must be the process of putting them in. -- Edsger Dijkstra

Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
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

Joined: Apr 14, 2011
Posts: 7

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

Joined: May 29, 2005
Posts: 479
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method help: how does this work?