Part A (4 points). What does this code draw? public static void main Stringi arg
ID: 3731248 • Letter: P
Question
Part A (4 points). What does this code draw? public static void main Stringi args World world1new World (200, 200); Turtle maria-new Turtle (100, 100, worlál) Turtle jose new Turtle (25, 50, world) Turtle sarajose; maria.forward (50) maria = Jose; maria.turnRight) maria.forward (50) jose.forward (50) jose- new Turtle (, 100, world1) maria.turnRigh maria.forward (50) Draw the result on the answer sheet in the box showing the World. Include both the paths the Turtles trace as well as the final positions of ALL the Turtles in the world (even if they didn't move You may draw all of the Turtles/paths in the same color . You may draw the turtles as circles and do not have to indicate their direction You do NOT have to indicate which variables reference which Turtles (though this information can help you in parts B, C, and D, so you may include it if you wish) Notice that the world is 200x200 and that the halfway marks both vertically and horizontally are indicated to help you. You will find a memory model helpful for the next 3 questions, but for part A we will grade only the path and positions of the Turtles. We will not grade your memory model directly Part B (1 point). In the program in Part A, how many Turtle objects are created? Part C (1 point). In the program in Part A, how many references to Turtle objects are created? Part D (2 points). At the end of the program in Part A, how many references point to the Turtle whose initial position was 100, 100 (the first Turtle created in main)?Explanation / Answer
An Object is created with the keyword "new". Now, for all new Turtle(x ,y, z) an object get created, which means the values are stored in the heap memory of JVM. Where as maria,jose and sara are reference variables of the Object Turtle. They are stored in the stack memory of JVM.
To answer your Questions :
B. Three Turtle Objects have been created since there are three new keywords are present.
new Turtle(100, 100, world1)
new Turtle(25, 50, world1)
new Turtle(0, 100, world1)
C. Three Turtle reference variables have been created
Turtle maria;
Turtle sara;
Turtle Jose;
D. The Turtle created at the start positioned at 100,100 was referenced by maria initially. At 6th line of main program maria started referring jose which is poiniting to the second Turtle created. Hence the reference has been lost for the first Turtle and no other reference variable pointed to that turtle. Hence, at the end of Part A Zero references are pointing to the initial turtle that was created at 100,100.