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

Respuesta :

Answer:

Check the explanation

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;

       

   

 

Q&A Education