Question

Programming Message Encoder - Write in Java and include comments Create an interface MessageEncoder that has...

Programming Message Encoder - Write in Java and include comments

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. (Hint: 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. (Hint: You may wish to define a private method that performs one shuffle.)

Grading Rubric

Task

Points

Working solution for the problem

6

Best practices in programming

2

Total

8

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

}

}

output

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("\nShuffled message is :"+s);//prints the ans

}

}

output

if you have any query please ask me in the comment i am here for help. thnx.

Add a comment
Know the answer?
Add Answer to:
Programming Message Encoder - Write in Java and include comments Create an interface MessageEncoder that has...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB....

    Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...

  • Cryptography, the study of secret writing, has been around for a very long time, from simplistic...

    Cryptography, the study of secret writing, has been around for a very long time, from simplistic techniques to sophisticated mathematical techniques. No matter what the form however, there are some underlying things that must be done – encrypt the message and decrypt the encoded message. One of the earliest and simplest methods ever used to encrypt and decrypt messages is called the Caesar cipher method, used by Julius Caesar during the Gallic war. According to this method, letters of the...

  • Write the programming C please, not C++. The main function should be to find the offset...

    Write the programming C please, not C++. The main function should be to find the offset value of the ciper text "wPL2KLK9PWWZ7K3ST24KZYKfPMKJ4SKLYOKRP4KFKP842LK0ZTY43 " and decrypt it. In cryptography, a Caesar Cipher is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be...

  • MasterMind in Java Activity MasterMind project Create a new Java Application project named MasterMind allowing Netbeans...

    MasterMind in Java Activity MasterMind project Create a new Java Application project named MasterMind allowing Netbeans IDE to create the main class called MasterMind.java MasterMind class Method main() should: Call static method System.out.println() and result in displaying “Welcome to MasterMind!” Call static method JOptionPane.showMessageDialog(arg1, arg2) and result in displaying a message dialog displaying “Let’s Play MasterMind!” Instantiate an instance of class Game() constants Create package Constants class Create class Constants Create constants by declaring them as “public static final”: public...

  • Needs Help In Java Programming language Exceptional Cars Purpose To review interfaces and exception usage. Directions...

    Needs Help In Java Programming language Exceptional Cars Purpose To review interfaces and exception usage. Directions Your task is to write a class called Car. Your class should have the following fields and methods: private int position private boolean headlightsOn public Car() - a constructor which initializes position to 0 and headlightsOn to false public Car(int position) - a constructor which initializes position to the passed in value and headlightsOn to false, and it should throw a NegativeNumberException with a...

  • Adv. Java program - create a math quiz

    Create a basic math quiz program that creates a set of 10 randomly generated math questions for the following operations:AdditionSubtractionMultiplicationDivisionModuloExponentThe following source files are already complete and CANNOT be changed:MathQuiz.java (contains the main() method)OperationType.java (defines an enumeration for valid math operations and related functionality)The following source files are incomplete and need to be coded to meet the indicated requirements:MathQuestionable.javaA Random object called RANDOM has already been declared and assigned, and you must not change this assignment in any way.Declare and assign an interface integer called MAX_SMALL and assign it the...

  • *URGENT JAVA PROGRAMMING LAB* so I need to write some classes for my lab if someone...

    *URGENT JAVA PROGRAMMING LAB* so I need to write some classes for my lab if someone could give me an outline or just how I should so it that would be awesom here is the questions please help ASAP A. A histogram is used to plot tabulated frequencies. Create a Histogram class that can be used to maintain and plot the frequencies of numbers that fall within a specified range. The Histogram class contain will an array of counters with...

  • Java Project In Brief... For this Java project, you will create a Java program for a...

    Java Project In Brief... For this Java project, you will create a Java program for a school. The purpose is to create a report containing one or more classrooms. For each classroom, the report will contain: I need a code that works, runs and the packages are working as well The room number of the classroom. The teacher and the subject assigned to the classroom. A list of students assigned to the classroom including their student id and final grade....

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • Do this using the C language. show me the code being executed and also copy and...

    Do this using the C language. show me the code being executed and also copy and paste the code so i can try it out for myseld Instructions A cipher is mirrored algorithm that allow phrases or messages to be obfuscated (ie. "scrambled"). Ciphers were an early form of security used to send hidden messages from one party to another. The most famous and classic example of a cipher is the Caesar Cipher. This cypher worked by shifting each letter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT