CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: Blue J code.

  1. #1
    Join Date
    May 2009
    Posts
    7

    Blue J code.

    Hi, I am a beginner programmer and am having some trouble with a few blocks of code. I can roughly work out the answers, but I can't really work through each line of code. I don't really know how each of these pieces of code works. If you could explain them that would be great cheers:

    public int someMethod(int a)
    {
    if(a>0){
    return a;
    }
    else if(a<0) {
    a = -a;
    return a;
    }
    else{
    a = 0;
    return 0;
    }
    }

    This method is used as followed in another class;

    int x = -4
    int y = someMethod(x);

    ???????????

    2) boolean b = true;
    int c = 0;
    for ( int a = 0; a < 4; a++ ) {
    if ( a / 2 == 0 ) {
    c = c + 1;
    b = false;
    }
    else {
    c = c - 1;
    b = true;
    }
    }

    What are the values of b and c? I got them as t and 0.

    Thanks for the help. I have a few more but if these could be explained that would be great.

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Blue J code.

    What's to explain? These code samples are short enough and simple enough that you can step through them by hand with various starting values and see for yourself what happens.

    If you want to be absolutely sure what happens, just run the code and see what the results are.

    If there is something you don't understand, or you are stuck on, please ask a specific question about it.

    This code and the questions you ask look very much like a homework assignment, which would be something you should do yourself, but even if they are not, you should be able to work out the expected results and check them by actually running the code yourself.

    Learning results from what the student does and thinks, and only from what the student does and thinks. The teacher can advance learning only by influencing the student to learn...
    H. Simon
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    May 2009
    Posts
    7

    Re: Blue J code.

    Its revision. For the first one, I don't understand how the end result is X = -4 and Y = 4?

    Is it because X is put in as int a, and thats all it does so it is still -4.

    Then for Y, a = -a;

    = -(- 4) = 4?

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Blue J code.

    Yes.

    'x' does not change value because Java passes by value and not by reference so any changes you make to the value in variable 'a' in the method have no effect on the value in the variable 'x'.

  5. #5
    Join Date
    May 2009
    Posts
    7

    Re: Blue J code.

    Cheers, second one I don't have a clue.

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Blue J code.

    Quote Originally Posted by OFWJ1967 View Post
    Cheers, second one I don't have a clue.
    Try stepping through it by hand with pencil and paper, noting down the values of the variables as they change.

    Consider the different parts of the code. You have a loop that runs 4 times, so that 'a' goes into it with values of 0, 1, 2, and 3. Inside the loop you have a part that runs if a/2 is 0 and a part that runs if it isn't. Just try each value of 'a' in turn and see what happens.

    Programs must be written for people to read, and only incidentally for machines to execute...
    H. Abelson and G. Sussman
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  7. #7
    Join Date
    May 2009
    Posts
    7

    Re: Blue J code.

    Ok thanks does C happen to go 1, 0 , -1 and final value 0?

  8. #8
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Blue J code.

    Quote Originally Posted by OFWJ1967 View Post
    Ok thanks does C happen to go 1, 0 , -1 and final value 0?
    You tell me - what happens when you run the code?

    If you don't know how to run the code, you must learn. Otherwise you'll be stepping through code by hand for ever.

    'c' starts with value 0. 'a' starts the loop with value 0. So a/2 = 0/2 which is 0. The 'if' in the loop says that if a/2 is 0, increment 'c' and set 'b' false. So the first time round the loop, 'c' becomes 1 and 'b' becomes false. The second time into the loop, 'a' is now 1. So a/2 would be 1/2 or 0.5, except that the values are all integers, so the fractional part is lost and a/2 still gives 0. You should be able to work out what happens to 'c' and 'b' from here on. It is not difficult, just step through with pencil and paper, noting what the values are each time round the loop.

    Give me a fish and I eat for a day. Teach me to fish and I eat for a lifetime...
    Chinese proverb
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  9. #9
    Join Date
    May 2009
    Posts
    7

    Re: Blue J code.

    Ha! I get it, thanks the 0.5 bit through me. The first two make C go up 1, and B false but after that it goes to the other part of the loop and B = true and C ends up as 0.

    Cheers, you have really helped me out. Might need some more later on.

  10. #10
    Join Date
    May 2009
    Posts
    7

    Re: Blue J code.

    int total = 0;
    for (int i=0; i < 3; i ++) {
    for (int j = 3; j < 5; j ++){
    total = total + j;
    }
    }

    Whats total..

    Rite so, for each time I is less than 3 and J is less than 5 we add J to total?

  11. #11
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Blue J code.

    Quote Originally Posted by OFWJ1967 View Post
    Rite so, for each time I is less than 3 and J is less than 5 we add J to total?
    Yes, that is actually true, but it's an odd way to look at it. Look at the code - it consists of a loop within a loop. The outer loop is controlled by 'i' and the inner loop by 'j'. The i < 3 and j < 5 are the loop termination conditions - the outer and inner loops will continue while i < 3 and j < 5 respectively. 'j' is added to the total every time through the inner loop.

    You only have to step through the loops a couple of times to see exactly what is going on.

    I cannot teach anybody anything, I can only make them think...
    Socrates
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  12. #12
    Join Date
    May 2009
    Posts
    7

    Re: Blue J code.

    Rite. I get the total as 7. Do you add the J to total and then put J up by one?

  13. #13
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Blue J code.

    Quote Originally Posted by OFWJ1967 View Post
    Do you add the J to total and then put J up by one?
    Me? no, I'm not doing the exercise. You might like to though...

    The important thing is to understand what the different parts of the code mean and how they work together. If you don't understand how 'for' loops work, find out.

    One must learn by doing the thing; for though you think you know it, you have no certainty, until you try...
    Sophocles
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured