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; ●
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
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.

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 :
- Create the class called MyCipher. Create the var key.
- Use the constructor to intialize the key of the class.
- Create the method called encode.
- 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 .
- If after adding the 5 the character surpasses the z then start again from a .
- 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 .
- Check code for more understanding everything is mentioned in the comments . Screenshots are also added .
Step by step
Solved in 2 steps with 5 images

Knowledge Booster
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
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

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)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON

Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education