Results 1 to 9 of 9
Thread: Reading Java Code x=b(a,b)
- 11-25-2012, 11:49 PM #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
-
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.
- 11-25-2012, 11:57 PM #3
Member
- Join Date
- Nov 2012
- Location
- Brisbane, Australia
- Posts
- 8
- Rep Power
- 0
Re: Reading Java Code x=b(a,b)
Sorry,
I'm not allowed to calculate it using a computer, I must calculate manually...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); } }
-
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:
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.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); } }
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?
- 11-26-2012, 12:37 AM #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... :\
-
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.
- 11-26-2012, 12:44 AM #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!
- 11-26-2012, 01:12 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Reading Java Code x=b(a,b)
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.So that's asking the difference between one and the other? Wow, now I feel stupid...
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...
- 11-26-2012, 02:00 AM #9
Member
- Join Date
- Nov 2012
- Location
- Brisbane, Australia
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
I want the source code of DUK's Bank , the example code in Java EE 5 Tutorial 2010
By zahra in forum New To JavaReplies: 16Last Post: 01-31-2012, 08:36 PM -
Is reading HTML code and printing on JSP pratical for web development with Java?
By KingdomX in forum New To JavaReplies: 6Last Post: 01-19-2012, 05:16 PM -
C server code - Java CLient Code _ TCP Connection Problem
By rmd22 in forum NetworkingReplies: 0Last Post: 02-21-2011, 11:50 AM -
Convert java code to midlet code
By coldvoice05 in forum New To JavaReplies: 1Last Post: 08-12-2009, 11:14 AM -
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 01:21 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks