******************************************************************************* 0) Administrative - hw: come to next class with questions for the review - A4/exam questions? Assignment 3 back: show sample solution, talk about common issues - no laundry list of data members - no methods for days: use helpers - no static: more than one copy should be able to run (setup not in main) - don't import something and then use the explicit pathname - use parameters AND return values to pass info b/w classes - DON'T use them too pass info within a class - put functionality in the class closest to the data (pickup, move, print) - take common things out of conditionals and combine similar methods (shorten) 1) Last time- exercises with Strings and exceptions - we'll go over answers next time as part of exam review - chapter 8 quiz 2) Go over file I/O in groups 3) Highlights A. Printing- how can you pass print() a String, char, or int? - overloaded method that takes any primitive type What happens if you pass an object in there? Volunteer vol = new Volunteer(...); S.o.p(vol); -> "Volunteer@13fac" (internal storage info) Point p = new Point(1,2); S.o.p(p); -> "Point[1,2]" (something more useful) Why the difference? - every class has a default method toString() by inheritance - when you print it, this gets called, and returns a String - the default version returns this internal storage info - if you define a new one, it overrides the default - Point class has done this: what must it look like? public String toString() { return "Point[" + x + "," + y + "]"; } - you could make one for Volunteer if you wanted B. What are the three classes involved in console input? InputStream (System.in) - represents the connection InputStreamReader - gets characters BufferedReader - gets whole lines What about in console output? PrintStream (System.out) - has print and println methods System.err is another; it's for errors (often same as System.out) How did we get them? - System class is included automatically in the program - it has these data members: public static InputStream in = new ... public static PrintStream out = new ... - your code can get to them like any public static data member C. File input is parallel to console input File - represents the file FileReader - gets characters BufferedReader - gets whole lines File file = new File("/etc/passwd"); BufferedReader buf = new BufferedReader(new FileReader(file)); String line = buf.readLine(); while (line != null) { // do something nefarious line = buf.readLine(); } buf.close(); - reads each line, returns null when none are left - making the reader could cause a FileNotFoundException - the readLine() could cause an IOException (both are checked) - could also just use the filename instead of a File object D. File output is similar to console output, but you have to make your own writer object because "out" only works for the console FileWriter - connects to the file PrintWriter - has the print and println methods PrintWriter p = new PrintWriter(new FileWriter("f", false), true); - the string is the filename - first boolean is whether data is appended (T) or overwritten (F) - second boolean is whether data is flushed on println pw.print(...); // Doesn't go right away pw.println(...); // Goes right away if set to do so pw.flush(); // Force things to go pw.close(); // Final flush and close connection E. StringTokenizer- helps to read files with specified input formats - breaks up a line into "tokens" around "delimiters" Gandalf:grey:Saruman:white:Radagast:brown - set up file reading as before, make a tokenizer for each line String line = buf.readLine(); while (line != null) { StringTokenizer st = new StringTokenizer(line, ":"); while (st.hasMoreTokens()) { makeWizard(st.nextToken(), st.nextToken()); } line = buf.readLine(); } - it's a lot like an iterator - makes string processing easier, but you could do it all yourself 4) Exercises A. Copy one file into another - you write this, then we go over it 5) Summary - printing and toString() methods - file I/O and parallels with console I/O - StringTokenizer *******************************************************************************