Once more you will write a Java class that works with a Driver file.The Driver p
ID: 3694427 • Letter: O
Question
Once more you will write a Java class that works with a Driver file.The Driver provides the user interface; your file will work with the data and make the results available to this program. This assignment builds upon Assignment 3.
Write a Java class. Name it Assignment4. To this class, add the following required elements:
A constructor that accepts a single argument, a String for the name of the input file to process.
A function named processFile(), that accepts no arguments, and throws an IOException. This function must read the input file's contents and count the number of times each token (word) exists in the file.
A member variable HashMap is a good class to use for this.
A getFileName() function, no arguments, returns a String that contains the input file name.
A getTokenCount() function, no arguments, returns an int that is the total number of tokens (words) in the input file.
A getTokenCount() function, that accepts a String argument, and returns the number of times that token (word) appears in the input file.
A getTokensHavingCount() function that accepts an int argument (the count), and returns an ArrayList of Strings, where those Strings are the tokens that appear exactly "count" times in the input file.
A getTokensHavingLength() function that accepts an int argument (the length), and returns an ArrayList of Strings, where those Strings are the tokens in theinput file that have that exact length.
A getAverageOfCounts() function, no arguments, returns the average of the token counts.
Bonus #1: Add a function named getCountOfTokensHavingCount(), that accepts an int argument (count), and returns the number of tokens which appear in the input file exactly "count" times.
Bonus #2: Add a function named getCountOfTokensHavingLength(), that accepts an int argument (length), and returns the number of tokens which appear in the input file having exactly "length" characters in the token.
Hint #1: Requirements 6 and 7 are nearly identical with respect to the code. Get one right, and the next one will be very similar. Hint #2: The bonus requirements are REALLY short. It's very easy to implement them with only 2 lines of code in each function body. And like requirements 6 and7, the solutions to the bonus requirements are very similar to one another. I'm providing a stub for you to get started. Use it if you want, or start from scratch. Even better, start from scratch, implement each function on your own, then check the stub to see if you got the function signature right.
Stub:
Assignment4Driver.java:
Explanation / Answer
class Assignment4 {
// Your member variables go here
private String fileName;
private HashMap<String,Integer> wordsHash;
public Assignment4(final String fileName) {
// your code here
this.fileName = fileName;
wordsHash = new HashMap<String,Integer>();
}
public void processFile() throws IOException {
// your code here
Scanner sc = new Scanner(new File(fileName));
String temp = null;
while(sc.hasNext()) {
temp = sc.next();
if(wordsHash.containsKey(temp.trim().toLowerCase())) {
wordsHash.put(temp.trim().toLowerCase(), wordsHash.get(temp.trim().toLowerCase())+1);
} else {
wordsHash.put(temp.trim().toLowerCase(), 1);
}
}
}
public String getFileName() {
return fileName; // You will need to fix this
}
public int getTokenCount() {
return wordsHash.size(); // You will need to fix this
}
public int getTokenCount(final String key) {
if(wordsHash.containsKey(key.trim().toLowerCase())) {
return wordsHash.get(key.trim().toLowerCase());
}
return 0; // You will need to fix this
}
public ArrayList<String> getTokensHavingCount(final int count) {
ArrayList<String> tokens = new ArrayList<String>();
Set<String> keys = wordsHash.keySet();
for(String key: keys){
if(wordsHash.get(key) == count)
tokens.add(key);
}
return tokens.size() == 0 ? null : tokens; // You will need to fix this
}
public ArrayList<String> getTokensHavingLength(final int length) {
ArrayList<String> tokens = new ArrayList<String>();
Set<String> keys = wordsHash.keySet();
for(String key: keys){
if(key.length() == length)
tokens.add(key);
}
return tokens.size() == 0 ? null : tokens; // You will need to fix this
}
public int getCountOfTokensHavingCount(final int count) {
ArrayList<String> tokens = getTokensHavingCount(count);
return tokens == null ? 0 : tokens.size(); // You will need to fix this
}
public int getCountOfTokensHavingLength(final int length) {
ArrayList<String> tokens = getTokensHavingLength(length);
return tokens == null ? 0 : tokens.size(); // You will need to fix this
}
public double getAverageOfCounts() {
Set<String> keys = wordsHash.keySet();
int sum = 0;
for(String key: keys){
sum += wordsHash.get(key);
}
int count = keys != null && keys.size() > 0 ? keys.size() : 0;
if(count == 0)
return 0;
return ((double)sum)/count;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.