Question

IN JAVA! The field of cryptography is a very rich and fascinating branch that combines math...

IN JAVA!

The field of cryptography is a very rich and fascinating branch that combines math and computer science. I encourage you to explore it more. For the time being though, in this lab assignment, we will focus on identifying most occurring letters in two different languages.

[You can then take your work a few steps further by applying it to deciphering by frequency analysis and see what happens!] To practice your skills in frequency analysis, you will be given two full books (txt version) to read (well, to have the computer parse J): one in French and one in English. They are the same book in two different languages: The Miserables, by Victor Hugo (some of you may have read it, or seen the recent movie about it). Your job will be to: • Parse each book; • As you parse the book, collect information about the number of times each letter of the alphabet occurs in the book (by using an array of 26 integers); and then • “Sort” this array (and maybe not just this array) to identify the 5 most frequent letters in English and the 5 most frequent letters in French.

Your goal is to write a few methods as follows: • FrequencyInBook: o Takes a txt file as input (TheMiserables-French.txt and then TheMiserablesEnglish.txt) as well as an alphabet o Returns an array of integers that contains the amount of times each letter occurred in the book (in our alphabet, it will be 26 integers, one per letter) • FiveMostFrequentLetters: o Takes as input the array that indicate the frequencies of all letters in the text o Takes an alphabet in the format of your choice o Returns the 5 most occurring letters • Main method: Below, you will find the pseudocode of your main method. You will have to write it in java in such a way that it uses your above 2 methods.

Use FrequencyInBook: Read TheMiserables-French.txt à array of frequencies FF Use FiveMostFrequentLetters: Identify and print out the 5 most frequent letters in the language under study as follows: “The French language has the following 5 most frequent letters: … ”

Followed by: Use FrequencyInBook: Read TheMiserables-English.txt à array of frequencies FE Use FiveMostFrequentLetters: Identify and print out the 5 most frequent letters in the language under study as follows: “The English language has the following 5 most frequent letters: …

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


// Java program to calculate the frequency of letters in a book and determine the top 5 frequent letters
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class BookFrequency {
  
   // method that counts the frequency of each letter in array alphabets in the input file filename
   public static int[] FrequencyInBook(String filename, String[] alphabets) throws FileNotFoundException
   {  
       int frequency[] = new int[alphabets.length]; // create an integer array of same length as the alphabets
       String line;
       // initialize the data for each frequency as 0
       for(int i=0;i<frequency.length;i++)
           frequency[i] = 0;
      
       File file = new File(filename); // open the file
       if(file.exists()) // if file exists
       {
           Scanner fileScan = new Scanner(file); // create a scanner object to read the file
          
           // loop to read till the end of file
           while(fileScan.hasNext())
           {
               line = fileScan.next(); // read a string from file
               // loop over the string and increment the frequency of the letters in the string
               for(int i=0;i<line.length();i++)
               {
                   // loop to find the index of the letter considered
                   for(int j=0;j<alphabets.length;j++)
                   {
                       if(line.substring(i,i+1).equalsIgnoreCase(alphabets[j]))
                       {
                           frequency[j]++;
                           break;
                       }
                      
                   }
               }
           }
          
           fileScan.close(); // close the file
       }
      
       return frequency; // return the frequency array
   }
  
   // method to find the top 5 most frequent letters in the letters array by the frequency given in frequency array
   public static String[] FiveMostFrequentLetters(int[] frequency, String[] letters)
   {
       int max;
       // sort the arrays by frequency in descending order
       for(int i=0;i<letters.length-1;i++)
       {
           max = i;
           for(int j=i+1;j<letters.length;j++)
           {
               if(frequency[j] > frequency[max])
                   max = j;
           }
          
           if(max != i)
           {
               int tempFreq = frequency[i];
               frequency[i] = frequency[max];
               frequency[max] = tempFreq;
              
               String tempLetter = letters[i];
               letters[i] = letters[max];
               letters[max] = tempLetter;
              
           }
       }
      
       // create an array of string to store the top 5 letters by frequency
       String top5[] = new String[5];
      
       for(int i=0;i<letters.length;i++)
           top5[i] = letters[i];
      
       return top5; // return the top5 array
   }

   public static void main(String[] args) throws FileNotFoundException {

       // create an array of english alphabets
       String alphabets[] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
       // get the frequency of letters in french book
       int frenchFreq[] = FrequencyInBook("TheMiserables-French.txt",alphabets);
       // get the frequency of letters in english book
       int englishFreq[] = FrequencyInBook("TheMiserablesEnglish.txt",alphabets);
      
       // get the top 5 most frequenct letters in english and french book
       String top5English[] = FiveMostFrequentLetters(englishFreq,alphabets);
       String top5French[] = FiveMostFrequentLetters(frenchFreq,alphabets);
      
       // display the top 5 frequent letters
       System.out.print("The French language has the following 5 most frequent letters: ");
       for(int i=0;i<top5French.length;i++)
           System.out.print(top5French[i]+" ");
      
       System.out.print("The English language has the following 5 most frequent letters: ");
       for(int i=0;i<top5English.length;i++)
           System.out.print(top5English[i]+" ");
   }

}
//end of program

