Question
Problem 5: Memory management and the ArrayBag class 12 points total; individual-only you will draw a series of memory diagrams that illustrate the execution of
Problem 5: Memory management and the ArrayBag class
12 points total; individual-only
you will draw a series of memory diagrams that illustrate the execution of a program that operates on objects of the ArrayBag class from lecture.
Consider the following Java program:
public class ArrayBagTest {
public static void main(String[] args) {
ArrayBag b1 = new ArrayBag(3);
ArrayBag b2 = new ArrayBag(3);
ArrayBag b3 = b2;
// part 1: what do things look like when we get here?
// part 2: what do things look like at the start of
// this method call?
b1.add("hello");
b2.add("world");
b3.add("hello");
// part 3: what do things look like when we get here?
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
}
}
1. In the appropriate section of ps3_partI, we have given you the beginnings of a memory diagram. On the stack (the region of memory where local variables are stored), we have included a frame for the main method. On the heap (the region of memory where objects are stored), we have included the ArrayBag object to which the variable b1 refers and its items array. As usual, we have used arrows to represent the necessary references.
Complete the provided memory diagram so that it shows what things look like in memory just before the call b1.add("hello").
To do so, you should:
Click on the diagram and then click the Edit link that appears below the diagram.
Make whatever changes are needed to the diagram. Below the thick horizontal line, we have given you a set of extra components that you can use as needed by dragging them above the thick line and putting them in the correct position. These components include String objects representing the strings "hello" and "world". You may not need all of the provided components.
You can also edit any of the values in a field or array by clicking on the corresponding cell and editing the text that is inside the cell.
Once you have made all of the necessary changes, click the Save & Close button.
2. In the appropriate section of ps3_partI, we have given you the beginnings of a memory diagram that you should complete to show what things look like in memory at the start of the execution of b1.add("hello")just before the first statement of that method is executed.
Note: To save time, it should be possible to select and copy some of the components of your diagram for 3-1 and paste them into your diagram for 3-2.
3. In the appropriate section of ps3_partI, we have given you the beginnings of a memory diagram that you should complete to show what things look like in memory after all of the calls to add have returnedjust before the print statements execute in main().
Note: We have included a method frame for add, but it may or not be needed in this diagram; you should delete it if you determine that it would no longer be present in memory.
4. What would be printed by this program? You may find it helpful to consult the toString method of the ArrayBag class.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started