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

Java code. I have this programming problem that already has been solved: Create

ID: 3914065 • Letter: J

Question

Java code.

I have this programming problem that already has been solved:

Create an interface MessageEncoder that has a single abstract method encode(plainText), where plainText is the message to be encoded. The method will return the encoded message.

Create a class SubstitutionCipher that implements the interface MessageEncoder, as described above. The constructor should have one parameter called shift. Define the method encode so that each letter is shifted by the value in shift. Defne the method encode so that each letter is shifted by the value in shift. For example, if shift is 3, a will be replaced by d, b will be replaced by e, c will be replaced by f, and so on. (Hint: You may wish to define a private method that shifts a single character.)

Create a class ShuffleCipher that implements the interface MessageEncoder, as described above. The constructor should have one parameter called n. Define the method encode so that the message is shuffled n times. To perform one shuffle, split the message in half and then take characters from each half alternately. For example, if the message is abcdefghi, the halves are abcde and fghi. The shuffled message is afbgchdie. (Hint: You may wish to define a private method that performs one shuffle.

What I actually need is to modify the given code from already solved programming problem 2

the code is given in this link https://www.chegg.com/homework-help/questions-and-answers/messageencoder-java-create-interface-messageencoder-single-abstract-method-encode-plaintex-q5674325

I need the following modifications:

Create an interface MessageDecoder that has a single abstract method decode(cipherText), where cipherText is the message to be decoded. The method will return the decoded message. Modify the classes SubstitutionCipher and ShuffleCipher, as described in Programming Problem 2, above, so that they implement MessageDecoder as well as the interface MessageEncoder described above. Finally, write a program that allows a user to encode and decode messages entered on the keyboard.

Explanation / Answer

interface MessageEncoder{

public String encode(String plainText);

public String decode(String ciperText);

}

class SubstitutionCipher implements MessageEncoder{

int offset;

SubstitutionCipher(int shift){

offset=shift;

}

public String encode(String plainText) {

// declare constants

final int ALPHABET_SIZE = 26; // used to wrap around A-Z

String encoded = ""; // base for string to return

char letter; // letter being processed

// convert message to upper case

plainText = plainText.toUpperCase();

  

// process each character of the message

for (int index = 0; index < plainText.length(); index++)

{

// get the letter and determine whether or not to

// add the cipher value

letter = plainText.charAt(index);

if (letter >='A' && letter <= 'Z')

{

// is A-Z, so add offset

// determine whether result will be out of A-Z range

if ((letter + offset) > 'Z') // need to wrap around to 'A'

letter = (char)(letter - ALPHABET_SIZE + offset);

else

if ((letter + offset) < 'A') // need to wrap around to 'Z'

letter = (char)(letter + ALPHABET_SIZE + offset);

else

letter = (char) (letter + offset);

}

// build encoded message string

encoded = encoded + letter;

}

return encoded;

}

public String decode(String ciperText) {

// declare constants

final int ALPHABET_SIZE = 26; // used to wrap around A-Z

String decoded = ""; // base for string to return

char letter; // letter being processed

// make ciperText message upper case

ciperText = ciperText.toUpperCase();

  

// process each letter of message

for (int index = 0; index < ciperText.length(); index++)

{

// get letter and determine whether to subtract cipher value

letter = ciperText.charAt(index);

if (letter >= 'A' && letter <= 'Z')

{

// is A-Z, so subtract cipher value

// determine whether result will be out of A-Z range

if ((letter - offset) < 'A') // wrap around to 'Z'

letter = (char)(letter + ALPHABET_SIZE - offset);

else

if ((letter - offset) > 'Z') // wrap around to 'A'

letter = (char)(letter - ALPHABET_SIZE - offset);

else

letter = (char) (letter - offset);

}

  

// build decoded message

decoded = decoded + letter;

}

return decoded;

}

}

class ShuffleCipher implements MessageEncoder{

private int n;

public ShuffleCipher(int n){

this.n = n;

}

public String encode(String plainText){

String cipherText = plainText;

for(int i=0; i<n; i++){

cipherText = shuffle(cipherText);

}

return cipherText;

}

private String shuffle(String s){

String shuffled = "";

int mid = s.length()/2;

String first = s.substring(0, mid);

String second = s.substring(mid, s.length());

for(int i=0;i<first.length(); i++){

shuffled = shuffled + first.charAt(i) + second.charAt(i);

}

// if the length of the message is odd; add in the last character

if(second.length() > first.length()){

shuffled = shuffled + second.charAt(second.length()-1);

}

return shuffled;

}

public String decode(String cipherText){

String plainText = cipherText;

for(int i=0; i<n;i++){

plainText = unshuffle(plainText);

}

return plainText;

}

private String unshuffle(String s){

String unshuffled = "";

for(int i=0;i<s.length();i+=2){

unshuffled = unshuffled + s.charAt(i);

}

for(int i=1;i<s.length();i+=2){

unshuffled = unshuffled + s.charAt(i);

}

return unshuffled;

}

}

public class Main{

public static void main(String args[]){

SubstitutionCipher obj=new SubstitutionCipher(3);

System.out.println(obj.encode("hai"));

System.out.println(obj.decode("KDL"));

ShuffleCipher obj2=new ShuffleCipher(1);

System.out.println(obj2.encode("abcdefghi"));

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote