Presentation is loading. Please wait.

Presentation is loading. Please wait.

What/how do we care about a program?

Similar presentations


Presentation on theme: "What/how do we care about a program?"— Presentation transcript:

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

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

3 Exceptions In Java, they are classes
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) 11/12/2018 IT 179

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

5 Try-Catch-Finally Blocks
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 other catch clauses catch ( ExceptionTypeN exception ) { program statements to handle exceptions of type ExceptionTypeN finally { this block is optional; this block will execute whether or not an exception is thrown

6 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<2 || 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"); catch (NumberFormatException ex) { System.out.println("\n"+ex.toString()); ex.printStackTrace(); } catch (IllegalArgumentException ex) { catch (ArrayIndexOutOfBoundsException ex) { catch (IndexOutOfBoundsException ex) { catch (RuntimeException ex) { finally { System.out.println("\nWe are done. Finally!!"); 11/12/2018 IT 179

7 String fname; Scanner inS = null; fname = (args.length != 0 ? args[0] : "/home/ADILSTU/ccli/Public/IT179/exps.txt"); try { File file = new File(fname); inS = new Scanner(file); } catch (FileNotFoundException ex) { System.out.println("Can't find file: "+fname); System.exit(-1); 11/12/2018 IT 179

8 Categories of exceptions
These categorization affect compile-time behavior only 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 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/12/2018 IT 179

9 Unchecked Exceptions handle:
Logical error – The program is incorrect  Environmental error - e.g. out of memory I/O error – e.g., lost network connection. Checked Exceptions handle: 11/12/2018 IT 179

10 Java’s Exception Hierarchy
Unchecked Checked

11 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/12/2018 IT 179

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

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

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

15 Consider the worst part of the program
if (....) { } else { n2  n3 n3 11/12/2018 IT 179

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

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

18 O(n-s)=O(n) O(3) = O(1) // 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/12/2018 IT 179

19 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); } Selection Sort (a quadratic sort) O(n2) 9 6 3 4 2 8 5 2 6 3 4 9 8 5 2 3 6 4 9 8 5 2 3 4 6 9 8 5 2 3 4 5 9 8 6 2 3 4 5 6 8 9 i i j i j i i j i j j j 11/12/2018 IT 179

20 Logarithm (binary search) Linear (sequential search)
n log n (quick sort) Quadratic (n2, selection sort, insertion sort) Polynomial (Primality test) Nondeterministic polynomial (Traveling Salesman problem) Exponential (en Backtracking) 11/12/2018 IT 179

21 Big-Oh (): Linear Search
TlinearSearch(n) = 3n + 3 = (n) for c = 4 and n0 = 0, in the worst case. n2 TlinearSearch(n) = (n2) But this is an abuse of O-notation 4n 3n + 3 11/12/2018 IT 179

22 Some Common Computing Times
log2n n n log2n n2 2n 1 2 4 8 16 3 24 64 256 65,536 5 32 160 1,024 4,294,967,296 6 384 4,096 1.84  1019 7 128 896 16,384 3.40  1038 2,048 1.16  1077 9 512 4,608 262,144 1.34  10154 10 10,240 1,048,576 1.80  10308 11/12/2018 IT 179


Download ppt "What/how do we care about a program?"

Similar presentations


Ads by Google