Please Execute manually! Show all the necesary steps both in the memory and the
ID: 3834264 • Letter: P
Question
Please Execute manually! Show all the necesary steps both in the memory and the output.
Sample Question 5.3: Trace execution of this program, beginning with main, showing contents of memory and output in the space provided:
public class Box { private int x, y, w, h;
private static int maxWidth = 6;
public Box(int xIn, int yIn) { if ( xIn > maxWidth ) {
x = maxWidth;
} else {
x = xIn;
} y=yIn; w=0; h=3; }
public void grow(int a) {
w+= a; h += a; }
public void fight( Box other ) {
x=other.y+5; other.x = 7; }
public int cross(){
return x * y; }
public String toString(){
return "[(" + x + "," + y + ") " + w + " " + h + "]"; }
public static void main(String[] args) {
Box a = new Box(12, 5);
Box b = new Box(2, 7);
System.out.println("new boxes: " + a + " " + b);
a.grow(3);
b.grow(2);
System.out.println("after grow: " + a + " " + b);
b.fight(a);
System.out.println("after fight: " + a + " " + b);
System.out.println("cross: " + a.cross() );
}
}
Static Variables
Display
Memory
Sample Question 5.3: Trace execution of this program, beginning with main, showing contents of memory and output in the space provided:
public class Box { private int x, y, w, h;
private static int maxWidth = 6;
public Box(int xIn, int yIn) { if ( xIn > maxWidth ) {
x = maxWidth;
} else {
x = xIn;
} y=yIn; w=0; h=3; }
public void grow(int a) {
w+= a; h += a; }
public void fight( Box other ) {
x=other.y+5; other.x = 7; }
public int cross(){
return x * y; }
public String toString(){
return "[(" + x + "," + y + ") " + w + " " + h + "]"; }
public static void main(String[] args) {
Box a = new Box(12, 5);
Box b = new Box(2, 7);
System.out.println("new boxes: " + a + " " + b);
a.grow(3);
b.grow(2);
System.out.println("after grow: " + a + " " + b);
b.fight(a);
System.out.println("after fight: " + a + " " + b);
Explanation / Answer
Static Variables:
maxWidth
Memory:
Box a
Box b
output:
new boxes: [(6,5) 0 3] [(2,7) 0 3]
after grow: [(6,5) 3 6] [(2,7) 2 5]
after fight: [(7,5) 3 6] [(10,7) 2 5]
cross: 35
public static void main(String[] args) {
Box a = new Box(12, 5);// (6,5) 0 3
Box b = new Box(2, 7); // (2,7) 0 3
System.out.println("new boxes: " + a + " " + b);//new boxes: [(6,5) 0 3] [(2,7) 0 3]
a.grow(3);
b.grow(2);
System.out.println("after grow: " + a + " " + b); // after grow: [(6,5) 3 6] [(2,7) 2 5]
b.fight(a);
System.out.println("after fight: " + a + " " + b); //after fight: [(7,5) 3 6] [(10,7) 2 5]
System.out.println("cross: " + a.cross() ); //cross: 35cross: 35
}