Recursive Tracing Java: Learn How to Trace a Method

In summary, the conversation discussed recursive tracing for a method, with a particular focus on the mystery() function. The conversation also included questions about whether the method was recursive, whether the person had tried typing it in and running it, and whether it was for homework. It also touched on the topic of tracing recursive methods in general and the speaker's approach to finding the tracing. The conversation concluded with well wishes for the person's upcoming exam.
  • #1
Neon32
68
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
  • #2
What's recursive in this method ?
Did you try typing it in and see what it does ?
Is this homework ?
 
  • #3
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
 
  • #4
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.
 
  • #5
All I've left is question 2 :smile:
BvU said:
Did you try typing it in and see what it does ?
 
  • #6
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.
 
  • #7
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
 
  • #8
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.
 
  • #9
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 :).
 

FAQ: Recursive Tracing Java: Learn How to Trace a Method

What is recursive tracing in Java?

Recursive tracing in Java refers to the process of tracing the execution of a recursive method, which is a method that calls itself in order to solve a problem. It involves keeping track of the values of variables and the order in which the method calls itself, in order to understand how the method is working and identify any errors or inefficiencies.

Why is recursive tracing important?

Recursive tracing is important in order to understand how a recursive method is functioning and to identify any potential issues or bugs. It also allows for a deeper understanding of the logic behind the method and can help with troubleshooting and optimizing the code.

How do you perform recursive tracing in Java?

To perform recursive tracing in Java, you will need to have a basic understanding of how recursive methods work and be familiar with concepts such as base cases and recursive calls. Then, you can use techniques such as printing out the values of variables and keeping track of the order of method calls to trace the execution of the recursive method.

What are some common mistakes when performing recursive tracing in Java?

One common mistake when performing recursive tracing in Java is not properly identifying and handling base cases, which can lead to an infinite loop. It is also important to keep track of the order of method calls to ensure that the recursive calls are being made in the correct order.

How can recursive tracing help with debugging and optimizing code?

Recursive tracing can help with debugging and optimizing code by allowing you to see exactly how the recursive method is functioning and where any errors or inefficiencies may occur. By tracing the execution, you can identify and fix any issues, as well as potentially improve the efficiency of the code by finding ways to reduce the number of recursive calls or improve the logic of the method.

Similar threads

Replies
5
Views
2K
Replies
1
Views
2K
Replies
2
Views
1K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
5
Views
1K
Back
Top