In: Computer Science
Modify the Encryption program so that it uses the following encryption algorithm:
Every letter (both uppercase and lowercase) converted to its successor except z and Z, which are converted to 'a' and 'A' respectively (i.e., a to b, b to c, …, y to z, z to a, A to B, B to C, …, Y to Z, Z to A)
Every digit converted to its predecessor except 0, which is converted to 9 (i.e., 9 to 8, 8 to 7, … 1 to 0, 0 to 9)
Everything else unchanged.
Hand in:
Encryption.java
Note:
You must modify the specifications (API) of the methods involved as well.
Thanks for the question, It would have good if you have shared the previous code that needed to be modified, nevertheless, I have written the method that takes in a plain text and encrypt into based on the logic asked in the quesiton. Here is the code. ================================================================================ import java.util.Scanner; public class Encryption { /** * Main logic of encryption * @param plainText : The text we want to encrypt * @return : The encrypted text using the given logic */ public static String encrypt(String plainText) { String encrypted = ""; for (int i = 0; i < plainText.length(); i++) { char letter = plainText.charAt(i); // logic to handle lowercase letters if (Character.isLowerCase(letter)) { letter = (char) (letter + 1); if (Character.isLowerCase(letter)) { encrypted += String.valueOf(letter); } else { encrypted += String.valueOf('a'); } } // logic to handle uppercase letters else if (Character.isUpperCase(letter)) { letter = (char) (letter + 1); if (Character.isUpperCase(letter)) { encrypted += String.valueOf(letter); } else { encrypted += String.valueOf('A'); } } // logic to handle digits character else if (Character.isDigit(letter)) { letter = (char) (letter - 1); if (Character.isDigit(letter)) { encrypted += String.valueOf(letter); } else { encrypted += String.valueOf('9'); } } // logic to handle characters which are not alpha numeric else { encrypted += String.valueOf(letter); } } return encrypted; // returns the encrypted stirng } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // ask user to enter a text to encrypt System.out.print("Enter text you like to encrypt: "); String plainText = scanner.nextLine(); // display the encrypted string System.out.println("Encrypted String: "+ encrypt(plainText)); } }
=====================================================================