Add a comment
Know the answer?
Add Answer to:
IN JAVA! The field of cryptography is a very rich and fascinating branch that combines math...
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
  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • 3. Consider an editor that compares a typed in word say of length n and changes...

    3. Consider an editor that compares a typed in word say of length n and changes it to the nearest word in the dictionary, which can be of size m. For example, if you mistyped ”floyre”, it can change the word to ”flower” or ”flow” or ”floe” or ”floor”. To judge the best word, the spell checker computes the minimum number of changes between the typed word and the words in its dictionary. The changes can be as follows; (i)...

  • Background: The first step towards helping someone 'decode' a message is often to count how many...

    Background: The first step towards helping someone 'decode' a message is often to count how many times each letter of the alphabet appears in the message. Then divide each count by the total number of letters to compute the relative 'frequency'.   From these frequencies, it may be easier to recognize which letters are assigned to the vowels. For your own reference, here is a table of standard letter frequencies from typical English text. (You do NOT need to display this...

  • Need help in C (a) Write a C program to read in a line of text...

    Need help in C (a) Write a C program to read in a line of text and count the occurrence of each English alphabet. The lowercase version of a letter is considered the same as the uppercase. To make viewing easy, the frequencies should be presented using a bar chart as follows. You can assume that the input contains only spaces, lowercase letters, uppercase letters and the newline character (i.e. the Enter key). Enter a line of text: Letter ZZz...

  • Question 2: Finding the best Scrabble word with Recursion using java Scrabble is a game in...

    Question 2: Finding the best Scrabble word with Recursion using java Scrabble is a game in which players construct words from random letters, building on words already played. Each letter has an associated point value and the aim is to collect more points than your opponent. Please see https: //en.wikipedia.org/wiki/Scrabble for an overview if you are unfamiliar with the game. You will write a program that allows a user to enter 7 letters (representing the letter tiles they hold), plus...

  • Program Description: A Java program is to be created to produce Morse code. The Morse code...

    Program Description: A Java program is to be created to produce Morse code. The Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few special characters (such as period, comma, colon, and semicolon). In sound-oriented systems, the dot represents a short sound and the dash represents a long sound. Separation between words is indicated by a space, or, quite simply, the absence of a dot or dash. In a sound-oriented...

  • 6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you...

    6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you will practice working with arrays. Your program will read lines of text from the keyboard and use an array to track the number of times each letter occurs in the input text. You will use the contents of this array to generate a histogram (bar graph) indicating the relative frequencies of each letter entered. Test cases are available in this document. Remember, in addition...

  • JAVA PROJECT Part 1 - Normalize Text The first thing we will do is normalize the...

    JAVA PROJECT Part 1 - Normalize Text The first thing we will do is normalize the input message so that it’s easier to work with. Write a method called normalizeText which does the following: Removes all the spaces from your text Remove any punctuation (. , : ; ’ ” ! ? ( ) ) Turn all lower-case letters into upper-case letters Return the result. The call normalizeText(“This is some \“really\” great. (Text)!?”) should return “THISISSOMEREALLYGREATTEXT” Part 2 - Obfuscation...

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

  • **DO IT AS PYTHON PLEASE** The Trifid Cipher General Problem Description The Trifid cipher (not to be confused with the...

    **DO IT AS PYTHON PLEASE** The Trifid Cipher General Problem Description The Trifid cipher (not to be confused with the creatures from the classic science-fiction film "The Day of the Triffids") is an algorithm that enciphers a plaintext message by encoding each letter as a three-digit number and then breaking up and rearranging the digits from each letter's encoded form. For this assignment, you will create a set of Python functions that can encode messages using this cipher (these functions...

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