Question: I have been struggling to write the code for this method. The tempplae for the method is given. How do i do the method for
I have been struggling to write the code for this method. The tempplae for the method is given. How do i do the method for TransformFile and displayMenu and the main method? Can you please help me?
You will be writing a simple Java program that implements an ancient form of encryption known as a substitution cipher or a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements:
Punctuation, numbers, and other non-alphabetic characters are unaffected by the shift. So the message "EAT AT JOE'S AT 1:00" becomes "HDW DW MRH'V 1:00".
You will be writing a Java program that prompts the user to enter a file name, then reads the text from that file, applies a Caesar cipher to it, and then outputs the result to another file of the user's choice. The code must work for uppercase and lowercase letters and must work correctly if punctuation and numbers are included in the message. A "skeleton" of this code is provided below.
NOTE: If you haven't noticed from the text above, only letters should have ROT-13 applied to them for this assignment. Anything else - punctuation, numbers, etc. - should be left untouched. You can use the functions Character.isLetter(), Character.isLowerCase() and Character.isUpperCase() to test if a character is an uppercase or lowercase letter or not.
You can see more utility methods for working with the char type in the Character API documentation.
NOTE 2: The shift method for this project requires you to change characters in a String based on changing individual characters. While there are many ways to do this, some will be easier than others. One way to do this easily is to take advantage of a String constant with all of the letters in a single String in order:
private final String UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
With this setup, each character is represented as a numeric value by its index in the String, and we can manipulate the characters using simple math. For example, this code snippet should display the letter 'C':
Note that you cannot just add your shift value to every index to get the encoded value - for example, adding 2 to the index value for the letter 'Z' gives you an index of 27, which is outside the bounds of the String. But 'Z' shifted by 2 should be the letter 'B' because the cipher requires that we wrap around to the beginning when we go past our final letter. You will need to implement the logic to allow your characters to "wrap around" yourself. There are many different ways you can approach this, but as a hint consider using the remainder operation (%) to get your indices to circle back around. The constant letters is already included in the code skeleton provided in the link above.
NOTE 3: Your shift method must work with negative numbers. If you provide a negative number to your shift it should move to the left instead of to the right. For example, the following code snippet should display the character 'X':
Note that as in the case of a positive shift, the negative shift must also "wrap around" correctly - the letter 'A' shifted left by 2 should result in a 'Y', not in an index out of bounds exception.
Note that if you have correctly implemented the shift method to work for both positive and negative values, you can use it to both encode and decode messages. To decode a message, just shift it back the negative amount you shifted it to encode it. For example, if you encoded a message with a shift of 3, you can shift the coded message by -3 to get the original message back.
NOTE 4: For full credit, your shift method must use a StringBuilder to build the coded message rather than using Stringconcatenation.
You must fill in the appropriate code. Pay close attention to what each method should be doing based on the information given in the comments before the method. You may add more methods to this skeleton if you feel that it would be useful, but if you do, remember to include comments on your added methods. A transcript of how this program should work is given below.
Input file is: Output file is: Shift amount is: 0 Select from the following options: Set [O]utput file Set [I]nput file Set [S]hift amount [Q]uit Enter your choice: S Enter an amount to shift: 13 Input file is: Output file is: Shift amount is: 13 Select from the following options: Set [O]utput file Set [I]nput file Set [S]hift amount [Q]uit Enter your choice: I Enter an input filename: test.txt Input file is: test.txt Output file is: Shift amount is: 13 Select from the following options: Set [O]utput file Set [I]nput file Set [S]hift amount [Q]uit Enter your choice: O Enter an output filename: testcoded.txt Input file is: test.txt Output file is: testcoded.txt Shift amount is: 13 Select from the following options: Set [O]utput file Set [I]nput file Set [S]hift amount [E]ncode test.txt into testcoded.txt [D]ecode test.txt into testcoded.txt [Q]uit Enter your choice: E Finished writing to file: testcoded.txt Input file is: test.txt Output file is: testcoded.txt Shift amount is: 13 Select from the following options: Set [O]utput file Set [I]nput file Set [S]hift amount [E]ncode test.txt into testcoded.txt [D]ecode test.txt into testcoded.txt [Q]uit Enter your choice: q Goodbye!
The above transcript uses the file test.txt as input. It contains the following text:
This is a test! The Quick Brown Fox Jumped Over the Lazy Dog. This is only a test, but it should still work! What will this file hold? 3 + 3 = 6 and 3 * 3 = 9
The above transcript creates the file testcoded.txt. After the program finishes, it contains the following text:
Guvf vf n grfg! Gur Dhvpx Oebja Sbk Whzcrq Bire gur Ynml Qbt. Guvf vf bayl n grfg, ohg vg fubhyq fgvyy jbex! Jung jvyy guvf svyr ubyq? 3 + 3 = 6 naq 3 * 3 = 9
NOTE: There are no separate test cases for the method transformFile. However there are separate test cases for the output of your program to the screen and the output produced by your program to a file. You will not be able to test transformFile through this interface until you have the main method written, but you will be able to test each of the other methods individually (and you can - and should! - write your own tests to convince yourself that your transformFile method is working properly).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
