In: Computer Science
in java
Create the classes SubstitutionCipher and ShuffleCipher, as described below: 1. SubstitutionCipher implements the interfaces MessageEncoder and MessageDecoder. The constructor should have one parameter called 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.)
// 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;
}
// helper method to shift the given character by given shift
value
private char shift(char ch, int shiftValue){
char shiftedChar = ch;
// if the char is in lower case
if(ch >= 'a' && ch <= 'z')
shiftedChar = (char) ( 'a' + ( ch - 'a' + shiftValue ) %26 );
// upper case char
else if(ch >= 'A' && ch <= 'Z')
shiftedChar = (char) ( 'A' + ( ch - 'A' + shiftValue ) %26 );
return shiftedChar;
}
// encode and returns the given plain text
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
NOTE:
PLEASE GIVE:) ME A POSITIVE RATING THANK YOU:)