Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*Using Java* 1) Implement HashSet and HashMap and use them to implement the following. 2) Counting the number of each keyword in a java file.

*Using Java* 1) Implement HashSet and HashMap and use them to implement the following. 2) Counting the number of each keyword in a java file. So basically, the instructions for the assignment are to counter the number of times that a word is used in the file BUT you have to implement HashMap and HashSet in order to get it. For example, the output of this program should look like this:

1 - [try, static, package, class, catch, while]:

2 - [for, else, public, if]:

3 - [private]:

4 - [void]:

5 - [import]:

7 - [new]:

This is my KeyboardCounter.java class that I made for this program, I just need help with writing and implementing HashSet and HashMap and actually being able to run the program. Thank you

KeyboardCounter.java

import java.io.File;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Scanner;

import java.util.TreeMap;

public class KeyboardCounter {

MyHashMap hm = new MyHashMap<>();

MyHashSet keywords = new MyHashSet<>(Arrays.asList(

"package", "import", "public", "class",

"this", "void", "private", "new", "static",

"try", "catch", "if", "else", "while",

"switch", "case", "for", "interface", "abstract", "protected",

"throws", "extends", "implements", "do", "break", "continue",

"final", "finally", "super", "default"));

String filename;

KeyboardCounter(){}

KeyboardCounter(String inputfile){

this.filename = inputfile;

}

private void scanFile(){

File f = new File(this.filename);

try {

Scanner input = new Scanner(f);

while (input.hasNext()) {

String s = input.next();

if (keywords.contains(s)) {

if(hm.containsKey(s)) {

int c = hm.get(s);

c++;

hm.put(s, c);

}else {

hm.put(s,1);

}

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

private void sortByValue() {

TreeMap> treeMap = new TreeMap<>();

for (MyMap.Entry ent: hm.entrySet()){

if(treeMap.containsKey(ent.value)) {

treeMap.get(ent.value).add(ent.key);

}

else {

ArrayList toAdd = new ArrayList<>();

toAdd.add(ent.key);

treeMap.put(ent.value, toAdd);

}

}

treeMap.forEach((s, c)->System.out.println(s+"-"+c+":"));

System.out.println(treeMap);

}

private void printCount() {

for (MyMap.Entry ent: hm.entrySet()) {

System.out.println(ent.getKey()+":"+ent.getValue());

}

}

public static void main(String...strings) {

KeyboardCounter kc = new KeyboardCounter("\\src\\HW4\\KeyboardCounter.java");

kc.scanFile();

kc.sortByValue();

kc.printCount();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started