Question 4 8 marks a complete the output when the

This preview shows page 8 - 15 out of 28 pages.

Question 4 (8 marks) a) Complete the output when the following start() method is executed. public void start() { Point p1 = new Point(5, 9); Point p2 = new Point(p1.y, p1.x); p1.move(2, 3); p2.translate(2, 3); System.out.println("1: " + p1.x + ", " + p1.y); System.out.println("2: " + p2.x + ", " + p2.y); } 1: 2, 3 2: 11, 8 (2 marks) b) Complete the following section of code which assigns the centre point of the Rectangle , r , to the Point variable, midPoint ; Rectangle r = new Rectangle( .... ); int midRectX = r.x + r.width / 2; int midRectY = r.y + r.height/ 2; Point midPoint = new Point(midRectX, midRectY); (2 marks)
Image of page 8

Subscribe to view the full document.

Question/Answer Sheet - 30 - CompSci 101 ID: ........................................................... c) Give the output when the following start() method is executed. public void start() { Point pt = new Point(5, 9); fiddle(pt); System.out.println("1: " + pt.x + ", " + pt.y); } private void fiddle(Point p1) { p1 = new Point(p1.y, p1.x); Point p2 = p1; p2.y = 6; System.out.println("2: " + p1.x + ", " + p1.y); } 2: 9, 6 1: 5, 9 (2 marks) d) Give the output when the following start() method is executed. public void start() { Point pt = new Point(3, 7); fiddle(pt); System.out.println("1: " + pt.x + ", " + pt.y); } private void fiddle(Point p1) { Point p2 = new Point(p1); p2.x = p2.x + 2; System.out.println("2: " + p1.x + ", " + p2.x); } 2: 3, 5 1: 3, 7 (2 marks)
Image of page 9
Question/Answer Sheet - 30 - CompSci 101 ID: ........................................................... Question 5 (6 marks) Give the output when the following program is executed. public class MyProgram { public void start() { int a = method2('Z'); System.out.println("1. " + a); String s = method1("ABCD", 5); System.out.println("2. " + s); } private String method1(String a, int b) { char letter = a.charAt(0); if (b > 4) { letter = a.charAt(1); } int d = method2(letter); System.out.println("3. " + d); return d + a; } private int method2(char c) { if (c == 'A') { return 1; } if (c == 'B') { return 2; } System.out.println("4. " + c); return -1; } }
Image of page 10

Subscribe to view the full document.