aspose file tools*
The moose likes Beginning Java and the fly likes Trace this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trace this code" Watch "Trace this code" New topic
Author

Trace this code

D. Jones
Greenhorn

Joined: Mar 06, 2013
Posts: 3
Would someone please help me trace this code?



It's a sample exam problem and I'm having problems following what it's doing. I know when I enter a "-2" or a "0" it prompts me to enter another value and when I enter "3" it returns a "3". But I can't figure out why. It'd be great if you could take "-2" and "3" and walk me through what is performed.
Michael Novello
Greenhorn

Joined: Mar 03, 2013
Posts: 9
Do you understand while loops? Saying 'while ( n < i || n > j )' means 'while n is less than i OR n is greater than j'. The while loop will keep looping until both of these conditions are false (until 'n' is between 1 and 3). Every time it loops, the next user input, 'n' in this case, is again compared to 'i' and 'j'.

It's hard to tell exactly what you're asking, because it depends on how much you already know. If you know nothing, then you'll have to do some homework on your own, because for example, there is already a LOT to explain to someone who doesn't even know what a variable is.
D. Jones
Greenhorn

Joined: Mar 06, 2013
Posts: 3
I know what a variable is. And I pretty much know how while loops work. I guess I'm looking for a walk through of what happens when "-2" and "3" are entered and how they interact within the private and public method.
Wayan Saryada
Ranch Hand

Joined: Feb 05, 2004
Posts: 105

My suggestion is. Take a pencil and paper, create a table that have a columns consist of Steps, n, i, j, loop condition check, result. Then for each step write the value of these variable (n, i, j, loop condition check, result). By doing this you can see how the variable changes in step-by-step, until you get the result.


Website: Learn Java by Examples
D. Jones
Greenhorn

Joined: Mar 06, 2013
Posts: 3
Right. That's what I'm trying to do, but I'm not getting the right answer. I'm doing something fundamentally wrong. That's why I was hoping someone could kindly walk me through for two values (-2 & 3)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

D. Jones wrote:Right. That's what I'm trying to do, but I'm not getting the right answer. I'm doing something fundamentally wrong. That's why I was hoping someone could kindly walk me through for two values (-2 & 3)


Why don't you post what you think is happening, and then somebody here can give you a hint as to where your mistake lies.

Or add a bunch of println() statements that correspond to what you're tracking in your manual walk-through, and see where the printout of what's actually happening differs from your predictions.
James X Peterson
Whizlabs Java Support
Ranch Hand

Joined: Feb 26, 2013
Posts: 158
Hi D. Jones,

Please post your analysis.

Regards,
James
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trace this code