Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Message Decoder - Write in Java and include comments Create an inter

ID: 3919149 • Letter: P

Question

Programming Message Decoder - Write in Java and include comments

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.

Grading Rubric

Task

Points

Working solution for the problem

5

Best practices in programming

1

Total

6

====================================================

for shift

code in java

import java.io.*;

import java.util.Scanner;//import scanner class for taking input from uer

interface MessageEncoder//creates interface named MessageEncoder

{

String encode( String plainText);//declare the encode method with return type string and argument string

}

class SubstitutionCipher implements MessageEncoder//class that implements the interface MessageEncoder

{

int shift;//integer shift

SubstitutionCipher(int shift)//constuctor that have one parameter

{

this.shift=shift;

}

private char ShiftSingleCharacter(char s)//private method that shift one character

{

return ((char)(((s - 'a' + this.shift) % 26) + 'a'));//return the shifted character

}

public String encode(String plainText)//abstract method implementation

{

String ans="";//ans string that holds the shifted characters

for (char c : plainText.toCharArray()) //first assgin the plainText characters to char c array

{

ans+=Character.toString((ShiftSingleCharacter(c)));//and call ShiftSingleCharacter for every single character in the plaintext

}

return ans;//return the ans

}

public static void main (String[] args)

{

Scanner input = new Scanner(System.in);//creates the object of the scanner class

System.out.print("Enter message : ");

String PlainText=input.nextLine();//taking plain text from the user

System.out.print("Enter Shift : ");//taking shift from the user

int shift=input.nextInt();

SubstitutionCipher t=new SubstitutionCipher(shift);//creates the object of the class SubstitutionCipher

String s=t.encode(PlainText);//calling the method encode

System.out.println("Shifted message is :"+s);//prints the ans

}

}

====================================================

For shuffle

code in java

import java.io.*;

import java.util.Scanner;//import scanner class for taking input from uer

interface MessageEncoder//creates interface named MessageEncoder

{

String encode( String plainText);//declare the encode method with return type string and argument string

}

class ShuffleCipher implements MessageEncoder//class that implements the interface MessageEncoder

{

int n;//integer n

ShuffleCipher(int n)//constuctor that have one parameter

{

this.n=n;

}

private String OneShuffle(String s)//private method that shuffle the string on time

{

int len=s.length();//assign the length of the string s

int half,len1,len2,i;//variables

char c;//character c

String first_half="",second_half="",ans="";//variable first_half holds the first half of the string and second_half holds the second half of the string

if(len%2!=0)//if length of the string s is odd

{

half=(len/2) +1;

}

else//if it is even

half=len/2;

for(i=0;i<half;i++)//assign the first half of the string s to first_half

{

c = s.charAt(i);

first_half+=c;

}

int j=0;

for(i=half;i<len;i++)//assign the second half of the string s to first_half

{

c = s.charAt(i);

second_half+=c;

}

len1=first_half.length();//calculate the length of the first_half

len2=second_half.length();//calculate the length of the second_half

if(len1==len2)//if both length are same

{

for(i=0;i<len1;i++)

{

c = first_half.charAt(i);

ans+=c;

c = second_half.charAt(i);

ans+=c;

}

}

else//if both length are not same

{

for(i=0;i<len2;i++)

{

c = first_half.charAt(i);

ans+=c;

c = second_half.charAt(i);

ans+=c;

}

c = first_half.charAt(i);

ans+=c;//add the last character of the first_half

}

return ans;

}

public String encode( String plainText)//abstract method implementation

{

String ans="";//ans string that holds the shuffle string

ans=OneShuffle(plainText);//calling theOneShuffle

for(int i=0;i<n-1;i++)//if n is more the 1 then

{

ans=OneShuffle(ans);

}

return ans;

}

public static void main (String[] args)

{

Scanner input = new Scanner(System.in);//creates the object of the scanner class

System.out.print("Enter message : ");

String PlainText=input.nextLine();//taking plain text from the user

System.out.print("Enter no of shuffle : ");//taking shuffle from the user

int shuffle=input.nextInt();

ShuffleCipher t=new ShuffleCipher(shuffle);//creates the object of the class SubstitutionCipher

String s=t.encode(PlainText);//calling the method encode

System.out.println(" Shuffled message is :"+s);//prints the ans

}

}

Task

Points

Working solution for the problem

5

Best practices in programming

1

Total

6

Explanation / Answer

Hii.. I have written decoders for the above. Please check below.

ShuffleDecoder.java

import java.io.*;

import java.util.Scanner;//import scanner class for taking input from uer

interface MessageDecoder// creates interface named MessageEncoder

{

String decode(String decodedText);// declare the encode method with return

// type string and argument string

}

class ShuffleDecoder implements MessageDecoder// class that implements the

// interface MessageEncoder

