CS 121 Intro to Programming Java Lecture 12

  • Slides: 18
Download presentation
CS 121 – Intro to Programming: Java - Lecture 12 Announcements New Owl assignment

CS 121 – Intro to Programming: Java - Lecture 12 Announcements New Owl assignment up soon (today? ) For this week: read Ch 8, sections 0 -3 Programming assignment six due Friday 12/3 Last programming assignment up soon For Thursday: read 169 -173 -- the material on the String. Tokenizer class

Today We’ll cover two fairly complicated ideas: Exceptions - mechanics/bullet-proofing File io (the file

Today We’ll cover two fairly complicated ideas: Exceptions - mechanics/bullet-proofing File io (the file stuff may spill over a bit into next week’s lecture)

Exceptions • a uniform way to handle exceptional behavior • a clean way to

Exceptions • a uniform way to handle exceptional behavior • a clean way to check for errors without cluttering code • gives us an organizing protocol for thinking about errors • gives a great boost to the principle of code reuse: a single class may be used differently in different applications, and the exception mechansism gives us a graceful way to allow for this. • two kinds of exceptions: checked(IOEX) and unchecked(Array. Out. OF) Example: Imagine a text file of integers. If the file represents data from a deep space probe, doing something with the data - e. g. adding the numbers - would surely involve skipping over the occasional non-integer that’s present because deep space transmissions are funky. . But if the same file represents my bank account deposits, I don’t want to skip over that deposit of $134 j 9 !!

Checked exceptions - exceptions you must check for explitly Unchecked exceptions - mostly tied

Checked exceptions - exceptions you must check for explitly Unchecked exceptions - mostly tied to potential logical errors- too complicated to check for (there are too many of them!) IOException - checked exception - we’ll see it at work soon. . Array. Index. Out. Of. Bounds. Exception - unchecked Exceptions are Objects! - You can create your own. . One important software development activity: bullet-proofing (idiot-proofing. . )

The general form of the exception handling mechanism: try{ statement 1 statement 2 …

The general form of the exception handling mechanism: try{ statement 1 statement 2 … } catch (Exceptionkind 1 e){do stuff. . } catch (Exceptionkind 2 e){do stuff. . } finally{blah}

public class Bullet. Proof{ public static void main(String[] args){ Console. Window c = new

public class Bullet. Proof{ public static void main(String[] args){ Console. Window c = new Console. Window(); int scoreboard[] = new int[26]; for(int j = 0; j < 26; j++) scoreboard[j] = (int)(Math. random()*100); int n = 0, k = 0; boolean good = false; while (!good){ c. out. println("enter an index, I'll give you the value"); try { n = c. input. read. Int(); k = scoreboard[n]; good = true; } catch(Array. Index. Out. Of. Bounds. Exception e){ c. out. println("out of bounds- try again"); } } c. out. println("contents of cell " + n + " is " + k); } }

import java. io. *; public class Echo{ String fname; //the text file name //the

import java. io. *; public class Echo{ String fname; //the text file name //the data buffer Buffered. Reader in_dat; public Echo(String data_file) throws IOException{ fname = data_file; File in_data = new File(fname); in_dat = new Buffered. Reader(new File. Reader(in_data)); }

public void get. Lines() throws IOException { String line = in_dat. read. Line(); while(line

public void get. Lines() throws IOException { String line = in_dat. read. Line(); while(line != null) { process. Line(line); line = in_dat. read. Line(); } close_file(); } public void process. Line(String line){ System. out. println(line); } public void close_file() throws IOException{ in_dat. close(); } }

import java. io. *; import element. *; public class Echo. Tester{ public static void

import java. io. *; import element. *; public class Echo. Tester{ public static void main(String[] args){ String file. Name = “Sum. Of. Cubes. java”; try{ Echo e = new Echo(file. Name); e. get. Lines(); } catch(IOException ex) {System. out. println(ex); } } }

œ´œ ----j. GRASP exec: java Echo. Tester œœßœclass Sum. Of. Cubes{ œœßœ public int

œ´œ ----j. GRASP exec: java Echo. Tester œœßœclass Sum. Of. Cubes{ œœßœ public int cube. Sum(int k){ œœßœ int sum, cur, ones, tens, hundreds; œœßœ cur = k; œœßœ ones = cur % 10; œœßœ cur = cur / 10; œœßœ tens = cur % 10; œœßœ cur = cur / 10; œœßœ hundreds = cur % 10; œœßœ return (cube(ones) + cube (tens) + cube(hundreds)); œœßœ } …

import element. *; import java. io. *; public class Console. Echo extends Echo{ Console.

import element. *; import java. io. *; public class Console. Echo extends Echo{ Console. Window d; Console. Echo(Console. Window w, String f) throws IOException { super(f); d = w; } public void process. Line(String line){ d. out. println(line); }

import java. io. *; import element. *; public class Echo. Tester{ public static void

import java. io. *; import element. *; public class Echo. Tester{ public static void main(String[] args){ Console. Window w = new Console. Window(); w. out. println("enter name of a file from the current directory"); String file. Name = w. input. read. String(); try{ Console. Echo e = new Console. Echo(w, file. Name); e. get. Lines(); } catch(IOException ex) {System. out. println(ex); } } }

œ´œ ----j. GRASP exec: java Echo. Tester œœßœjava. io. File. Not. Found. Exception: a;

œ´œ ----j. GRASP exec: java Echo. Tester œœßœjava. io. File. Not. Found. Exception: a; sdfhab (No such file or directory)

public class Line. Count extends Echo{ Console. Window d; private int count = 0;

public class Line. Count extends Echo{ Console. Window d; private int count = 0; Line. Count(Console. Window w, String f) throws IOException { super(f); d = w; } public int get. Count(){return count; } public void process. Line(String line){ count++; } }

public class Echo. Tester{ public static void main(String[] args){ Console. Window w = new

public class Echo. Tester{ public static void main(String[] args){ Console. Window w = new Console. Window(); w. out. println("enter name of a file from the current directory"); Line. Count e = null; String file. Name = w. input. read. String(); try{ e = new Line. Count(w, file. Name); e. get. Lines(); } catch(IOException ex) {System. out. println(ex); } w. out. println(" line count in file is: " + e. get. Count()); } }