What if... There is no file with the name given to the File - - PowerPoint PPT Presentation

what if
SMART_READER_LITE
LIVE PREVIEW

What if... There is no file with the name given to the File - - PowerPoint PPT Presentation

What if... There is no file with the name given to the File constructor: new File (IDontExist.txt); Throws FileNotFoundException 1 Handling Exceptions Call that throws the exception try { File f = new File (file.txt);


slide-1
SLIDE 1

What if...

There is no file with the name given to the File constructor: new File (“IDontExist.txt”); Throws FileNotFoundException

Handling Exceptions

Call that throws the exception Exception Handler Exception being handled try { File f = new File (“file.txt”); Scanner in = new Scanner (f); ... } catch (FileNotFoundException e) { ... }

What happens if no handler

public void someMethod() { File f = new File (“file.txt”); Scanner in = new Scanner (f); ... }

Compiler error: Unhandled exception type FileNotFoundException Solution: method must declare that it throws the exception:

public void someMethod() throws FileNotFoundException

1 2 3 Wednesday, February 27, 13

slide-2
SLIDE 2

Exceptions and Contracts

Exceptions should be documented in a method’ s contract What exception is thrown Under what conditions is the exception thrown

/** Does something really wonderful @throws FileNotFoundException if the file named file.txt does not exist. */

public void someMethod () throws FileNotFoundException

What if...

You call nextInt() and the next thing in the file is not an integer? InputMismatchException You call one of the next methods and there is nothing left in the fille? NoSuchElementException You call one of the next methods but the file is closed? IllegalStateException

Be sure to call has… methods before next… methods to avoid these exceptions.

Doesn’ t that beg the question?

What if hasNextInt() returns false? What do you do??? Report an error to the user? Use a default value? Ignore that part of the input but try to continue somehow? Throw an exception?

4 5 6 Wednesday, February 27, 13

slide-3
SLIDE 3

Partial Exception Class Hierarchy

Throwable Exception Error OutOfMemoryError AssertionError ArrayIndexOutOfBoundsException NullPointerException RuntimeException FileNotFoundException

Sending Output to a File

To open a file for writing: PrintWriter outFile = new PrintWriter ( new File (filename))); File constructor - takes a file name and creates a file

  • bject

PrintWriter constructor - takes a File parameter and provides methods to write individual pieces of data to the file

Reading from a URL

To open a url for reading: Scanner outFile = new Scanner ( new URL (url).openStream()); URL constructor - takes a string URL and creates a URL

  • bject
  • penStream - connects to the remote Web page

Scanner constructor is the same as before

7 8 9 Wednesday, February 27, 13

slide-4
SLIDE 4

Measuring Time

Idea: Implement a program. Time how long it takes. Problems? Effects of the programming language? Effects of the processor? Effects of the amount of memory? Effects of other things running on the computer? Effects of the input values? Effects of the input size?

Searching for a Value in an Array

int indexOf (int[] x, int target) { for (int i = 0; i < x.length; i++) { if (x[i] == target) { return i; } } return -1; }

10 11 12 Wednesday, February 27, 13

slide-5
SLIDE 5

Finding Distance between Closest Pair of Points

double closestDistance (Point[] pts) { double smallestDist = distance(pts[0], pts[1]); for (int i = 0; i < pts.length; i++) { for (int j = 0; j < pts.length; j++) { if (i != j) { double nextDist = distance(pts[i], pts[j]); if (nextDist < smallestDist) { smallestDist = nextDist; } } } } return smallestDist; }

O() notation

Capture the running time of the algorithm as a function of the input size Consider only the worst case Make no assumptions about input other than its size In the worst case, the search function looks at every array element. If the array has n elements, we would say the algorithm is O(n), or linear Closest distance looks at every pair of points and is O(n2), or quadratic

Running Times as Functions of Input Size

13 14 15 Wednesday, February 27, 13

slide-6
SLIDE 6

Analyzing an Algorithm’ s Running Time

Count the number of steps in an algorithm Determine how this number of steps scales with input size. Sum elements in array: index = 0; sum = 0; while (index < arrayLength) { sum = sum + array[index] index++; }

1 1 arrayLength arrayLength arrayLength 3*arrayLength + 2

Total

Big O Notation

Let T(n) be a function that defines the worst-case running time of an algorithm. T(n) is O(f(n)) if T(n) ≤ c · f(n), where c is a constant ≥ 0 for all n ≥ n0 Example: Let T(n) = 3n + 2 T(n) is O(n) because 3n + 2 ≤ 4n for all n ≥ 2 O(n) is the asymptotic upper bound of T(n).

3n + 2 4n n0 = 2

Visualizing O()

3n + 2 is O(n)

16 17 18 Wednesday, February 27, 13

slide-7
SLIDE 7

Searching for a Value in an Array

int indexOf (int[] x, int target) { for (int i = 0; i < x.length; i++) { if (x[i] == target) { return i; } } return -1; }

Steps x.length x.length at most 1 at most 1 Total = 2 * x.length + 1 more commonly written as 2*n + 1

O() Cost of Linear Search

T(n) is O(f(n)) if T(n) ≤ c · f(n), where c is a constant ≥ 0 for all n ≥ n0 For linear search, T(n) = 2n + 1 2n + 1 ≤ 3 * n, n ≥ 1 Therefore, linear search is O(n)

Steps

Finding Distance between Closest Pair of Points

double closestDistance (Point[] pts) { double smallestDist = distance(pts[0], pts[1]); for (int i = 0; i < pts.length; i++) { for (int j = 0; j < pts.length; j++) { if (i != j) { double nextDist = distance(pts[i], pts[j]); if (nextDist < smallestDist) { smallestDist = nextDist; } } } } return smallestDist; }

19 20 21 Wednesday, February 27, 13

slide-8
SLIDE 8

Steps 1 n n2 n2 n2 - n at most n2-n 1 Total = 5n2 - 2n + 2

Finding Distance between Closest Pair of Points

double closestDistance (Point[] pts) { double smallestDist = distance(pts[0], pts[1]); for (int i = 0; i < pts.length; i++) { for (int j = 0; j < pts.length; j++) { if (i != j) { double nextDist = distance(pts[i], pts[j]); if (nextDist < smallestDist) { smallestDist = nextDist; } } } } return smallestDist; }

n2 - n

Visualizing O()

n

5n2 - 2n + 2

5n2 1 5 5 2 18 20 3 41 45 4 74 80 5 117 125

5n2 - 2n + 2 is O(n2) In general, we can drop coefficients and low-

  • rder terms when determining O()

Summary

Want to understand algorithm’ s efficiency relative to input size. In order of decreasing efficiency: O(1) - means independent of input size O(log n) O(n) O(n log n) - fastest sorting algorithms O(nk), for constant k > 1 O(kn), for constant k > 1 O(n!)

22 23 24 Wednesday, February 27, 13