NotesWeek16.Dec 6-10

NotesWeek16.Dec 6-10 - 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 hLp://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/ This Week: Notes for Week 16: Dec 6-10, 2010 Aditya Mathur 12/6 1. ExcepTons 2. Recursion Department of Computer Science Purdue University West LafayeLe, IN, USA Readings and Exercises for Week 16 Readings: Chapter: 7.4, 11.2, 11.3 Exercises: 7.6, 7.8, 11.1, 11.2, 11.3 6/12/2010 Aditya Mathur. CS 180. Fall 2010. Week 16 2 Revised Project Schedule AddiTonal office hours: Tuesday Dec 7: 2-4pm. LWSN 1177 Saturday Dec 11: 2-4pm. LWSN 1177 Special class: Sunday Dec 12, 2010. 4-7pm. LWSN 3102 6/12/2010 Aditya Mathur. CS 180. Fall 2010. Week 16 3 Announcements Final exam: Monday Dec 13, 2010. 1:30pm. EE 129. 6/12/2010 Please aLend the class on Wednesday Dec 8. Programming compeTTon. Chief Guest Dr. Tim Korb. Cash prizes to winner and runner up teams. Thanks to Lockheed MarTn. I will announce the format of the final exam. Please complete course evaluaTon. Thanks. Aditya Mathur. CS 180. Fall 2010. Week 16 4 ExcepTons An abnormal or unexpected condiTon during execuTon. When such a condiTon occurs, Java run Tme system does not always know how best to proceed and hence raises an excepTon. Some excepTons must be explicitly handled by the programmer. Aditya Mathur. CS 180. Fall 2010. Week 16 6/12/2010 5 ExcepTon: Example 1 public class ExcepTonExampleDiv0{ public staTc void main(String args){ int y=1, z=0; int x=y/z; } } java.lang.ArithmeTcExcepTon: / by zero at ExcepTonExampleDiv0.main(ExcepTonExampleDiv0.java:8) 6/12/2010 Aditya Mathur. CS 180. Fall 2010. Week 16 6 ExcepTon: Example 2 public class ExcepTonExampleDiv0{ public staTc void main(String args){ int y=1, z=0; try{ } Divide by zero java.lang.ArithmeTcExcepTon: / by zero 6/12/2010 Aditya Mathur. CS 180. Fall 2010. Week 16 7 int x=y/z; Try-catch not required }catch(ArithmeTcExcepTon e){ System.out.println("Divide by zero:"+e); } ExcepTon: Unchecked (or Error) Arise during runTme due to some programming error. Not flagged by the compiler. Programmer may choose to handle these. Not subject to Catch requirement. Examples: ArithmeTcExcepTon ArrayIndexOutOfBounds NullPOinterExcepTon 6/12/2010 Aditya Mathur. CS 180. Fall 2010. Week 16 8 ExcepTon: Example 3 public class ExcepTonExampleFile{ public staTc void main(String args){ File f=new File("test.txt"); Scanner source=new Scanner(f); } } 1 error found: File: /Users/apm/www/courses/CS180Fall2010/Notes/ Programs/ExcepTons/ExcepTonExampleFile.java [line: 8] Error: /Users/apm/www/courses/CS180Fall2010/Notes/ Programs/ExcepTons/Except 6/12/2010 Aditya Mathur. CS 180. Fall 2010. Week 16 9 ExcepTon: Example 4 public class ExcepTonExampleFile{ public staTc void main(String args){ File f=new File("test.txt"); try{ Scanner source=new Scanner(f); }catch(FileNotFoundExcepTon e){ System.out.println("File not found "+e); } } } 6/12/2010 File not found java.io.FileNotFoundExce pTon: test.txt (No such file or directory) Aditya Mathur. CS 180. Fall 2010. Week 16 10 ExcepTon: Checked excepTons These are subject to the catch requirement. Programmer is expected to write code to recover from these excepTons. Example: FileNotFoundExcepTon 6/12/2010 Aditya Mathur. CS 180. Fall 2010. Week 16 11 ExcepTon: try-catch try{ code where excepTon might occur; }catch(excepTon object){ code to handle excepTon } catch-block try-block A catch-block serves as an excepTon handler. 6/12/2010 Aditya Mathur. CS 180. Fall 2010. Week 16 12 ExcepTon: try-catch: mulTple catch blocks try{ code where excepTon might occur; }catch(excepTon object){ code to handle excepTon; }catch(another excepTon object{ code to handle excepTon; } Another catch- block catch-block try-block 6/12/2010 Aditya Mathur. CS 180. Fall 2010. Week 16 13 ExcepTon: try-catch-finally try{ Code where excepTon might occur; }catch(excepTon object){ code to handle excepTon; }finally{ code to handle excepTon; } Finally-block always executes aqer the try-block. 6/12/2010 Aditya Mathur. CS 180. Fall 2010. Week 16 14 try-block catch-block finally-block Recursion 6/12/2010 Aditya Mathur. CS 180. Fall 2010. Week 16 15 What is recursion? Recursion is a technique to code funcTons (or methods) in Java. We say that a method is recursive, when it calls itself. This is known as direct recursion. Indirect recursion occurs when a method is called, before it completes, by another method. Recursion is complementary to iteraTon. While solving a problem, you may choose to use either. 6/12/2010 Aditya Mathur. CS 180. Fall 2010. Week 16 16 Example: Factorial DefiniTon: IteraTve [n>=0] !n= 1, if n=0 or 1 = 1*2*3*...*n; DefiniTon: Recursive [n>=0] !n= 1, if n=0 or 1 = n*!(n-1), if n>1 Basis Recursion 6/12/2010 Aditya Mathur. CS 180. Fall 2010. Week 16 17 Example: Factorial: IteraTve method DefiniTon: IteraTve [n>=0] !n= 1, if n=0 or 1 = 1*2*3*...*n; public int factIter(int n){ if(n==0 || n==1) return 1; int prod=1; for (int i=2; i<=n; i++){ prod=prod*i; } return (prod); } !5=prod=1*2*3*4*5 Aditya Mathur. CS 180. Fall 2010. Week 16 6/12/2010 18 Example: Factorial: Recursive method DefiniTon: Recursive [n>=0] !n= 1, if n=0 or 1 = n*!(n-1), if n>1 public int factRecur(int n){ if(n==0 || n==1){ return 1; }else{ return (n*factRecur(n-1)); } } factRecur(5)=5*factRecur(4)*factRecur(3)*factRecur(2)*factRecur(1) =5*factRecur(4)*factRecur(3)*factRecur(2)*1 =5*factRecur(4)*factRecur(3)*2*1 =5*factRecur(4)*3*2*1 =5*4*3*2*1 Aditya Mathur. CS 180. Fall 2010. Week 6/12/2010 19 16 Recursion versus IteraTon IteraTon suffices for most problems that arise in pracTce. However, in some cases recursion allows compact and quick soluTon. An example follows. 6/12/2010 Aditya Mathur. CS 180. Fall 2010. Week 16 20 Towers of Hanoi: The problem Given: Three towers A, B, and C. A contains n discs arranged from boLom to top as shown below. Problem: Find a sequence of moves such that at the end of these moves all discs are in C. No larger disc should ever be on top of a smaller disk. Only one disk can be moved at a Tme from one tower to another. Only the top disc can be moved from a tower. A 6/12/2010 B Aditya Mathur. CS 180. Fall 2010. Week 16 C 21 Towers of Hanoi: IniTal and final configuraTons IniTal configuraTon A B C Final configuraTon A 6/12/2010 B C 22 Aditya Mathur. CS 180. Fall 2010. Week 16 Towers of Hanoi: IteraTve soluTon Try this! This will be difficult but certainly worth a try. 6/12/2010 Aditya Mathur. CS 180. Fall 2010. Week 16 23 Towers of Hanoi: Recursive soluTon What is a move? Only one disc can be moved from one tower to another. Hence we will write a move as: move x, y; x is the tower from where the top disc is moved and placed on top in tower y. For example: move A, B; will move the top disc on A to tower B 6/12/2010 Aditya Mathur. CS 180. Fall 2010. Week 16 24 Towers of Hanoi: Recursive soluTon: VisualizaTon Start 1 3 A B C A B C 2 4 A 6/12/2010 B C A B C 25 Aditya Mathur. CS 180. Fall 2010. Week 16 Towers of Hanoi: Recursive program: Live 6/12/2010 Aditya Mathur. CS 180. Fall 2010. Week 16 26 Towers of Hanoi: Challenge! What is the least number of moves required to move n discs from tower A to tower C? 6/12/2010 Aditya Mathur. CS 180. Fall 2010. Week 16 27 Week 16: December 6-10, 2010 Hope you enjoyed this week! QuesTons? Contact your recitaTon instructor. Make full use of our office hours. 6/12/2010 Aditya Mathur. CS 180. Fall 2010. Week 16 28 ...
View Full Document

Ask a homework question - tutors are online