Question

In: Computer Science

Hi guys, I'm working on an assignment for my Java II class and the narrative for...

Hi guys, I'm working on an assignment for my Java II class and the narrative for this week's program has me a bit lost. I've put the narrative of the assignment in the quotation marks below.

" Included with this assignment is an encrypted text file. A simple Caesar cipher was used to encrypt the data. You are to create a program which will look at the frequency analysis of the letters (upper and lowercase) to help “crack” the code

A simple Caesar cipher was used to encrypt the data. You are to create a program which will look at the frequency analysis of the letters (upper and lowercase) to help “crack” the code. To help a bit: Using their ASCII codes, a Caesar cipher was used to encrypt uppercase letters, uppercase letters, and digits. This means the shift for uppercase letters forces the encrypted data to still be in the A-Z range. Lowercase letters will still be in the a-z range, and digits will be in the 0-9 range.

Your job is it so create a frequency analysis of the alphabetic characters in the file. To do this: Read a record from the file, look at each character in the record, count the frequency for each letter A-Z and a-z. You can use an array to keep the counts. Display the frequency distribution (percentage for each letter) in a GUI. "

So to put this frankly, I'm a bit lost. I know how to read the file, but I'm having a mini-stroke figuring out how to count all of the characters and their frequency. This is what I've got to read the file.

BufferedReader inFile = null;

PrintWriter outFile = null;

try

{

inFile = new BufferedReader(new FileReader("filename.csv"));

outFile = new PrintWriter(new FileWriter("filelines.txt"));

String readData;

System.out.println ("---OUTPUT FILE BEING PRODUCED---\n");

int[] freqs = new int[26];

while ((readData = inFile.readLine()) != null)

{

outFile.println(readData);

}

}

finally

{

System.out.println ("\n\n---PROCESSING COMPLETED---\n");

if (inFile != null)

{

inFile.close();

}

if (outFile != null)

{

outFile.close();

}

}

Can anyone offer any insight/tips in how to go about this? Thanks for reading!

Solutions

Expert Solution

Here is the solution if you have any doubt then please write in the comment section.

Please give feedback.

Solution: I have written comments for each line for better understanding.

I have provided two solution:

1st which is used in program is : storing frequency of uppercase and lowercase seperately using array of size 123

2nd which is commented after program is : storing frequency of uppercase and lowercase combinely in the array of length 27

import java.io.*;
public class Main
{
    //main method throws IOException because IOException is checked Exception
        public static void main(String[] args) throws IOException
        {
            //BufferedReader and PrintWriter reference variables
                BufferedReader inFile = null;
                PrintWriter outFile = null;
                try
                {
                    //open files in read and write mode respectively
                        inFile = new BufferedReader(new FileReader("filename.csv"));
                        outFile = new PrintWriter(new FileWriter("filelines.txt"));
            
            //String to store the line from file
                        String readData;
            //print msg
                        System.out.println ("---OUTPUT FILE BEING PRODUCED---\n");

            /*declare array of size 123 because ascii of a-b in the range 97-122
            and ascii of A-B is in the range 65-90, and here i am storing 
            frequency seperately for lowercase character and uppercase character
            so if you want to store frequency of both lowercase and uppercase
            together i mean (Case insensitive) then you can use modulo to store
            the frequency in the array of range 26 i will provide the same 
            implementation seperately so you can use anyone which you want*/
            
            /*Here 65 index of array is to store frequncy of Capital A and 66 
            for Capital B and so on */
                        int[] freqs = new int[123];
                        
                        /*count to store total number of character, i am counting only 
                        digits and alphabetic character*/
            int count=0;
            
            //read from file until there is data in the file
                        while ((readData = inFile.readLine()) != null)

                        {
                            //loop to iterate through all character in a single line of file 
                            for(int i=0;i<readData.length();i++)
                            {
                                /*if character is alphabetic [we are checking using ascii ]
                                then add frequency in the array*/
                                if((readData.charAt(i)>=65&&readData.charAt(i)<=90)||
                                (readData.charAt(i)>=97&&readData.charAt(i)<=122))
                                {
                                    //increment count
                                    count++;
                                    //increment frequency of particular alphabate  
                                    freqs[readData.charAt(i)]+=1;
                                }
                                //if character is digit then increment only count
                                else if(readData.charAt(i)>=48&&readData.charAt(i)<=57)
                                {
                                    count++;
                                }
                                
                            }

                                

                        }
                        //printing frequency of characters in file for your understanding
                        for(int i=65;i<=90;i++)
                        {
                            char ch=(char)i;
                            outFile.println(ch+" "+freqs[i]);
                        }
                        for(int i=97;i<=122;i++)
                        {
                            char ch=(char)i;
                            outFile.println(ch+" "+freqs[i]);
                        }
                        //printing total count
                        outFile.println("Total Count: "+count);

                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }

                finally

                {

                        System.out.println ("\n\n---PROCESSING COMPLETED---\n");

                        if (inFile != null)

            {

                inFile.close();

            }

            if (outFile != null)

            {

            outFile.close();

            }
                        

                }
                System.out.println("Hello World");
        }
}


