Question

In: Computer Science

Caesar Cipher in Java Problem? Objective Practice the cumulative sum and char data type Problem You...

Caesar Cipher in Java Problem?

Objective

Practice the cumulative sum and char data type

Problem

You want to create an app to encrypt the text messages that you send to your friends. Once your friend gets the message, the message should be decrypted so that your friend understands it. To implement this app Caesar cipher algorithm should be used. Caesar Cipher text is formed by rotating each letter by a given amount. For example, if you rotate the letter ‘A’ by 3 you should get ‘D’. rotate ‘B’ by 3 you should get ‘E’. Toward the end of the alphabet you wrap around, if example rotate ‘X’ by 3 you should get ‘A’. rotate ‘Y’ by 3 you should get ’B’

Methods

Public static void main(String[] args)

  • Create a scanner object
  • Call the method run

public static void run(Scanner kb)

  • Ask the user how many times they want to use the app
  • Create a for loop
    • Ask the user to enter a message
    • Ask the user to enter a key
    • Call the method encrypt
    • Call the method decrypt
    • Display the encrypted and decrypted message

Public static String encrypt (String message, int key)

{

Convert the message to uppercase using the method toUpperCase from the String class

Declare a variable of type string called result to hold the encrypted message, initialize it to “”;

Create a loop to go through each letter of the message

{

Get each letter and store it in a variable of type char called c, use the charAt method: char c = message.charAt(i)

If the letter is between ‘A’ and ‘Z’

{

Add the key to the letter: c = c + key

//checking for wrap around

If c is greater than ‘Z’

{

Subtract 26 from c

                                           }

else

{

Add 26 to c

                                            }

}//end of if

Add the content of the variable c to the variable result (cumulative sum)

   }//end of the loop

Return result

}//end of the method

Public static String decrypt (String message, int key):

