I need help with the last 2 methods only in Huffman.java file the methods are encode() and
decode(). for more information or context visit cs112 website and navigate to the Trees
assignment in the Assignments tab.
Please do this code in java. Write code where it's highlighted
package huffman;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collections;
/**
* This class contains methods which, when used together, perform the
* entire Huffman Coding encoding and decoding process
*
* @author Ishaan Ivaturi
* @author Prince Rawal
*/
public class HuffmanCoding {
private String fileName;
private ArrayList sortedCharFreqList;
private TreeNode huffmanRoot;
private String[] encodings;
/**
* Constructor used by the driver, sets filename
* DO NOT EDIT
* @param f The file we want to encode
*/
public HuffmanCoding(String f) {
fileName = f;
}
/**
* Reads from filename character by character, and sets sortedCharFreqList
* to a new ArrayList of CharFreq objects with frequency > 0, sorted by frequency
*/
public void makeSortedList() {
StdIn.setFile(fileName);
/* Your code goes here */
}
/**
* Uses sortedCharFreqList to build a huffman coding tree, and stores its root
* in huffmanRoot
*/
public void makeTree() {
/* Your code goes here */
}
/**
* Uses huffmanRoot to create a string array of size 128, where each
* index in the array contains that ASCII character's bitstring encoding. Characters not
* present in the huffman coding tree should have their spots in the array left null.
* Set encodings to this array.
*/
public void makeEncodings() {
/* Your code goes here */
}
/**
* Using encodings and filename, this method makes use of the writeBitString method
* to write the final encoding of 1's and 0's to the encoded file.
*
* @param encodedFile The file name into which the text file is to be encoded
*/
public void encode(String encodedFile) {
StdIn.setFile(fileName);
/* Your code goes here */
}
/**
* Writes a given string of 1's and 0's to the given file byte by byte
* and NOT as characters of 1 and 0 which take up 8 bits each
* DO NOT EDIT
*
* @param filename The file to write to (doesn't need to exist yet)
* @param bitString The string of 1's and 0's to write to the file in bits
*/
public static void writeBitString(String filename, String bitString) {
byte[] bytes = new byte[bitString.length() / 8 + 1];
int bytesIndex = 0, byteIndex = 0, currentByte = 0;
// Pad the string with initial zeroes and then a one in order to bring
// its length to a multiple of 8. When reading, the 1 signifies the
// end of padding.
int padding = 8 - (bitString.length() % 8);
String pad = "";
for (int i = 0; i < padding-1; i++) pad = pad + "0";
pad = pad + "1";
bitString = pad + bitString;
// For every bit, add it to the right spot in the corresponding byte,
// and store bytes in the array when finished
for (char c : bitString.toCharArray()) {
if (c != '1' && c != '0') {
System.out.println("Invalid characters in bitstring");
return;
}
if (c == '1') currentByte += 1 << (7-byteIndex);
byteIndex++;
if (byteIndex == 8) {
bytes[bytesIndex] = (byte) currentByte;
bytesIndex++;
currentByte = 0;
byteIndex = 0;
}
}
// Write the array of bytes to the provided file
try {
FileOutputStream out = new FileOutputStream(filename);
out.write(bytes);
out.close();
}
catch(Exception e) {
System.err.println("Error when writing to file!");
}
}
/**
* Using a given encoded file name, this method makes use of the readBitString method
* to convert the file into a bit string, then decodes the bit string using the
* tree, and writes it to a decoded file.
*
* @param encodedFile The file which has already been encoded by encode()
* @param decodedFile The name of the new file we want to decode into
*/
public void decode(String encodedFile, String decodedFile) {
StdOut.setFile(decodedFile);
/* Your code goes here */
}
/**
* Reads a given file byte by byte, and returns a string of 1's and 0's
* representing the bits in the file
* DO NOT EDIT
*
* @param filename The encoded file to read from
* @return String of 1's and 0's representing the bits in the file
*/
public static String readBitString(String filename) {
String bitString = "";
try {
FileInputStream in = new FileInputStream(filename);
File file = new File(filename);
byte bytes[] = new byte[(int) file.length()];
in.read(bytes);
in.close();
// For each byte read, convert it to a binary string of length 8 and add it
// to the bit string
for (byte b : bytes) {
bitString = bitString +
String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0');
}
// Detect the first 1 signifying the end of padding, then remove the first few
// characters, including the 1
for (int i = 0; i < 8; i++) {
if (bitString.charAt(i) == '1') return bitString.substring(i+1);
}
return bitString.substring(8);
}
catch(Exception e) {
System.out.println("Error while reading file!");
return "";
}
}
/*
* Getters used by the driver.
* DO NOT EDIT or REMOVE
*/
public String getFileName() {
return fileName;
}
public ArrayList getSortedCharFreqList() {
return sortedCharFreqList;
}
public TreeNode getHuffmanRoot() {
return huffmanRoot;
}
public String[] getEncodings() {
return encodings;
}
}
input1.txt:
aaaabbbccd
code file:
public class CharFreq implements Comparable {
private Character character;
private double probOcc;
public CharFreq() {
character = 0;
probOcc = 0;
}
public CharFreq(Character c, double p) {
character = c;
probOcc = p;
}
public Character getCharacter () {
return character;
}
public void setCharacter (Character character) {
this.character = character;
}
public double getProbOccurrence () {
return probOcc;
}
public void setProbOccurrence (double probOcc) {
this.probOcc = probOcc;
}
public int compareTo (CharFreq cf) {
Double d1 = probOcc, d2 = cf.probOcc;
if (d1.compareTo(d2) != 0) return d1.compareTo(d2);
return character.compareTo(cf.character);
}
example input file:
- Implement this method to use encodings, an input file containing some text we want to encode,
and an output file we want to encode into. It will write the compressed encoding of the input file
into the output file. - Notice that your provided code begins by setting the file with Stdln. You
can now use methods like Stdln.hasNextChar() and Stdln.readChar() which will operate on the
file as if it was standard input. - You are to create a String of ones and zeros which represents
your encoding of the input text file using your encodings array. The last line of this method must
use the writeBitString method to write this String to the file in bits. DO NOT try to write to the
file manually. - If the file was successfully compressed, your output file will have a significantly
smaller file size than the original text file. Feel free to open your file explorer and check! -
Below is an example of running the driver to help test this method. Enter an input text file name
input1.txt What nethod would you like to test? Later nethods rely on previous methods. 1.
nakeSortedlist 2. nakeTree 3. nakeEncodings 4. encode 5. decode Enter a number 4 File to
encode into (can be new) encoded The input text file has been encoded into encoded Check out
the encoded file in your file explorer. It will be significantly smaller than the original! decode -
Implement this method to take in your encoded file and use huffmanRoot, and print out the
decoding. If done correctly, the decoding will be the same as the contents of the text file used for
encoding. - Notice that your provided code begins by setting the output file with StdOut. You
can now use methods like StdOut.println() and StdOut.print() which will operate on the
decodings file as if it was standard output. - You must start your method using the provided
readBitString method in order to get the string of ones and zeros from the encoded file. DO NOT
try to read the encoded file manually. - You must then use your tree and the procedure outlined
above to decode the bit string into characters, according to the tree's encoding scheme. - Below is
an example of running the driver to help test this method. Enter an input text file name input
1.txt What method would you like to test? Later methods rely on previous methods. 1.
makeSortedlist The decoded output will be an exact copy of 2. makeTree your input file. 3.
makeEncodings 4. encode 5. decode Enter a number 5 File to encode into (can be new) encoded
The input text file has been encoded into encoded File to decode into (can be new) decoded.txt
The file has been decoded into decoded.txt
NOTE: You are allowed (encouraged, even) to make helper methods in your
HuffmanCoding.java file to be used in your graded methods. Just make sure that ihey are created
with the private keyword. Do not add new imports. Methods provided to you: 1. writeBitString -
This method takes in a file name and a string consisting of the characters ' 1 ' and ' 0 ', and writes
the string to the file. - You must use this provided method to write your encoding to your output
file, and must not try to write your string to a file. - Note that it does not actually write the
characters ' 1 ' and '0', and actually writes in bits. - The file name given does not need to exist yet,
and if it doesn't the method will create a new file. If the file exists, the method will overwrite it. -
Do not edit this method. 2. readBitString - This method takes in a file name containing an
encoded message, and returns a string consisting of the characters ' 1 ' and ' 0 '. - You must use
this provided method to recover your encoded string from your input file, and must not try to
read the encoded file yourself. - Note that it reads the file byte by byte and converts the bits back
into characters. - The given file name must exist, and it must have been written to by
writeBitString already. - Do not edit this method.

I need help with the last 2 methods only in Huffman.java file the me.pdf

