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...
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...
Write a Java program for RSA encryption that has the following inputs and outputs: Given a...
Write a Java program for RSA encryption that has the following inputs and outputs: Given a message and an integer n = pq where p and q are odd primes and an integer e > 1 relatively prime to (p − 1)(q − 1), encrypt the message using the RSA cryptosystem with key (n, e).
Write a program in java that can perform encryption/decryption. In the following let the alphabet A...
Write a program in java that can perform encryption/decryption. In the following let the alphabet A be A={A, a, B, b, . . ., “ ”, “.”,“’ ”}. The encoding is A→0, a→1, B→2, b→4, . . ., Z→50, z→51, “ ”→52, “.”→53 and “’”→54.
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...
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...
Assume you need to write a Java program that uses a binary search algorithm to search...
Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value. 1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT