Data Structures and Algorithms — Homework 2

Due by 11:59pm Wednesday, February 7

Reading

Programming Exercises

  1. Finish your Hangman program from Lab 2.

  2. Write a program called TextFileStats that accepts a single command-line argument, which should be the name of a text file, and reports the total number of tokens, lines, and characters in the file (where token means a contiguous group of characters surrounded by whitespace). If the wrong number of arguments is provided (zero, or more than one), the program should print a warning message as shown below. You can use this code as your starting point, which includes comments outlining the main steps of the process. See ReadData.java for an example of a program that reads tokens from a file. Test your program on the text files haunting.txt, data.txt, and alice.txt, as shown below.

    > java TextFileStats haunting.txt
    haunting.txt has 83 tokens, 11 lines, and 504 characters
    
    > java TextFileStats data.txt
    data.txt has 23 tokens, 6 lines, and 116 characters
    
    > java TextFileStats alice.txt
    alice.txt has 26465 tokens, 3596 lines, and 148505 characters
    
    > java TextFileStats
    Usage: java TextFileStats <filename>
    
    > java TextFileStats haunting.txt data.txt alice.txt
    Usage: java TextFileStats <filename>
    


Turning in Your Homework

Files to submit:

Submit the above Java source code files using the Homework Upload Site. Please include your name and the assignment number in a comment at the top of each file. You DO NOT need to submit any other files from the lab, or any of your compiled .class files.

If you have questions about anything, just ask!