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

Create an interface MessageEncoder that has a single abstract method encode(plai

ID: 3916990 • Letter: C

Question

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.)

Explanation / Answer

public interface MessageEncoder {

public String encode(String plaintext);

}

public class SubstitutionCipher implements MessageEncoder{

int shift;

public SubstitutionCipher(int shift) {

this.shift = shift;

}

public static void main(String[] args) {

SubstitutionCipher sc=new SubstitutionCipher(3);

System.out.println(sc.encode("Hi nasir how are u"));

}

@Override

public String encode(String plaintext) {

StringBuilder strBuilder = new StringBuilder();

char c;

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

{

c = plaintext.charAt(i);

if (Character.isLetter(c))

{

c = (char) (plaintext.charAt(i) + shift);

if ((Character.isLowerCase(plaintext.charAt(i)) && c > 'z')

|| (Character.isUpperCase(plaintext.charAt(i)) && c > 'Z'))

c = (char) (plaintext.charAt(i) - (26 - shift));

}

strBuilder.append(c);

}

return strBuilder.toString();

}

}

public class ShuffleCipher implements MessageEncoder{

int n;

public ShuffleCipher(int n) {

super();

this.n = n;

}

public static void main(String[] args) {

System.out.println(new ShuffleCipher(1).encode("abcdefghi"));

}

@Override

public String encode(String plaintext) {

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

String s1=plaintext.substring(0, (int)Math.floor(plaintext.length()/2+1));

String s2=plaintext.substring((int)Math.ceil(plaintext.length()/2+1), plaintext.length());

int length=plaintext.length();

String result="";

for(int j=0; j< Math.ceil(length/2); j++) {

if(s1.length()>j)

result+=s1.charAt(j);

if(s2.length()>j)

result+=s2.charAt(j);

}

plaintext=result;

}

return plaintext;

}

}

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