10.9 HOMEWORK


10.9 HOMEWORK

  1. Many methods defined for the Java I/O stream classes throw exceptions of type IOException. Therefore, in a large Java program with numerous I/O operations, it is sometimes difficult to pinpoint the statement that might be responsible for generating this type of an exception.[07] This homework exercise is about using a programmer-defined exception type that can be more helpful for localizing a problem in a designated set of I/O operations.

    Define a new exception by

         class FileIOException extends Exception {} 

    to be used for a new Java class, FileIO with the static methods listed below. Each method should re-throw any exceptions of the form IOException as a FileIOException.

     
    public static String readOneString( String filename ) throws File IOException public static int readOneInt( String filename ) throws FileIOException public static double readOneDouble( String filename ) throws FileIOException public static String[] readAllStrings( String filename ) throws FileIOException public static int[] readAllInts( String filename ) throws FileIOException public static double[] readAllDoubles ( String filename) throws FileIOException public static void writeOneString( String data, String filename ) throws FileIOException public static void writeOneInt(int data, String filename) throws FileIOException public static void writeOneDouble(double data, String filename) throws FileIOException public static void writeAllStrings(String[] strArray, String filename) throws FileIOException public static void writeAllInts( int[] intArray, String filename) throws FileIOException public static void writeAllDoubles( doubles[] doubleArray, String filename) throws FileIOException

    Note that the first three function headers listed above are for reading just one data item from a file; the next three are for reading all the data in a file and returning it in the form of an array of the appropriate type; the next three are for writing just one item into a file; and, finally, the last three are for writing arrays of different types into an output file. Each "read" method listed above should open a source file for reading data and then close it after the data are acquired. Similarly, each "write" method should open a destination file for writing and then close it after the writing is done.

  2. The goal of this homework is to use C++ and Java exceptions to terminate search when you find a given word in a dictionary of words stored in the form of a binary tree. To keep matters simple, assume that all the words are stored in the leaf nodes of the tree and that the word stored at each internal node is the ‘greater' of the words at the two child nodes according to ASCII-based lexicographic ordering of the words. You'd obviously need to first write C++ and Java programs that can construct binary-tree dictionaries from the words in a text file. And then write search programs in each language that search for a query word by descending down the dictionary tree. When there is a match between a query word and the word stored at a node, throw an exception to terminate the search.

  3. Write a Java class WordFinder for locating a a word in a text file; the designated word could be a substring of a longer word. Your class should possess one method with the following prototype:

         public static boolean searchFor(String file, String word) 

    The method should use the readAllStrings method of the FileIO class of Problem 1 to read all the words in the text file. Concatenate the words thus read into one long string and use the substr method of the String class to locate the desired word. Make sure your program has an appropriate handler for the FileIOException thrown by the methods of the FileIO class.

  4. Write a C++ function that returns an input stream to a file. But if something were to go wrong, such as if the file was missing, the function should throw the following user-specified exception:

          class IfstreamException {}; 
  5. Why is the following not a good example for demonstrating the utility of the finally clause in a try-catch-finally structure? The program tries to locate the character ‘A' in a text file. If it finds the character, the program prints out a message to that effect and terminates, but only after closing the input stream properly in the finally clause. If it does not find the character, a message to that effect is printed out and the input stream is closed as before in the finally clause.

     
    import java.io.*; class Test { public static void main(String[] args) { FileReader input = null; try { input = new FileReader("infile"); int ch; while (-1 != (ch = input.read())) { if ( ch == ‘A‘ ) { System.out.println("foundit"); System.exit(0); } } System.out.println("did not find it"); } catch( FileNotFoundException e ) { System.out.println( "file not located" ); } catch(IOException e) { System.out.println("problems with I/O"); } finally { if ( input != null ) { try { System.out.println("closing input stream"); input.close(); } catch(IOException e) { System.out.println("problems with close()"); } } } } }

[07]Although this problem can be mitigated by invoking printStackTrace() on a caught exception and/or by printing out some features of the state of the environment when an exception is caught.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net