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
 
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
I got confused at your explaination but Ill try and explain best I can.
So when you run the program it will run the method bludger with 2001 as the parameter.

In this method it sets its own variables x and z so first x becomes 2001 /1000.
then Z becomes (2001/1000) + 2001
It then sets x to the output of quaffle.

Quaffle takes z and y from bludger but names them x and y. On the first line of quaffle it runs the snitch method using x+y and y as the parameters. so x+y is the addition of both the parameters passed in and y is just the 2nd one. so at this point x would be 2003.001 and y would be 2001.

So we now pass (2003.001 + 2001) and 2001 into snitch. Which is where you are up to. Hope that helps.
 
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!
 
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!

Yeh I completely missed that they were ints. Good spot.
Also with stuff like this just log everything so you could have just added
println() lines just before each method call to see what is passed in.
 
Last edited:
Back
Top Bottom