Java Visualizer Learning how to trace a program (i.e., run the code in your mind) is an important skill. In this activity, you will learn how to draw diagrams that represent the state of a program. Manager: Recorder: Presenter: Reflector: Content Learning Objectives After completing this activity, students should be able to: • Explain how using well defined roles can improve a team’s success. • Draw memory diagrams with multiple variables and multiple methods. • Describe what it means for a variable to store a reference to an object. Process Skill Goals During the activity, students should make progress toward: • Tracing the execution of code to determine the contents of memory. (Critical Thinking) Copyright © 2021 Chris Mayfield. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Meta Activity: Team Disruptions Common disruptions to learning in teams include: talking about topics that are off-task, team- mates answering questions on their own, entire teams working alone, limited or no commu- nication between teammates, arguing or being disrespectful, rushing to complete the activity, not being an active teammate, not coming to a consensus about an answer, writing incomplete answers or explanations, ignoring ideas from one or more teammates. Questions (10 min) Start time: 1. Pick four of the disruptions listed above. For each one, find something from the role cards that could help improve the team’s success. Use a different role for each disruption. a) Manager: b) Presenter: c) Recorder: d) Reflector: Dilbert by Scott Adams. © Andrews McMeel Syndication. http://dilbert.com/strip/2010-08-15 Model 1 Local Variables Consider the following example. The memory diagram shows the state of the program just before printResult returns for the second time: public static void printResult(int qty, double amt) { System.out.printf("%d for $%.2f\n", qty, amt); } public static void main(String[] args) { int count = 3; double price = 9.99; char grade = 'A'; boolean okay = true; printResult(count, price); count++; price *= 2; okay = !okay; printResult(count, price); } The output of the program is: 3 for $9.99 4 for $19.98 4count 19.98price 'A'grade falseokay main 4qty 19.98amt printResult Questions (15 min) Start time: 2. How many variables are declared . . . a) in main? b) in printResult? 3. How many times is each variable assigned? a) count b) price c) grade d) okay e) qty f) amt 4. Is there a small box for each declaration or each assignment? Justify your answer. 5. What do the six small boxes in the memory diagram represent? 6. What do the two large boxes in the memory diagram represent? 7. Why does the diagram indicate that count is 4 and price is 19.98, even though the source code says that count = 3 and price = 9.99? 8. Based on the source code: a) Which method is defined first? b) Which method is executed first? 9. Copy the contents of LocalVariables.java into Java Visualizer. Click the “Visualize execution” button, and then click “Forward >” multiple times to see the code run. a) What does the diagram look like on Step 11 of 19, just before count++ executes? b) Why is there no frame for the printResult method on Step 11 of 19? c) Run the program to Step 17 of 19, just before printResult returns for the second time. What differences do you notice between the diagram on the previous page and the one on Java Visualizer? Model 2 Reference Types Java has eight primitive types (byte, short, int, long, float, double, boolean, char). All other types of data (including String and Scanner) are reference types. int count; double price; String name; Scanner in; count = 0; price = 1.99; name = "Beyonce"; in = new Scanner(System.in); Notice the difference between a variable and an object. For reference types, each variable stores the memory address of an object. (Memory addresses are integers generated by the operating system.) When drawing diagrams, we use arrows to represent memory addresses. Questions (20 min) Start time: 10. In the example above, which variables are . . . a) reference types? b) primitive types? 11. Notice the convention Java uses for type names like int and String. a) Are reference type names all lowercase or capitalized? b) Are primitive type names all lowercase or capitalized? 12. Variables in Java can use at most eight bytes of memory. Explain why the values "Beyonce" and System.in cannot be stored directly in the memory locations for name and in. 13. What value is stored in the memory for the variable count? What value is stored in the memory for the variable price? 14. What value is stored in the memory for the variable name? What value is stored in the memory for the variable in? 15. Carefully explain what it means to assign one variable to another. For example, what does the statement price = count; do in terms of memory? 16. Draw a memory diagram for the following code. Make sure your answer is consistent with what you wrote for #15. int width; double score; Scanner input; String first; String other; width = 20; score = 0.94; input = new Scanner(System.in); first = "Taylor"; score = width; other = first; 17. What is the output of the following statements after running the code above? Explain your answer using the diagram. first = "Swift"; System.out.println(other); 18. Paste the contents of TaylorSwift.java into Java Visualizer. What differences do you notice between the diagram in Java Visualizer and yours from #16?