Question

Question 1 : Create an interface MessageDecoder that has a single abstract method decode( cipherText), where...

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 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 MessageDecoder. The constructor should have one parameter called n. Define the method decode 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. Finally, write a program that allows a user to encode and decode messages entered on the keyboard

Question 2

Create a JavaFX application that uses a TextField to get a message and encode or decode it using the classes described in Question 1. Use four buttons to control the kind of cipher used and to specify whether to encode or decode the message. Also, use a TextField to get the number used in the constructor for the ciphers.

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

MessageDecoder.java

public interface MessageDecoder {

   public String decode(String cipherText);
}

ShuffleCipher.java

public class ShuffleCipher implements MessageDecoder{
  
private int num;
  
   public ShuffleCipher(int number) {
      
       this.num = number;
   }

   @Override
   public String decode(String text) {
      
       int size = text.length();
       String text1;
       String text2;
      
       if(size%2==0) {
          
       while(num>0) {
      
           text1=text.substring(0, size/2);
           text2=text.substring(size/2,size);
           text ="";
           for(int i=0; i<text2.length();i++) {
               text = (text + text1.charAt(i))+ text2.charAt(i);
           }
          
       num--;  
       }
      
       }
      
       else {
          
           while(num>0) {
               text1=text.substring(0, size/2+1);
               text2=text.substring(size/2+1 ,size);
              
               text ="";
               for(int i=0; i<text2.length();i++) {
                   text = (text + text1.charAt(i))+ text2.charAt(i);
               }
              
               text = text + text1.charAt(size/2);
              
               num--;  
               }
          
       }
          
       return text;
   }

}

SubstitutionCipher.java

public class SubstitutionCipher implements MessageDecoder{
  
   private int num;
  
   public SubstitutionCipher(int number) {
      
       this.num = number;
   }

   @Override
   public String decode(String text) {
      
      
       char[] array = text.toCharArray();
      
       int size = text.length();
      
       String ans = "";
      
       for(int i=0; i<size; i++) {
          
           array[i] = (char) (array[i] + num);
          
           ans = ans + array[i];
          
       }
      
       return ans;
   }

}

MainMethod .java

public class MainMethod {

   public static void main(String[] args) {
  
      
       Scanner sc = new Scanner(System.in);
      
       //Testing SubstitutionCipher
      
       System.out.println("Testing Substitution Cipher ");
       System.out.println("Enter the shift value. It should be integer");
      
       int shiftValue = sc.nextInt();
      
       MessageDecoder md = new SubstitutionCipher(shiftValue);
      
       System.out.println("Enter the input to decode. It should be the string of alphabets");

       String input = sc.next();
      
       System.out.println("Decoded Message is " + md.decode(input));
      
       //Testing ShuffleCipher
      
       System.out.println("********Testing SuffleCipher*************");
      
System.out.println("Enter the shift value. It should be integer");
      
   shiftValue = sc.nextInt();
      
       md = new ShuffleCipher(shiftValue);
      
       System.out.println("****************Testing Decoding***************************");
      
       System.out.println("Enter the input to decode. It should be the string of alphabets");

       input = sc.next();
      
       System.out.println("Decoded Message is " + md.decode(input));
      
      
      
      
   }

}

Output is

Testing Substitution Cipher
Enter the shift value. It should be integer
2
Enter the input to decode. It should be the string of alphabets
abcd
Decoded Message is cdef
********Testing SuffleCipher*************
Enter the shift value. It should be integer
3
****************Testing Decoding***************************
Enter the input to decode. It should be the string of alphabets
john
Decoded Message is jhon

If you left with any doubts. feel free to ask.

Add a comment
Know the answer?
Add Answer to:
Question 1 : Create an interface MessageDecoder that has a single abstract method decode( cipherText), where...
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
  • 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...

  • MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...

    MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an abstract class that will be the base class for other two classes. It should have: A...

  • 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...

  • 1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of...

    in java ..write all complete program from a- e 1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of the left and right subtrees in the above binary trees. Add this method to the class BinaryTree and create a program to test this method for these 2 trees. Show the original trees and the resulting trees. Note: To test your algorithm, first create a binary search tree. Write a method called singleParent,...

  • 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...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   and please put comment with code! Problem:2 1. Class Student Create...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   and please put comment with code! Problem:2 1. Class Student Create a "Hello C++! I love CS52" Program 10 points Create a program that simply outputs the text Hello C++!I love CS52" when you run it. This can be done by using cout object in the main function. 2. Create a Class and an Object In the same file as...

  • Task 1: 1. Write a generic class named MyList, with a type parameter T. The type...

    Task 1: 1. Write a generic class named MyList, with a type parameter T. The type parameter T should be constrained to an upper bound: the Number class. The class should have as a field an ArrayList of type T. Write the class constructor to create the ArrayList. 2. Write a public method named add, which accepts a parameter of type T. When an argument is passed to the method, add it to the ArrayList. 3. Write a public method...

  • PART I: Create an abstract Java class named “Student” in a package named “STUDENTS”. This class...

    PART I: Create an abstract Java class named “Student” in a package named “STUDENTS”. This class has 4 attributes: (1) student ID: protected, an integer of 10 digits (2) student name: protected (3) student group code: protected, an integer (1 for undergraduates, 2 for graduate students) (4) student major: protected (e.g.: Business, Computer Sciences, Engineering) Class Student declaration provides a default constructor, get-methods and set-methods for the attributes, a public abstract method (displayStudentData()). PART II: Create a Java class named...

  • Registry Java Programming 2) Interface Comparator: The method for comparing two objects is written outside of...

    Registry Java Programming 2) Interface Comparator: The method for comparing two objects is written outside of the class of the objects to be sorted. Several methods can be written for comparing the objects according to different criteria. Specifically, write three classes, DescriptionComparator, FirstOccComparator, and LastOccComparator that implement the interface java.util.Comparator. DescriptionComparator implements the interface Comparator. The method compare compares the description of two objects. It returns a negative value if the description of the first object comes before the description...

  • PLEASE DO IN JAVA 1) Create interface named “Person” which exposes getter/setter methods for the person’s...

    PLEASE DO IN JAVA 1) Create interface named “Person” which exposes getter/setter methods for the person’s name and college ID: 1. getName 2. setName 3. getID 4. setID. The college ID is represented as “int”. Define two classes, Student and Faculty, which implement Person. Add field GPA to Student and department to faculty. Make them private and create corresponding getters (getGPA, getDepartment) and setters (setGPA, setDepartment). Use appropriate types. 2) Write a class College, which contains the name of the...

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