Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Program single Task : I need help with a single task (class) in this progra

ID: 3730970 • Letter: J

Question

Java Program single Task:

I need help with a single task (class) in this program (class VigenereCipher only) that will be extended from class SymmetricCipher (provided). This program is about Encryption (Hierarchy). All required classes to be used to extend will be provided below (links). Tester cases for this class is also at the bottom. Please read and help me. Thank you so much.

- Here is an overview of this program.

-----------------Java codes----------------------

Here is the link to the java codes for class Alphabet, AlphabetException, BadIndexAlphabetExceptionandMissingCharAlphabetException that I have finished (Will be used to inheret for this CaesarCipher class):https://paste.ee/p/19qsM

Here is the link to the java codes for class SymmetricCipher (will be used to extend):https://paste.ee/p/PIkkA

Note:

Encryption/Decryption

This is a bit trickier here, as Vigenere ciphers are stateful during encryption: they need to track which password integer to use for each position of the message. It is tempting to override encrypt()/decrypt() and write your own loops for this. Resist this urge and take the following approach:

Use an internal field associated with each instance of the class to track the position in the password array

Override encrypt() to set this field to 0, then call the parent method's version of encrypt()

In encrypt1() use the field to determine how much shifting is to be done. Then update the internal password position by incrementing it

Take a similar approach for decryption: override decrypt() to set the password position to 0 and use that position during decrypt1().

class VigenereCipher--------------------

Fields

protected String password The password, used to generate shift values.

protected int passwordPos; Records the current location in a String that is being crypted. This is the 'state' that is used in successive encrypt1calls by encrypt (similarly for decrypting).

Methods

public VigenereCipher(String password,Alphabet alphabet) The constructor initializes the data members. This includes the data member in the parent class SymmetricCipher.

public VigenereCipher(String password). The constructor initializes the data members. This includes the data member in the parent class SymmetricCipher, which is initialized to the DEFAULT Alphabet.

public String getPassword(). Returns password.

protected char encrypt1(char c). Relies upon password and passwordPos to encrypt a single char. Must increment passwordPos. Will throw MissingCharAlphabetException if any character is found that isn't in the alphabet.

Manual Inspection Criteria (5%): implement encrypt1/decrypt1 methods but rely on the inherited encrypt/decrypt methods fromSymmetricCipher.

protected char decrypt1(char c). Relies upon password and passwordPos to decrypt a single char. Must increment passwordPos. Will throw MissingCharAlphabetException if any character is found that isn't in the alphabet.

Manual Inspection Criteria (same as above): implement encrypt1/decrypt1 methods but rely on the inherited encrypt/decrypt methods from SymmetricCipher.

public String encrypt(String s). We can't wholly use the inherited version, because we need to know at what position in the string we're encrypting. Initializes passwordPos to 0, and then invokes the parent class's version of encrypt (which in turn uses this class's encrypt1 definition). When characters not in the alphabet are encountered, a MissingCharAlphabetException is thrown.

public String decrypt(String s). We can't wholly use the inherited version, because we need to know at what position in the string we're decrypting. Initializes passwordPos to 0, and then invokes the parent class's version of decrypt (which in turn uses this class's decrypt1 definition). When characters not in the alphabet are encountered, a MissingCharAlphabetException is thrown.

public String toString(). Return a string representation of the cipher in the following format

where the value associated with password is taken from the internal field of the class.

--------------------------Here is the link to the tester cases--------------------

Tester cases: https://paste.ee/p/aLj3o

---------------------------Additional Info--------------------------------

Summary of the whole Program (all tasks--clases): https://paste.ee/p/oyr5d

Introduction of this program (encryption): https://paste.ee/p/6iJER

Explanation / Answer


SymmetricCipher.java

public abstract class SymmetricCipher extends Cipher {
  
   // Fields
   protected Alphabet alphabet;
  
   // Methods
  
   // Standard constructor
   public SymmetricCipher (Alphabet alphabet) {
       this.alphabet = alphabet;
   }
  
   public int wrapInt (int i) {
       if (i >= alphabet.length())
           while (i >= alphabet.length())
               i -= alphabet.length();
       else if (i < 0)
           i = alphabet.length() + i;
       return i;
   }
  
   public int rotate (int index, int shift) {
       int test = index + wrapInt(shift);
       if (test > alphabet.length())
           return test - alphabet.length();
       else
           return test;
      
   }
  
   public Alphabet getAlphabet () {
       return this.alphabet;
   }
  
   @Override public String encrypt(String s) {
       char[] charArray = s.toCharArray();
       for (int i = 0; i < charArray.length; i++) {
           charArray[i] = encrypt1(charArray[i]);
       }
       return new String(charArray);
   }
  
   @Override public String decrypt(String s) {
       char[] charArray = s.toCharArray();
       for (int i = 0; i < charArray.length; i++) {
           charArray[i] = decrypt1(charArray[i]);
       }
       return new String(charArray);
   }
  
   protected char encrypt1 (char c){
       return 'a';
   }
  
   protected char decrypt1 (char c){
       return 'a';
   }
  
}
VigenereCipher.java

public class VigenereCipher extends SymmetricCipher {
  
   // Fields
   protected String password;
   protected int passwordPos= 0;
   char[] charArray;
  
   // Methods
   public VigenereCipher(String password,Alphabet alphabet) {
       super(alphabet);
       this.password = password;
       charArray = password.toCharArray();
   }
  
   public VigenereCipher(String password) {
       super(Alphabet.DEFAULT);
       this.password = password;
       charArray = password.toCharArray();
   }
  
   public String getPassword() {
       return this.password;
   }
  
   @Override protected char encrypt1(char c) {
       int index = this.alphabet.indexOf(c);
       int shift = this.alphabet.indexOf(charArray[passwordPos]);
       passwordPos++;
       char ret = this.alphabet.get(rotate(index, shift));
       if (passwordPos <= password.length())
           passwordPos++;
       else
           passwordPos = 0;
       return ret;
   }
  
   @Override protected char decrypt1(char c) {
       int index = this.alphabet.indexOf(c);
       int shift = this.alphabet.indexOf(charArray[passwordPos]);
       passwordPos++;
       char ret = this.alphabet.get(rotate(index, -shift));
       if (passwordPos <= password.length())
           passwordPos++;
       else
           passwordPos = 0;
       return ret;
   }
  
   @Override public String encrypt(String s) {
       return super.encrypt(s);
   }
  
   @Override public String decrypt(String s) {
       return super.decrypt(s);
   }
  
   @Override public String toString() {
       return String.format("Vigenere Cipher (password='%s')", password);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote