Question

In: Computer Science

Create an interface MessageEncoder that has a single abstract method encode(plainText) where the plainText is the...

Create an interface MessageEncoder that has a single abstract method encode(plainText) where the plainText is the message to be encoded. The method will return the encoded version of the message. Then create a class SubstitutionCipher that implements this interface. The constructor should have on parameter called shift. Define 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. Create a private method that shifts one character to help with this. Write a demo program that will take a message from a user , a shift value, and then return the encoded message.

use java

Solutions

Expert Solution

1. Interface

public interface MessageEncoder {
        public StringBuilder encode(String plainText);
}

2.  SubstitutionCipher Class

public class SubstitutionCipher implements MessageEncoder {
        int shift;
        public SubstitutionCipher(int shift) {
                this.shift = shift;
        }
        
        @Override //overridding the encode interface method
        public StringBuilder encode(String plainText) {
  StringBuilder  cipherText=new StringBuilder();
         for (int i=0; i<plainText.length(); i++) {
        cipherText.append(shifter(plainText.charAt(i)));
     }
            
      return cipherText;
        }
        
        private char shifter(char letter) {
                char cipherAlpha;
       if (Character.isUpperCase(letter))                        // If  plaintext charcter is Uppercase
       
           cipherAlpha = (char)((((int)letter + shift - 65) % 26) + 65); // To restrictcharcters to english alphabets only 
       
                
       else // If  plaintext character is Lowercase

              cipherAlpha = (char)((((int)letter + shift - 97) % 26) + 97);   // To restrict characters to english alphabets only
                                  
         
          return cipherAlpha;
              }

                                  
                                  
                                  

}

3. Demo Class

import java.util.*;
public class EncoderDemo {
        public static void main(String[] args) {
   int shift;
   Scanner s = new Scanner(System.in);
   System.out.println("Enter the shift Value");
   shift= s.nextInt();
   System.out.println("Enter the Plaintext you want to Encode");
   String plainText;
   plainText  = s.next();
   SubstitutionCipher aCipher = new SubstitutionCipher(shift);
   StringBuilder CipherText= aCipher.encode(plainText);
   System.out.println("Encoded plaintex is: "+CipherText);
        }
   }

Screenshot


Related Solutions

Create an abstract class DiscountPolicy. It should have a single abstract method computeDiscount that will return...
Create an abstract class DiscountPolicy. It should have a single abstract method computeDiscount that will return the discount for the purchase of a given number of a single item. The method has two parameters, count and itemCost. Derive a class BulkDiscount from DiscountPolicy. It should have a constructor that has two parameters minimum and percent. It should define the method computeDiscount so that if the quantity purchased of an item is more then minimum, the discount is percent percent.
In Java Create an interface Create an interface Printing, which have a method getPageNumber () that...
In Java Create an interface Create an interface Printing, which have a method getPageNumber () that return page number of any printing. Create an abstract class named Novel that implements Printing. Include a String field for the novel's title and a double field for the novel price. Within the class, include a constructor that requires the novel title, and add two get methods--one that returns the title and one that returns the price. Include an abstract method named setPrice().. Create...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then create 4 classes: 1- Leaf: that implements turn(), which changes the color of the Leaf object and returns true. If for any reason it is unable to change color, it should return false (you can come up with a reason for failure). The new color can be determined at random. 2- Document: that implements turn(), which changes the page on the document to the...
Create an abstract class polygon that has a method getnoofsides() and getarea() and it has three...
Create an abstract class polygon that has a method getnoofsides() and getarea() and it has three subclasses triangle, rectangle and square each with its own two methods getnoofsides and getarea and their respective implementations accordingly. python
JAVA Given the company and Employee interface, create a findLeastPaid() method (that will do the opposite...
JAVA Given the company and Employee interface, create a findLeastPaid() method (that will do the opposite of whats below). instead of finding the the best paid convert it find the lowest paid person. Given the Company and FullTimeEmployee classes and the Employee interface, write a Java program that reads in a file of full time employees with each line of input containing the name of the employee (String) and gross Salary (double) and stores them in an array list. Then...
JAVA 1. Create an Interface Vehicle. Declare one public void method in it paint( ). 2....
JAVA 1. Create an Interface Vehicle. Declare one public void method in it paint( ). 2. Create a class Car. Implements Vehicle. It's paint() method prints "Car Painted". It's drive( ) method prints "Car Driven". 3. Create a class Bus. Implements Vehicle. It's paint() method prints "Bus Painted" . It's drive( ) method prints "Bus Driven".   4. Create a method AutoWork which accepts an object of type Vehicle. The method makes a call to the vehicle's paint( ) and drive()...
Find a method of ArrayList that is not in the List interface, specifically a method that...
Find a method of ArrayList that is not in the List interface, specifically a method that trims the internal array down to fit exactly. A Google search for this did work, but the JDK API of course is the definitive source. Give the method header for the method. Add a call to this method to TestArrayList.java (which is available online in TestArrayList.zip), and see that it compiles and runs fine. Now change the line creating the ArrayList to use type...
4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
2) Explain the “Abstract and Opinion” method?
2) Explain the “Abstract and Opinion” method?3) Explain what is meant by the term assumption of a mortgage?4) Under what circumstances would a buyer use seller financing?
Create a web interface that has a backend database that takes a value of one table...
Create a web interface that has a backend database that takes a value of one table and displays it match from another table. Create a web interface, that has a backend database, the database contains two tables, one table contains courses in one curriculum, while the other table in the database has corresponding classes from another curriculum. A user should be able to pick a course from the curriculum, and the matching course from the other table should populate on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT