Answer:
Check the explanation
Step-by-step explanation:
// MessageEncoder. java
public interface MessageEncoder {
// encode and returns the given plain text
public abstract String encode(String plainText);
}
Next step
// MessageDecoder. java
public interface MessageDecoder {
// decode and returns the given cipher text
public abstract String decode(String cipherText);
}
Next step
// SubstitutionCipher. java
public class SubstitutionCipher implements MessageEncoder, MessageDecoder {
// value to shift the chacacters
private int shiftBy;
//1-argument constructor
public SubstitutionCipher (int shiftBy){
this. shiftBy = shiftBy;
}
public String encode(String plainText){
String encodedMsg = "";
for( int i = 0; i < plainText. length(); i++){
char ch = plainText. charAt(i);
encodedMsg += shift(ch, shiftBy);
}
return encodedMsg;
}
//decode and return the given cipher text
public String decode(String cipherText){
String decodedMsg = "";
for( int i = 0; i < cipherText. length(); i++){
char ch = cipherText. charAt(i);
decodedMsg += shift(ch, -shiftBy);
}
return decodedMsg;
}
} // SubstitutionCipher
Next step
// ShuffleCipher. java
public class ShuffleCipher implements MessageEncoder, MessageDecoder {
// number of iteration to shuffle
private int n;
//1-argument constructor which takes the shuffle value
public ShuffleCipher (int n){
this. n = n;
}
// shuffle and returns the given text for a single time
private String shuffle(String text){
int mid;
if (text. length() % 2 == 0)
mid = text. length()/2;
else
mid = (text. length()+1)/2;
// first half of given string
return shuffled;
}
// used to decode the shuffled message
private String reShuffle(String text){
String first ="", second="";
// splits into two halves by taking alternative chars
for(int i=0;i<text. length();i++)
{
if(i%2==0)
first += text. charAt(i);
else
second += text. charAt(i);
}
// merges the both halves
return first+second;