{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture12 - Q1 Consider the following code(15 marks public...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Q1) Consider the following code: (15 marks) public class D { public static void f (C c, int y) { System.out.println("Line #2 is " + c.getX()); c.setX(y); y++; System.out.println("Line #3 is " + c.getX()); c c = new C(); c.setX(y+2); System.out.println("Line #4 is " + c.getX()); c } public static void main (String[] args) { int z = 4; C c = new C(); c.setX(3); System.out.println("Line #1 is " + c.getX()); f(c, z); System.out.println("Line #5 is " + c.getX()); System.out.println("Line #6 is " + z); } } What would be printed if this program were run? public class C { private int x; public int getX(){ return x; } public void setX(int z){ x = z; } } x: 0 x: 0 Solution: Line#1: 3 Line#2: 3 Line#3: 4 Line#4: 7 Line#5: 4 Line#6: 4
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon