LECTURE NOTES 10 DECEMBER 2004 ASSIGNMENTS 11:58 PM CodeLab #13 12/15 Assignment 5 Code REVIEW File IO: files File class current directory path streams source and destination buffers text files BufferedReader class PrintWriter class exception handling StringTokenizer class File srcFile, dstFile; // declare the object variables boolean repeat = false; // assume a valid path is entered on first try do { System.out.print("Enter the path of the source file: "); srcFile = new File(stdin.readLine()); // create File object if (!srcFile.exists()) { System.out.println("ERROR: file not found"); repeat = true; } else if (!srcFile.isFile() || !srcFile.canRead()) { System.out.println("ERROR: file not readable"); repeat = true; } else repeat = false; } while (repeat); repeat = false; // assume a valid path is entered on the first try do { System.out.print("Enter the path of the destination file: "); dstFile = new File(stdin.readLine()); // create the File object if (dstFile.exists() && (dstFile.isDirectory() || !dstFile.canWrite())) { System.out.println("ERROR: file not writeable"); repeat = true; } else repeat = false; } while (repeat); BufferedReader inFile = new BufferedReader(new FileReader(srcFile)); PrintWriter outFile = new PrintWriter( new BufferedWriter( new FileWriter(dstFile))); while (inFile.ready()) { // readLine method reads an entire line (same as console input) String line = inFile.readLine(); StringTokenizer tokenizer = new StringTokenizer(line, "|"); String name = tokenizer.nextToken(); int year = Integer.parseInt(tokenizer.nextToken()); System.out.println("Name: " + name + ", Year: " + year); outFile.println(name + "|" + year + "|"); } // Finally, always close your files before finishing to guarantee that // buffered data isn't lost. inFile.close(); outFile.close(); } Arrays (Chapter 10): declaration and creation of one dimensional arrays arrays of primitives vs. arrays of objects indexed expressions elements and subarrays fixed-sized vs. variable-sized creation array manipulations (e.g. linear search, average, display, add, remove) garbage collection passing arrays to and returning arrays from methods self-referencing pointer (i.e.this) accessors and mutators Scope Rules (to associate identifier with a declaration) zero-based indexing defensive programming single-task object principle interface and controller objects array.length this memory diagrams of arrays Inheritance (Chapter 13): class hierarchies inheritance signature prototype superclass and subclass base class and derived class ancestors and descendants Object class and its methods (clone, equals, getClass, toString) method overriding instanceof operator casting from Object references polymorphism polymorphic message (dynamic binding) related versus unrelated classes neither is an ancesotor or descendent of the other, nor do they share an ancestor sibiling classes abstract class inheritance vs. interface protected extends super OLD TOPICS Object-Oriented Programming Software Development Classes and Objects Messages and Methods Class and Instance Data Values Software Life Cycle: analysis, design, coding, testing, operation Declaring variables and objects Comments Import statements Method declaration Compile-Edit-Run cycle String class Date and SimpleDateFormat classes Arithmetic expressions Constants Getting numerical input values Standard output Standard input Math class GregorianCalendar class Defining instantiable classes Instantiable classes and constructors Visibility modifiers Local variables Return values Parameter passing Accessors Mutators Overloaded methods Passing and returning objects Packages boolean type Boolean expressions if statement Nested-if statements switch statement while loop do-while loop for statement nested for statement random number generation Catching exceptions Throwing exceptions Propagating exceptions Types of exceptions Programmer-defined exceptions StringBuffer