{

Declare a String called result and initialize it to “”

Create a for loop to go through each letter of the message

{

Get the character at each index   char c = message.charAt(i)

If the variable c is between ‘A’ and ‘Z’

{

Subtract the value of the variable key from the variable c

If the content of the variable c is less than ‘A’ //check for wrap around

{

Find the difference between the letter ‘A’ and the variable c : int diff = ‘A’ - c

c = (char)(‘Z’ – diff + 1)

                                            }

                                           else if c > ‘Z’    //

                                        {

                                                Int diff = ‘Z’ - c

                                               C = (char)((‘A’ + diff + 1)

                                       }

                          }

                          Concatenate the variable c to the variable result

              }//end for

             Return result

}//end of the method

Requirements

  • Must provide all the methods
  • Must generate the given output
  • Must follow the naming rules, and conventions
  • Must follow the indentation rules

Sample output:

How many times to you want to use the app: 4

Your message? I love java programming

Encoding key? 5

The encrypted message is:

N QTAJ OFAF UWTLWFRRNSL

The decrypted message is:

I LOVE JAVA PROGRAMMING

Solutions

Expert Solution

import java.util.*;
public class CaesarCipherProgram {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println(" Input the plaintext message : ");
        String plaintext = sc.nextLine();
        System.out.println(" Enter the value by which 
        each character in the plaintext         
        message gets shifted : ");
        int shift = sc.nextInt();
        String ciphertext = "";
        char alphabet;
        for(int i=0; i < plaintext.length();i++) 
        {
             // Shift one character at a time
            alphabet = plaintext.charAt(i);
            
            // if alphabet lies between a and z 
            if(alphabet >= 'a' && alphabet <= 'z') 
            {
             // shift alphabet
             alphabet = (char) (alphabet + shift);
             // if shift alphabet greater than 'z'
             if(alphabet > 'z') {
                // reshift to starting position 
                alphabet = (char) (alphabet+'a'-'z'-1);
             }
             ciphertext = ciphertext + alphabet;
            }
            
            // if alphabet lies between 'A'and 'Z'
            else if(alphabet >= 'A' && alphabet <= 'Z') {
             // shift alphabet
             alphabet = (char) (alphabet + shift);    
                
             // if shift alphabet greater than 'Z'
             if(alphabet > 'Z') {
                 //reshift to starting position 
                 alphabet = (char) (alphabet+'A'-'Z'-1);
             }
             ciphertext = ciphertext + alphabet;
            }
            else {
             ciphertext = ciphertext + alphabet;   
            }
        
    }
    System.out.println(" ciphertext : " + ciphertext);
  }
}

Related Solutions

Caesar Cipher Encryption] Write a method that takes two parameters: A parameter of type str and...
Caesar Cipher Encryption] Write a method that takes two parameters: A parameter of type str and a parameter of type int. The first parameter is the plaintext message, and the second parameter is the encryption key. The method strictly does the following tasks: a. Convert the string into a list (let us refer to it as lista). An element in the generated list is the position of the corresponding letter in the parameter string in the English alphabet. Example: ‘C’...
Problem 2: Caesar Cipher Decryption] Write a python method that takes two parameters: A parameter of...
Problem 2: Caesar Cipher Decryption] Write a python method that takes two parameters: A parameter of type str and a parameter of type int. The first parameter is the plaintext message, and the second parameter is the encryption key. The method strictly does the following tasks: a. Reverse the operations performed by the encryption method to obtain the plaintext message. The method’s header is as follows: def casesardecryption(s, key):
use C++ You will implement the following encryption and decryption functions/programs for the Caesar cipher. Provide...
use C++ You will implement the following encryption and decryption functions/programs for the Caesar cipher. Provide the following inputs and outputs for each function/program: EncryptCaesar Two inputs: A string of the plaintext to encrypt A key (a number) ▪ For the Caesar cipher: This will indicate how many characters to shift (e.g. for a key=3, A=>D, B=>E, ..., X=>A, Y=>B, Z=>C). Note that the shift is circular. One output: ◦ A string of the ciphertext or codeword DecryptCaesar Two inputs:...
Writing a caesar cipher in ARM assembly. INSTRUCTIONS: Step 1: The first thing you should do...
Writing a caesar cipher in ARM assembly. INSTRUCTIONS: Step 1: The first thing you should do is modify the case conversion program String.s (provided) Instead of subtracting 32 from all numbers you want to add a constant number which we will call the key. Assume that all input will be lowercase. So it'll look like this, k = 2; letter = 'a'; newletter = k+letter; Above is pseudocode and ABOVE NOT ASSEMBLY CODE DO NOT COPY. Use bl puts to...
Writing a caesar cipher in ARM assembly. INSTRUCTIONS: Step 1: The first thing you should do...
Writing a caesar cipher in ARM assembly. INSTRUCTIONS: Step 1: The first thing you should do is modify the case conversion program String.s (provided) Instead of subtracting 32 from all numbers you want to add a constant number which we will call the key. Assume that all input will be lowercase. So it'll look like this, k = 2; letter = 'a'; newletter = k+letter; Above is pseudocode and ABOVE NOT ASSEMBLY CODE DO NOT COPY. Use bl puts to...
Question Objective: The objective of this lab exercise is to give you practice in programming with...
Question Objective: The objective of this lab exercise is to give you practice in programming with one of Python’s most widely used “container” data types -- the List (commonly called an “Array” in most other programming languages). More specifically you will demonstrate how to: Declare list objects Access a list for storing (i.e., writing) into a cell (a.k.a., element or component) and retrieving (i.e., reading) a value from a list cell/element/component Iterate through a list looking for specific values using...
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is...
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is selected from the online contest problem archive (Links to an external site.), which is used mostly by college students world wide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest (Links to an external site.). For your convenience, I copied the description of the problem below with my note on the...
In Java!! Problem 2 – Compute the sum of the series (19%) The natural logarithm of...
In Java!! Problem 2 – Compute the sum of the series (19%) The natural logarithm of 2, ln(2), is an irrational number, and can be calculated by using the following series: 1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + 1/7 - 1/8 + ... 1/n The result is an approximation of ln(2). The result is more accurate when the number n goes larger. Compute the natural logarithm of 2, by adding up to n terms in...
IN JAVA Objectives Practice Link list, List node, compareTo, user defined data type, interfaces Movie List...
IN JAVA Objectives Practice Link list, List node, compareTo, user defined data type, interfaces Movie List Create a link list of the movies along with the rating, number of the people who watched the movie and the genre of the movie. Required classes Movie class ListNode class MovieList class List interface Driver class Movie class implements Comparable Attributes: movie’s name, genre, rating, number of people watched Methods: constructor, getter, setter, equals, compreTo, toString ListNode class Attributes: each node has two...
You need a data structure that contains fields for name (char array), color (char array), age...
You need a data structure that contains fields for name (char array), color (char array), age (int), and height (int). Name the struct Info when you define it. Create a function named getUserInfo() that asks the user for name, color, age, and height and then returns a data structure containing that information. It should do the following: What is your name? George What is your favorite color? Green What is your age? 111 What is your height in inches? 72...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT