wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How do I turn 2 bottles of beer into 1 bottle of beer? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I turn 2 bottles of beer into 1 bottle of beer?" Watch "How do I turn 2 bottles of beer into 1 bottle of beer?" New topic
Author

How do I turn 2 bottles of beer into 1 bottle of beer?

Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
Here is a code taken from the Head First Java book:


When I run this, I get 99-2 bottles of beer, yada yada, but here are the last few lines:

2 bottles of beer on the wall.
2 bottles of beer on the wall, 2 bottles of beer.
Take one down, pass it around.
1 bottles of beer on the wall.
1 bottle of beer on the wall, 1 bottle of beer.
Take one down, pass it around.
No more bottles of beer on the wall


How would I change this code so it says "1 BOTTLE of beer on the wall"?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Well, the



is not really in the right place. It needs to be before the first printed line that would contain a changed word, yes? So where would that be?


[Jess in Action][AskingGoodQuestions]
Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
I feel like then it is in the right place. The first changed word would be in this line:



right?
"1 bottle of beer on the wall" ??
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Actually, it's the (very similar) line with the period, rather than the comma, that is the one where the number changes, right? The last line of each verse. Therefore, it's right before the last line that the number needs to be computed and the word reevaluated. Note that this is already where the number is being calculated; the word needs to change at the same place.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

I feel like then it is in the right place.
And yet, your output clearly shows you it is not.

walk through the code, when beerNum is 2:


this should print:
2 bottles of beer on the wall,
2 bottles of beer.
Take one down,
pass it around.


so, beerNum is now 1.

it is, so we go into this block:

and we print "1 bottles of beer on the wall".
AHA!!!


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
woot woot, thanks a lot Fred. Very helpful.
Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
Sorry for double post, but another question. Why would the book present the code this way, that clearly is not fully correct. I mean the program runs fine, but there is this more efficient/correct way to write the script. FYI, here is the new modified script:

David Briggs
Greenhorn

Joined: Jul 16, 2007
Posts: 1
Hi there,

I've just reached the same point in Chapter 1.

I fixed the problem by duplicating the if (beerNum == 1) test. If you simply move the if, then the output will not print correctly if the starting point is 1 rather than 99 (more than 1).

My feeling was that the "correct" solution was a plural testing class that decided whether to use bottle or bottles at print time.

I'm just as much a Java newbie, so I don't know if there is such a thing already or whether it would be a good idea to do it that way! Clearly the class would have a run-time overhead, but it should work.

Sorry if that's confusing.

David


David Briggs
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
How 'bout... instead of using a word string variable

(beerNum + " " + (beerNum==1?"bottle":"bottles") + (continued...)

?


------------------------
Bob
SCJP - 86% - June 11, 2009
Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
Originally posted by Bob Ruth:

(beerNum + " " + (beerNum==1?"bottle":"bottles") + (continued...)


I am not really sure what this is saying - I have not reached anything like this yet. Can you maybe elaborate on what this does?
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1344
    
  12
Hi Jake,

That is a sort of one-liner if statement (called a ternary operator, since it has three parts).

You can write the following



Or you can mash it together like this:



The basic pattern is


Note that I am only using parens for readability here.

Of course, this is not the ideal example, because here you can do this:



and only change the value "if something".
[ July 18, 2007: Message edited by: Katrina Owen ]
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1344
    
  12
Originally posted by Jake Miller:
Why would the book present the code this way, that clearly is not fully correct. I mean the program runs fine, but there is this more efficient/correct way to write the script.


My guess is that by having to mess around a bit to find the "bug" teaches you more than just using the program as is. (It sure teaches me more, anyway!)
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
(beerNum==1?"bottle":"bottles")

You bet, Jake.

This is a ternary operator. It's general form is (A ? B : C)
If A is true then the entire expression evaluates to B. If A is NOT true then the entire expression evaluates to C.

So, for instance, (this is just snippets not an entire class...)


int fred = 1;

System.out.println("Fred is equal to one: " + (fred==1?"TRUE":"FALSE");
fred = 2;
System.out.println("Fred is equal to one: " + (fred==1?"TRUE":"FALSE");

in the first case the value of fred is 1 so fred==1 will be true so "TRUE" will be appended to the first string.

in the second case the value of fred is 2 os fred==1 will be false and "FALSE" will be appended.

It is just a shorthand method for selecting one of two evaluations based on a logical test.

[ July 19, 2007: Message edited by: Bob Ruth ]
[ July 19, 2007: Message edited by: Bob Ruth ]
Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
Katrina-
I would agree, it is very helpful to not just have to copy something and to have to work your way through it. And in looking back at the example now, I missed the first time through a little note on the bottom saying "Our example has one little flaw in it. See if you can fix it."
Bob/Katrina-
Thanks for the help on the ternary.
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1344
    
  12
If I remember correctly, there are a couple of other instances where they say something like "oops, that didn't do what I expected". I really enjoy finding a solution to things like that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I turn 2 bottles of beer into 1 bottle of beer?
 
Similar Threads
Head First Java - Beer question
Head First Question
Flaw in Program
Why does this program start from 60 bottles of Beer and not 99
Head first Beersong