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.

Slides:



Advertisements
Similar presentations
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology/ George Koutsogiannakis 1.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Exception Handling. Introduction An exception is an abnormal condition that arises in a code sequence at run time. In computer languages that do not support.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
1. 2 Examples for Exception?... An exception is an abnormal condition that arises in a code sequence at run time (Run time error). In other computer languages.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 13: Nov 14-18, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 15: Nov 28-Dec 2, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 8: Oct 10-14, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 4: September 12-16, 2011 Aditya Mathur/Tim Korb Department of Computer.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 13: Nov 15-19, 2010 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 3: September 5-9, 2011 Aditya Mathur Department of Computer Science Purdue.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
Exceptions and Assertions Recitation – 03/13/2009 CS 180 Department of Computer Science, Purdue University.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Exception Handling Recitation – 10/(23,24)/2008 CS 180 Department of Computer Science, Purdue University.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
Exception Handling (Chapter 8) CS 180 Recitation - February 29, 2008 Department of Computer Science Purdue University.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 11: Oct 31-Nov 4, 2011 Aditya Mathur Department of Computer Science Purdue.
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 3: September 6-10, 2010 Aditya Mathur Department of Computer Science Purdue.
Chapter 8 Recursion Modified.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
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.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 14: Nov 21-25, 2011 Aditya Mathur Department of Computer Science Purdue.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 9: Oct 18-22, 2010 Aditya Mathur Department of Computer Science Purdue.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
COP 3330 Notes 3/7. Today’s Topics Exceptions Abstract Classes.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to Exceptions in Java CS201, SW Development Methods.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS
Chapter 12 Recursion (methods calling themselves)
TRY CATCH BLOCK By Kosala Rajapaksha.
Lecture 11 Objectives Learn what an exception is.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Tutorial Exceptions Handling.
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 12 Exception Handling and Text IO Part 1
Java Basics Exception Handling.
Exception Objects An exception is an abnormal condition that arises in a code sequence at rum time. Exception is a way of signaling serious problem.
CS Problem Solving and Object Oriented Programming Spring 2019
Java Programming: From Problem Analysis to Program Design, 4e
CS Problem Solving and Object Oriented Programming Spring 2019
Presentation transcript:

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 This Week: 12/6 1.Exceptions 2.Recursion

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 Week 16 6/12/20102

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, pm. LWSN 3102 ©Aditya Mathur. CS 180. Fall Week 16 6/12/20103

Announcements Final exam: Monday Dec 13, :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 Week 16 6/12/20104

Exceptions ©Aditya Mathur. CS 180. Fall 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

Exception: Example 1 ©Aditya Mathur. CS 180. Fall 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

Exception: Example 2 ©Aditya Mathur. CS 180. Fall 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

Exception: Unchecked (or Error) ©Aditya Mathur. CS 180. Fall 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

Exception: Example 3 ©Aditya Mathur. CS 180. Fall Week 16 public class ExceptionExampleFile{ public static void main(String [] args){ File f=new File("test.txt"); Scanner source=new Scanner(f); } 6/12/ 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

Exception: Example 4 ©Aditya Mathur. CS 180. Fall 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

Exception: Checked exceptions ©Aditya Mathur. CS 180. Fall 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

Exception: try-catch ©Aditya Mathur. CS 180. Fall 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

Exception: try-catch: multiple catch blocks ©Aditya Mathur. CS 180. Fall 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

Exception: try-catch-finally ©Aditya Mathur. CS 180. Fall 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

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

©Aditya Mathur. CS 180. Fall 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

©Aditya Mathur. CS 180. Fall 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

©Aditya Mathur. CS 180. Fall 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

©Aditya Mathur. CS 180. Fall 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

©Aditya Mathur. CS 180. Fall 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

©Aditya Mathur. CS 180. Fall Week 16 Towers of Hanoi: The problem 6/12/ 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

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

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

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

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

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

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

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 Week 16 6/12/201028