Foundations of Software Design Fall 2002 Marti Hearst - PowerPoint PPT Presentation

About This Presentation
Title:

Foundations of Software Design Fall 2002 Marti Hearst

Description:

Fish freddy = new Fish('yellow'); Vector v = new Vector(); v.add(charlie); v.add(freddy); Enumeration e = v.elements(); while(e.hasMoreElements ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 8
Provided by: coursesIs8
Category:

less

Transcript and Presenter's Notes

Title: Foundations of Software Design Fall 2002 Marti Hearst


1
Foundations of Software DesignFall 2002Marti
Hearst
Lab 7 discussion topics Casting, File I/O,
Enumeration, and Stacks John Fritch, Kaichi
Sung, Leah Zagreus    
2
Type Casting
  • Assigning primitive data when it would result in
    loss of precision
  • (a) System.out.println(1/2)
  • (b) System.out.println((float)1/2)
  • Assigning an object to a subclass type
  • Fish oneFish new Fish("one fish")
  • stack.push(oneFish)
  • Fish twoFish (Fish) stack.pop()
  • System.out.println(twoFish.getColor())

Adapted from Brian Overland, Java in Plain
English 2nd ed.
3
Type Casting
  • Used a lot in Java because nearly all data
    structures store generic Object types.
  • Stack
  • Vector
  • HashTable
  • etc.

4
Enumeration
  • A convenient way to iterate through the elements
    in a data structure
  • Must use type casting
  • Fish charlie new Fish("pink")
  • Fish freddy new Fish("yellow")
  • Vector v new Vector()
  • v.add(charlie)
  • v.add(freddy)
  • Enumeration e v.elements()
  • while(e.hasMoreElements())
  • Fish f (Fish) e.nextElement()
  • System.out.println(f.getColor())

5
File I/O
  • Java uses streams for input and output
  • Input streams reading data from files, user
    input, keyboard
  • Output streams writing data to files, screen,
    printer
  • Basic outline for File I/O
  • Open the file for reading or writing
  • Read or write the data
  • Close the file

Adapted from Oliver Mason, Programming for Corpus
Linguistics
6
Reading from a File
  • Import the IO classes
  • import java.io.
  • To open a file for reading (1 char at a time)
  • FileReader fr new FileReader("myFile.txt")
  • To open a file for reading (more functionality)
  • BufferedReader br new BufferedReader(new
    FileReader("myFile.txt"))
  • Requires try-catch Eclipse will help

Adapted from Oliver Mason, Programming for Corpus
Linguistics
7
StringTokenizer
  • Break up a string into tokens separated by
    delimiters
  • Words (what is the delimiter?)
  • Or any substrings, separated by the delimiter of
    your choosing
  • StringTokenizer st new StringTokenizer("this is
    a test")
  • while (st.hasMoreTokens())
  • System.out.println(st.nextToken())

Adapted from Oliver Mason, Programming for Corpus
Linguistics
Write a Comment
User Comments (0)
About PowerShow.com