JAVA Language   Caesar Shift  Question:  Modify the Caesar class so that it will allow various sized shifts to be used, instead of just a shift of size 3. (Hint: Use an instance variable in the Caesar class to represent the shift, add a constructor to set it, and change the encode method to use it.)         import java.util.*;     public class TestCipher {       public static void main(String[] args)  {         int shift = 7;          Caesar caesar = new Caesar();          String text = "hello world";         String encryptTxt = caesar.encrypt(text);         System.out.println(text + " encrypted with shift " + shift + " is " + encryptTxt);       }     }     abstract class Cipher {       public String encrypt(String s) {         StringBuffer result = new StringBuffer("");         // Use a StringBuffer         StringTokenizer words = new StringTokenizer(s);     // Break s into its words         while (words.hasMoreTokens()) {                     // For each word in s           result.append(encode(words.nextToken()) + " ");   //  Encode it         }         return result.toString();                            // Return the result       } // encrypt()       public String decrypt(String s) {         StringBuffer result = new StringBuffer("");        // Use a StringBuffer         StringTokenizer words = new StringTokenizer(s);    // Break s into words         while (words.hasMoreTokens()) {                    // For each word in s           result.append(decode(words.nextToken()) + " ");  //  Decode it         }         return result.toString();                       // Return the decryption       } // decrypt()       public abstract String encode(String word);            // Abstract methods       public abstract String decode(String word);      } // Cipher     class Caesar extends Cipher {       public String encode(String word) {         StringBuffer result = new StringBuffer(); // Initialize a string buffer         for (int k = 0; k < word.length(); k++) { // For each character in word           char ch = word.charAt(k);               //  Get the character           ch = (char)('a' + (ch -'a'+ 3) % 26);   //  Perform caesar shift           result.append(ch);                   //  Append it to new string         }         return result.toString();              // Return the result as a string       } // encode()       public String decode(String word) {         StringBuffer result = new StringBuffer(); // Initialize a string buffer         for (int k = 0; k < word.length(); k++) { // For each character in word         char ch = word.charAt(k);                 //  Get the character            ch = (char)('a' + (ch - 'a' + 23) % 26); //  Perform reverse shift            result.append(ch);                     //  Append it to new string         }         return result.toString();            // Return the result as a string       } // decode()     } // Caesar

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter2: Using Data
Section: Chapter Questions
Problem 14RQ
icon
Related questions
Question
100%

JAVA Language 

 Caesar Shift 

Question: 

Modify the Caesar class so that it will allow various sized shifts to be used, instead of just a shift of size 3. (Hint: Use an instance variable in the Caesar class to represent the shift, add a constructor to set it, and change the encode method to use it.)
 
 

    import java.util.*;

    public class TestCipher {
      public static void main(String[] args)  {
        int shift = 7; 
        Caesar caesar = new Caesar(); 
        String text = "hello world";
        String encryptTxt = caesar.encrypt(text);
        System.out.println(text + " encrypted with shift " + shift + " is " + encryptTxt);
      }
    }

    abstract class Cipher {
      public String encrypt(String s) {
        StringBuffer result = new StringBuffer("");         // Use a StringBuffer
        StringTokenizer words = new StringTokenizer(s);     // Break s into its words
        while (words.hasMoreTokens()) {                     // For each word in s
          result.append(encode(words.nextToken()) + " ");   //  Encode it
        }
        return result.toString();                            // Return the result
      } // encrypt()

      public String decrypt(String s) {
        StringBuffer result = new StringBuffer("");        // Use a StringBuffer
        StringTokenizer words = new StringTokenizer(s);    // Break s into words
        while (words.hasMoreTokens()) {                    // For each word in s
          result.append(decode(words.nextToken()) + " ");  //  Decode it
        }
        return result.toString();                       // Return the decryption
      } // decrypt()
      public abstract String encode(String word);            // Abstract methods
      public abstract String decode(String word); 
    } // Cipher

    class Caesar extends Cipher {

      public String encode(String word) {
        StringBuffer result = new StringBuffer(); // Initialize a string buffer
        for (int k = 0; k < word.length(); k++) { // For each character in word
          char ch = word.charAt(k);               //  Get the character
          ch = (char)('a' + (ch -'a'+ 3) % 26);   //  Perform caesar shift
          result.append(ch);                   //  Append it to new string
        }
        return result.toString();              // Return the result as a string
      } // encode()

      public String decode(String word) {
        StringBuffer result = new StringBuffer(); // Initialize a string buffer
        for (int k = 0; k < word.length(); k++) { // For each character in word
        char ch = word.charAt(k);                 //  Get the character
           ch = (char)('a' + (ch - 'a' + 23) % 26); //  Perform reverse shift
           result.append(ch);                     //  Append it to new string
        }
        return result.toString();            // Return the result as a string
      } // decode()
    } // Caesar

 
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT