212k views
4 votes
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.

1 Answer

4 votes

Answer:

Check the explanation

Step-by-step explanation:

MessageEncoder. java

public interface MessageEncoder

{

//single abstract method encode(plainText) where plainText is the message to be encoded.

public String encode(String plainText);

}//end interface

SubstitutionCipher. java

import java.util.Scanner;

public class SubstitutionCipher implements MessageEncoder, MessageDecoder

{

/**

The constructor should have 1 parameter called shift

*/

private int shift;

/**

Non-default constructor with one parameter

*/

public SubstitutionCipher(int numberSpaces)

{

shift = numberSpaces;

}//end non default constructor

/**

Define the method encode so that each letter is shifted by the value in shift

*/

public String encode(String plainText)

{

String cipherText = "";

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

{

Character encodeLetter = plainText.charAt(i);

cipherText = cipherText + shift1Character(encodeLetter);

}//end for loop

return cipherText;

}//end encode method

/**

Define the method decode so that each letter is shifted by minus the value in shift

*/

public String decode(String cipherText)

{

String plainText = "";

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

{

Character decodeLetter = cipherText.charAt(i);

plainText = plainText + shift1CharacterBack(decodeLetter);

}//end for loop

return plainText;

}//end encode method

/**

Hint: You may wish to define a private method that shifts a single character

*/

private Character shift1Character(Character oneLetter)

{

return (char)(oneLetter + shift);

}//end method

/**

Hint: You may wish to define a private method that shifts a single character

back to its original position

*/

private Character shift1CharacterBack(Character oneLetter)

{

return (char)(oneLetter - shift);

}//end method

////////////////////////////////////////////////////////////////////////////////////////////////////////

public static void main(String[] args)

{

Scanner keyboard = new Scanner(System.in);

SubstitutionCipher shift3Places = new SubstitutionCipher(3);

System.out.println("Substituation method using caesar shift: \\");

String plainText = "I think I am finally back on track\\";

System.out.println("Plain text to be encoded: " + plainText);

String cipher = shift3Places.encode(plainText);

System.out.println("Cipher text after encoding: " + cipher);

SubstitutionCipher shiftMinus3 = new SubstitutionCipher(-3);

System.out.println("\\\\Time to decode the cipher now.\\");

String decoded = shiftMinus3. encode(cipher);

System.out.println("Decoded text after removing cipher: " + decoded);

////////////////////////////////////////////////////////////////////////////System.out.println("Testing the decode method.");

////////////////////////////////////////////////////////////////////////////decoded = shift3Places. decode(cipher);

////////////////////////////////////////////////////////////////////////////System.out.println("Text after decoding: " + decoded);

}//end main

}//end class

MessageDecoder. java

public interface MessageDecoder

{

//single abstract method decode(cipherText) where cipherText is the message to be decoded.

public String decode(String cipherText);

}//end interface

Create an interface MessageDecoder that has a single abstract method decode(cipherText-example-1
User Cahit Gungor
by
5.2k points