what if
play

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


  1. 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”); � � � Scanner in = new Scanner (f); � � � ... } catch (FileNotFoundException e) { � � � ... } Exception being handled Exception Handler 2 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 3 Wednesday, February 27, 13

  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 4 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. 5 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? 6 Wednesday, February 27, 13

  3. Partial Exception Class Hierarchy Throwable Error Exception FileNotFoundException RuntimeException AssertionError OutOfMemoryError NullPointerException ArrayIndexOutOfBoundsException 7 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 object PrintWriter constructor - takes a File parameter and provides methods to write individual pieces of data to the file 8 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 object openStream - connects to the remote Web page Scanner constructor is the same as before 9 Wednesday, February 27, 13

  4. 10 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? 11 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; } 12 Wednesday, February 27, 13

  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; } 13 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(n 2 ), or quadratic 14 Running Times as Functions of Input Size 15 Wednesday, February 27, 13

  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; 1 sum = 0; 1 while (index < arrayLength) { arrayLength sum = sum + array[index] arrayLength arrayLength index++; Total 3*arrayLength + 2 } 16 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 ≥ n 0 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). 17 Visualizing O() 4n 3n + 2 n 0 = 2 3n + 2 is O(n) 18 Wednesday, February 27, 13

  7. Searching for a Value in an Array Steps int indexOf (int[] x, int target) { x.length for (int i = 0; i < x.length; i++) { x.length if (x[i] == target) { at most 1 return i; } } at most 1 return -1; } Total = 2 * x.length + 1 more commonly written as 2*n + 1 19 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 ≥ n 0 For linear search, T(n) = 2n + 1 2n + 1 ≤ 3 * n, n ≥ 1 Therefore, linear search is O(n) 20 Finding Distance between Closest Pair of Points Steps 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; } 21 Wednesday, February 27, 13

  8. Finding Distance between Closest Pair of Points Steps double closestDistance (Point[] pts) { 1 double smallestDist = distance(pts[0], pts[1]); n for (int i = 0; i < pts.length; i++) { n 2 for (int j = 0; j < pts.length; j++) { n 2 if (i != j) { n 2 - n double nextDist = distance(pts[i], pts[j]); n 2 - n if (nextDist < smallestDist) { at most n 2 -n smallestDist = nextDist; } } } } 1 return smallestDist; } Total = 5n 2 - 2n + 2 22 Visualizing O() n 5n 2 - 2n + 2 5n 2 1 5 5 2 18 20 3 41 45 4 74 80 5 117 125 5n 2 - 2n + 2 is O(n 2 ) In general, we can drop coefficients and low- order terms when determining O() 23 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(n k ), for constant k > 1 O(k n ), for constant k > 1 O(n!) 24 Wednesday, February 27, 13

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend