Question

In: Computer Science

Modify the Encryption program so that it uses the following encryption algorithm: Every letter (both uppercase...

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

Solutions

Expert Solution

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));

    }
}

=====================================================================


Related Solutions

Finish the following java question:  Modify a Encryption program so that it uses the following encryption algorithm:...
Finish the following java question:  Modify a 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...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks:...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks: The program prompts the user for the number of contestants in this year’s competition; the number must be between 0 and 30. The program continues to prompt the user until a valid value is entered. The expected revenue is calculated and displayed. The revenue is $25 per contestant. For example if there were 3 contestants, the expected revenue would be displayed as: Revenue expected...
in C++, Modify the quicksort algorithm such that it uses the last item as the pivot...
in C++, Modify the quicksort algorithm such that it uses the last item as the pivot instead of the 1st. Also, sort in descending order, instead of ascending order. NOTE: Do not move the last element into the first element of the array. You must treat the algorithm as if the pivot is actually sitting in the last location of the array. After it has been sorted in descending order, go through all the items in the array and make...
Modify the program below so the driver class (Employee10A) uses a polymorphic approach, meaning it should...
Modify the program below so the driver class (Employee10A) uses a polymorphic approach, meaning it should create an array of the superclass (Employee10A) to hold the subclass (HourlyEmployee10A, SalariedEmployee10A, & CommissionEmployee10A) objects, then load the array with the objects you create. Create one object of each subclass. The three subclasses inherited from the abstract superclass print the results using the overridden abstract method. Below is the source code for the driver class: public class EmployeeTest10A { public static void main(String[]...
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year,...
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year, price and rating (G,PG,R…) -Enhance the program so it provides a find by rating function that lists all of the movies that have a specified rating def list(movie_list): if len(movie_list) == 0: print("There are no movies in the list.\n") return else: i = 1 for row in movie_list: print(str(i) + ". " + row[0] + " (" + str(row[1]) + ")") i += 1...
Modify the partition.java program (Listing 7.2) so that the partitionIt() method always uses the highest-index (right)...
Modify the partition.java program (Listing 7.2) so that the partitionIt() method always uses the highest-index (right) element as the pivot, rather than an arbitrary number. (This is similar to what happens in the quickSort1.java program in Listing 7.3.) Make sure your routine will work for arrays of three or fewer elements. To do so, you may need a few extra statements. // partition.java // demonstrates partitioning an array // to run this program: C>java PartitionApp //////////////////////////////////////////////////////////////// class ArrayPar { private...
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when...
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when the program runs. Have the program request the number of initial numbers to be entered." //C++ Program 7.14 as follows #include #include #include #include using namespace std; int main() { const int NUMELS = 4; int n[] = {136, 122, 109, 146}; int i; vector partnums(n, n + NUMELS); cout << "\nThe vector initially has the size of " << int(partnums.size()) << ",\n and...
Modify the following 'MessageBoxes' application so it uses a single action listener for each button. This...
Modify the following 'MessageBoxes' application so it uses a single action listener for each button. This will require you to separate the single action listener logic into multiple listeners, one for each button. Then modify the code to provide additional options to two or more buttons. /* * The source code for this assignment started with * a sample from "Thinking in Java" 3rd ed. page 825 * by Bruce Eckel. I have finished adding the rest of the action...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The program should be able to handle different keys by deciding the key at run time. Thank you :)
You are required to modify the attached simulation program. This program currently uses an array to...
You are required to modify the attached simulation program. This program currently uses an array to implement the queue. You will modify the program so that it uses the STL queue instead. /* This is for CSC 611 class at NSU Computer Science Department This program is a modified C++ version of the C program From the Second Edition Simulation Modeling & Analysis Averill M. Law W. David Kelton */ #include using namespace std; #include #include double time = 0;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT