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 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 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...
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[]...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
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...
Create a program in java with the following information: Design a program that uses an array...
Create a program in java with the following information: Design a program that uses an array with specified values to display the following: The lowest number in the array The highest number in the array The total of the numbers in the array The average of the numbers in the array Initialize an array with these specific 20 numbers: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 example...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT