Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 16: Dec 6-10, 2010 Aditya Mathur Department of Computer Science Purdue.

Similar presentations


Presentation on theme: "CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 16: Dec 6-10, 2010 Aditya Mathur Department of Computer Science Purdue."— Presentation transcript:

1 CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 16: Dec 6-10, 2010 Aditya Mathur Department of Computer Science Purdue University West Lafayette, IN, USA http://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/ This Week: 12/6 1.Exceptions 2.Recursion

2 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 ©Aditya Mathur. CS 180. Fall 2010. Week 16 6/12/20102

3 Revised Project Schedule Additional 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 ©Aditya Mathur. CS 180. Fall 2010. Week 16 6/12/20103

4 Announcements Final exam: Monday Dec 13, 2010. 1:00pm. EE 129. Please attend the class on Wednesday Dec 8. Programming competition. Chief Guest Dr. Tim Korb. Cash prizes to winner and runner up teams. Thanks to Lockheed Martin. I will announce the format of the final exam. Please complete course evaluation. Thanks. ©Aditya Mathur. CS 180. Fall 2010. Week 16 6/12/20104

5 Exceptions ©Aditya Mathur. CS 180. Fall 2010. Week 16 An abnormal or unexpected condition during execution. When such a condition occurs, Java run time system does not always know how best to proceed and hence raises an exception. 6/12/2010 Some exceptions must be explicitly handled by the programmer. 5

