Results 1 to 9 of 9
Like Tree2Likes
  • 2 Post By pbrockway2

Thread: Reading Java Code x=b(a,b)

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

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

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

    Default 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...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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?

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

    Default 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... :\

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

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

    Default 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!

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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...
    sezzy and Fubarable like this.

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

    Default 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! :)

Similar Threads

  1. Replies: 16
    Last Post: 01-31-2012, 08:36 PM
  2. Replies: 6
    Last Post: 01-19-2012, 05:16 PM
  3. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  4. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  5. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM

Posting Permissions

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