This assignment will consist of a single class, with all code in the main() method. Call your class SentenceCipher (that is, the source code will be in a file called SentenceCipher.java). If you choose to use an IDE other than DrJava, ensure that your program is not defined in a package (there should be no package statement at the top of the file).
When your assignment is complete, a user running your program will be prompted to enter the shared ‘source’ text, then asked if they want to encrypt or decrypt a message. The source text may contain any mixture of characters, including punctuation and spaces. Before continuing, the source text must be converted to all lower case.
After receiving the source text from the user, they will be asked to choose one of two options:
- Encrypt a message
- The user is prompted to enter a word to be encrypted (this word is converted to lower case).
- The program finds the position in the source text of the first occurrence of each character from the word (or -1 if the character cannot be found) and creates a new String containing those positions, separated by spaces and ending with -2.
- The program displays the list of positions, preceded by the text “Result: ”.
1. On a separate line, the program outputs the amount of information loss, which is charactersnotfoundwordlength×100
- This should be preceded by “Information loss (%): ”.
- The user is prompted to enter a list of positions, separated by spaces and ending with -2 (to indicate the end of the word).
- As long as the next position is not -2, the program reconstructs the word one character at a time:
- if the position is within the bounds of the source text then the character at that position is added to the word;
- if the position is -1 then an underscore (_) is appended to the string, indicating an unrecoverable character; and
- if the position is less than -1 or beyond the end of the source text then an exclamation mark (!) is appended to the word.
- If there were any invalid positions (less than -1 or beyond the end of the source text) then the program should print a message indicating how many. The number should be followed by the text ” invalid position(s)“. The message should not be displayed at all if there were no invalid positions.
- The program outputs the recovered message, preceded by the text “Result: ”
- On a separate line, the program outputs the amount of information loss, which is charactersnotfoundwordlength×100, , where the number of characters not found includes both positions of -1 and invalid positions. This should be preceded by “Information loss (%): ”.