Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 StringTokenizer class

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

3 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(ArrayOutOF) 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 $134j9 !!

4 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.. ArrayIndexOutOfBoundsException - unchecked Exceptions are Objects! - You can create your own.. One important software development activity: bullet-proofing (idiot-proofing..)

5 The general form of the exception handling mechanism: try{ statement1 statement2 … } catch (Exceptionkind1 e){do stuff..} catch (Exceptionkind2 e){do stuff..} finally{blah blah}

6 public class BulletProof{ public static void main(String[] args){ ConsoleWindow c = new ConsoleWindow(); 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.readInt(); k = scoreboard[n]; good = true;} catch(ArrayIndexOutOfBoundsException e){ c.out.println("out of bounds- try again"); } } c.out.println("contents of cell " + n + " is " + k); } }

7

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

9 public void getLines() throws IOException { String line = in_dat.readLine(); while(line != null) { processLine(line); line = in_dat.readLine();} close_file(); } public void processLine(String line){ System.out.println(line); } public void close_file() throws IOException{ in_dat.close(); }

10 import java.io.*; import element.*; public class EchoTester{ public static void main(String[] args){ String fileName = “SumOfCubes.java”; try{ Echo e = new Echo(fileName); e.getLines(); } catch(IOException ex) {System.out.println(ex);} } }

11 œ´œ ----jGRASP exec: java EchoTester œœßœclass SumOfCubes{ œœßœ œœßœ œœßœ public int cubeSum(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)); œœßœ } …

12 import element.*; import java.io.*; public class ConsoleEcho extends Echo{ ConsoleWindow d; ConsoleEcho(ConsoleWindow w,String f) throws IOException { super(f); d = w; } public void processLine(String line){ d.out.println(line); }

13 import java.io.*; import element.*; public class EchoTester{ public static void main(String[] args){ ConsoleWindow w = new ConsoleWindow(); w.out.println("enter name of a file from the current directory"); String fileName = w.input.readString(); try{ ConsoleEcho e = new ConsoleEcho(w,fileName); e.getLines(); } catch(IOException ex) {System.out.println(ex);} } }

14

15 œ´œ ----jGRASP exec: java EchoTester œœßœjava.io.FileNotFoundException: a;sdfhab (No such file or directory)

16 public class LineCount extends Echo{ ConsoleWindow d; private int count = 0; LineCount(ConsoleWindow w,String f) throws IOException { super(f); d = w; } public int getCount(){return count;} public void processLine(String line){ count++; } }

17 public class EchoTester{ public static void main(String[] args){ ConsoleWindow w = new ConsoleWindow(); w.out.println("enter name of a file from the current directory"); LineCount e = null; String fileName = w.input.readString(); try{ e = new LineCount(w,fileName); e.getLines(); } catch(IOException ex) {System.out.println(ex);} w.out.println(" line count in file is: " + e.getCount()); } }

18


Download ppt "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."

Similar presentations


Ads by Google