  • 1.
    I need helpwith the last 2 methods only in Huffman.java file the methods are encode() and decode(). for more information or context visit cs112 website and navigate to the Trees assignment in the Assignments tab. Please do this code in java. Write code where it's highlighted package huffman; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.Collections; /** * This class contains methods which, when used together, perform the * entire Huffman Coding encoding and decoding process * * @author Ishaan Ivaturi * @author Prince Rawal */ public class HuffmanCoding { private String fileName; private ArrayList sortedCharFreqList; private TreeNode huffmanRoot; private String[] encodings; /** * Constructor used by the driver, sets filename * DO NOT EDIT * @param f The file we want to encode */ public HuffmanCoding(String f) { fileName = f; } /** * Reads from filename character by character, and sets sortedCharFreqList * to a new ArrayList of CharFreq objects with frequency > 0, sorted by frequency */ public void makeSortedList() {
  • 2.
    StdIn.setFile(fileName); /* Your codegoes here */ } /** * Uses sortedCharFreqList to build a huffman coding tree, and stores its root * in huffmanRoot */ public void makeTree() { /* Your code goes here */ } /** * Uses huffmanRoot to create a string array of size 128, where each * index in the array contains that ASCII character's bitstring encoding. Characters not * present in the huffman coding tree should have their spots in the array left null. * Set encodings to this array. */ public void makeEncodings() { /* Your code goes here */ } /** * Using encodings and filename, this method makes use of the writeBitString method * to write the final encoding of 1's and 0's to the encoded file. * * @param encodedFile The file name into which the text file is to be encoded */ public void encode(String encodedFile) { StdIn.setFile(fileName); /* Your code goes here */ } /** * Writes a given string of 1's and 0's to the given file byte by byte * and NOT as characters of 1 and 0 which take up 8 bits each * DO NOT EDIT * * @param filename The file to write to (doesn't need to exist yet) * @param bitString The string of 1's and 0's to write to the file in bits
  • 3.
    */ public static voidwriteBitString(String filename, String bitString) { byte[] bytes = new byte[bitString.length() / 8 + 1]; int bytesIndex = 0, byteIndex = 0, currentByte = 0; // Pad the string with initial zeroes and then a one in order to bring // its length to a multiple of 8. When reading, the 1 signifies the // end of padding. int padding = 8 - (bitString.length() % 8); String pad = ""; for (int i = 0; i < padding-1; i++) pad = pad + "0"; pad = pad + "1"; bitString = pad + bitString; // For every bit, add it to the right spot in the corresponding byte, // and store bytes in the array when finished for (char c : bitString.toCharArray()) { if (c != '1' && c != '0') { System.out.println("Invalid characters in bitstring"); return; } if (c == '1') currentByte += 1 << (7-byteIndex); byteIndex++; if (byteIndex == 8) { bytes[bytesIndex] = (byte) currentByte; bytesIndex++; currentByte = 0; byteIndex = 0; } } // Write the array of bytes to the provided file try { FileOutputStream out = new FileOutputStream(filename); out.write(bytes); out.close(); } catch(Exception e) { System.err.println("Error when writing to file!");
  • 4.
    } } /** * Using agiven encoded file name, this method makes use of the readBitString method * to convert the file into a bit string, then decodes the bit string using the * tree, and writes it to a decoded file. * * @param encodedFile The file which has already been encoded by encode() * @param decodedFile The name of the new file we want to decode into */ public void decode(String encodedFile, String decodedFile) { StdOut.setFile(decodedFile); /* Your code goes here */ } /** * Reads a given file byte by byte, and returns a string of 1's and 0's * representing the bits in the file * DO NOT EDIT * * @param filename The encoded file to read from * @return String of 1's and 0's representing the bits in the file */ public static String readBitString(String filename) { String bitString = ""; try { FileInputStream in = new FileInputStream(filename); File file = new File(filename); byte bytes[] = new byte[(int) file.length()]; in.read(bytes); in.close(); // For each byte read, convert it to a binary string of length 8 and add it // to the bit string for (byte b : bytes) { bitString = bitString + String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'); }
  • 5.
    // Detect thefirst 1 signifying the end of padding, then remove the first few // characters, including the 1 for (int i = 0; i < 8; i++) { if (bitString.charAt(i) == '1') return bitString.substring(i+1); } return bitString.substring(8); } catch(Exception e) { System.out.println("Error while reading file!"); return ""; } } /* * Getters used by the driver. * DO NOT EDIT or REMOVE */ public String getFileName() { return fileName; } public ArrayList getSortedCharFreqList() { return sortedCharFreqList; } public TreeNode getHuffmanRoot() { return huffmanRoot; } public String[] getEncodings() { return encodings; } } input1.txt: aaaabbbccd code file: public class CharFreq implements Comparable { private Character character; private double probOcc; public CharFreq() {
  • 6.
    character = 0; probOcc= 0; } public CharFreq(Character c, double p) { character = c; probOcc = p; } public Character getCharacter () { return character; } public void setCharacter (Character character) { this.character = character; } public double getProbOccurrence () { return probOcc; } public void setProbOccurrence (double probOcc) { this.probOcc = probOcc; } public int compareTo (CharFreq cf) { Double d1 = probOcc, d2 = cf.probOcc; if (d1.compareTo(d2) != 0) return d1.compareTo(d2); return character.compareTo(cf.character); } example input file: - Implement this method to use encodings, an input file containing some text we want to encode, and an output file we want to encode into. It will write the compressed encoding of the input file into the output file. - Notice that your provided code begins by setting the file with Stdln. You can now use methods like Stdln.hasNextChar() and Stdln.readChar() which will operate on the file as if it was standard input. - You are to create a String of ones and zeros which represents your encoding of the input text file using your encodings array. The last line of this method must use the writeBitString method to write this String to the file in bits. DO NOT try to write to the file manually. - If the file was successfully compressed, your output file will have a significantly smaller file size than the original text file. Feel free to open your file explorer and check! -
  • 7.
    Below is anexample of running the driver to help test this method. Enter an input text file name input1.txt What nethod would you like to test? Later nethods rely on previous methods. 1. nakeSortedlist 2. nakeTree 3. nakeEncodings 4. encode 5. decode Enter a number 4 File to encode into (can be new) encoded The input text file has been encoded into encoded Check out the encoded file in your file explorer. It will be significantly smaller than the original! decode - Implement this method to take in your encoded file and use huffmanRoot, and print out the decoding. If done correctly, the decoding will be the same as the contents of the text file used for encoding. - Notice that your provided code begins by setting the output file with StdOut. You can now use methods like StdOut.println() and StdOut.print() which will operate on the decodings file as if it was standard output. - You must start your method using the provided readBitString method in order to get the string of ones and zeros from the encoded file. DO NOT try to read the encoded file manually. - You must then use your tree and the procedure outlined above to decode the bit string into characters, according to the tree's encoding scheme. - Below is an example of running the driver to help test this method. Enter an input text file name input 1.txt What method would you like to test? Later methods rely on previous methods. 1. makeSortedlist The decoded output will be an exact copy of 2. makeTree your input file. 3. makeEncodings 4. encode 5. decode Enter a number 5 File to encode into (can be new) encoded The input text file has been encoded into encoded File to decode into (can be new) decoded.txt The file has been decoded into decoded.txt NOTE: You are allowed (encouraged, even) to make helper methods in your HuffmanCoding.java file to be used in your graded methods. Just make sure that ihey are created with the private keyword. Do not add new imports. Methods provided to you: 1. writeBitString - This method takes in a file name and a string consisting of the characters ' 1 ' and ' 0 ', and writes the string to the file. - You must use this provided method to write your encoding to your output file, and must not try to write your string to a file. - Note that it does not actually write the characters ' 1 ' and '0', and actually writes in bits. - The file name given does not need to exist yet, and if it doesn't the method will create a new file. If the file exists, the method will overwrite it. - Do not edit this method. 2. readBitString - This method takes in a file name containing an encoded message, and returns a string consisting of the characters ' 1 ' and ' 0 '. - You must use this provided method to recover your encoded string from your input file, and must not try to read the encoded file yourself. - Note that it reads the file byte by byte and converts the bits back into characters. - The given file name must exist, and it must have been written to by writeBitString already. - Do not edit this method.