{

int n;// integer n

ShuffleDecoder(int n)// constuctor that have one parameter

{

this.n = n;

}

private String OneShuffle(String s)// private method that shuffle the string

// on time

{

int len = s.length();// assign the length of the string s

int half, len1, len2, i;// variables

char c;// character c

String first_half = "", second_half = "", ans = "";// variable

// first_half holds

// the first half of

// the string and

// second_half holds

// the second half

// of the string

if (len % 2 != 0)// if length of the string s is odd

{

half = (len / 2) + 1;

}

else// if it is even

half = len / 2;

for (i = 0; i < half; i++)// assign the first half of the string s to

// first_half

{

c = s.charAt(i);

first_half += c;

}

//System.out.println("dhhd "+first_half);

int j = 0;

for (i = half; i < len; i++)// assign the second half of the string s to

// first_half

{

c = s.charAt(i);

second_half += c;

}

//System.out.println("s "+second_half);

len1 = first_half.length();// calculate the length of the first_half

len2 = second_half.length();// calculate the length of the second_half

if (len1 == len2)// if both length are same

{

for (i = 0; i < len1; i++)

{

c = first_half.charAt(i);

ans += c;

i+=1;

}

for(i=0;i<len2;i++){

c = second_half.charAt(i);

ans += c;

i+=1;

}

//System.out.println(ans);

for (i = 1; i < len1; i++)

{

c = first_half.charAt(i);

ans += c;

i+=1;

}

for(i=1;i<len2;i++){

c = second_half.charAt(i);

ans += c;

i+=1;

}

}

else// if both length are not same

{

for (i = 0; i < len1; i++)

{

c = first_half.charAt(i);

ans += c;

i+=1;

}

for(i=1;i<len2;i++){

c = second_half.charAt(i);

ans += c;

i+=1;

}

//System.out.println(ans);

for (i = 1; i < len1; i++)

{

c = first_half.charAt(i);

ans += c;

i+=1;

}

for(i=0;i<len2;i++){

c = second_half.charAt(i);

ans += c;

i+=1;

}

//ans += c;// add the last character of the first_half

}

return ans;

}

public String decode(String decodeText)// abstract method implementation

{

String ans = "";// ans string that holds the shuffle string

ans = OneShuffle(decodeText);// calling theOneShuffle

for (int i = 0; i < n - 1; i++)// if n is more the 1 then

{

ans = OneShuffle(ans);

}

return ans;

}

public static void main(String[] args)

{

Scanner input = new Scanner(System.in);// creates the object of the

// scanner class

System.out.print("Enter decoded message : ");

String PlainText = input.nextLine();// taking plain text from the user

System.out.print("Enter no of shuffle : ");// taking shuffle from the

// user

int shuffle = input.nextInt();

ShuffleDecoder t = new ShuffleDecoder(shuffle);// creates the object of

// the class

// SubstitutionCipher

String s = t.decode(PlainText);// calling the method encode

System.out.println(" Plain message is :" + s);// prints the ans

}

}

Output:

Enter decoded message : snhsatoh

Enter no of shuffle : 2

Plain message is :santhosh

SubstitutionDecoder.java

import java.util.Scanner;

interface MessageDecoder// creates interface named MessageEncoder

{

String decode(String decodedText);// declare the encode method with return

// type string and argument string

}

class SubstitutionDecoder implements MessageDecoder// class that implements the

// interface MessageEncoder

{

int shift;// integer shift

SubstitutionDecoder(int shift)// constuctor that have one parameter

{

this.shift = shift;

}

private char ShiftSingleCharacter(char s)// private method that shift one

// character

{

//System.out.println((char) (((s - 'a' - this.shift) % 26) + 'a'));

return ((char) (((s - 'a' - this.shift) % 26) + 'a'));// return the

// shifted

// character

}

public String decode(String decodeText)// abstract method implementation

{

String ans = "";// ans string that holds the shifted characters

for (char c : decodeText.toCharArray()) // first assgin the plainText

// characters to char c array

{

ans += Character.toString((ShiftSingleCharacter(c)));// and call

// ShiftSingleCharacter

// for every

// single

// character

// in the

// plaintext

}

return ans;// return the ans

}

public static void main(String[] args)

{

Scanner input = new Scanner(System.in);// creates the object of the

// scanner class

System.out.print("Enter decoded message : ");

String PlainText = input.nextLine();// taking plain text from the user

System.out.print("Enter Shift : ");// taking shift from the user

int shift = input.nextInt();

SubstitutionDecoder t = new SubstitutionDecoder(shift);// creates the

// object of the

// class

// SubstitutionCipher

String s = t.decode(PlainText);// calling the method encode

System.out.println("Plain message is :" + s);// prints the ans

}

}

Output:

Enter decoded message : vdqwkrvk

Enter Shift : 3

Plain message is :santhosh

Please check it and let me know any issues. Thank you. All the best.