Question: This assignment is designed to help you practice working with 1 D arrays, reading from a file and writing to a file using StdIn and
This assignment is designed to help you practice working with D arrays, reading from a file and writing to a file using StdIn and StdOut libraries.
You will use an array to keep track of number of times each character appears in a file.
Overview
The ASCII American Standard Code for Information Interchange has a decimal valuerepresentation for encoding different letters, punctuation and other characters. In the age where millions of bytes of data are stored and sent across networks with a single click, its important to optimize the memory and time of the computers that carry out these tasks.
Huffman Coding is a method of data compression that counts the occurrences of each character and maps them to a certain bit string based on their frequency for optimized space.
You do not need to understand Huffman Coding for this assignment. Here are two resources if you are interested in learning more about it
Video about Huffman Coding Method
More reading about Huffman
In this assignment, you will be implementing your own program that will keep track of the number of occurrences of these characters. This is equivalent to the first part of the Huffman Coding algorithm.
Programming
You will use a D integer array to keep track of the number of times each of the characters appears in the input file.
Then, once the entire file has been read and each character has been accounted for, you will print only from index to inclusive to the output file in the following format:
characterascii valueoccurrencesfrequency
NO SPACE before or after the comma.
character: is the character itself eg A rascii value: is the decimal value of the character egoccurrences: number of times the character appears in the file.frequency: is a percentage computed by occurrencestotal number of characters in the file
How to read one character at a time from the input file?
The first argument, args will be the input file.
Use StdIn.setFileargs to set the input file.
Read characters while there are characters to be read from the file. StdIn.hasNextChar returns true if there are characters to be read.
To read one character use StdIn.readChar
How to keep track of the number of times a character appears in the input file?
Use a D integer array size to keep track of the number of times each character appears in the input file.
If you look at the ASCII table you will notice that:
The first characters through cannot be printed and ARE NOTof interest to this assignment.
Character through inclusive ARE the characters of interest to this assignment.
Notice that each character has a decimal value. The decimal value represents the character is the value stored in the computer.
The character A has decimal value When you cast a char as an int, the value is the characters decimal value.The println below will print when the two lines of code are executed. Try it
char c A;System.out.printlnint c;
Once a character is read from the file, use the character as the array index cast the character as an integer
If you read an A index is expected to be incremented.
If for example, you read then index is expected to be incremented.
This way you will be able to keep track of the number of times each character appears in the file.
How to write to the output file?
The second argument, args will be the output file.
Use StdOut.setFileargs to set the output file.
Then use StdOut.println to write to the file.
Once the entire file has been read. Print to the output fileonly from index to inclusive in the following format:
characterascii valueoccurrencesfrequency
NO SPACE before or after the comma.
Notes:
Only modify the main method, no helper methods necessary.Keep track of the number of times the character appears for all characters in the file, but only print out char values of to inclusive to the output file.
Executing and Debugging
First navigate toCharacterCounterdirectoryfolder
to compile:javac CharacterCounter.javato execute:java CharacterCounter HuffmanCoding.txt output.out
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
