Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A cipher is a way of encoding a message to disguise it and keep it secret. In his book, CODE Breaking: A History and Exploration

A cipher is a way of encoding a message to disguise it and keep it secret. In his book, CODE Breaking: A History and Exploration, Rudolf Kippenhahn begins Chapter 8 on Shuffled Texts with this example from Encyclopedia Britannica:

A popular schoolboy cipher is the rail fence, in which the plaintext is staggered between rows and the rows are then read sequentially to give the cipher. In a depth-two rail fence (two rows) the message

we are discovered save yourself 

becomes

 w a e i c v r d a e o r e f e r d s o e e s v y u s l 

or

 waeicvrdaeoreferdsoeesvyusl 

You will implement a program named Cipher that uses a scheme similar to the one given above to encode and decode files. To make it possible to decode an encoded file and recover the original file, all characters on each line, including spaces, tabs, punctuation, etc., will be shuffled. For example, the line

Hello World! 

would be encoded as

HloWrdel ol! 

The user will provide an input file. Your application will provide the encoded/decoded file in an output file.

Your program must display a header that lists the name of the application and provides instructions about using the program.

The user should be continually prompted for the action to perform, input file, and output file until they desire to quit.

Action - The program should accept both upper and lower case letters for the actions below:

E-ncode

D-ecode

Q-uit

Note the action should be a single letter.

Input File - The user should be prompted for the input files name. If the file is not found, the program should output an error message and prompt the user for another filename.

Output File - The user should be prompted for the output files name. If the file already exists, the file should not be overwritten. The program should print File Exists and then quit. If a FileNotFoundException occurs when attempting to open an output file, the program should also print an error message and quit.

Here are some examples:

DISPLAY HEADER AS DESCRIBED ABOVE! Enter E-ncode, D-ecode, or Q-uit: e Enter input file: test-files/TaleOfTwoCities.txt Enter output file: test-files/TaleEncoded.txt Enter E-ncode, D-ecode, or Q-uit: D Enter input file: test-files/TaleEncoded.txt Enter output file: test-files/TaleDecoded.txt Enter E-ncode, D-ecode, or Q-uit: q 
DISPLAY HEADER AS DESCRIBED ABOVE! Enter E-ncode, D-ecode, or Q-uit: encode Invalid Action Enter E-ncode, D-ecode, or Q-uit: E Enter input file: test-files/Tales.txt test-files/Tales.txt (No such file or directory) Enter input file: test-files/TaleOfTwoCities.txt Enter output file: test-files/TaleEncoded.txt File exists 

After each encode/decode operation the output file will contain the encode/decoded file.

The program must contain and use the completed version of the following methods: getInputScanner(), getOutputPrintStream(), processFile(), encodeLine(), and decodeLine(). The method headers are listed below. You MUST use these exact method headers in your program so that we can automate the testing of your program.

The two methods getInputScanner() and getOutputPrintStream() interact with the user to prompt for the filenames and then create and return references to Scanner and PrintStream objects, respectively. These methods MUST use a try/catch block (rather than a throws clause) to handle any FileNotFoundExceptions that occur.

//Repeatedly prompts the user for the name of an input file until the user enters //the name of an existing file; then creates and returns a Scanner for the input file. //See the lecture notes for your section and/or the textbook for examples. //Use a try/catch block to catch and handle any FileNotFoundException's that occur public static Scanner getInputScanner(Scanner console){ } 
//Prompts the user for the name of an output file.  //If the file does not exist, creates and returns a PrintStream for the output file. //If the file does exist, prints an error message and returns null OR //does one of the Extra Credit options described below. //Use a try/catch block to catch and handle any FileNotFoundException's that occur and //return null public static PrintStream getOutputPrintStream(Scanner console){ } 
//If encode is true, encodes lines in input and outputs encoded file. //If encode is false, decodes lines in input and outputs decoded file. public static void processFile (boolean encode, Scanner input, PrintStream output){ } 
//Returns string containing encoded line. public static String encodeLine(String line){ } 
//Returns string containing decoded line. public static String decodeLine(String line){ } 

Do NOT use throws clause(s) in your program. Instead use try/catch blocks within the getInputScanner() and getOutputPrintStream() methods as described above.

For the encodeLine()/decodeLine() methods, you will need to create an empty String, add characters to it, and then return the String. Below is a copy() method that provides an example of this type of text processing:

public static String copy(String x) { String s = ""; for (int i = 0; i < x.length(); i++) { s += x.charAt(i); } return s; } 

If the user enters an output file that already exists, the user should be asked if its OK to overwrite the file. If not, they should be reprompted for a new filename.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+4. Does the source speak with dynamism and authority?

Answered: 1 week ago