What/how do we care about a program? Robustness Correctness Efficiency (speed, space) 11/2/20151IT 179  Software Testing  Error Handling  Efficiency.

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Complexity Analysis (Part I)
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
1 Exception Handling  Introduction to Exceptions  How exceptions are generated  A partial hierarchy of Java exceptions  Checked and Unchecked Exceptions.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
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.
COMP s1 Computing 2 Complexity
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
CS203 Java Object Oriented Programming Errors and Exception Handling.
SIGCSE Tradeoffs, intuition analysis, understanding big-Oh aka O-notation Owen Astrachan
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Program Performance & Asymptotic Notations CSE, POSTECH.
Week 2 CS 361: Advanced Data Structures and Algorithms
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Analysis of Algorithms
Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…
COMP Exception Handling Yi Hong June 10, 2015.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Asymptotic Notation (O, Ω, )
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.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Data Structure Introduction.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Introduction to Analysis of Algorithms CS342 S2004.
Christina Aiello, 12/3/2015. Reminder: Seen in lecture notes for Exceptions: “Side note: we will NOT expect you to know/remember how to do keyboard input/output.
Algorithm Analysis (Big O)
CS 150: Analysis of Algorithms. Goals for this Unit Begin a focus on data structures and algorithms Understand the nature of the performance of algorithms.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
Searching Topics Sequential Search Binary Search.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
DS.A.1 Algorithm Analysis Chapter 2 Overview Definitions of Big-Oh and Other Notations Common Functions and Growth Rates Simple Model of Computation Worst.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
3/21/2016IT 2751 Tow kinds of Lists Array What can be done? What can be easily done? student 1 student 2 student 3 student 4 Linked List student 2 student.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.
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.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Data Structures in Java: From Abstract Data Types to the Java Collections.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Chapter 2: Error Handling, Software Testing and Program Efficiency
Algorithm Analysis 1.
Java Exceptions a quick review….
Chapter 10 – Exception Handling
MIT AITI 2003 Lecture14 Exceptions
GC 211:Data Structures Algorithm Analysis Tools
What/how do we care about a program?
Advanced Java Programming
The father of algorithm analysis
Chapter 13 Exception Handling
Chapter 12 Exception Handling and Text IO Part 1
Exception Handling.
Presentation transcript:

What/how do we care about a program? Robustness Correctness Efficiency (speed, space) 11/2/20151IT 179  Software Testing  Error Handling  Efficiency Analysis

Things that can go wrong Logic error – The program is incorrect  Environment error - e.g. out of memory I/O error – e.g., lost network connection. 11/2/20152IT 179 Let’s handle it!! Beyond programmer’s control

Exceptions Throwing Exceptions is Java’s way of telling you something has gone wrong When an “exceptional condition” occurs, an exception object is created storing information about the nature of the exception (kind, where it occurred, etc.). When this happens, we say that “an exception is thrown”. The JVM looks for a block of code to catch and handle the exception (do something with it) In Java, they are classes 11/2/20153IT 179

Exception throwing The sequence of calling The sequence of throwing exceptions X bad thing happens JVM starts application at main() main() calls  method a() method a  method b() method b  method c() method c  exception occurs If the exception cannot be handled here, it will continue to throw back 11/2/20154IT 179

Categories of exceptions Checked exceptions – descended from class Exception, but outside the hierarchy rooted at RuntimeException. The compiler will check that you either catch or re-throw checked exceptions. Unchecked exceptions – (aka Runtime Exception ) represent the kinds of errors your program can avoid through careful programming and testing. The compile does not check to see that you handle these exceptions. These categorization affect compile-time behavior only These represent some error, not programmer’s fault, but the programmer can (should) do something about it They are programmer’s fault, the compiler can’t do a thing. 11/2/20155IT 179

Unchecked Exceptions handle: Logic error – The program is incorrect  Environment error - e.g. out of memory I/O error – e.g., lost network connection. 11/2/20156IT 179 Checked Exceptions handle:

Java’s Exception Hierarchy Unchecked Checked

Try-Catch-Finally Blocks try { program statements; some of which may throw an exception } catch ( ExceptionType1 exception ) { program statements to handle exceptions of type ExceptionType1 or any of its subclasses } catch ( ExceptionType2 exception ) { program statements to handle exceptions of type ExceptionType2 or any of its subclasses } …... other catch clauses … catch ( ExceptionTypeN exception ) { program statements to handle exceptions of type ExceptionTypeN or any of its subclasses } finally { this block is optional; this block will execute whether or not an exception is thrown }