6 Exception: Example 1 ©Aditya Mathur. CS 180. Fall 2010. Week 16 public class ExceptionExampleDiv0{ public static void main(String [] args){ int y=1, z=0; int x=y/z; } 6/12/2010 java.lang.ArithmeticException: / by zero at ExceptionExampleDiv0.main(ExceptionExampleDiv0.java:8) 6

7 Exception: Example 2 ©Aditya Mathur. CS 180. Fall 2010. Week 16 public class ExceptionExampleDiv0{ public static void main(String [] args){ int y=1, z=0; try{ int x=y/z; }catch(ArithmeticException e){ System.out.println(“Divide by zero:”+e); } } 6/12/2010 Divide by zero java.lang.ArithmeticException: / by zero 7 Try-catch not required

8 Exception: Unchecked (or Error) ©Aditya Mathur. CS 180. Fall 2010. Week 16 Arise during runtime due to some programming error. Not flagged by the compiler. Programmer may choose to handle these. Not subject to Catch requirement. Examples: ArithmeticException ArrayIndexOutOfBounds NullPOinterException 6/12/20108

9 Exception: Example 3 ©Aditya Mathur. CS 180. Fall 2010. Week 16 public class ExceptionExampleFile{ public static void main(String [] args){ File f=new File("test.txt"); Scanner source=new Scanner(f); } 6/12/2010 1 error found: File: /Users/apm/www/courses/CS180Fall2010/Notes/Programs/Exc eptions/ExceptionExampleFile.java [line: 8] Error: /Users/apm/www/courses/CS180Fall2010/Notes/Programs/Exc eptions/Except 9

10 Exception: Example 4 ©Aditya Mathur. CS 180. Fall 2010. Week 16 public class ExceptionExampleFile{ public static void main(String [] args){ File f=new File("test.txt"); try{ Scanner source=new Scanner(f); }catch(FileNotFoundException e){ System.out.println("File not found "+e); } 6/12/2010 File not found java.io.FileNotFoundExce ption: test.txt (No such file or directory) 10

11 Exception: Checked exceptions ©Aditya Mathur. CS 180. Fall 2010. Week 16 These are subject to the catch requirement. 6/12/2010 Programmer is expected to write code to recover from these exceptions. Example: FileNotFoundException 11

12 Exception: try-catch ©Aditya Mathur. CS 180. Fall 2010. Week 16 try{ code where exception might occur; }catch(exception object){ code to handle exception } 6/12/2010 catch-block try-block A catch-block serves as an exception handler. 12

13 Exception: try-catch: multiple catch blocks ©Aditya Mathur. CS 180. Fall 2010. Week 16 try{ code where exception might occur; }catch(exception object){ code to handle exception; }catch(another exception object{ code to handle exception; } 6/12/2010 catch-block try-block Another catch- block 13

14 Exception: try-catch-finally ©Aditya Mathur. CS 180. Fall 2010. Week 16 try{ Code where exception might occur; }catch(exception object){ code to handle exception; }finally{ code to handle exception; } 6/12/2010 catch-block try-block finally-block Finally-block always executes after the try-block. 14

15 Recursion ©Aditya Mathur. CS 180. Fall 2010. Week 16 6/12/201015

16 ©Aditya Mathur. CS 180. Fall 2010. Week 16 What is recursion? 6/12/2010 We say that a method is recursive, when it calls itself. This is known as direct recursion. Recursion is a technique to code functions (or methods) in Java. Indirect recursion occurs when a method is called, before it completes, by another method. Recursion is complementary to iteration. While solving a problem, you may choose to use either. 16

17 ©Aditya Mathur. CS 180. Fall 2010. Week 16 Example: Factorial 6/12/2010 !n= 1, if n=0 or 1 = 1*2*3*…*n; Definition: Iterative [n>=0] !n= 1, if n=0 or 1 = n*!(n-1), if n>1 Definition: Recursive [n>=0] 17 Basis Recursion

18 ©Aditya Mathur. CS 180. Fall 2010. Week 16 Example: Factorial: Iterative method 6/12/2010 !n= 1, if n=0 or 1 = 1*2*3*…*n; Definition: Iterative [n>=0] 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 18

19 ©Aditya Mathur. CS 180. Fall 2010. Week 16 Example: Factorial: Recursive method 6/12/2010 public int factRecur(int n){ if(n==0 || n==1){ return 1; }else{ return (n*factRecur(n-1)); } !n= 1, if n=0 or 1 = n*!(n-1), if n>1 Definition: Recursive [n>=0] 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 19

20 ©Aditya Mathur. CS 180. Fall 2010. Week 16 Recursion versus Iteration Iteration suffices for most problems that arise in practice. However, in some cases recursion allows compact and quick solution. An example follows. 6/12/201020

21 ©Aditya Mathur. CS 180. Fall 2010. Week 16 Towers of Hanoi: The problem 6/12/201021 Given: Three towers A, B, and C. A contains n discs arranged from bottom 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 time from one tower to another. Only the top disc can be moved from a tower. ABC

22 ©Aditya Mathur. CS 180. Fall 2010. Week 16 Towers of Hanoi: Initial and final configurations 6/12/201022 ABC Initial configuration ABC Final configuration

23 ©Aditya Mathur. CS 180. Fall 2010. Week 16 Towers of Hanoi: Iterative solution 6/12/201023 Try this! This will be difficult but certainly worth a try.

24 ©Aditya Mathur. CS 180. Fall 2010. Week 16 Towers of Hanoi: Recursive solution 6/12/201024 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

25 ©Aditya Mathur. CS 180. Fall 2010. Week 16 Towers of Hanoi: Recursive solution: Visualization 6/12/201025 BCA Start 1 BCA 2 BCA 3 BCA 4

26 ©Aditya Mathur. CS 180. Fall 2010. Week 16 Towers of Hanoi: Recursive program: Live 6/12/201026

27 ©Aditya Mathur. CS 180. Fall 2010. Week 16 Towers of Hanoi: Challenge! 6/12/201027 How many moves are required to move n discs from tower A to tower C?

28 Week 16: December 6-10, 2010 Hope you enjoyed this week! Questions? Contact your recitation instructor. Make full use of our office hours. ©Aditya Mathur. CS 180. Fall 2010. Week 16 6/12/201028


Download ppt "CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 16: Dec 6-10, 2010 Aditya Mathur Department of Computer Science Purdue."

Similar presentations


Ads by Google