Programming Assignment 6


Due: Week of Feb. 27 before lab


  1. (25 points) Problem 12.7 of the book. NumberFormatException. Write the bin2Dec(String binaryString) method to convert a binary string into a decimal number. Implement the bin2Dec method to throw a NumberFormatException if the string is not a binary string. In main, allow a user to enter a binary string and test your method.
    • Name the file and program ch12pr7.java.
    • Example execution (java ch12pr7) user provides input:
      Enter a binary number: 1001
      Decimal value is 9
    • Example execution (java ch12pr7) user provides input:
      Enter a binary number: 1001a1001
      Error. Input was not a binary number.
  2. (25 points) Based on problem 12.11 of the book. Remove text. Write a program that removes all occurrences of a specified string from a text file. You should take three inputs from the user, first an input filename, second a string to remove, and third an output filename to output the new text. You should assume the string to remove is a single word without spaces and that the input file only has occurrences of the string as whole words (as in it will not appear in the middle of a string).
    • Name the file and program ch12pr11.java.
    • Example execution (java ch12pr11) user provides input, example file:
      Enter an input filename: withJLD.txt
      Enter a string to remove: JLD
      Enter an output filename: withoutJLD.txt
      Resulting file:
      Hello class!
    • Example execution (java ch12pr11) user provides input:
      Enter an input filename: nothing
      Enter a string to remove: JLD
      Enter an output filename: withoutJLD.txt
      Error. Cannot find input filename.
  3. (25 points) Problem 12.19 of the book. Count words. Write a program that counts the number of words in President Abraham Lincoln's Gettysburg address.
    • Name the file and program ch12pr19.java.
    • Example execution (java ch12pr19):
      There are 277 words in the Gettysburg Address.
  4. (25 points) Problem 12.21 of the book. Data sorted? Write a program that reads the strings from a file and reports whether the strings in the files are sorted in alphabetical order. If the strings are not sorted in the file, the program displays the first two strings that are out of order.
    • Name the file and program ch12pr21.java.
    • Example execution (java ch12pr21) user provides input, example file:
      Please enter a filename: Scrabble.txt
      File 'Scrabble.txt' lists words out of order.
      The first two out of order words are 'ZA' and 'AAH'.
    • Example execution (java ch12pr21) user provides input, example file:
      Please enter a filename: LongScrabble.txt
      File 'LongScrabble.txt' lists words in order.
  5. Bonus. (10 points) Based on problem 12.25 of the book. Process large dataset. A university posts its employees' salaries here. Each line in the file consists of a faculty member's first name, last name, rank, and salary. Write a program that outputs a file named Summary.txt that contains the total salary for assistant professors, associate professors, full professors, and all faculty, respectively, and contains the average salary for assistant professors, associate professors, full professors, and all faculty, respectively.
    • Name the file and program ch12pr25.java.
    • Example execution (java ch12pr25) note the program itself does not display any output:
      Resulting file:
                               total        average
            assistant    20246511.91       65949.55
            associate    28844146.58       83849.26
                 full    35678051.41      102229.37
                  all    84768709.90       84768.71


General Instructions, Turning in assignments, and Grading

General Instructions

Turn in Instructions

Each assignment will be turned in to both Blackboard (soft copy) and in class (hard copy). Assignments are due BEFORE, let me repeat, before class starts. This does not mean five minutes after class starts.

Points