public class HandleExceptions { public static void main(String[] args) { System.out.println("Testing Exception Handling\n"); try { int i=1,j=2; Exception myEx=null; String s="0.2"; i=Integer.parseInt(s); if (i > j) throw new RuntimeException(); int[] A = new int[5]; i=1; if (i 4) throw new IndexOutOfBoundsException(); i = 5; i = A[i]; } catch (ArithmeticException ex) { System.out.println("\n"+ex.toString()); ex.printStackTrace(); catch (NullPointerException ex) { System.out.println("I'm here 12345"); System.out.println("\n"+ex.toString()); ex.printStackTrace(); } catch (NumberFormatException ex) { System.out.println("\n"+ex.toString()); ex.printStackTrace(); } catch (IllegalArgumentException ex) { System.out.println("\n"+ex.toString()); ex.printStackTrace(); } catch (ArrayIndexOutOfBoundsException ex) { System.out.println("\n"+ex.toString()); ex.printStackTrace(); } catch (IndexOutOfBoundsException ex) { System.out.println("\n"+ex.toString()); ex.printStackTrace(); } catch (RuntimeException ex) { System.out.println("\n"+ex.toString()); ex.printStackTrace(); } finally { System.out.println("\nWe are done. Finally!!"); } 11/2/20159IT 179

Correctness  Software Testing You should “test” throughout the software development cycle, namely, every stages: –After the analysis and design are completed –During the implementation (test driven development) (Write a little, test a little) –After the implementation is completed 11/2/201510IT 179

An algorithm’s performance can be measured by: Time complexity Space complexity 11/2/ IT 179 How long it takes to execute. How much additional space the algorithm needs. (memory, storage, tape). Efficiency (speed, space)  Efficiency Analysis

Time Complexity: Count Instructions StatementsCost 1 double average( int []a, int n ) { 2 double sum = 0; 1 3 int count = 0; 1 4 while ( count < n ) { n sum += a[count]; n 6 count++; n 7 } 8 if ( n > 0 ) 1 9 return sum / n; 1 10 else 11 return 0.0f; 12 } TOTAL 3n + 5 How many times will each instruction execute? 11/2/201512IT 179

asymptotic approach for (int i=0; i< n; i++) { for (int j=i; j < n; j++) { } } n+(n-1)+(n-2) = n(n+1)/2 = (½)(n 2 +n)  n2 n2 11/2/201513IT 179

Consider the worst part of the program if (....) { } else { } n2n2 n3n3  n3 n3 11/2/201514IT 179

Big-O notation in Algorithm Analysis for (int i=0; i< n; i++) { } for (int i=0; i< n; i++) { for (int j=0; j< n; j++) { for (int k=0; k<n; k++) { } } for (int i=0; i< n; i++) { for (int j=0; j< n; j++) { } }  n n  n3 n3  n2 n2 O(c+n+n 3 +n 2 )  O(n 3 ) 11/2/201515IT 179

Linear Search StatementsCost 1int linearSearch( int [] a, int n, int target ) { 2 int i = 0; 1 3 while ( i < n ) { n if ( target == a[i] ) n 5 return i; 1 6 i++; n 7 } 8 return –1; 1 9} TOTAL 3n /2/201516IT 179

11/2/2015IT // select the minimum between a[s] to the end // and return the index of the minimum // private int min(int a[], int s){ int m = s; for (int i=s; i < a.length;i++) { if (a[i] < a[m]) m=i; } return m; } // exchange the contents of a[i] and a[j] // private void exchange(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } O(n-s)=O(n) where n is the size of the array O(3) = O(1)

11/2/2015IT Selection Sort public void sort(int a[]) { // select right element for a[i] for (int i = 0; i<a.length-1; i++) { int j = min(a,i); exchange(a,i,j); } i j i j i j i j i j i j (a quadratic sort) O(n 2 )

11/2/201519IT Logarithm (binary search) 2.Linear (sequential search) 3.n log n (quick sort) 4.Quadratic (n 2, selection sort, insertion sort) 5.Polynomial (Primality test) 6.Nondeterministic polynomial (Traveling Salesman problem) 7.Exponential (e n Backtracking)

Big-Oh (  ): Linear Search T linearSearch (n) = 3n + 3 =  (n) for c = 4 and n 0 = 0, in the worst case. 4n4n n2n2 3n + 3 T linearSearch (n) =  (n 2 ) But this is an abuse of O-notation 11/2/201520IT 179

Some Common Computing Times log 2 n n n log 2 nn2n2 2n2n , ,0244,294,967, ,  ,  ,04865,  ,608262,  ,02410,2401,048,  /2/201521IT 179

Most commonly used Asymptotic notations Big-O notation: O(f) Big-  notation:  (f) Big-  notation:  (f) Little-o notation, o(f) There are collections of functions with certain asymptotic properties. 11/2/201522IT 179

Asymptotic notation (for all but finitely many) Upper bound: Big-O notation, O(f) g  O(f) iff  c  n 0  n [n≥ n 0  g(n)  cf(n)] Lower bound: Big-  notation,  (f) g   (f) iff  c  n 0  n [n≥ n 0  g(n) ≥ cf(n)] Approximation: Big-  notation,  (f) g   (f) iff  c 1  c 2  n 0  n [n≥ n 0  c 2 f(n)]  g(n)  c 2 f(n)] equivalently g  O(f) and g   (f) Tied Upper bound: Little-o notation, o(f) g  o(f) iff  c  n 0  n [n≥ n 0  g(n)  cf(n)] 11/2/201523IT 179