Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise 1 Description You will be writing a simple Java program that implements ROT-13 encryption. ROT-13 is a very simple encryption scheme that can be

Exercise 1 Description

You will be writing a simple Java program that implements ROT-13 encryption. ROT-13 is a very simple encryption scheme that can be used to obfuscate text and is based on a very old encryption technique called 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: Original alphabet: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Shifted alphabet: D E F G H I J K L M N O P Q R S T U V W X Y Z A B C 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". ROT-13 is a special case of the Caesar cipher where the characters of the new alphabet are shifted (or "rotated") by 13 positions from the original alphabet: Original alphabet: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ROT-13 alphabet: N O P Q R S T U V W X Y Z A B C D E F G H I J K L M So in the case of ROT-13, the message "EAT AT JOE'S AT 1:00" becomes "RNG NG WBR'F NG 1:00". Unlike the general case of the Caesar cipher, ROT-13 has a special property that an encrypted message can be decrypted by applying ROT-13 a second time. So the message "RNG NG WBR'F NG 1:00" becomes "EAT AT JOE'S AT 1:00" when passed through a ROT-13 encryption. You will be writing a Java program that takes an arbitrary string as input, applys ROT13 to the string, and displays the results in a specific format. 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 in the file given in the instructions above. You will see that there are three methods declared in this file with no code provided. 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 you MUST include code for all of the methods in the skeleton to receive full credit.

