CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 11: Nov 1-5, 2010 Aditya Mathur Department of Computer Science Purdue University West Lafayette, IN, USA 1.Q&A 2.Class and instance variables 3.private, public, static 11/1 This Week: 11/3 1.Methods and parameters 2.Inheritance 3.Thread class 4.Concurrency: Task decomposition Data decomposition
Readings and Exercises for Week 11 Readings: Method and data hiding: 7.2, 7.3 Class: 8.2, 8.3 Inheritance: 10.2, 10.3 Exercises: 7.1, 7.2, 7.3, 7.4, 7.5, 8.1, 8.2, /5/2010 ©Aditya Mathur. CS 180. Fall Week 11 2
Special Sessions Lab Help: Thursday November 4, 5:30-7:00pm Monday November 11, 5:30-7pm LWSN B158 Project 4 Due: Monday November 15, 2010 Special Class: Sunday November 7, 5-7pm LWSN 3102AB 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 3
Lunch meeting When: Thursday November 4, Noon-1:30pm Where: Ford Dining Hall Meet: Upstairs in the separate dining room Attendees: Please sign up with your recitation instructor or me by Wednesday November 3 Look forward to seeing you! 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 4
Feedback: I liked Android labs. 11/5/2010 (a)A lot (b)Somewhat (c)Not at all ©Aditya Mathur. CS 180. Fall Week 11 5
Feedback: Lab instructors are helpful. 11/5/2010 (a)A lot (b)Somewhat (c)Not at all ©Aditya Mathur. CS 180. Fall Week 11 6
Feedback: So far I am liking the course (10 liking a lot, 1 not liking at all). 11/5/2010 (a)8-10 (b)4-7 (c)1-3 ©Aditya Mathur. CS 180. Fall Week 11 7
End of Feedback: 11/1/ /5/2010 ©Aditya Mathur. CS 180. Fall Week 11 8
Back to Classes, objects, methods! 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 9
Review 11/5/2010 Listeners: MenuListener, KeyListener Widgets: JMenuBar, JMenu, JMenuItem Menu events: menuSelected(), menuDeselected(), menuCanceled() Key events: keyPressed(), keyReleased(), keyTyped() getSource() and getKeyChar() ©Aditya Mathur. CS 180. Fall Week 11 10
Dividing work into small segments 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 class Instance variables Class variables Methods class Instance variables Class variables Methods class Instance variables Class variables Methods Program 11
Instance variables 11/5/2010 public class Fruit{ public String name; public String color; public int type } ©Aditya Mathur. CS 180. Fall Week 11 public class Market{ public static void main(String [] args){ System.out.println(Fruit.color); } Will this compile? No: because color is not static 12
Instance variables 11/5/2010 public class Fruit{ public String name; public String color=“Red”; public int type; public void changeColor(String c){ color=c; } ©Aditya Mathur. CS 180. Fall Week 11 public class Market{ public static void main(String [] args){ Fruit apple=new Fruit(); Fruit pear=new Fruit(); pear.changeColor(“Green”); System.out.println(apple.color); System.out.println(pear.color); } Will this compile? Yes because we are accessing instance variables via their objects. 13
Class variables 11/5/2010 public class Fruit{ public String name; public static String color; public int type; } ©Aditya Mathur. CS 180. Fall Week 11 public class Market{ public static void main(Strings[] args){ System.out.println(Fruit.color); } Will this compile? Yes, because color is static and public. 14
Class: variables 11/5/2010 public class Fruit{ public String name; public static String color; public int type } ©Aditya Mathur. CS 180. Fall Week 11 public class Market{ public static void main(Strings[] args){ Fruit apple=new Fruit(); System.out.println(apple.color); } Will this compile? Yes, because color will be obtained from Fruit which is the parent class of apple. 15
Private/Public variables 11/5/2010 public class Fruit{ public String name; private String color=“Red”; int type; } ©Aditya Mathur. CS 180. Fall Week 11 public class Market{ public static void main(String [] args){ Fruit apple=new Fruit(); Fruit pear=new Fruit(); System.out.println(apple.color); System.out.println(pear.color); } Will this compile? No, color is private to the Fruit class. 16
Private/Public variables 11/5/2010 public class Fruit{ public String name; private String color=“Red”; int type; public void changeColor(String c){ color=c; } public String getColor(){ return color; } ©Aditya Mathur. CS 180. Fall Week 11 public class Market{ public static void main(String [] args){ Fruit apple=new Fruit(); Fruit pear=new Fruit(); pear.changeColor(“Green”); System.out.println(apple.getColor()); System.out.println(pear.getColor()); } Will this compile? Yes, because now we are using the public getColor() method to get color. 17
Accessor and mutator methods 11/5/2010 public class Fruit{ public String name; private String color=“Red”; int type; public void changeColor(String c){ color=c; } public String get Color(){ return color; } ©Aditya Mathur. CS 180. Fall Week 11 Accessor method Mutator method 18
Accessibility rules: Let us fill this table 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 ModifierClassPackageSubclassWorld publicYYYY privateYNNN noneYYNN 19 If a variable or an object declaration uses this modifier then can this variable or object be used inside ? Package: A collection of classes identified as a Java package. World: Collection of packages Y: Yes. N: No.
Local and global variables/objects 11/5/2010 public class X{ public JButton plus; public void doSomething(String c){ int z; for (int i=0; i<10; i++){ int p; } System.out.println(i, p); } // end of method }// end of class ©Aditya Mathur. CS 180. Fall Week 11 z, c can be use here i can be Used here i, p out of scope plus can be used here 20
Methods and parameters 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 21
Method [header] declaration 11/5/2010 public int find( JButton [] b, Object o) ©Aditya Mathur. CS 180. Fall Week Method Name (required) Formal Parameters 0 or more Return type (required) (Optional) Access modifier
Method call 11/5/2010 public int find( JButton [] b, Object o) ©Aditya Mathur. CS 180. Fall Week 11 Actual Parameters 23 int index=find( button, source) One-to-one correspondence of formal and actual parameters
Parameter types 11/5/2010 Value parameters: That are passed by value. All parameters in Java are passed by value. Reference parameters: Objects and arrays are reference parameters. This means that a reference to an object, not the object itself, is passed to the method. ©Aditya Mathur. CS 180. Fall Week 11 24
Pass by value: value of a primitive type is passed 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 public void test (int x){ x=3; } int p=0; test(p); System.out.println(p); Formal parameter Actual parameter The value of p does not change even though test sets its formal parameter to 3. 25
Pass by value: reference to an object is passed 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 public void test (int [] a){ a[0]=15; } Formal parameter int [] b=new int [100]; b[0]=21; test(b); System.out.println(b[0]); Actual parameter The value of b[0] changes to 15 26
Pass by value: reference to an object is passed 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 Formal parameter public static void changeString(String [] x){ x[0]="Zero"; x=new String [2]; } public static void main(String [] args){ String [] y=new String [5]; y[0]="Z"; y[1]="O"; System.out.println(y[0]+” “+ y[1]); changeString(y); System.out.println(y[0] +” “+ y[1]); } Actual parameter 27
Parameter passing: another example 11/5/2010 ©Aditya Mathur. CS 180. Fall Week public static void test(String [] x, int z){ x[0]="Zero"; // An element changed to "Zero" x=new String [2]; z=100; // z set to 100 } public static void main(String [] args){ String [] y=new String [5]; int p=0; y[0]="Z"; y[1]="O"; System.out.println(p); System.out.println(y[0]+ " "+y[1]); test (y, p); System.out.println(y[0]+ " "+y[1]); System.out.println(p); }
Parameter passing: yet another example 11/5/2010 ©Aditya Mathur. CS 180. Fall Week public class PassByRefArray{ static int [] b=new int [100]; public static void test(int [] a){ b=a; a[0]=99; a[1]=299; } public static void main(String [] args){ b[0]=15; System.out.println(b[0]+" "+b[1]); test(b); System.out.println(b[0]+" "+b[1]); } What will be displayed?
Method parameters 11/5/2010 public int find( JButton [] b, Object o){ int len=b.length, index=0; boolean found=false; while(!found && index<len){ if(o==b[i]){ found=true; break; } index++; } // end of while if(!found) return (-1); else return (index); } // end of method ©Aditya Mathur. CS 180. Fall Week 11 Method to find which button was clicked. Returns button index if found else returns a -1. Parameters 30
Method parameter: summary 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 A method may have zero or more parameters. Each parameter has a name. Each parameter must have a type Primitive types are passed by value; references to objects are passed and not the object itself Each method must have a return type. Each parameter becomes a local variable for the method. A constructor is a special method that has no return type. 31
Inheritance 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 32
Inheritance 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 public class Fruit{ public String name; private String color=“Red”; int type; public void changeColor(String c){ Color=c; } public String getColor(){ return color; } public class Mango extends Fruit{ public String origin; public void harvest(){ } public String getOrigin(){ return color; } All methods and local variables/objects are available to Mango. 33
Inheritance: Another example 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 public class Gui extends JFrame{ } All methods and local variables/objects of JFrame are available to Gui. 34
More on inheritance later! 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 35
Concurrent Programming 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 36
Concurrency 11/5/2010 ©Aditya Mathur. CS 180. Fall Week TT1T1 T2T2 TNTN.. T Main thread controls the distribution of work N threads executing concurrently to execute N tasks T 1, T 2 …T N. Distribute work Task T is divided into N simpler tasks and executed in parallel
Threads 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 Thread is a class A thread is a sequence of computations that can run in parallel with other threads. One uses the Thread class to create a thread. 38
Problem 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 Given doubles x and y and a boolean z, write a program to compute the following sin(x)/cos(y)+(x 2 +y 2 ); if z is true sin(x)/cos(y)+(x 2 - y 2 ); if z is false 39
Problem: Solution architecture 11/5/2010 ©Aditya Mathur. CS 180. Fall Week T T1T1 T2T2 T T: Perform the given task T 1 : Compute a part of the expression T 2 : Compute a other part of the expression Combine the results of T 1 and T 2 How many threads?
Problem: Algorithm 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 Thread A: Input: x and y Compute sin(x)/cos(y) Thread B: Input: x, y, z Compute x 2 +y 2 if z is true or x 2 -y 2 if z is false Thread C (control thread): Input: x, y, z; Create an object e1(Thread A ); Create can object e2(Thread B); Start e1; Start e2; Wait for e1 and e2 to complete; Get value computed by e1; Get value computed by e2; Add the two values and display the result; 41
Problem: Program 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 42
Week 11: November 1-5, 2010 Hope you enjoyed this week! Questions? Contact your recitation instructor. Make full use of our office hours. 11/5/2010 ©Aditya Mathur. CS 180. Fall Week 11 43