Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, in Java i need to write a program that uses the Caesar\'s Cipher to encrypt

ID: 3774506 • Letter: H

Question

Hi, in Java i need to write a program that uses the Caesar's Cipher to encrypt plain text messages. Using the instructions below,
An example run might look like this:

Enter the shift value: 11

A = K
B = L
C = M
D = N
E = O
F = P
G = Q
H = R
I = S
J = T
K = U
L = V
M = W
N = X
O = Y
P = Z
Q = A
R = B
S = C
T = D
U = E
V = F
W = G
X = H
Y = I
Z = J

Enter a message in plain text all upper case: NOW IS THE TIME

The plain text is...NOW IS THE TIME
The encrypted text...YZH TD ESP ETXP

1.There are many, many ways to implement a solution. You might one or more arrays to hold the alphabet and the shift for example.

2.Keep in mind as you work the requested shift that the alphabet has to "wrap around" for letters beyond Z. Modulus division can help with that problem.

3.Be sure to verify the shift worked! Just having the program run to completion without abending is not proof of validity.

Explanation / Answer

EncryptTextTest.java


import java.util.Scanner;


public class EncryptTextTest {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print("Enter the shift value: ");
       int shift = scan.nextInt();
       scan.nextLine();
       char characters[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
       char shiftChars[] = new char[characters.length];
       for(int i=0; i<characters.length; i++){
           shiftChars[i] = characters[(i+shift)%characters.length];
       }
       System.out.print("Enter a message in plain text all upper case: ");
       String plainText = scan.nextLine();
       String encryptedText ="";
         
       boolean found = false;
       for(int i=0; i<plainText.length(); i++){
           found = false;
           for(int j=0; j<characters.length; j++){
           if(plainText.charAt(i) == characters[j]){
               encryptedText = encryptedText+ shiftChars[j];
               found = true;
               break;
           }
             
           }
           if(!found)
               encryptedText = encryptedText+ plainText.charAt(i);
             
       }
       System.out.println("The plain text is "+plainText);
         
       System.out.println("The encrypted text is "+encryptedText);
   }

}

Output:

Enter the shift value: 11
Enter a message in plain text all upper case: NOW IS THE TIME
The plain text is NOW IS THE TIME
The encrypted text is YZH TD ESP ETXP

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote