Question

In: Computer Science

Finish the following java question:  Modify a Encryption program so that it uses the following encryption algorithm:...

  1. 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 to 8, 8 to 7, … 1 to 0, 0 to 9)

    • Everything else unchanged.

Note:

  • You must modify the specifications (API) of the methods involved as well.  

Solutions

Expert Solution

Code For Above Problem:

import java.util.Scanner;

public class Encryption1 {

        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.println("Enter message to encrypt: ");
                String message = sc.nextLine();
                String result = encrypt(message);
                System.out.println("Encrypted Message is: \n" + result);
                sc.close();
        }

        // Method to encrypt the message
        public static String encrypt(String message) {
                // Create StringBuffer Object to hold encrypted message
                StringBuffer result = new StringBuffer();
                // for all letters in message
                for (int i = 0; i < message.length(); i++) {
                        // take the current character
                        char current = message.charAt(i);

                        // if current character is Uppercase
                        // converted to its successor
                        if (current >= 'A' && current <= 'Z') {
                                // if current character is UpperCase 'Z'
                                // add the 'A' to result
                                if (current == 'Z') {
                                        result.append('A');
                                }
                                // Otherwise add its successor to result
                                else {
                                        result.append((char) (current + 1));
                                }
                        }
                        // if current character is lowercase
                        // converted to its successor
                        else if (current >= 'a' && current <= 'z') {
                                // if current character is UpperCase 'z'
                                // add the 'a' to result
                                if (current == 'z') {
                                        result.append('a');
                                }
                                // Otherwise add its successor to result
                                else {
                                        result.append((char) (current + 1));
                                }
                        }
                        // if current character is digit
                        // converted to its predecessor except 0
                        else if (current >= '0' && current <= '9') {
                                // if current character is digit '0'
                                // add the '9' to result
                                if (current == '0') {
                                        result.append('9');
                                }
                                // Otherwise add its predecessor to result
                                else {
                                        result.append((char) (current - 1));
                                }
                        } else {//if any other character add to result
                                result.append(current);
                        }
                }
                //return result as String
                return result.toString();
        }

}

Sample Run Results:

Enter message to encrypt: 
AbZikd12 Kowlu091
Encrypted Message is: 
BcAjle01 Lpxmv980

Images Of Code;

Image Of Sample Run Results:


Related Solutions

Modify the Encryption program so that it uses the following encryption algorithm: Every letter (both uppercase...
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...
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...
Temperature Converter Modify the previous version of this program so that it uses a loop to...
Temperature Converter Modify the previous version of this program so that it uses a loop to display a range of temperature conversions for either Fahrenheit to Celsius or Celsius to Fahrenheit. Note: You can start with the code from the previous version, then modify it slightly after it prompts the user for the direction to convert. It will then ask the user for a starting temperature and ending temperature. Assuming they entered the lower number first (if not, tell them...
Question The given plaintext is “Feistel cipher structure uses the same algorithm for both encryption and...
Question The given plaintext is “Feistel cipher structure uses the same algorithm for both encryption and decryption”. Write Java or Python code to implement either Monoalphabetic cipher or Hill cipher or Transposition Cipher (Encryption and Decryption) and test your code on given plaintext. User may enter value of key at the command prompt, if required.
Write a Java program that uses the RC4 cipher algorithm to encrypt the following message using...
Write a Java program that uses the RC4 cipher algorithm to encrypt the following message using the word CODES as the key:   Cryptography is a method of protecting information and communications through the use of codes so that only those for whom the information is intended can read and process it. Instead of using stream length 256, we will use length 26. When encrypting, let A = 0 to Z = 25. Ignore spaces and punctuations and put the message...
Question: Java Programming. ** Modify Current Program. Current Program class Account{       //instance variables   ...
Question: Java Programming. ** Modify Current Program. Current Program class Account{       //instance variables    private long accountNumber;    private String firstName;    private String lastName;    private double balance;       //constructor    public Account(long accountNumber, String firstName, String lastName, double balance) {        this.accountNumber = accountNumber;        this.firstName = firstName;        this.lastName = lastName;        this.balance = balance;    }    //all getters and setters    public long getAccountNumber() {        return...
What is the output of the following program? Slightly modify the program so that: [Pts. 10]...
What is the output of the following program? Slightly modify the program so that: [Pts. 10] The Parent process calculates the val such a way that its value is 20 and it prints “This is parent process and val = 20”. Also, slightly modify The child process calculates the val such a way that its value is 25 and it prints “This is child process and val = 25”. int main() { int val = 15; int pid; if (pid...
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...
to be done in java Your task is to finish the StockItem class so that it...
to be done in java Your task is to finish the StockItem class so that it meets the following criteria • The StockItem class will have 4 attributes: a stock number; a name; the price of the item; the total number of items currently in stock • The first three of the above characteristics will need to be set at the time a StockItem object is created, with the total number of items set to 0 at this time. The...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** *...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each symbol (e.g., the * reverse complement of "GTCA" is "TGAC"). * * @param dna a char array representing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT