1 / 7

Foundations of Software Design Fall 2002 Marti Hearst

Foundations of Software Design Fall 2002 Marti Hearst. Lab 7 discussion topics: Casting, File I/O, Enumeration, and Stacks John Fritch, Kaichi Sung, Leah Zagreus. Type Casting. Assigning primitive data when it would result in loss of precision (a) System.out.println(1/2);

Télécharger la présentation

Foundations of Software Design Fall 2002 Marti Hearst

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 FishoneFish=newFish("one fish"); stack.push(oneFish); FishtwoFish=(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 Fishcharlie=newFish("pink"); Fishfreddy=newFish("yellow"); Vectorv=newVector(); v.add(charlie); v.add(freddy); Enumeratione=v.elements(); while(e.hasMoreElements()){ Fishf=(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) BufferedReaderbr=newBufferedReader(newFileReader("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 StringTokenizerst=newStringTokenizer("this is a test"); while(st.hasMoreTokens()) System.out.println(st.nextToken()); Adapted from Oliver Mason, Programming for Corpus Linguistics

More Related