question archive Create an interface MessageEncoder that has a single abstract method encode(plainText), where plainText is the message to be encoded
Subject:Computer SciencePrice:2.89 Bought3
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. 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. ( 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.
// 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;
}
// 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
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
String first = text.substring(0, mid);
//second half
String second = text.substring(mid);
String shuffled = "";
// shuffling
for(int i=0, j=0 ;i < first.length(); i++, j++)
{
shuffled += first.charAt(i);
if(j < second.length())
shuffled += second.charAt(i);
}
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;
}
// encodes and returns the given plain text
public String encode(String plainText){
String encodedMsg = plainText;
for(int i=0;i<n;i++)
encodedMsg = shuffle(encodedMsg);
return encodedMsg;
}
//decode and returns the cipher text
public String decode(String cipherText){
String decodedMsg = cipherText;
for(int i=0;i<n;i++)
decodedMsg = reShuffle(decodedMsg);
return decodedMsg;
}
} // ShuffleCipher
Next step
// CipherDriver.java (main program)
public class CipherDriver {
// driver program
public static void main(String[] args) {
int choise, choise2;
// to get input from the console
java.util.Scanner input = new java.util.Scanner(System.in);
while (true) {
System.out.println("\n\n1.To Encode A Message");
System.out.println("2.To Decode A Message");
System.out.println("3.Exit");
do {
System.out.print("Enter your choise: ");
choise = input.nextInt();
input.nextLine();
if (choise == 3)
System.exit(0);
} while (choise > 3 || choise < 1);
System.out.println("\n\n1.Substitution Cipher");
System.out.println("2.Shuffle Cipher");
System.out.println("3.Exit");
do {
System.out.print("Enter your choise: ");
choise2 = input.nextInt();
input.nextLine();
if (choise2 == 3)
System.exit(0);
} while (choise2 > 3 || choise2 < 1);
String text;
int n;
// outer switch to choose encoding / decoding
switch (choise) {
case 1: // Encoding a message
System.out.print("Enter text to be encode: ");
text = input.nextLine();
String encodedMsg = "";
switch (choise2) { // inner switch to choose cipher type
case 1: // using substitution cipher
System.out.print("Enter shift value: ");
n = input.nextInt();
input.nextLine();
SubstitutionCipher sub = new SubstitutionCipher(n);
encodedMsg = sub.encode(text);
System.out.println("Encode Message: " + encodedMsg);
break;
case 2: // using shuffle cipher
System.out.print("Enter number of shuffles: ");
n = input.nextInt();
input.nextLine();
ShuffleCipher shu = new ShuffleCipher(n);
encodedMsg = shu.encode(text);
System.out.println("Encode Message: " + encodedMsg);
break;
}// inner switch to choose cipher type
break;
case 2: // decoding a message
System.out.print("Enter text to be decode: ");
text = input.nextLine();
String decodedMsg = "";
switch (choise2) {
case 1: // using substitution cipher
System.out.print("Enter shift value: ");
n = input.nextInt();
input.nextLine();
SubstitutionCipher sub = new SubstitutionCipher(n);
decodedMsg = sub.decode(text);
System.out.println("Decoded Message: " + decodedMsg);
break;
case 2: // using shuffle cipher
System.out.print("Enter number of shuffles: ");
n = input.nextInt();
input.nextLine();
ShuffleCipher shu = new ShuffleCipher(n);
decodedMsg = shu.decode(text);
System.out.println("Encode Message: " + decodedMsg);
break;
} // inner switch to choose cipher type
break;
} // outer switch to choose encoding / decoding
} // while
} // main
} // CipherDriver
Please rate me. thanks alot