L11

Due by the end of Tuesday, 30 March 2021


SUBMIT   /   CHECK

Today's Activities

Activity 1
Download the program CopyNumbers, which reads all the numbers from a file then prints them in reverse order into another file.

Rename the program to NumberLines. The purpose of this program is to copy a text file, adding numbers to the lines.

First code revision is to change it so that it reads and saves all lines from the file instead of all integer values. You can use any/all of the following files to test your revisions:

When done, your program should print the lines of the file in reverse order.

Activity 2
Revise the program so that it prints out the lines of the file in the original order, but with line numbers. The line numbers should be at the beginning of each line and be followed by a dot and a space. So, for example, the line:
This line is first.
becomes
1. This line is first.

Revise it again so that the numbers aren't printed on blank lines.

NOTE: blank lines are still counted; they just doesn't have the number printed on them. See the expected output for the poems above:

Activity 3
Create a method to get a yes/no answer. It returns true for a yes answer, and false for a no.

The method is given a question to ask. It asks the question, then reads the user's answer. It repeats asking the question until it gets a yes or no. Accept the full words, of course, but also any one- or two-letter abbreviation of yes as a yes, and any one-letter abbreviation of no as a no.

Note: anything that "yes" starts with is counted as a yes; anything that "no" starts with is counted as a no -- except empty Strings, of course. The user should be able to enter answers using capital letters as well, so "N" should also count as a no.
Activity 4
It's rather rude to over-wite an existing file without asking. Revise method getTheWriter so that if the file the user requested already exists, it asks if it's OK to overwrite that file.
Please enter the name of the output file: exists.txt File 'exists.txt' exists. OK to over-write? no Please enter the name of the output file: alsoExists.txt File 'alsoExists.txt' exists. OK to over-write? y

NOTE: This can be done by creating a variable for the File object then adding a simple if control. if the file doesn't exist, OR if the user says it's OK to overwrite, then return a PrintWriter connected to that file. Because of short-circuit evaluation, the question about being OK to over-write will only be asked if the file exists.

If the user says the file can't be overwritten, then that can count as another "failure", so that any three reasons to request a new file name will result in the program ending.

Please enter the name of the output file: exists.txt File 'exists.txt' exists. OK to over-write? no Please enter the name of the output file: alsoExists.txt File 'alsoExists.txt' exists. OK to over-write? n Please enter the name of the output file: noSuchFolder/cantopenthis.txt Could not open noSuchFolder/cantopenthis.txt for output Too many failed names. Quitting.

Submit this/these files:

You will be graded on the following:

  1. Program reads all lines from the file and saves them in a List
  2. Program prints into a file all lines of the original file in the original order
  3. ... with line numbers on non-bank lines
  4. ... and no numbers on blank lines
  5. ... but with blank lines counted
  6. Class has a method that returns whether user agrees to a request provided to the method as an argument
  7. ... repeatedly repeats that request until gets a yes or no answer
  8. ... allows one- or two-character abbreviations of yes/no (but not empty responses)
  9. getTheWriter asks about overwriting files that exist
  10. ... but not files that don't exist
  11. ... and does not erase files unless they are OK to overwrite
  12. ... and behaves as it did before for output files that cannot be created

SUBMIT   /   CHECK