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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education