0

The following is my adaptation of an AP Computer Science question. The book says it should print 00100123 I think it should print 0010012 But the code below actually prints 3132123

What is going on? Also it does not appear to have any stop condition?!

public class Mystery {

    public static void main(String[] args) {
        int n;
        n = 3;
        mystery(n);
    }

    public static void mystery(int n) {
        int k;
        for (k = 0; k < n; k++) {
            mystery(k);
            System.out.print(n);
        }
    }
}

The actual question reads: Consider the following method.

public void mystery (int n)
{
     int k;
     for (k=0 ; k < n ; k++)
     {
          mystery(k);
          System.out.print(n);
     }
}

What value is returned by the call mystery (3)?

I now understand that the loop forms the stop condition and I think I understand jhamon's comment "last instruction is to print n. n is 3, there is no way it prints only '0010012'" but I don't understand why the book says it should print 00100123.

As Batsheba says I have tried watching the variables while debugging but the book says it should print 00100123. Thanks very much jhamon for the corrected code, so from what I can see the book is wrong?

4
  • 1
    last instruction is to print n. nis 3, there is no way it prints only '0010012' Commented Apr 6, 2017 at 10:07
  • 3
    Also it does not appear to have any stop condition?! It has one: k<n in the for loop. Can you also post the question you are trying to solve? Commented Apr 6, 2017 at 10:07
  • 1
    k<n becomes the stop condition. In fact, the whole thing is obvious if you use a line by line debugger, or run the code logically in your head. Commented Apr 6, 2017 at 10:08
  • 1
    hint: the print is not at the right place, try to move it out of the loop Commented Apr 6, 2017 at 10:15

1 Answer 1

1

That program cannot print "0". It prints n, and if n is 0 it will never reach the System.out.print(n) line. That line is inside the for statment with k<n.

As others have pointed out in the comments, that condition in the for loop is also the stop condition. mystery will recursively call itself n times. However, in each of these calls, the parameter is smaller.
(For fun, try changing the condition to k <= n. Then it will not terminate, because the parameter is smaller or equal).

If you change the line to print(k), which is what was probably intended, it will indeed print "0010012". Here is an ideone link to show that.

Sign up to request clarification or add additional context in comments.

2 Comments

put the print outside the lop and you get the result given by the book. Here is a fork of your ideone http://ideone.com/1s1YYt
@jhamon Good find! BTW you should edit the comment. There's a ")" where there should be a "]", breaking the link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.