Using JGRASP, code assignment4 Assignement4: 1. A constructor that accepts a sin
ID: 3692173 • Letter: U
Question
Using JGRASP, code assignment4
Assignement4:
1. A constructor that accepts a single argument, a String for the name of the input file to process.
2. 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.
3. A getFileName() function, no arguments, returns a String that contains the input file name.
4. A getTokenCount() function, no arguments, returns an int that is the total number of tokens (words) in the input file.
5. A getTokenCount() function, that accepts a String argument, and returns the number of times that token (word) appears in the input file.
6. 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.
7. 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.
8. A getAverageOfCounts() function, no arguments, returns the average of the token counts.
9. 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.
10. 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: 9 and 10 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 (9 and 10) requirements are very similar to one another.
=======================================
GIVEN AssignementDriver4 :
Explanation / Answer
Hello there ,
Please find below code for Assignment4 .
Also in your code , you will have to make following 2 change. they are minor. OR YOU CAN TAKE MY MODIFIED Assignment4Driver CODE. I HAVE ATTACHED BELOW. Choice is urs.
1.
field.addKeyListener(
new KeyListener() {
public void keyTyped(KeyEvent event) {}
public void keyPressed(KeyEvent event) {}
public void keyReleased(KeyEvent event) {
String contents = field.getText();
/* contents = contents.toLowerCase();
*/ contents = getInfo(contents);
infoLabel.setText(contents);
}
}
);
in field key listener you will have to comment out " contents = contents.toLowerCase();" .Not elsewhere.
2. you can add casting to (String) , if you find any errors in ur code in setText.
like this.
textArea2.setText((String) list.get(0));
Let me know if you have any doubts .
Thanks . It's a good application ! Enjoy !
==================================================Assignment4======================
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
public class Assignment4 {
private String inputFileName;
Map<String, Integer> wordCount = new HashMap<String, Integer>();
public Assignment4(String inputFileName) {
this.inputFileName = inputFileName;
}
public void processFile() {
StringBuffer buffer = null;
try (BufferedReader br = new BufferedReader(new FileReader(inputFileName))) {
String sCurrentLine;
buffer = new StringBuffer();
while ((sCurrentLine = br.readLine()) != null) {
buffer.append(sCurrentLine + " ");
}
br.close();
System.out.println(buffer.toString());
StringTokenizer tokenizer = new StringTokenizer(buffer.toString());
while (tokenizer.hasMoreTokens()) {
String word = tokenizer.nextToken();
if (wordCount.containsKey(word))
wordCount.put(word, wordCount.get(word) + 1);
else
wordCount.put(word, 1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String getFileName() {
return inputFileName;
}
public int getTokenCount() {
return wordCount.size();
}
public int getTokenCount(String key) {
return wordCount.get(key);
}
public List getTokensHavingCount(int count) {
List<String> list = new ArrayList<String>();
for (Map.Entry<String, Integer> entry : wordCount.entrySet()) {
if (entry.getValue() == count) {
list.add(entry.getKey());
}
}
return list;
}
public List getTokensHavingLength(int length) {
List<String> list = new ArrayList<String>();
for (Map.Entry<String, Integer> entry : wordCount.entrySet()) {
if (entry.getKey().length() == length) {
list.add(entry.getKey());
}
}
return list;
}
public double getAverageOfCounts() {
double sum = 0;
for (Map.Entry<String, Integer> entry : wordCount.entrySet()) {
sum = sum + entry.getValue();
}
return sum / wordCount.size();
}
public int getCountOfTokensHavingCount(int count) {
return getTokensHavingCount(count).size();
}
public int getCountOfTokensHavingLength(int length) {
return getTokensHavingLength(length).size();
}
public static void main(String args[]) {
Assignment4 assignment4 = new Assignment4("textFile.txt");
assignment4.processFile();
System.out.println(assignment4.getTokenCount());
System.out.println(assignment4.getTokenCount("A"));
}
}
=========MY Assignment4Driver===== Modified one
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Assignment4Driver extends JFrame {
public static void main(String args[]) {
Assignment4 a = null;
if(args.length == 1) {
a = new Assignment4(args[0]);
}
else {
JFileChooser chooser = new JFileChooser();
int result = chooser.showOpenDialog(null);
if(result == JFileChooser.APPROVE_OPTION) {
a = new Assignment4(chooser.getSelectedFile().getName());
}
else {
System.exit(0);
}
}
try {
a.processFile();
new Assignment4Driver(a);
} catch(Exception e) {
e.printStackTrace();
}
}
private final Assignment4 a;
public Assignment4Driver(final Assignment4 a) {
super("Assignment 4");
this.a = a;
JPanel main = new JPanel();
main.setLayout(new GridLayout(3,0));
// Summary
JPanel summaryPanel = new JPanel();
summaryPanel.setLayout(new GridLayout(0,1));
int tokenCount = a.getTokenCount();
JLabel label = new JLabel(a.getFileName() + " : " + String.format("%,d", tokenCount) + " tokens in file.");
summaryPanel.add(label);
double d = a.getAverageOfCounts();
String s = "Words appear an average of " + String.format("%.2f", d) + " times.";
summaryPanel.add(new JLabel(s));
// Count for token, enter a token, the label shows its count
JPanel tokenCountPanel = new JPanel();
tokenCountPanel.add(new JLabel("Count for this Token"));
String info = getInfo(null);
final JLabel infoLabel = new JLabel(info);
final JTextField field = new JTextField(20);
field.addKeyListener(
new KeyListener() {
public void keyTyped(KeyEvent event) {}
public void keyPressed(KeyEvent event) {}
public void keyReleased(KeyEvent event) {
String contents = field.getText();
/* contents = contents.toLowerCase();
*/ contents = getInfo(contents);
infoLabel.setText(contents);
}
}
);
tokenCountPanel.add(field);
tokenCountPanel.add(infoLabel);
JPanel panel = new JPanel();
panel.add(summaryPanel);
panel.add(tokenCountPanel);
main.add(panel);
// Tokens having count, enter a count, show tokens having that count
JPanel tokenEnumerationPanel = new JPanel();
tokenEnumerationPanel.add(new JLabel("Tokens Having Count"));
final JTextArea textArea2 = new JTextArea(5, 30);
textArea2.setLineWrap(true);
final JTextField textField2 = new JTextField(10);
textField2.addKeyListener(
new KeyListener() {
public void keyTyped(KeyEvent event) {}
public void keyPressed(KeyEvent event) {}
public void keyReleased(KeyEvent event) {
String contents = textField2.getText();
textArea2.setText("");
contents = contents.toLowerCase();
int count = 0;
try {
count = Integer.parseInt(contents);
} catch(Exception e) {
// silently ignore
}
List list = a.getTokensHavingCount(count);
if(list.size() > 0) {
textArea2.setText((String) list.get(0));
}
if(list.size() > 1) {
for(int i = 1; i < list.size(); i++) {
textArea2.append(" " + list.get(i));
}
}
textArea2.setCaretPosition(0);
}
}
);
JScrollPane scrollPane2 = new JScrollPane(textArea2);
tokenEnumerationPanel.add(textField2);
tokenEnumerationPanel.add(scrollPane2);
main.add(tokenEnumerationPanel);
// Tokens having length, enter a length, show tokens having that length
JPanel tokenLengthPanel = new JPanel();
tokenLengthPanel.add(new JLabel("Tokens Having Length"));
final JTextArea textArea3 = new JTextArea(5, 30);
textArea3.setLineWrap(true);
final JTextField textField3 = new JTextField(10);
textField3.addKeyListener(
new KeyListener() {
public void keyTyped(KeyEvent event) {}
public void keyPressed(KeyEvent event) {}
public void keyReleased(KeyEvent event) {
textArea3.setText("");
String contents = textField3.getText();
contents = contents.toLowerCase();
int length = 0;
try {
length = Integer.parseInt(contents);
} catch(Exception e) {
// silently ignore
}
List list = a.getTokensHavingLength(length);
if(list.size() > 0) {
textArea3.setText((String) list.get(0));
}
if(list.size() > 1) {
for(int i = 1; i < list.size(); i++) {
textArea3.append(" " + list.get(i));
}
}
textArea3.setCaretPosition(0);
}
}
);
tokenLengthPanel.add(textField3);
final JScrollPane scrollPane3 = new JScrollPane(textArea3);
tokenLengthPanel.add(scrollPane3);
main.add(tokenLengthPanel);
add(main);
setSize(500, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
// populate the label text
private String getInfo(final String key) {
if(key == null || key.length() < 1) {
return "No data";
}
int count = a.getTokenCount(key);
return "Count: " + count;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.