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

guide me in to the right direction to solve without using array , scanner, syste

ID: 3873822 • Letter: G

Question

guide me in to the right direction to solve without using array , scanner, systemout

Caesar Cipher. The core idea is that two people will agree in advance on a number (between 1 and 64 in the case of this task) and then to encode a message, rotate each character forward that number of positions (with wrap-around if needed), and to decode a message, rotate each character backward that number of positions (again, with wrap-around if needed). If the person picks a number greater than 64, we'll just keep subtracting 64 from that until we get a value in the valid range (so if they pick 131 we'd subtract 64 and get 67 which is still too big so subtract 64 again and get 3 which we would then use as the shift amount). The Rotate 32 system is actually just a special case of the Caesar Cipher, where the agreed-upon number is always 32 For this task, there are two methods to design and implement public static String EACaah!String plaintext, dRE shift) and public static StringRER (String ciphertext, dRE shift) Each takes a String and an integer. The integer represents the agreed-upon shift amount in both cases. The String is either the plaintext form of the message or the ciphertext version. Each method returns the encoding/decoding of the String it got, as indicated by the method name The toCaesar method will need to "shift and wrap" forward. For example, if the message had an H and the agreed-upon shift were 7, the H (UNICODE 72) would become an O (UNICODE 79) since 72+7 is 79. If the message had a Zand the agreed upon shift were 7, the Z (UNICODE 90) would become a ! (UNICODE 33) due to the wrap-around; 90+7 is 97 which is outside the valid range so we need to subtract 64 from that to effectively "wrap around" the valid range, which gets us 33 Similarly, the fromCaesar method will need to "shift and wrap" backward. For example, if the coded message had an O and the agreed-upon shift were 7, the O (UNICODE 79) would become an H (UNICODE 72) since 79-7 is 72. If the message had a # and the agreed-upon shift were 7, the # (UNICODE 35) would become a l (UNICODE 90) due to the wrap-around; 35-7 is 28 which is outside the valid range so we need to add 64 to that to effectively "wrap around" the valid range, which gets us 92

Explanation / Answer

import java.util.Scanner;

class Caeser {

   public static String toCaeser(String plaintext, int shift) {
       char ch; //variable to hold a cahracter of string
       int value;//integer value of respective character
       String temp = "";//temporary string

       for (int i=0; i<plaintext.length(); i++)
        {
           //convert strings value to the integer value and add
           //shift value to it
            value = ((int)plaintext.charAt(i) + shift);
            //if value is greater than 90 just subtract 64 to get
            //it into the range
            if (value > 90) value -= 64;
            //convert integer value to the character value
            ch = (char)value;
            //concatinate character to temp string
            temp += ch;
        }
        //return final string
       return temp;
   }

   public static String fromCaeser(String ciphertext, int shift) {
       char ch; //variable to hold a cahracter of string
       int value;//integer value of respective character
       String temp = "";//temporary string

       for (int i=0; i<ciphertext.length(); i++)
        {
           //convert strings value to the integer value and add
           //shift value to it
            value = ((int)ciphertext.charAt(i) - shift);
            //if value is less than 33 just add 64 to get
            //it into the range
            if (value < 33) value += 64;
            //convert integer value to the character value
            ch = (char)value;
            //concatinate character to temp string
            temp += ch;
        }
        //return final string
       return temp;
   }

   public static void main(String[] args) {
       //Print Welcome message and promt user for input of plain text and
       //the agreed upon shift value
       System.out.println("Hello Welcome to Caeser Cipher!");
       System.out.println("=============================================="
                           + "========================");
       System.out.println("Please enter text you want to convert to Caeser"
                           + " Cipher.");
       System.out.print("=> ");
       //Create a scanner object to get input
       Scanner sc = new Scanner(System.in);
       String plaintext = sc.nextLine();
       System.out.print("Please enter the Shift value: ");
       //Input plain text
       int shift = sc.nextInt();
       //If plain text is greater then 64 then we reduce it by 64 generate
       // number between 1 to 64
       while(shift > 64)
           shift -= 64;

       System.out.println("==============================================="
                           + "=======================");
       System.out.println("Converting "" + plaintext +"" to Caeser with "
                           + "shift value: "" + shift + """);
       System.out.println("================================================"
                           + "======================");
       //stroring the value of ciphered text into string
       String cCipher = toCaeser(plaintext, shift);
       System.out.println("Caeser Cipher: " + cCipher);
       System.out.println("================================================"
                           + "======================");
       System.out.println("Plaint text converted from Caeser Cipher: "
                           + fromCaeser(cCipher, shift));
       System.out.println("================================================"
                           + "======================");

   }
}


==========================================
OUTPUT
==========================================
javac Caeser.java && java Caeser
Hello Welcome to Caeser Cipher!
======================================================================
Please enter text you want to convert to Caeser Cipher.
=> ABCDEFGHIJKLMNOPQRSTWXYZ
Please enter the Shift value: 7
======================================================================
Converting "ABCDEFGHIJKLMNOPQRSTWXYZ" to Caeser with shift value: "7"
======================================================================
Caeser Cipher: HIJKLMNOPQRSTUVWXYZ ! {Note:output contains some non printable characters}
======================================================================
Plaint text converted from Caeser Cipher: ABCDEFGHIJKLMNOPQRSTWXYZ
======================================================================