1. Member Join Date
Nov 2012
Location
Brisbane, Australia
Posts
8
Rep Power
0

## Reading Java Code x=b(a,b)

Hi All,

I have a code that I have been given, and I need to calculate manually - I know that the answer is 66, but I'm not exactly sure how they get to that answer.

Can someone please explain to me the x=b(a,b) equation? Because for the life of me the maths I'm doing doesn't add up to 66?

a=3
b=-3
x=10  Reply With Quote

2. ## Re: Reading Java Code x=b(a,b)

I'm not sure that this question can be answered given the data you've presented so far.  Reply With Quote

3. Member Join Date
Nov 2012
Location
Brisbane, Australia
Posts
8
Rep Power
0

## Re: Reading Java Code x=b(a,b)

Sorry,

Java Code:
```public class Question4
{
public static int a(int b, int c)
{
int x=0;
x=c-b;
return x;
}
public static int b(int c, int d)
{
int x;
if (c<d)
{
x=a(c,d);
}
else
{
if (d<c)
{
x=a(d,c);
}
else
{
x=0;
}
}
return x;
}
public static void main(String args[])
{
int a=3;
int b=-3;
int x=10;
x=b(a,b);
System.out.print(x);
x=b(b,a);
System.out.print(x);
}
}```
I'm not allowed to calculate it using a computer, I must calculate manually...  Reply With Quote

4. ## Re: Reading Java Code x=b(a,b)

Your code's indentation is all over the place making it *very* hard to read and understand. For the benefit of others, here is your code re-formatted:

Java Code:
```public class Question4 {
public static int a(int b, int c) {
int x = 0;
x = c - b;
return x;
}

public static int b(int c, int d) {
int x;
if (c < d) {
x = a(c, d);
} else {
if (d < c) {
x = a(d, c);
} else {
x = 0;
}
}
return x;
}

public static void main(String args[]) {
int a = 3;
int b = -3;
int x = 10;
x = b(a, b);
System.out.print(x);
x = b(b, a);
System.out.print(x);
}
}```
In the future, please format your code better when posting it here. You are asking volunteers to help you, and if we can't read your code, we can't understand it, and if we can't understand it, we can't help. Besides, since you're asking effort from us, it's only fair that you put in effort likewise.

OK, you need to walk through your code step-by-step to see why it returns 66. Have you done this yet? If so and you're still confused, which part confuses you?  Reply With Quote

5. Member Join Date
Nov 2012
Location
Brisbane, Australia
Posts
8
Rep Power
0

## Re: Reading Java Code x=b(a,b)

Sorry, I'm very new to all this stuff.

I'm confused by the "," in the equation, so if a=3, b= -3 and x=10,

Then my equation looks like 10=-3(3,-3)...which should equal 6, but I'm not seeing 6...unless my maths is as bad as my java coding... :\  Reply With Quote

6. ## Re: Reading Java Code x=b(a,b)

Hint 1: the x = 10 part is just to throw you off. x is promptly given another value, that returned by the b method. So you can safely ignore that x was assigned 10 and assume that it was initially assigned nothing.
Hint 2: the b method and the b variable are *completely* unrelated. It's not -3(3, -3), but rather it's b(3, -3). Same for the a method and variable.  Reply With Quote

7. Member Join Date
Nov 2012
Location
Brisbane, Australia
Posts
8
Rep Power
0

## Re: Reading Java Code x=b(a,b)

Oh! So that's asking the difference between one and the other? Wow, now I feel stupid...I was trying to figure out why it was 0 when it should have been 6...

Thank you so much, now I just need to learn to pay attention!   Reply With Quote

8. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Re: Reading Java Code x=b(a,b)

So that's asking the difference between one and the other? Wow, now I feel stupid...
Don't feel stupid, but *do* feel annoyed. That question was given to you in a form that was designed to be difficult to read. The major lessons to be learnt from question 4 are not to use one letter variable names, nor reuse them for methods, and not to write methods which don't have a clear (and, imao, documented) intent.

I know some will claim that there's some pedagogical purpose to the question. But I think it makes as much sense as learning English Literature by doing crossword puzzles...  Reply With Quote

9. Member Join Date
Nov 2012
Location
Brisbane, Australia
Posts
8
Rep Power
0

## Re: Reading Java Code x=b(a,b)

Haha, thank you.

I'm fine with actually writing the code, it's reading through code that I haven't written that confuses me somewhat...nested for loops and all that jazz...it's driving me to drink. Luckily, after today it's on to Binary! :)  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•