java question help!

Associate
Joined
22 Jun 2005
Posts
1,007
Hi all,

I have been doing the youtube videos for cs106a to try and learn to use java. I am however struggling with this one question:

For the program below, trace through its execution by hand to show what output is
produced when it runs

Code:
/*
* File: Hogwarts.java
* -------------------
* This program is just testing your understanding of parameter passing.
*/
import acm.program.*;
public class Hogwarts extends ConsoleProgram {
public void run() {
bludger(2001);

}

private void bludger(int y) {
int x = y / 1000;
int z = (x + y);
x = quaffle(z, y);
println("bludger: x = " + x + ", y = " + y + ", z = " + z);

}

private int quaffle(int x, int y) {
int z = snitch(x + y, y);
y /= z;
println("quaffle: x = " + x + ", y = " + y + ", z = " + z);
return z;

}

private int snitch(int x, int y) {
y = x / (x % 10);
println("snitch: x = " + x + ", y = " + y);
return y;

}

}


I can pretty much follow most of the question apart from this part here:
Code:
int z = snitch(x + y, y);

More specifically the snitch(x+y, y); I know the x+y part but im not sure what the , y part means or how I find its value.

Is it that I have to do the snitch method, and where it says return y, I return that value to the function where it was called (which is the int z = snitch (x+y ,y) <-- this second y?


Hope I explained that well enough to help me out!

Thanks
 
Thanks for the response. Yes Ive followed exactly how you showed! However the problem is they give you the answer to the question:

snitch: x = 4004, y = 1001
quaffle: x = 2003, y = 1, z = 1001
bludger: x = 1001, y = 2001, z = 2003

And this is where I get lost. How they got the y = 1 for quaffle?
 
Okay I actually figured it out in the end... I was an idiot and shouldve just printed it out and used a pen to run through it rather then just reading and remembering...

Well this is at least a good problem to show how definitely not to code!!

Thanks for the help mate
 
y /= z; This part was very easy to catch me out... it is basically saying divide 2001/1001 and assign that to y. When I look at it I instantly think in my head "2" however it is 1.99 or something which of course would be 1 as it is type int!
 
Back
Top Bottom