Can someone help me on this Java program, Thanks in advance This lab has you cre
ID: 3722743 • Letter: C
Question
Can someone help me on this Java program, Thanks in advance
This lab has you create a MyHashMap object for storing the frequency counts for each individual character found in a String. The MyHashMap key is a FileChar object holding the character and the MyHashMap value is a HuffElement object holding the character, the character frequency, and the character code. These two classes will be carried over to Project 2. The book, the slides and given Lesson 7 Source Code provide most of the code needed for this program, including the MyMap interface and the MyHashMap class. You must create two additional classes, one to represent the map key (FileChar) and the other to represent the map value (HuffElement). In addition, you must provide half of the code for the TestMyHashMap driver program. Most of the code for this driver class will also be used in both the VigHuff driver class (given to you for Project 2) and the Huffman class (which you must supply for Project 2). The tables below show you the API for the FileChar and HuffElement classes. You must supply the method implementations. The TestMyHashMap class has the main method and a second method that reads in a file that is provided: FDREconomics.txt. The TestMyHashMap class main method should follow the following algorithm: • Create a MyHashMap object that has FileChar keys and HuffElement values. • Read the FDREconomics.txt file into a String object using the provided readFile method. • Walk through the String object, character-by-character, to record the character frequency in the String object: For each new different character encountered you must instantiate a new FileChar object and add the character to it. Then you must instantiate a HuffElement object and add the character to it and increment the character count in it. This map entry pair is then added to the HashMap object you created. For each character previously encountered you must use the FileChar key object for it to retrieve the HuffElement value object and increment the character count in it. • Once you have completely read the whole file, you must retrieve the key set from the hash map and iterate through the key set using a for each loop and for each key, retrieve the character from the FileChar key and the count from the HuffElement value and print these, as follows: Sample Output: Char: D Count: 1 Char: E Count: 1 Char: F Count: 2 Char: A Count: 4 … Char: d Count: 49 Char: Count: 271 Char: e Count: 149 Char: b Count: 12 Char: c Count: 42 Char: ' Count: 2 … Table 1: FileChar API Data & Methods Description char ch Character from the file string. FileChar () Constructor: initializes ch to the blank character. FileChar (char c) Constructor: initializes ch to the incoming char c. char getChar() Returns ch. void setChar (char c) Sets ch to the incoming char c. int hashCode() Returns an int version of ch. int compareTo (FileChar ch) Returns -1, 0, or 1: See implementing Comparable Interface boolean equals (Object chr) Compares chr.ch to ch: See overriding equals in book or Google String toString() Return string version of ch Table 2: HuffElement API Data & Methods Description char ch Character from the file string. int chCount Character frequency in file string String code Binary character code for compression HuffElement () Constructor: initializes ch to the blank character chCount to 0 code to the empty string HuffElement (char c) Constructor: initializes ch to the incoming char c. chCount to 0 code to the empty string char getChar() Returns ch. void setChar (char c) Sets ch to the incoming char c. int getCount() Returns chCount. void setCount (int c) Sets chCount to the incoming count c. void incCount() Increments chCount String getCode() Returns code. void setCode (String c) Sets code to the incoming String c. int compareTo (HuffElement he) Returns -1, 0, or 1: Compare the counts. See implementing Comparable Interface String toString() Return string version of the three data members as: Char: y Code: Count: 15
Explanation / Answer
Please find the program source code below
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
myHashMap.java
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
public class MyHashMap {
public FileChar[] key;
public HuffElement[] vlaue;
}
public class FileChar{
public char ch;
public FileChar()
{
ch = '';
}
public FileChar(char c)
{
ch = c;
}
public char getChar(){
return ch;
}
public void setChar (char c){
ch = c;
}
public int hashCode(){
return ch.hashCode();
}
public int compareTo (FileChar Char) {
return ch.equals(Char.ch);
}
//Returns -1, 0
}
public class HuffElement{
//Description char ch Character from the file string
public char ch;
//int chCount Character frequency in file string
public int chCount = 0;
//String code Binary character code for compression
public String code;
//HuffElement () Constructor: initializes ch to the blank character chCount to 0 code to the empty string
public HuffElement(){
ch = '';
chCount = 0;
code = "";
}
//HuffElement (char c) Constructor: initializes ch to the incoming char c. chCount to 0 code to the empty string
public HuffElement(char c){
ch = c;
chCount = 0;
code = "";
}
//char getChar() Returns ch. void
public char getChar(){
return ch;
}
//setChar (char c) Sets ch to the incoming char c
public void setChar (char c){
ch = c;
}
//int getCount() Returns chCount
public int getCount(){
return chCount;
}
//void setCount (int c) Sets chCount to the incoming count c
public void setCount (int c){
chCount = c;
}
//void incCount() Increments chCount
public void incCount (){
chCount+=1;
}
//String getCode() Returns code
public String getCode(){
return code;
}
//void setCode (String c) Sets code to the incoming String c
public void setCode (String c){
code = c;
}
//int compareTo (HuffElement he) Returns -1, 0, or 1: Compare the counts. See implementing Comparable Interface String toString() Return string version of the three data members as: Char: y Code: Count: 15
public int compareTo (HuffElement he) {
return chCount.equals(he.chCount);
}
}
public class TestMyHashMap {
public static void main(String []args){
MyHashMap hashMap = new MyHashMap();
char tempCh;
String content = readFile("FDREconomics.txt", Charset.defaultCharset());
char[] charArray;
for(int i =0 ; i< content.length();i++){
tempCh = content.CharAt(i);
MyHashMap hashMap = new MyHashMap();
if (!(new String(charArray).contains(tempCh))) {
charArray[i] = tempCh;
FileChar fileChar = new FileChar(tempCh);
//instantiate a HuffElement object and add the character to it and increment the character count in it
HuffElement huffElement = new HuffElement();
huffElement.ch = tempCh;
huffElement.chCount +=1;
//This map entry pair is then added to the HashMap object you created.
hashMap.key[i] = fileChar;
hashMap.value[i] = huffElement;
}
else {
// For each character previously encountered you must use the FileChar key object for it to retrieve the HuffElement value object and increment the character count in it
FileChar fileChar = new FileChar(tempCh);
int ispresent = hashMap.key.indexOf(tempCh);
//hashMap.key[ispresent].getChar(tempCh);
hashMap.value[ispresent].incCount();
}
}
// Once you have completely read the whole file, you must retrieve the key set from the hash map and iterate through the key set using a for each loop and for each key, retrieve the character from the FileChar key and the count from the HuffElement value
//Sample Output: Char: D Count: 1 Char: E Count: 1 Char: F Count: 2 Char: A Count: 4
for (int k : hashMap.key)
{
Char retrivedKey = k.getChar();
intretrivedCount = hashMap.value[hashMap.key.indexOf(retrivedKey)].getCount();
System.out.println("Char: " + retrivedKey + " Count:" + intretrivedCount + "\t");
}
}
public String readFile{
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Happy Coding
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.