2. Set up a class named MyCipher. The class will have an integer variable named key. It also has the following methods: • • • a constructor initializing key with 0. a method named setKey(int k) that takes a positive integer and puts it into key, a method named encode(String p) that takes a plain text p and encodes it to a cipher text by adding the key value to each alphabet character of plain text p, (For example, if p="attack at dawn" and key is 5, it should be encrypted as "fyyfhpefyeifas" because 'a'+5 = 'f', 't'+5= 'y', 'c'+5= 'h', 'k'+5= 'p', '+5= 'e', 'd'+5 = 'i', 'w'+5 = 'a', and 'n'+5 = 's'.) • a method named decode(String c) that takes a cipher text c and decodes it to a plain text by subtracting the key value from each alphabet character of c. 3. Assume that the only characters allowed in both plain text and cipher text are lower-case alphabet letters and blanks. 4. Provide steps in the main method to test each method of MyCipher class by including the following steps: • Read a key; • Read a plain text and print the encoded cipher text; Read a cipher text and print the decoded plain text; ●

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

Could use some assistance on this Java exercise. Loops have proven to be quite challenging for me so any clarity on how/steps to complete this problem would be seriously appreciated.

2. Set up a class named `MyCipher`. The class will have an integer variable named `key`. It also has the following methods:
   - A constructor initializing `key` with 0.
   - A method named `setKey(int k)` that takes a positive integer and puts it into `key`.
   - A method named `encode(String p)` that takes a plain text `p` and encodes it to a cipher text by adding the key value to each alphabet character of plain text `p`. For example, if `p = "attack at dawn"` and `key` is 5, it should be encrypted as "fyyfhp fYifas" because `a+5 = 'f'`, `t+5 = 'y'`, `c+5 = 'h'`, `k+5 = 'p'`, `' '+5 = 'e'`, `d+5 = 'i'`, `w+5 = 'a'`, and `n+5 = 's'`.
   - A method named `decode(String c)` that takes a cipher text `c` and decodes it to a plain text by subtracting the key value from each alphabet character of `c`.

3. Assume that the only characters allowed in both plain text and cipher text are lower-case alphabet letters and blanks.

4. Provide steps in the main method to test each method of `MyCipher` class by including the following steps:
   - Read a key;
   - Read a plain text and print the encoded cipher text;
   - Read a cipher text and print the decoded plain text.
Transcribed Image Text:2. Set up a class named `MyCipher`. The class will have an integer variable named `key`. It also has the following methods: - A constructor initializing `key` with 0. - A method named `setKey(int k)` that takes a positive integer and puts it into `key`. - A method named `encode(String p)` that takes a plain text `p` and encodes it to a cipher text by adding the key value to each alphabet character of plain text `p`. For example, if `p = "attack at dawn"` and `key` is 5, it should be encrypted as "fyyfhp fYifas" because `a+5 = 'f'`, `t+5 = 'y'`, `c+5 = 'h'`, `k+5 = 'p'`, `' '+5 = 'e'`, `d+5 = 'i'`, `w+5 = 'a'`, and `n+5 = 's'`. - A method named `decode(String c)` that takes a cipher text `c` and decodes it to a plain text by subtracting the key value from each alphabet character of `c`. 3. Assume that the only characters allowed in both plain text and cipher text are lower-case alphabet letters and blanks. 4. Provide steps in the main method to test each method of `MyCipher` class by including the following steps: - Read a key; - Read a plain text and print the encoded cipher text; - Read a cipher text and print the decoded plain text.
Expert Solution
Explanation

Here is the approach : 

  1. Create the class called MyCipher. Create the var key. 
  2. Use the constructor to intialize the key of the class.
  3. Create the method called encode. 
  4. For the encoding iterate over the string one by one . Convert the character to ASCII value . After getting the ASCII value for the character check if the character is lowercase or not . If the character is lowercase then add the value of  key in the character ASCII and add the char in the encoded string . 
  5. If after adding the 5 the character surpasses the z then start again from a . 
  6. Similarly do for the decode subtract the key value from  the ASCII value of the character. During the decode also take care of the character ASCII value getting smaller than a . In such case again start from z and go back .
  7. Check code for more understanding everything is mentioned in the comments . Screenshots are also added .
steps

Step by step

Solved in 2 steps with 5 images

Blurred answer
Knowledge Booster
Adjacency Matrix
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
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