





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Example # 1 (Static Methods) Draw a memory map for the following program at the point in the program execution indicated by the comment /HERE/*. public class Driver { public static int sumOfSquares(int x, int y) { int answer; answer = x * x + y * y; / HERE */ return answer; } public static void main(String[] args) { int a = 3, b = 4, result; result = sumOfSquares(a, b); System.out.println("Answer: " + result); } } Stack Heap Static Area answer 25 y 4 x 3 result b 4 a 3 length 0
Example # 3 (Methods/Objects/this Reference) Draw a memory map for the following program at the point in the program execution indicated by the comment /HERE/*. It is the same code as the previous example, but / HERE / is in a different position. public class Person { public static double MINIMUM_SALARY = 1000.00; private String name; private double salary; private StringBuffer calls; public Person(String name, double salary) { this.name = name; this.salary = salary; calls = new StringBuffer("Calls: "); } public Person(String name) { this(name, MINIMUM_SALARY); } public Person increaseSalary(double delta) { salary += delta; / HERE / / Returning reference to current object */ return this; } public void addCall(String newCall) { calls.append(newCall); } public String toString() { return name + ", $" + salary + ", " + calls; } } public class Driver { public static void task(Person friend, double salary) { double newSal = salary + 100; Person p = friend; p.increaseSalary(newSal); friend.addCall("toschool"); friend = null; } public static void main(String[] args) { double pay = 4000; Person laura = new Person("Laura", 2000); task(laura, pay); System.out.println(laura); } } Stack Heap name salary 6100 calls Static Area MINIMUM_SALARY
delta 4100 this p newSal 4100 salary 4000 friend laura pay 4000 length 0
Laura
Example # 4 (One Dimensional Array of Primitives) Draw a memory map for the following program at the point in the program execution indicated by the comment /HERE/*. public class Driver { public static void increaseBy(int[] data, int delta) { for (int i = 0; i < data.length; i++) { data[i] += delta; } / HERE */ } public static void main(String[] args) { int[] src = { 10, 30, 40 }; increaseBy(src, 5); for (int i = 0; i < src.length; i++) { System.out.println(src[i]); } } } Stack Heap 15 35 45 length 3 Static Area delta 5 data src main^ length^0
Example #6 (Two Dimensional Arrays) Draw a memory map for the following program at the point in the program execution indicated by the comment /HERE/*. } } public class Driver { public static void decrease(int[][] allScores, int delta, Cat[][] allCats) { for (int row = 0; row < allScores.length; row++) { for (int col = 0; col < allScores[row].length; col++) { allScores[row][col] - = delta; } } allCats[1][2].decreaseLives(); / HERE */ } public static void main(String[] args) { int[][] scores = { { 90, 85 }, { 10, 30, 40 } }; Cat[][] cats = new Cat[2][]; cats[0] = new Cat[2]; cats[1] = new Cat[3]; cats[0][1] = new Cat("Garfield"); cats[1][2] = new Cat("Fluffy"); decrease(scores, 5, cats); String answer = "Scores:\n"; for (int i = 0; i < scores.length; i++) { answer += Arrays.toString(scores[i]) + "\n"; } answer += "\nCats:\n"; for (int row = 0; row < cats.length; row++) { for (int col = 0; col < cats[row].length; col++) { if (cats[row][col] != null) { answer += cats[row][col] + "\n"; } } } System.out.println(answer); } } public class Cat { private String name; private int lives; public Cat(String name) { this.name = name; this.lives = 7; } public void decreaseLives() { lives--; } public String toString() { return "Cat [name=" + name + ", lives=" + lives + "]";
Stack Heap length 3 length 2 null null length 2 name lives 6 null (^) name lives 7 length 2 length (^3) 5 25 35 length 2 85 80 Static Area allCats delta 5 allScores cats scores
length 0 Garfield Fluffy