CS 178 Programming with Multimedia Objects

Fall 2004

Recitation Exercise:

Prepared by: Aditya Mathur.

To be completed in class on Oct 22, 2004.

Learning objectives:
  1. How to set up file I/O and data I/O streams.
  2. How to write data to a file.
  3. How to read data from a file.
  4. Reference: Chapter 10.2

Complete the following set of instructions to develop a few simple methods. Note that we are NOT writing an applet. Instead, we are writing methods that could be used later inside a Java applet.

Global Array and other declarations:

  1. Declare inFile as a FileInputStream that opens a file named "genes".
  2. Declare inDataFile as a DataInputStream that connects to inFile.
  3. Declare maxGenes as an integer constant and initialize it to 100.
  4. Declare maxNameLen as an integer constant and initialize it to 20.
  5. Declare dispGene as an array of TextField with maxGenes elements.

Write the following methods.

readData

  • This method reads data from the genes file and displays the gene names in the TextField array.
  • The data is to be read in as follows:
    • Declare geneName as an array of characters with maxNameLen elements.
    • Read an integer that denotes the number of genes in the file.
    • Next read an integer that denotes the length of the first gene name.
    • Next read the characters in the gene name. Save these characters in a char array named geneName.
    • Next convert the characetrs in the char array geneName to a string gName using the following statement:
    • String gname=new String(geneName);

    • Display gname in the next empty TextField.
    • Read and display the remaining gene names as described above.

As an example, suppose that the file genes contains the following data:

2 9 cleaopatra 5 tudor

Your method should read two gene names in the file and display them in two text fields.

Last update: October 22, 2004