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

In Java, design a class Named CryptGram.java with the following requirements: Da

ID: 3850944 • Letter: I

Question

In Java, design a class Named CryptGram.java with the following requirements: Data Members: a Letter(it is provided) array named orderedFrequency to store frequency of the 26 alphabetic letters. Methods: createLetterFrequencyFromFile(File file) getFrequencyByChar(char letter) encode(String textToBeEncoded) decode(String textToBeDecoded) The method: createLetterFrequencyFromFile, reads a file and creates the letter frequency, the letter frequency array MUST by sorted by frequency, for example, the first three Letter elements in the array may look like: d[2], r[5], a[6], which means d appears in the file twice, r five time, and a six times. If the frequencies are ties, then being ordered by alphabetic order. For instance, e[9] and c[9], c[9] should be placed before e[9]. The method: getFrequencyByChar, returns the number of times the char passed as argument appears in the file The method: encode, encodes the String and returns encoded String. The text to be encoded is encrypted as follows: each character in the text will be encrypted to the character in the letter frequency array: orderedFrequency. For example, letter a or A will be encrypted to the letter of the first element in the orderedFrequency array. The letter z or Z will be encrypted to the letter of the last element in the orderedFrequency array. The method: decode, reverse the encode process and decrypt the text (ignore cases)

Explanation / Answer

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author Sam
*/
public class CryptGram {
    private int[] orderedFrequency;
  
    public void createLetterFrequencyFromFile(File file) {
        try {
            Scanner sc = new Scanner(file);
            String line;
            while (sc.hasNextLine()) {
                line = sc.nextLine();
                for (int i = 0; i < line.length(); i++) {
                    char ch = line.charAt(i);
                    if (ch >= 'A' && ch <= 'Z')
                        orderedFrequency[ch - 'A'] ++;
                    else if (ch >='a' && ch <= 'z')
                        orderedFrequency[ch - 'a'] ++;
                }
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(CryptGram.class.getName()).log(Level.SEVERE, null, ex);
        }
      
    }
  
    public int getFrequencyByChar(char letter) {
        if (letter >= 'A' && letter <= 'Z')
                return orderedFrequency[letter - 'A'];
        else if (letter >='a' && letter <= 'z')
                return orderedFrequency[letter - 'a'];
        else
                return -1;
    }
  
  
    public String encode(String textToBeEncoded) {
        String result = "";
      
        for (int i = 0; i < textToBeEncoded.length(); i++) {
            char letter = textToBeEncoded.charAt(i);
            if (letter >= 'A' && letter <= 'Z'){
                letter = (char)(letter + orderedFrequency[letter - 'A']);
                if (letter > 90)
                    letter = (char) (letter - 26);
            }
            else if (letter >= 'a' && letter <= 'z'){
                letter = (char)(letter + orderedFrequency[letter - 'a']);
                if (letter > 122)
                    letter = (char) (letter - 26);
            }
          
            result += letter;
        }
        return result;
    }
  
    public String decode(String textToBeDecoded) {
        int[] frequency = new int[26];
        for (int i = 0; i < 26; i++) {
            int index = i + orderedFrequency[i];
            if (index > 26)
                index -= 26;
            frequency[index] = orderedFrequency[i];
        }
          
        String result = "";
        for (int i = 0; i < textToBeDecoded.length(); i++) {
            char letter = textToBeDecoded.charAt(i);
            if (letter >= 'A' && letter <= 'Z'){
                letter = (char)(letter - frequency[letter - 'A']);
                if (letter < 65)
                    letter = (char) (letter + 26);
            }
            else if (letter >= 'a' && letter <= 'z'){
                letter = (char)(letter - frequency[letter - 'a']);
                if (letter < 97)
                    letter = (char) (letter + 26);
            }
          
            result += letter;
        }
        return result;
    }

    public CryptGram() {
        orderedFrequency = new int[26];
    }
  
    public static void main(String[] args) {
        CryptGram cg = new CryptGram();
        cg.createLetterFrequencyFromFile(new File("Some file"));
        String encode = cg.encode("JavaS");
        System.out.println("encode: " + encode);
        System.out.println("decode: " + cg.decode(encode));
    }
}


here you go champ :)

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