Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ho Sun Lee
2,003 PointsI have a question from one of the "Think Java" problems
So I was doing some problems in this book "Think Java" and one of the questions was to find out what the output of this program is :
public class Narf {
public static void zoop(String fred, int bob) {
System.out.println(fred);
if (bob == 5) {
ping("not ");
} else {
System.out.println("!");
}
}
public static void main(String[] args) {
int bizz = 5;
int buzz = 2;
zoop("just for", bizz);
clink(2*buzz);
}
public static void clink(int fork) {
System.out.print("It's ");
zoop("breakfast ", fork) ;
}
public static void ping(String strangStrung) {
System.out.println("any " + strangStrung + "more ");
}
}
Can anyone take me step by step and explain why?
I know when it runs, it runs as:
just for any not more It's breakfast !
But why? Because from what I read in the book, I can't seem to pinpoint exactly what goes where, and whatnot.
I mean, I know that main is where it's executed, but zoop and clink are confusing me.
Thanks in advance!
2 Answers
Kristian Terziev
28,449 PointsOk let me try to explain it step by step.
1) First you have a fucntion called "zoop". When you call this function you need to pass in 2 parameters - String and int. It prints the string and depending on the number it prints some more text.
2) Then you have the function "ping". It requires a string. And it simply prints "any " + theStringPassedWhenCalled + "more ".
3) And finally the function "clink" which requires an int. First it prints "It's " and then it calls funtion "zoop" with some string (in this case "breakfast ") and fork for the int. The "fork" you should've passed when called the function. So it goes to zoop and prints the string passed (int this case "breakfast ") and then it check if "fork" is = 5. It's not so it enters the "else" block whichi prints an "!".
So this in your code is:
1)
int bizz = 5;
zoop("just for", bizz);
So what happens now? - The compilator calls the function zoop and passes for String fred - "just for" and for int bob - 5. And then it runs the function line by line. It prints "just for" then checks if bizz is equal to 5. It is so it enters inside the block and calls function ping with "not" as a parameter. Ping prints "any not more". But from the function zoop you have "just for ". So up to now you have "just for any not more".
Then you have
int buzz = 2;
clink(2*buzz); // 4
It this case "fork" = 4. So now "It's " is printed. And you call zoop with "breakfast " as the String and "4" as the int. It prints "breakfast" and checks if 4 = 5. It's not so it enters the else block where it prints "!"
So in the end you have "just for any not more It's breakfast !"
Hope this explains it. If there's anything else please feel free to ask :)
Gloria Dwomoh
13,116 PointsThis code is a nice brain teaser. You have to look at many things in order to come up with the code exactly as the compiler does.
You have to look at:
1- spaces
2- when it is print and when it is println as both will change the structure of the output
3- when it is calling a method, and what arguments it is being passed on
Below is my explanation, re-read the code as well as I have added some comments to it. You will probably have to read it more than once to get it maybe.
In main you see…
int bizz = 5;
int buzz = 2;
zoop("just for", bizz); // breaked down deeper zoop("just for", 5);
clink(2*buzz); // breaked down deeper clink(4);
It called zoop first “zoop("just for", 5);” the argument for fred is “just for” and for bob it is “5”
public static void zoop(String fred, int bob) {
System.out.println(fred);
if (bob == 5) {
ping("not ");
} else {
System.out.println("!");
}
}
It firsts Prints…. “just for”
And then since Bob is 5 calls ping(“not ”) // take notice there is a space there
public static void ping(String strangStrung) {
System.out.println("any " + strangStrung + "more ");// 1 space after any and 1 after not
}
That prints “any not more”
Now we have finished this line… main follows up by calling
clink(2*buzz); // breaked down deeper clink(4);
public static void clink(int fork) {
System.out.print("It's ");
zoop("breakfast ", fork) ;
}
That prints (notice it says print and not println so it will append what is written in zoop at the same line) “It’s “ (this line is to get appended)
After the print function we have zoop("breakfast ", fork) ;
public static void zoop(String fred, int bob) {
System.out.println(fred);
if (bob == 5) {
ping("not ");
} else {
System.out.println("!");
}
}
We have a println which will append the argument that was pass on to fred to the previous statement
“It’s breakfast”
And because it is println it will move the cursor to the next line Since bob is not 5 but 4 it will print out the exclamation mark “!’
So all together you will have the code below in this exact structure. It is not a coincidence why I have broken it down into other lines.
OUTPUT:
just for
any not more
It’s breakfast
!