CS 180 Lab 7

File I/O and Singletons

1

Begin by moving into the labs directory in your /Classes/CS180 directory. Next, create a new directory named Lab7. Move into the Lab7 directory and create java program named Singletons.java. Use VIM to edit your program following the specifications below.

NOTE: You must finish this assignment during the allotted lab time to receive credit. You may work alone, or with others to complete the assignment. The lab assignments should not be electronically submitted. Once you are finished, show your solution to the GTA.

Problem Description:

Consider this data sequence:

"fish bird reptile reptile bird bird bird mammal fish"

Let's define a SINGLETON to be a dataelement that is not repeated immediately before or after itself in the sequence. So, here there are four SINGLETONs that appear in the data stream:

  • The first appearance of “fish”
  • The first appearance of “bird”
  • “mammal”
  • And the second appearance of “fish”

Write a program that reads a single line of words from an input file. The input and output files for your program should be captured as command line arguments.

Assumptions:

  1. There will be at least one word in the data stream.
  2. There is only one line of data in the input file.
  3. All values are separated by space.

Your program should then calculate the SINGLETON values from the input stream, print the number of SINGLETONs to the user, and write those values to an output file, one SINGLETON per line.

Example Output:

Where input.dat holds:

Fish bird reptile reptile bird bird bird mammal fish

$java Singletons input.dat output.dat

4 Singletons in input.dat.

Writing to output.dat… finished.

$cat output.dat

fish

bird

mammal

fish

Analysis:

(Describe the problem including input and output in your own words.)

Create a file named Singletons.txt to use for this section.

Design:

(Describe the major steps for solving the problem.)

Create a file named design.txt to use for this section.

Code:

The Java code solution should be placed in Singletons.java.

Testing:

(Describe how you test this program)

Create a file named testing.txt to use for this section.

a b c

a a a b b c c c

yellow blue blue green green green red red white

1 1 11 2 2 3 red red blue bird fish 2 c

1