public class Lab08a { public static final String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; public static void main(String[] args) { // Fill in the body } // Given a Scanner, prompt the user for an input string. Check to make sure that // the user inputs a non-empty string and return the String to the calling program./ private static String getText(Scanner inScanner) { // Fill in the body } // Given a string, return the ROT13 transformation of the String. For example, given the // input String "ABCXYZ" the function returns the String "NOPKLM". private static String rot13(String input) { // Fill in the body } // Given an input text and an encrypted text, display the strings in the formatted // output described by the instructions for closed lab 08. private static void displayResults(String inText, String encText) { // Fill in the body. } } 

Exercise 1 Sample Output

This is a sample transcript of what your program should do. Text in bold is expected input from the user rather than output from the program. Enter your text to encrypt (!!! to quit): THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG. +----------------------------------------------------------+ | ORIGINAL: THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG. | | ENCRYPTED: GUR DHVPX OEBJA SBK WHZCRQ BIRE GUR YNML QBT. | +----------------------------------------------------------+ Enter your text to encrypt (!!! to quit): GUR DHVPX OEBJA SBK WHZCRQ BIRE GUR YNML QBT. +----------------------------------------------------------+ | ORIGINAL: GUR DHVPX OEBJA SBK WHZCRQ BIRE GUR YNML QBT. | | ENCRYPTED: THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG. | +----------------------------------------------------------+ Enter your text to encrypt (!!! to quit): EAT AT JOE'S AT 1:00PM +-----------------------------------+ | ORIGINAL: EAT AT JOE'S AT 1:00PM | | ENCRYPTED: RNG NG WBR'F NG 1:00CZ | +-----------------------------------+ Enter your text to encrypt (!!! to quit): 1234567890 +-----------------------+ | ORIGINAL: 1234567890 | | ENCRYPTED: 1234567890 | +-----------------------+ Enter your text to encrypt (!!! to quit): ERROR! String must not be empty! Enter your text to encrypt (!!! to quit): !!! Goodbye! The program should continue executing until the user provides exactly three exclamation points as input. If those exclamation points include any other text, the program should function normally. (HINT: Use the String equals() method to test whether the input string is "!!!"): Enter your text to encrypt (!!! to quit): YAY!!! +-------------------+ | ORIGINAL: YAY!!! | | ENCRYPTED: LNL!!! | +-------------------+ Enter your text to encrypt (!!! to quit): !!! Goodbye! NOTE: The formatting here in important - your output should be contained within a box that changes size based on the length of your input and output strings. Take this code in stages - first get the ROT-13 encryption working properly, then try to get the output in the correct format. NOTE 2: If you haven't noticed from the text above, only capital letters should have ROT-13 applied to them for this assignment. Anything else - punctuation, numbers, even lowercase letters - should be left untouched. You can use the functions Character.isLetter() and Character.isUpperCase() to test if a character is an uppercase letter or not. boolean test1 = Character.isLetter('A'); // test1 will be true boolean test2 = Character.isUpperCase('A'); // test2 will be true boolean test3 = Character.isLetter('?'); // test3 will be false boolean test4 = Character.isUpperCase('a'); // test4 will be false NOTE 3: This lab requires you to change characters 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: final String letters = "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 'B': int myA = letters.indexOf("A"); int myB = myA+1; char let = letters.charAt(myB); System.out.println(let); Note that you cannot just add 13 to every index to get the ROT-13 value - adding 13 to the index value for the letter 'Z' gives you an index of 38, which is outside the bounds of the String. 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.

Exercise 2 Description

Create a copy of your solution for Exercise 1 and name it Lab08b.java in the same ClosedLab08 folder. For this exercise, you should extend the code that you wrote in Exercise 1 with a new method. This new method should use the following method header: private static String shiftMessage(String input, int n) It should take a String as input and return the Caesar cipher of that input string with a shift of n. For example, calling shiftMessage("EAT AT JOE'S",3) should result in the string "HDW DW MRH'V". Then modify your main program so that after it accepts a String from the user as input, it also asks for the shift to use to encode the string and reports the proper encryption of that string. You may add more methods if you would like, but you must implement at least the shiftMessage method. (You may also want to implement a method to prompt the user for a shift value that is similar to the method used

Exercise 2 Sample Output

This is a sample transcript of what your program should do. Enter your text to encrypt (!!! to quit): ABCDEFGHIJKLMNOPQRSTUVWXYZ Enter amount to shift text by: 13 +---------------------------------------+ | ORIGINAL: ABCDEFGHIJKLMNOPQRSTUVWXYZ | | ENCRYPTED: NOPQRSTUVWXYZABCDEFGHIJKLM | +---------------------------------------+ Enter your text to encrypt (!!! to quit): ABCDEFGHIJKLMNOPQRSTUVWXYZ Enter amount to shift text by: 3 +---------------------------------------+ | ORIGINAL: ABCDEFGHIJKLMNOPQRSTUVWXYZ | | ENCRYPTED: DEFGHIJKLMNOPQRSTUVWXYZABC | +---------------------------------------+ Enter your text to encrypt (!!! to quit): ABCDEFGHIJKLMNOPQRSTUVWXYZ Enter amount to shift text by: 13 +---------------------------------------+ | ORIGINAL: abcdefghijklmnopqrstuvwxyz | | ENCRYPTED: NOPQRSTUVWXYZABCDEFGHIJKLM | +---------------------------------------+ Enter your text to encrypt (!!! to quit): ABCDEFGHIJKLMNOPQRSTUVWXYZ Enter amount to shift text by: 0 +---------------------------------------+ | ORIGINAL: ABCDEFGHIJKLMNOPQRSTUVWXYZ | | ENCRYPTED: ABCDEFGHIJKLMNOPQRSTUVWXYZ | +---------------------------------------+ Enter your text to encrypt (!!! to quit): ABCDEFGHIJKLMNOPQRSTUVWXYZ Enter amount to shift text by: 6 +---------------------------------------+ | ORIGINAL: ABCDEFGHIJKLMNOPQRSTUVWXYZ | | ENCRYPTED: GHIJKLMNOPQRSTUVWXYZABCDEF | +---------------------------------------+ Enter your text to encrypt (!!! to quit): !!! Goodbye! The user MUST provide a shift value between 0 and 26. If the user goes out of this range, the program should force the user to enter a valid value: Enter your text to encrypt (!!! to quit): THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG Enter amount to shift text by: -3 ERROR! Shift must be non-negative! Enter amount to shift text by: 33 ERROR! Shift must be no more than 26! Enter amount to shift text by: 25 +---------------------------------------------------------+ | ORIGINAL: THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG | | ENCRYPTED: SGD PTHBJ AQNVM ENW ITLODC NUDQ SGD KZYX CNF | +---------------------------------------------------------+ Enter your text to encrypt (!!! to quit): !!! Goodbye!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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