COP 3330 ObjectOriented Programming Summer 2011 EXAM 1
COP 3330: Object-Oriented Programming Summer 2011 EXAM #1 – In Class Practice Instructor : Dr. Mark Llewellyn markl@cs. ucf. edu HEC 236, 407 -823 -2790 http: //www. cs. ucf. edu/courses/cop 3330/sum 2011 Department of Electrical Engineering and Computer Science Division University of Central Florida COP 3330 : EXAM 1 REVIEW Page 1 © Dr. Mark Llewellyn
public class Example 1 { public static void main(String args[]){ C 1 o 1, o 2; C 1. y = 10; C 1. x = 10; C 1. print. Y(); Static vs. Class Variables and C 1. set. X(10); Methods – In-Class Practice o 1 = new C 1(); Problem o 2 = new C 1(); o 1. x = 2; o 2. x = 3; First – identify which statements o 1. y = 4; are legal and which are illegal. C 1. y = 6; o 1. set. X(7); o 2. set. X(8); Second – fix the illegal statements. C 1. print. Y(); o 2. print. Y(); Third – trace the program } execution and show the output. } class C 1 { public int x; public static int y = 5; public C 1() { x = 1; } public void set. X(int val) {x = val; } public static void print. Y() {System. out. println("y: " + y); } } COP 3330 : EXAM 1 REVIEW Page 2 © Dr. Mark Llewellyn
public class Example 1 { public static void main(String args[]){ C 1 o 1, o 2; C 1. y = 10; C 1. x = 10; //cannot make a static reference to a non-static field C 1. print. Y(); //C 1. set. X(10); //cannot make a static reference to a non-static method o 1 = new C 1(); o 2 = new C 1(); o 1. x = 2; o 2. x = 3; o 1. y = 4; //static variable – should be accessed thru the class C 1. y = 6; o 1. set. X(7); o 2. set. X(8); C 1. print. Y(); o 2. print. Y(); // static method should be accessed thru the class } } class C 1 { public int x; public static int y = 5; public C 1() { x = 1; } public void set. X(int val) {x = val; } public static void print. Y() {System. out. println("y: " + y); } } COP 3330 : EXAM 1 REVIEW Page 3 © Dr. Mark Llewellyn
public class Example 1 { public static void main(String args[]){ C 1 o 1, o 2; The Fix o 1 = new C 1(); o 2 = new C 1(); C 1. y = 10; o 1. x = 10; //make reference to instance variable thru an instance C 1. print. Y(); o 2. set. X(10); //invoke instance method thru an instance o 1. x = 2; o 2. x = 3; C 1. y = 4; //static variable referenced/accessed thru the class C 1. y = 6; o 1. set. X(7); o 2. set. X(8); C 1. print. Y(); // static method invoked thru the class } } class C 1 public public } { int x; static int y = 5; C 1() { x = 1; } void set. X(int val) {x = val; } static void print. Y() {System. out. println("y: " + y); } COP 3330 : EXAM 1 REVIEW Page 4 © Dr. Mark Llewellyn
Execution COP 3330 : EXAM 1 REVIEW Page 5 © Dr. Mark Llewellyn
Parameter Passing Example COP 3330 : EXAM 1 REVIEW Page 6 © Dr. Mark Llewellyn
Parameter Passing Example Step 1 - Execution begins in Main in main before invocation of ch. Values i 1 n 1 ival 3 COP 3330 : EXAM 1 REVIEW Page 7 n 2 n 3 ival 5 ival 7 © Dr. Mark Llewellyn
Step 2 – Invoke Method chvalues(i, n 1. ival, n 2, n 3) in main i 1 in ch. Values x 1 actual parameters n 1 n 2 n 3 ival 5 ival 7 formal parameters y 3 w z Red dotted lines indicate the passing of parameter values. Notice that formal parameters x and y are primitive types and thus copies of the actual parameters are passed (pass by value) while w and z are objects and thus references to the objects are passed (pass by reference). COP 3330 : EXAM 1 REVIEW Page 8 © Dr. Mark Llewellyn
Step 3 – Method ch. Values Executes in main i 1 in ch. Values x 10 = actual parameters n 1 n 2 n 3 ival 5 ival 79 = formal parameters y 34 = w z ival 8 COP 3330 : EXAM 1 REVIEW Page 9 © Dr. Mark Llewellyn
Step 4 – Return to Main Method – Print Results in main i 1 in ch. Values x 10 = actual parameters n 1 n 2 n 3 ival 5 ival 79 = formal parameters y 34 = w z ival 8 Output: 1 – 3 – 5 – 9 COP 3330 : EXAM 1 REVIEW Page 10 © Dr. Mark Llewellyn
COP 3330 : EXAM 1 REVIEW Page 11 © Dr. Mark Llewellyn
- Slides: 11