• Post Reply Bookmark Topic Watch Topic
  • New Topic

why do we need boolean and why does d.snare need an if statement?  RSS feed

 
Justin Groid
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Head First Java...

So I'm doing the code magnets exercise in chapter 2.

Really confused by something not explained and/or pointless.



Looked at solution:

At the top for class Drumkit,

theres boolean topHat = true;
boolean snare = true;

why do we need these?

Then....when making the object

"
Drumkit d = new Drumkit();
d.playSnare();
d.snare = false;
d.playTopHat();

if (d.snare == true) {
d.playsnare();

"

Why is playsnare different than TopHat? Why does it get a false?

[Edited for language -- please be sure to use professional language on the Ranch]


both of their functions are exactly the same:
System.out.println
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case, I think they are just trying to illustrate a few things. (I don't have the book in front of me, so I can't say for sure)

My guess is they are trying to show you that you can use boolean variables to control the flow of a program, change their values, etc. I'm surprised our friend Campbell hasn't pointed out that there is no reason to do:

if (d.snare == true)

when you can just do

if (d.snare)

But again, I think they are just trying to show the idea.
 
Justin Groid
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you're saying I don't actually need

d.snare = false;

and

if (d.snare == true) {
d.playsnare();

all I need is

d.playTopHat();
and d.playSnare();

Well but... I still don't understand how all that if stuff works even....is it setting it false so that later on he can set it true just as a demonstration? If so then why is he setting d.snare as false after it's already been played? Shouldn't that produce double the output of println?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote: . . . I'm surprised our friend Campbell hasn't pointed out that there is no reason to do: . . .
Timezones, timezones. According to my browser, that post was at 1.32am. Even I had managed to get safely off to bed by then
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that is not what Fred is saying. What he is saying is that the drum kit example includes an option to play the snare drum; you can turn it on and off like thisI would have preferred to have the fields all private, so you can set the options with setXXX methods. I would prefer to see a play() method in the drum kit classThe playSnare() and playTophat() methods can now be marked private and will still workYou get the top hat, and the snare drum too if you set it to true.
A real snare drum has options like that; you can flick a lever which raises or lowers the snares (wire springs) so you can play it with and without snares.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Groid wrote:So you're saying I don't actually need

d.snare = false;

and

if (d.snare == true) {
d.playsnare();

No...that's not what I meant.

Let's say you own a music store. You may have many, many drum kits for sale. each drum kit has different components, depending on what pieces are included. One kit in particular you call 'd'. When you first got it, it had a snare drum and a top hat. So, you set those two booleans to true.

Then, since we are in bad economic times, somebody comes in and says "I can't buy an entire drum kit, can I just buy the snare from your drum kit you call 'd'?" You need the money, so you sell it. You now have to mark that 'd' does not have a snare drum anymore (d.snare = false;). It may someday, so you would keep the variable around, in case that snare comes back.

Now, you have an intern in your store. He's not very bright - he can only do exactly what you tell him to do. Sometimes, customers come in and say "Can you play each piece of equipment in a drum set for me?" You give your intern directions. The problem is that if you say "play the snare", and the snare drum isn't there, he gets very confused, and complains to you "THERE IS NO SNARE!!! I DON'T KNOW WHAT TO DO!!! I QUIT!!!".

So, you give him more specific directions: "Check to see if there is a snare drum, and if so, play the snare". Now you can use this set of directions on any drum kit, regardless of whether there is a snare or not.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!