/*  This procedure is for if you want that frequency of a and A will be treated
    as same
int[] freqs = new int[27];
                        
                        /*count to store total number of character, i am counting only 
                        digits and alphabetic character*/
                        
                        /*
            int count=0;
            
            //read from file until there is data in the file
                        while ((readData = inFile.readLine()) != null)

                        {
                            //loop to iterate through all character in a single line of file 
                            for(int i=0;i<readData.length();i++)
                            {
                                /*if character is alphabetic [we are checking using ascii ]
                                then add frequency in the array*/
                                
                          /*      if((readData.charAt(i)>=65&&readData.charAt(i)<=90))
                                {
                                    //increment count
                                    count++;
                                    //here modulo 65 used so that A will have frequency at
                                    //index 1 and B will have frequency at index 2 and so on  
                                    freqs[readData.charAt(i)%65+1]+=1;
                                }
                                else if(readData.charAt(i)>=97&&readData.charAt(i)<=122)
                                {
                                    //increment count
                                    count++;
                                    //here modulo 97 used so that a will have frequency at
                                    //index 1 and b will have frequency at index 2 and so on
                                    freqs[readData.charAt(i)%97+1]+=1;
                                }
                                //if character is digit then increment only count
                                else if(readData.charAt(i)>=48&&readData.charAt(i)<=57)
                                {
                                    count++;
                                }
                                
                            }

                        

                        }
*/

Input and Output:

filename.csv

abx,Abc,ABC
cde,RTY,QWEQWEQ,XCVXCV
tytyu,WERWER,fghfgh

output file: filelines.txt

A 2
B 1
C 3
D 0
E 4
F 0
G 0
H 0
I 0
J 0
K 0
L 0
M 0
N 0
O 0
P 0
Q 3
R 3
S 0
T 1
U 0
V 2
W 4
X 2
Y 1
Z 0
a 1
b 2
c 2
d 1
e 1
f 2
g 2
h 2
i 0
j 0
k 0
l 0
m 0
n 0
o 0
p 0
q 0
r 0
s 0
t 2
u 1
v 0
w 0
x 1
y 2
z 0
Total Count: 45

If you have any type of doubts then please write in the comment section, I will feel happy to help you.

Please give feedback.

Thank You!


Related Solutions

Hi, I'm doing an assignment for my med-surg class. Here are the questions listed below. Describe...
Hi, I'm doing an assignment for my med-surg class. Here are the questions listed below. Describe in your own words what medical surgical nursing is. List 4 typical medical diagnosis you could see on a medical surgical unit. Give brief description of each diagnosis. List 3 nursing interventions for each diagnosis. One nursing outcome for each diagnosis you would want to see.
Hi, Working on a project in my group for class and I am having some issues...
Hi, Working on a project in my group for class and I am having some issues My part is current state of the business. It is a store and the annual sales are $460,000 Other info I have is: Ownership and Compensation; Percent Ownership Personal Investment Mitchell George, Founder & CEO 25% $125,000Katie Beauseigneur, COO 15% $75,000 Melissa Dunnells, CFO15% $75,000 Also, a medium coffee price from store is $3.75 Sarah Griffin, Marketing Officer 10% $50,000 Katharina Ferry, HR Director10%...
Hello i am working on an assignment for my programming course in JAVA. The following is...
Hello i am working on an assignment for my programming course in JAVA. The following is the assignment: In main, first ask the user for their name, and read the name into a String variable. Then, using their name, ask for a temperature in farenheit, and read that value in. Calculate and print the equivalent celsius, with output something like Bob, your 32 degrees farenheit would be 0 degrees celsius Look up the celsius to farenheit conversion if you do...
I am struggling with this assignment for my java class. Objectives: Your program will be graded...
I am struggling with this assignment for my java class. Objectives: Your program will be graded according to the rubric below. Please review each objective before submitting your work so you don’t lose points. 1.Create a new project and class in Eclipse and add the main method. (5 points) 2. Construct a Scanner to read input from the keyboard. (5 points) 3. Prompt the user with three questions from the Psychology Today quiz, and use the Scanner to read their...
This is an auditing question not a accounting question sorry about that. Hi! I'm working on...
This is an auditing question not a accounting question sorry about that. Hi! I'm working on the following auditing problem and would like some help: Write an executive summary of what is contained in the article. The format should be as follows and should not be more than one to two pages in length: • Summary of information contained within the article. • Explanation as to why the article you chose has relevancy to the accounting environment. • Explanation as...
I have the following code for my java class assignment but i am having an issue...
I have the following code for my java class assignment but i am having an issue with this error i keep getting. On the following lines: return new Circle(color, radius); return new Rectangle(color, length, width); I am getting the following error for each line: "non-static variable this cannot be referenced from a static context" Here is the code I have: /* * ShapeDemo - simple inheritance hierarchy and dynamic binding. * * The Shape class must be compiled before the...
I'm working in Java and am working on a project where I need to find an...
I'm working in Java and am working on a project where I need to find an average. The catch is that for some of the values there is no data because they will be entered at a later date. I have variables assigned so that for each entry if there is an input I'll have it say _____available = 1, otherwise the variable will equal 0. I'll use an example to make this more clear. Let's say I am trying...
Hi Guys, The assignment in Process Scheduling Operating System to write C code that implement FCFS...
Hi Guys, The assignment in Process Scheduling Operating System to write C code that implement FCFS & Round-Robin ( Without using any array) but we can use linkedlist
Hi, i'm working on the lab report for Iodometric determination of Cu in Brass and i...
Hi, i'm working on the lab report for Iodometric determination of Cu in Brass and i need help with these questions. I greatly appreciate your help. Info: This experiment has two parts: standardization of the thiosulfate solution, and assay of the copper. In the standardization of the thiosulfate solution, a known amount of iodine is created from KIO3 and excess iodide, then titrated with the thiosulfate. In the copper assay, iodine is formed from the reaction between copper and excess...
Java Program to Fully Parenthesize an expression. Hi guys. I have a small task which I...
Java Program to Fully Parenthesize an expression. Hi guys. I have a small task which I cannot find the solution for. It involves expressions with Infix I'll need a Java Program to convert an expression, eg. 3+6*(x-y)+x^2 to ((3 + (6 * (x-y))) + (x ^ 2)) Kindly assist. Thanks
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT