Python Tutor (Java visualisation)
This tool is going to be useful to you when we start getting into classes and instances.
The tool was originally created for Python, hence the name Python Tutor.
However, it has been extended to Java.
You are encouraged to play around with it when we start exploring new concepts.
This guide is to help you get started, as you might need to tweak the code you give it in order to get it to work.
Running the Java visualiser with your own code snippets
Go to the Python Tutor for Java website.
You should see something like this:
Scroll down until you see the “Write code in” section, and make sure that “Java” is selected:
Let’s say we want to visualise the code provided in our Pokemon example from the OOP Thought Process page:
Copy the entire code from our Main.java
file:
Now paste it into Python Tutor, but make sure you add public
in front of the class name:
Go back to our Repl, click on the “Show Files” button, and copy the code from the Pokemon.java
file:
Since we are pasting all out code into the same section, Python Tutor will complain if we don’t have exactly one public
class.
We therefore need to remove the public
keyword from the Pokemon
class when we paste it into Python Tutor, at the end of where we pasted the Main
class:
Now scroll to the bottom of the page and click on the “Visualize Execution” button:
If all goes well, you should see something like this:
You can now click on the control buttons to step through the code, and see how everything changes:
There is also a “Get AI Help” button, which will try and help you with your code:
What is does is generate the prompt that you can then copy-paste into ChatGPT to get an explanation or ask further questions.
Try and follow the same approach when using other code that you want to visualise.
The main thing to remember is that you can only have one public
class.