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 |
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.
Programming Message Encoder - Write in Java and include comments Create an interface MessageEncoder that has...
Question 1 : 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. Create a class SubstitutionCipher that implements the interface MessageDecoder as described above. The constructor should have one parameter called shift. Define the method decode 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...
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 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 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 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 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...
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...
Please I need help with the following questions in JAVA programming language. Please comment the code to help me understand, thanks. Exercise 1: Inheritance 1. Firstly, create and compile a simple class called Parent. Give it the following behaviour: a. A default constructor that does nothing other than print out “Parent default constructor” using System.out b. A single method called getMessage which returns a String, e.g. “Parent message” 2. Next, create and compile a class called Child. Give it the...
Java: Create a class called Vehicle with the following features: a. It has three private data members (instance variables): one is the manufacturer’s name (String manufacturer), the second is the number of cylinders in the engine (int cylinder), and the third is the owner (Person owner). The class Person is described above. b. It has three constructors, a no-argument constructor, a constructor with three parameters, and a copy constructor. The no-argument constructor will set the manufacturer to “None”, cylinder to...
Programming Language: Java 1. Write a class called Pair that stores a pair of numbers in private fields. Include a two-arg constructor, mutators and accessors. Override the equals method so that a Pair (1,2) would be considered equal to Pair (2,1). Write an appropriate toString method and make the toString method non-overrideable. 2. Write an interface called Comparable that includes a method isGreater, isLesser, and isSame. These methods should each take a Pair object as a parameter. 3. Write an...