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

What/how do we care about a program?

Robustness

Correctness

Efficiency (speed, space)

04/20/23 1IT 179

Software Testing

Error Handling

Efficiency Analysis

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

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.

04/20/23 2IT 179

Let’s handle it!! Beyond programmer’s control

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

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

04/20/23 3IT 179

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

Exception throwing

The sequence of calling

The sequence of throwingexceptions

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

04/20/23 4IT 179

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

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.

04/20/23 5IT 179

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

Unchecked Exceptions handle:

• Logic error – The program is incorrect

• Environment error - e.g. out of memory

• I/O error – e.g., lost network connection.

04/20/23 6IT 179

Checked Exceptions handle:

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

Java’s Exception Hierarchy

Unchecked

Checked

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

Try-Catch-Finally Blockstry { 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}

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

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<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"); 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!!"); } }}

04/20/23 9IT 179

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

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

04/20/23 10IT 179

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

An algorithm’s performance can be measured by:

• Time complexity

• Space complexity

04/20/23 11IT 179

How long it takes to execute.

How much additional space the algorithm needs. (memory, storage, tape).

Efficiency (speed, space) Efficiency Analysis

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

Time Complexity: Count Instructions

Statements Cost

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;

110 else

11 return 0.0f;

12 }

TOTAL 3n + 5

How many timeswill each instructionexecute?

04/20/23 12IT 179

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

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

= (½)(n2 +n)

n2

04/20/23 13IT 179

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

Consider the worst part of the program

if (....) { ......... .........} else { ......... .........}

n2

n3

n3

04/20/23 14IT 179

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

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

n3

n2

O(c+n+n3+n2)

O(n3)

04/20/23 15IT 179

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

Linear SearchStatements Cost

1 int 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

04/20/23 16IT 179

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

04/20/23 IT 179 17

// 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)

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

04/20/23 IT 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

ij

(a quadratic

sort) O(n2)

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

04/20/23 19IT 179

1. Logarithm (binary search)

2. Linear (sequential search)

3. n log n (quick sort)

4. Quadratic (n2, selection sort, insertion sort)

5. Polynomial (Primality test)

6. Nondeterministic polynomial (Traveling Salesman problem)

7. Exponential (en Backtracking)

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

Big-Oh (): Linear SearchTlinearSearch(n) = 3n + 3 = (n) for c = 4 and n0 = 0, in the worst case.

4n

n2

3n + 3

TlinearSearch(n) = (n2)But this is an abuse ofO-notation

04/20/23 20IT 179

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

Some Common Computing Timeslog2n n n log2n n2 2n

1 2 2 4 4

2 4 8 16 16

3 8 24 64 256

4 16 64 256 65,536

5 32 160 1,024 4,294,967,296

6 64 384 4,096 1.84 1019

7 128 896 16,384 3.40 1038

8 256 2,048 65,536 1.16 1077

9 512 4,608 262,144 1.34 10154

10 1,024 10,240 1,048,576 1.80 10308

04/20/23 21IT 179

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

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.

04/20/23 22IT 179

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

Asymptotic notation (for all but finitely many)

• Upper bound: Big-O notation, O(f)g O(f) iff c n0 n [n≥ n0 g(n) cf(n)]

• Lower bound: Big- notation, (f)g (f) iff c n0 n [n≥ n0 g(n) ≥ cf(n)]

• Approximation: Big- notation, (f)g (f) iff c1 c2 n0 n [n≥ n0 c2f(n)] g(n) c2f(n)]

equivalently g O(f) and g (f) • Tied Upper bound: Little-o notation, o(f)

g o(f) iff c n0 n [n≥ n0 g(n) cf(n)]

04/20/23 23IT 179


Top Related