Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

{

c = s.charAt(i);

first_half+=c;

}

int j=0;

for(i=half;i

{

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

{

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

{

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

{

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

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DATABASE Administrator Make A Difference

Authors: Mohciine Elmourabit

1st Edition

B0CGM7XG75, 978-1722657802

More Books

Students also viewed these Databases questions

Question

3. How frequently do the assessments occur?

Answered: 1 week ago