1. What is the output of the following program? 2. Draw a state diagram that sho
ID: 3592311 • Letter: 1
Question
1. What is the output of the following program?
2. Draw a state diagram that shows the state of the program just before the end of main. Include all local variables and the objects they refer to.
3. At the end of main, are p1 and p2 aliased? Why or why not?
public static void printPoint(Point p) {
System.out.println("(" + p.x + ", " + p.y + ")"); }
public static Point findCenter(Rectangle box) {
int x = box.x + box.width/2;
int y = box.y + box.height/2;
return new Point(x, y); }
public static void main(String[] args) {
Rectangle box1 = new Rectangle(2, 4, 7, 9);
Point p1 = findCenter(box1);
printPoint(p1);
box1.grow(1, 1);
Point p2 = findCenter(box1);
printPoint(p2); }
Explanation / Answer
The output:
(5,8)
(5,8)
p1 and p2 are not aliased because they are pointing to different objects
State of the program:
p1(5,8)
p2(5,8)
Rectangle - 1,3,9-width,11-height
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.