Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will be writing a simple Java program that implements ROT-13 encryption. ROT

ID: 3594636 • Letter: Y

Question

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.

/*
* Lab08a.java
*
*   A program that performs ROT13 encryption on a string.
*   Used to practice breaking code up into methods.
*
* @authors ENTER YOUR NAMES HERE
*
*/
package osu.cse1223;
import java.util.Scanner;

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.
}

}

Explanation / Answer

/*
* Lab08a.java
*
* A program that performs ROT13 encryption on a string.
* Used to practice breaking code up into methods.
*
* @authors ENTER YOUR NAMES HERE
*
*/

import java.util.Scanner;

public class Lab08a {

public static final String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = getText(sc);
  
String encText = rot13(input);
displayResults(input, encText);

}

// 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) {
String line;
while(true) {
System.out.print("Enter a text: ");
line = inScanner.nextLine();
if (line.isEmpty()) {
System.out.println("Enter non empty line");
}
else {
break;
}
}
// Fill in the body
return line;
}


// 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
StringBuilder sb = new StringBuilder();
for(int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (Character.isLetter(ch)) {
if (Character.isUpperCase(ch))
ch = (char)('A' + (((ch - 'A')+ 13)%26));
else
ch = (char)('a' + (((ch - 'a')+ 13)%26));
}
sb.append(ch);
}
return sb.toString();
}

// 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.
System.out.print("Original alphabet: " + inText);
  
System.out.println();
System.out.println("ROT-13 alphabet: " + encText);
}

}

Sample output

Enter a text: EAT AT JOE'S AT 1:00
Original alphabet: EAT AT JOE'S AT 1:00
ROT-13 alphabet: RNG NG WBR'F NG 1:00

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote