Ask Question

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

+2
Answers (1)
  1. 19 May, 20:32
    0
    See Explaination

    Explanation:

    public interface MessageEncoder

    {

    String encode (String s);

    }

    public class SubstitutionCipher implements MessageEncoder

    {

    private int shift;

    public SubstitutionCipher (int shift)

    {

    this. shift=shift;

    }

    private char shift_char (char a)

    {

    return (char) ((int) a+shift);

    }

    public String encode (String s)

    {

    String s1="";

    for (int i=0; i
    {

    s1=s1+shift_char (s. charAt (i));

    }

    return s1;

    }

    }

    public class ShuffleCipher implements MessageEncoder

    {

    private int shift;

    public ShuffleCipher (int shift)

    {

    this. shift=shift;

    }

    private String shuffle (String s1, String s2)

    {

    String s3="";

    int t=s1. length () + s2. length ();

    for (int i=0, j=0, k=0; i
    {

    if (i%2==0) {s3=s3+s1. charAt (j); j++; }

    else {s3=s3+s2. charAt (k); k++; }

    }

    return s3;

    }

    public String encode (String s)

    {

    String s1="", s2="", s3="";

    if (s. length () %2==0)

    {

    for (int i=0; i
    {

    if (i
    s1=s1+s. charAt (i);

    else

    s2=s2+s. charAt (i);

    }

    }

    else

    {

    for (int i=0; i
    {

    if (i<=s. length () / 2)

    s1=s1+s. charAt (i);

    else

    s2=s2+s. charAt (i);

    }

    }

    for (int i=0; i
    s3=shuffle (s1, s2);

    return s3;

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Create a class SubstitutionCipher that implements the interface MessageEncoder, as described above. The constructor should have one ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers