Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 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

3 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

4 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

5 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

6 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:

7 Java’s Exception Hierarchy Unchecked Checked

8 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 }

9 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

10 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

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

12 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 + 1 5 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

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

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

15 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

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

17 11/2/2015IT 17917 // 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)

18 11/2/2015IT 179 18 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); } 9 6 3 4 2 8 5 i j 2 6 3 4 9 8 5 i j 2 3 6 4 9 8 5 i j 2 3 4 6 9 8 5 i j 2 3 4 5 9 8 6 i j 2 3 4 5 6 8 9 i j (a quadratic sort) O(n 2 )

19 11/2/201519IT 179 1.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)

20 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

21 Some Common Computing Times log 2 n n n log 2 nn2n2 2n2n 12244 24816 382464256 4166425665,536 5321601,0244,294,967,296 6643844,096 1.84  10 19 712889616,384 3.40  10 38 82562,04865,536 1.16  10 77 95124,608262,144 1.34  10 154 101,02410,2401,048,576 1.80  10 308 11/2/201521IT 179

22 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

23 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


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

Similar presentations


Ads by Google