NotesWeek12.Nov 8-12

NotesWeek12.Nov 8-12 - CS 180 Problem Solving and Object...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 180 Problem Solving and Object Oriented Programming Fall 2010 hKp://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/ This Week: Notes for Week 12: Nov 812, 2010 Aditya Mathur 11/8 11/10 1. Review Concurrent programming 2. Concurrent linear search 3. CHALLENGE PROBLE 4. Concurrent bubble sort and merge Department of Computer Science Purdue University West LafayeKe, IN, USA Readings and Exercises for Week 12 [PreparaZon for Exam 2] Readings: Chapters: 6.3, 6.4, 6.5, 6.6, 6.7; 7.2, 7.3; 8.2, 8.3; 9.2, 9.3; 10.2, 10.3; 12.3. Exercises: Conceptual exercises at the end of chapters menZoned above. Aditya Mathur. CS 180. Fall 2010. Week 12 11/10/2010 2 Special Sessions Lab Help: Mathur: Thursday November 11, 5:307:00pm Alvin: Sunday Nov 14, 24pm LWSN B158 Project 4 Due: Monday November 15, 2010 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 3 Lunch meeZng When: Based on appointments requested. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 4 Exam 2 When: Where: Format: BRING YOUR ID! Tuesday November 9, 2010; 810pm. EE129 Parts: A and B. Part A: 30 points. Closed book/notes 15 mulZple choice quesZons. Two programming quesZons. One quesZon asks you to write methods. Another quesZon asks you to develop a simple GUI. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 5 Part B: 70 points. Open book/notes Review: Arrays 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 6 Arrays of primiZve types int a; // Declare an array int a=new int [n]; // Create an array long x; // Declare an array long x=new long [10]; // Create an array long x=new long [10][5]; // Create an array 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 7 Array of primiZve types: Visual int a=new int [3]; // Create an array a 0 1 2 0 0 0 a[0]=5; a 0 1 2 5 0 0 8 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 Arrays of objects JBuKon b; JBuKon b=new JBuKon[n]; 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 9 Array of objects: Visual JBuKon b=new JBuKon[3]; b 0 1 2 null null null 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 10 Array of objects: Null pointer excepZon JBuKon b=new JBuKon[3]; b 0 1 2 null null null What happens if we do the following? buKon[0].setText("Hello"); 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 11 Array of objects: CreaZng an object JBuKon b=new JBuKon[3] b[0]=new JBuKon("Hello"); b 0 1 2 null null Hello 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 12 Array of objects: Modifying an object JBuKon b=new JBuKon[3] b[0]=new JBuKon("Hello"); What happens if we do the following? b[0].setText("Hi!"); b 0 1 2 null null Hi! 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 13 Array of objects: Another example Fruit f=new Fruit[3] f 0 1 2 null null null f[0]=new Fruit("Mango"); f 0 1 2 null null name: "Mango" color: price: f[0].setFruitColor(Color.Yellow); f[0].setPrice(3.99); 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 name: "Mango" color: Yellow price:3.99 14 Review: Methods 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 15 Method header Modifier public/ private/ none Modifier staZc/ none Return Name type Parameters 0 or more public boolean find(int a, int x) 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 16 Method parameters Reference parameter Value parameter public boolean find(int a, int x) Value parameter: primiZve types Reference parameter: objects 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 17 Value parameters public boolean find(int a, int x){ System.out.println(x); x++; Value of actual parameter is passed } What is displayed? int y=15; int num={2, 3, 15, 29}; boolean found=find(num, y) System.out.println(found + " "+ y); 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 18 Reference parameters public boolean find(int a, int x){ System.out.println(a[1]); x++; a[1]++; } Reference to actual parameter is passed What is displayed? int y=15; int num={2, 3, 15, 29}; boolean found=find(num, y) System.out.println(found + " "+ num[1]); 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 19 The main() method Is this value or a reference parameter? public staZc void main(String args){ } How can we pass parameters to the main() method? 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 20 Method parameter: summary A method may have zero or more parameters. Each parameter has a name. Each parameter must have a type All parameters are passed by value. For primiZve types the value of the actual parameter is passed. For reference types a reference to the object is passed. 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. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 21 Review: Class and instance variables 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 22 Class and instance variables public class Home{ Class variables public staZc String measureUnit="Square Feet"; public staZc String code="Pub. L. 110140"; private String address=""; Instance variables private int bedRooms=4; public Home(String a, int r){ address=a; bedRooms=r; } public int getBedrooms(){ return(bedRooms); } 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 23 Class and instance variables Home h1=new Home("1400 X Street", 4); System.out.println(h1.getBedRooms()); h1 address="1400 X Street" bedRooms=4 h2 Home h2=new Home("1500 Y Street", 3); System.out.println(h2.getBedRooms()); System.out.println(h1.code); System.out.println(h2.code); System.out.println(Home.getBedRooms()); System.out.println(Home.code); 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 24 address="1500 Y Street" bedRooms=3 Class variables Declared using the staZc keyword. Not a part of the object, but part of the class in which they are declared. Accessible via the objects created from that class Declare a variable or a method as staZc only if does not depend on the object. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 25 Instance variables Declared without using the staZc keyword Part of an object Accessible via the objects created from the parent class Declare a variable as an instance variable if its value depends on the object. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 26 The main() method Is declared to be staZc Can it access instance variables in its parent class? What variables and methods in the parent class can the main method access? 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 27 Accessibility rules If a variable or an object declaraZon uses this modifier Modifier public private none then can this variable or object be used inside ? Package Y N Y Y: Yes. N: No. Class Y Y Y Subclass Y N N World Y N N Package: A collecZon of classes idenZfied as a Java package. World: CollecZon of packages 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 28 Review: Local and global variables 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 29 Local variables public void test(int x){ int p, q; } x, p, q are local to method test. test is public and hence global and can be used in all classes. Variables and objects declared inside a method are local to that method. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 30 Local variables: Use of public and private modifiers public void test(int x){ int p, q; public int z; } x, p, q are local to method test. test is public and hence global and can be used in all classes. DeclaraZons inside a method cannot be preceded by public or private. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 31 Global variables public class Toy{ public String name; private String owner; } name is global as it is declared to be public. Owner is local to this class Toy is global and can be used anywhere. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 32 Local and global variables: Example public class X{ public JBuKon plus; public void doSomething(String c){ int z; for (int i=0; i<10; i++){ i can be int p; Used here } System.out.println(i, p); } // end of method }// end of class i, p out of scope 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 plus can be used here z, c can be use here 33 Review: Inheritance 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 34 Inheritance public class Fruit{ public String name; private String color="Red"; int type; public void changeColor(String c){ Color=c; } public String getColor(){ return color; } } 11/10/2010 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. 35 Aditya Mathur. CS 180. Fall 2010. Week 12 Inheritance: Another example public class Gui extends JFrame{ } All methods and local variables/ objects of JFrame are available to Gui. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 36 Review: GUI 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 37 GUI: Widget/Methods JFrame JPanel JBuKon JTextField JMenuBar JMenu JMenuItem setSize() setVisible( ) setText() getText add() 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 38 GUI: Listeners/Methods AcZonListener MenuListener KeyListener MouseListener getSource() addAcZonListener() 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 39 GUI: Interface A class implements an interface All methods in the interface must be implemented. AcZonListener is an interface. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 40 GUI: Abstract Class Similar to interface but may implement some methods thus avoiding the need to implement all methods. MouseAdapter is an abstract class. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 41 Back to Concurrency 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 42 Thread: What is it? A thread is a sequence of computaZon that can run in parallel with other threads. Every program has at least one thread of computaZon. Each thread is associated with an instance of the class Thread. A program with two or more threads is generally referred to as a mulZthreaded program. A mulZthreaded program is generally used to speed up the soluZon to a problem. Aditya Mathur. CS 180. Fall 2010. Week 12 11/10/2010 43 Thread: Typical lifecycle Define class that extends Thread. Create thread. Start thread. Wait for thread to terminate. Get results from thread. Use results from thread. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 44 Thread: Defining a class Thread is a class. One way to create a new thread is to first define a class that extends the Thread class. public class Search extends Thread{ String x; // For use by a thread String s; // Another object for use by the thread int tID; // Thread ID if needed int start, end; // Start and end indices for search boolean found; // Computed by the thread } 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 45 Thread: The constructor A class that extends Thread generally has a constructor that is used to pass parameters to a thread as follows. public Search (String a, String s, int start, int end, int tID, ){ // Save parameters for use when the thread executes x=a; this.s=s; this.start=start; this.end=end; this.tID=tID } 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 46 Thread: The run() method Unlike the main() method, every thread object must have a run() method. When a thread is started, its execuZon begins in the run() method and terminates when the run() method terminates 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 47 Thread: The run() method: Example public void run (){ // Must not have any parameters // Thread execuZon begins here. System.out.println("Hi! I am thread "+tID); System.out.println("Bye bye! See you!"); } The run() method does not take any parameters. The run() methods does not return any value. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 48 Thread: Other methods A thread may have methods other than the run() method. Here is an example. public boolean getOutcome(){ } return (found); // Return the outcome of search Methods in a thread may be called during the execuZon of the run() method and even aver the compleZon of the run() method. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 49 Thread: run() method A run() method is like any other method except that it does not have any parameters or return type. A run() method may call any other method to complete its task. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 50 Thread: CreaZon Create a thread object just as you would for any other object. The following examples create two thread objects named one and two. Search one=new Search (a, s, start, end, 1); Search two=new Search (a, s, start, end, 2); 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 51 Thread: Start of execuZon ExecuZon of a thread must be started using the start() method for that thread. Here are two examples for starZng threads one and two. one.start(); // Starts the execuZon of thread object one two.start(); // Starts the execuZon of thread object two 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 52 Thread: WaiZng for compleZon You may wait for a thread to complete execuZon using the join() method as follows. try{ one.join(); // Wait for thread one to complete two.join(); // Wait for thread one to complete catch(ExcepZon e){} The trycatch block is needed. More on this later! 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 53 Thread: ExtracZng results You may extract the outcome of a thread's execuZon in a variety of way. One way is to use an accessor method to do so. Here are examples. boolean f1=one.getOutcome(); // Get search outcome of thread one boolean f2=two.getOutcome(); // Get search outcome of thread two 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 54 Thread: Typical lifecycle [Review] Define class that extends Thread. Create thread. Start thread. Wait for thread to terminate. Get results from thread. Use results from thread. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 55 Example: Concurrent search 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 56 Problem 1 We are given a rather large array of strings. We wish to write a program that will determine, and display, whether or not a given string appears in the array. Given that the array is large, we are required to write a concurrent program that uses threads to solve the problem faster than it would if done sequenZally. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 57 Understanding the problem The problem is rather straighxorward! 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 58 Search algorithm There are many search algorithms available. Given no informaZon about how the array of strings is ordered, we will use a simple linear search algorithm. The given string will be compared with the first element of the array, then the next and so on unZl a match is found or all elements have been compared. If a match is found, the algorithm returns true, otherwise it returns false. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 59 Concurrent search: Create threads 0 1 2 3 4 5 p q r s t u Create thread one Pass a, s and start and end indices to the constructor [start=0, end=2] Create thread two Pass a, s and start and end indices to the constructor [start=3, end=5] a= Given array Given string to be searched: s 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 60 Concurrent search: Array parZZoned Thread one searches in this part of the array 0 1 2 p q r Thread two searches in this part of the array 3 4 5 s t u 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 61 Concurrent search: Next steps Start the threads; Wait for the threads to complete; Extract the search outcome of each thread; If any one of the threads found the string then the search was successful otherwise the search failed. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 62 Live demo 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 63 Challenge Problem You are given an array of integers. The integers in the array vary from 0 (inclusive) to 500 (inclusive). You are also given 3 bins. Write a concurrent program that distributes the integers in the array into the 3 bins such that bin 1 gets all integers less than 170, bin 2 gets all integers greater than 170 but less than 240, and bin 3 gets all integers greater than 240. Sort the bins in ascending order and display. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 64 Challenge Problem: Example Given: array={3, 43, 129, 32, 400, 452, 5} Final contents of the three bins as displayed: bin 1={3, 5, 32, 43} bin 2={129} bin 3={400, 452} 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 65 Week 12: November 813, 2010 Hope you enjoyed this week! QuesZons? Contact your recitaZon instructor. Make full use of our office hours. 11/10/2010 Aditya Mathur. CS 180. Fall 2010. Week 12 66 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online