Results 1 to 9 of 9
  1. #1
    bs3ac is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default Exercise problem, how was this output achieved?

    Hi guys,

    I am reading through a book on Java where they have asked us to match the correct output from a list code inputs. The book is 'Head first Java' and after writing the code I see the answers they have given are correct but I cannot work out how the code resulted in those answers and was wondering if anyone could give me a few hints...



    Thanks for you time!

    Bs3ac

  2. #2
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    Well, We might be able too.

    One Problem.

    Where is your code?

    Please Post it!

    Otherwise

    Your SOL! :)
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    The easiest way on something like this is to step through the code by hand, using pencil and paper to keep track of x and y...just make sure you loop the correct number of times (and break at the correct point of the inner loop).

  4. #4
    bs3ac is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default

    Hi guys,

    Tolls I completley agree with you and that is how I have come about to try and understand how this code works but the bit that gets me is the two 'For' loops which uses the int inner and int outer even though there are no variables set with those values. Perhaps you could explain to me how these two for loops work in terms of the inner and outer values?

    FAO:Sno, that is the whole code. That was a picture taken straight from the book. I have simply typed all that code in my java console to create the class to try and gain an understanding in how the code operates by modifying it to see its effect on the X and Y outcome...
    Last edited by bs3ac; 08-05-2010 at 11:41 PM. Reason: typo

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    I have simply typed all that code in my java console to create the class
    Did you try to compile it and execute it?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    You do know how for loops work don't you?
    The first part (int outer = 0) is declaring and setting the variable...

  7. #7
    bs3ac is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default

    Norm: yes it compiles and runs fine and gives the expected results as those in the solutions part in the picture.

    Tolls: Yes I know how loops work, the bit that was bugging me however was the 'inner' amd 'outer' part (which I think I have found the solution).

    Am I right in saying that the first 'for' loop is only run twice as its '< 3' where the vairables starts at 0 but added +1 each time.

    I am after the 'x' and 'y' outcome but the inner and outer for loop was bugging me as I seem to get diffrent answers than those stated... maybe I am just going through the program wrong...

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    To see how the variables change, add a println() to display their values as it loops:

    System.out.println("outer=" + outer + ", inner=" + inner); // show values of variables

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    For the outer loop:
    First loop: outer = 0.
    Second loop: outer = 1.
    Third loop: outer = 2.
    exit.

Similar Threads

  1. problem with output of code
    By amosexy in forum New To Java
    Replies: 15
    Last Post: 07-05-2010, 06:57 PM
  2. xml output problem
    By tsili in forum XML
    Replies: 2
    Last Post: 05-24-2010, 12:27 AM
  3. problem with output of jar file
    By nishant.4545 in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2009, 04:00 PM
  4. Output Problem
    By jazz2k8 in forum New To Java
    Replies: 4
    Last Post: 05-26-2008, 10:59 AM
  5. Output problem
    By jvasilj1 in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 06:39 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
  •