Recursive Tracing Java: Learn How to Trace a Method

  • Context: Java 
  • Thread starter Thread starter Neon32
  • Start date Start date
  • Tags Tags
    Java tracing
Click For Summary

Discussion Overview

The discussion revolves around tracing a recursive method in Java, with participants exploring the concept of recursion, sharing their experiences with tracing, and clarifying misunderstandings about the method's structure. The scope includes technical explanations and conceptual clarifications related to programming and recursion.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Homework-related

Main Points Raised

  • Some participants question what is considered recursive in the method, with a focus on the call to 'mystery' and whether it is present.
  • One participant asserts that 'mystery(n-1)' is recursive, while another challenges this by stating that the function does not call itself directly.
  • Several participants share their attempts to trace the output of the method, with varying degrees of success and understanding.
  • There are discussions about the differences in behavior between Java and Visual Basic (VB), particularly regarding integer division and how it affects the method's logic.
  • A participant mentions the usefulness of debugging tools in Excel VBA compared to Java, suggesting that additional print statements might help in Java.
  • One participant expresses a desire to learn systematic methods for tracing recursive methods, indicating the context of an upcoming exam.
  • Another participant shares their approach of trial and error for tracing, acknowledging the challenges involved.

Areas of Agreement / Disagreement

Participants express differing views on what constitutes recursion in the method, and there is no consensus on the best approach to trace the method. The discussion remains unresolved regarding the clarity of the recursive nature of the method and the effectiveness of different tracing strategies.

Contextual Notes

Participants note limitations in their understanding of the method's behavior, particularly in relation to the differences between programming languages and the specific implementation details of the method.

Who May Find This Useful

Readers interested in programming, particularly those studying recursion in Java or comparing it with other languages like Visual Basic, may find this discussion relevant.

Neon32
Messages
68
Reaction score
1
It would be much appreciated if you could show me how to do recursive tracing for this method.
upload_2017-4-4_22-1-2.png
 
Last edited:
Technology news on Phys.org
What's recursive in this method ?
Did you try typing it in and see what it does ?
Is this homework ?
 
BvU said:
What's recursive in this method ?
Did you try typing it in and see what it does ?
Is this homework ?

1) mystery(n-1) is recursive
2) No it isn't homework. I'm studying for my exam
 
Neon32 said:
1) mystery(n-1) is recursive
I see. Maybe I misunderstand 'recursive' : I then expect a call to 'mystery' inside the method. Don't see one, so your idea of recursive is something else. Can you explain ?
Must be blind. Sorry.
 
All I've left is question 2 :smile:
BvU said:
Did you try typing it in and see what it does ?
 
BvU said:
All I've left is question 2 :smile:

Yes I've tried to trace it and all I got was (6 + ((4 + ((2 + . I don't understand how to continue it.
 
I get
Code:
 ----- 6 ---------- 
(6 + ((4 + ((2 + ( 0 
 + 1)
)
 + 3)
)
 + 5)
)
 --------------------------
when I input the number 6 in :

Code:
Sub mist(n As Integer)

    If n / 2 = 0 Then
    
        Debug.Print n
    ElseIf n Mod 2 = 0 Then
        Debug.Print "(" & n & " + ";
        mist n - 1
        Debug.Print ")"
    Else
        Debug.Print "(";
        mist n - 1
        Debug.Print " + " & n & ")"
    End If
    
End Sub

Sub test()
Dim n As Integer
again:
    n = CInt(InputBox("Give n "))
    Debug.Print "  "
    Debug.Print " ----- " & n & " ---------- "
    If n < 0 Then Exit Sub
    Call mist(n)
    Debug.Print " -------------------------- "
    GoTo again
End Sub
 
Neon32 said:
1) mystery(n-1) is recursive
No. The mystery() function is recursive because it calls itself.

Neon32 said:
Yes I've tried to trace it and all I got was (6 + ((4 + ((2 + . I don't understand how to continue it.
BvU didn't ask about tracing the function; he asked if you had tried typing it in and running it. Seeing what the function produces might be helpful in understanding what it does.
 
I am not fluent in Java. I find 1 / 2 is integer division and yields zero, so in the context of Myst "if N/2==0" is equivalent to " if n= 1" in VB.
If I take that into account the 0 goes away.
(and if I add some ; at the end of the print statements it all ends up on one line:
Code:
 ----- 6 ----------
(6 + ((4 + ((2 +  1 ) + 3)) + 5))
 --------------------------

Wonder why your routine doesn't render the odd numbers ...
 
  • #10
Excel VBA is easy with its debugger at hand.
Don't know what Java has for you. If no debugger, then perhaps some more print statements ?
 
  • #11
There's a systematic way to find out the tracing for any recursive method like trees method or in a table or whatever. I want to know the method you used to find out the tracing not the final answer. My exam is in paper so I won't have jcreator or VBA during the exam xD
 
  • #12
Didn't know that; was just trying to help. My systematic method has always been trial and error :rolleyes: -- especially the latter.
Good luck with your exam !
 
  • #13
BvU said:
Didn't know that; was just trying to help. My systematic method has always been trial and error :rolleyes: -- especially the latter.
Good luck with your exam !
Thank you :).
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
1
Views
2K
  • · Replies 39 ·
2
Replies
39
Views
8K
  • · Replies 8 ·
Replies
8
Views
4K