OOP: Java (java8b.ppt) by HC120520191456


									OOP: Java (java8b.ppt)
   String Tokenizing (3.1.1)
   Streams & File I/O (4.1.1)
   User-Defined Exceptions (3.3.4)
   Ex 7 Dow Jones Case Study

                                      CIT503 EW
StringTokenizer Class
   It takes an input string and parses it into "tokens",
    allowing the tokens to be read one at a time.

   call the nextToken method for each item you want
   call the nextToken method for each element and
   to instance variable
       ex: name = st.nextToken();
       ex: qpa=Double.parseDouble(st.nextToken());

                                                            CIT503 EW
StringTokenizer Class Methods
 stringTokenizer(String str, String DELIM) -
 nextToken() - Parses the next token from the input
  stream of this tokenizer.
 hasMoreTokens() – returns true if

 more tokens exist
 countTokens() - returns # of tokens in this tokenizer

 Lets Look at StringTokenizerClassDemo

                                                     CIT503 EW
Streams & File I/O
     Stream refers to any source of input or output in a Java
     Java has several classes to handle I/O. Check the API
      under java.io

     We have used two of these:
          BufferedReader to handle the input stream
          PrintWriter to handle the output stream

                                                                 CIT503 EW
Review of Input/Output in Java

   Java uses two constants:
       in to represent standard input from the keyboard
       out to represent standard output to the console window
   in belongs to the InputStream Class
       System.in
   out belongs to the OutputStream Class
       System.out

                                                                 CIT503 EW
   To handle the input stream we use readLine()
   Then we can use the wrapper classes for each type
   To handle the output stream we use several methods
       println(), print() and flush()
   files can be handled as input streams as well

                                                    CIT503 EW
Tokenizers in Java
   Files in general have more than one piece of data per
        Joe White 3.45 29
        Janet Blue 3.75 31
   Each line has more than one item, or token
   In Java, we can read a line to determine how many
    tokens it has and what their values are.
   There are two classes :
           StringTokenizer
           and StreamTokenizer.

                                                      CIT503 EW
Tokenizers in Java
   The tokens are separated by ‘white space characters’
       blank spaces
       tabs (vertical and horizontal)
       form feed char
       new line

                                                     CIT503 EW
StringTokenizer & Exceptions
   Need to handle exceptions
       Bad numeric data types
       Lines with too few or too many elements
       Can use multiple catch clauses to handle different

                                                             CIT503 EW
StringTokenizer & Exceptions
   Need to handle exceptions
       Bad numeric data types
       Lines with too few or too many elements
       Can use multiple catch clauses to handle different

                                                             CIT503 EW
StringTokenizer & Exceptions
   NumberFormatException
   NoSuchElementException
       Thrown when you attempt to get a token and it does not
       Ex: data required to have 5 tokens
         It has only 4 – when you attempt the get the 5th
         • An exception is thrown

                                                             CIT503 EW
File Input/Output in Java
   Text file are commonly used for sources of input and
   All we need to do is to replace the in and out
    constants with another object that relates to a file
   Files have a name and a path (location)
    a:\mydemos\stocks.txt ==> “a:\\mydemos\\stocks.txt” on
    c:\temp\users\data.txt ==> “c:\\temp\\users\\data.txt”

   Otherwise, data files must be in the same directory
    as your .java program!
                                                         CIT503 EW
File Input/Output in Java

   There are many classes to handle files
   Disk files are supported by the classes FileReader
    and FileWriter. Data files are instances of either
    class. To “read” from a file (using a file as input
    source) use FileReader.
   To “write” to a file (using a file as output source) use

                                                         CIT503 EW
File Input/Output in Java
   Files need to be open before you use them
       instantiate a file
   Program must make sure that a file has been opened
    before it tries to read data from it!
   Files should be closed when we are done using them
       use the close() method
   Lets look at examples for reading text files:
       FileReadStrings, +ReadTextFile, +ReadTextFile2,
   And doing both I/O:
       Prices, +Copy
                                                      CIT503 EW
   An interface is not part of the class hierarchy
   An interface is not a class, and cannot be used to
    instantiate an object
   Constants in an interface can be used in the
    implementing class as if they were declared locally
       This feature provides a convenient technique for
        distributing common constant values among multiple
   See online java notes ch 53

                                                             CIT503 EW
Creating Exception Classes

   You can create your own exception classes to handle
   Such a class must be derived from class Throwable
   You can be more specific and extend classes further
    down the hierarchy
       class MyException extends Exception {..}
   printStackTrace()
       You can trace the methods the computer uses to find a
        catch block
   See Digit, DigitOutOfBoundsException, DigitTester

                                                                CIT503 EW
finally Keyword
   finally defines a block of code guaranteed to be
    executed before the computer exits a method
       regardless of whether an exception was thrown or return
        statement was executed in a try block
       syntax:
            finally { .... statements }
       See ch9Ex6.java

                                                             CIT503 EW
EX Dow Jones Assignment

   See Class Handout
       Case Study Specs
       Data Files
       Sample Executable
            Hw7exe.jar
       Class Hierarchy UML View

   Support material on WAN
       Overall Project Design
       Design & Test of each class

                                      CIT503 EW

To top