Introduction: The goal of this assignment is to practice string processing funct
ID: 3870568 • Letter: I
Question
Introduction:
The goal of this assignment is to practice string processing functions, and design a solution which distributes the functionalities among appropriate classes.
Problem:
When you are given a text file, parse, tokenize, and further split the tokens into specified sized letter groups.
Example:
Input: “Second Programming Assignment”
Tokens: “second” “programming” “assignment”
Letter Groups (2) : "se" "ec" "co" "on" "nd" "pr" "ro" "og" "gr" "ra" "am" "mm" "mi" "in" "ng" "as" "ss" "si" "ig" "gn" "nm" "me" "en" "nt"
Letter Groups (3): "sec" "eco" "con" "ond" "pro" "rog" "ogr" "gra" "ram" "amm" "mmi" "min" "ing" "ass" "ssi" "sig" "ign" "gnm" "nme" "men" "ent"
After generating the letter groups, generate the histogram (frequency of occurrence) of the letter groups.
The name of the text file will be the first argument of your main function and letterGroupLen will be the second argument of your main function.
Parse the input text file
You will have 2 classes in your design. SentenceUtils and Histogram.
SentenceUtils class will tokenize and partition the tokens into letter groups
Histogram class using a HashMap data structure will count the number of occurrence of each letter group, and print the results when requested.
The solution is composed of 2 classes: a SentenceUtils Class to convert a string into letterGroups and also an Histogram class to do histogram processing. These 2 functionalities are independent of each other.
You need to implement body of the following 4 functions and also the main function:
private String[] getTokens(String line)
private void splitTokenstoLetterGroups(String[] tokens)
public void generateHistogram(ArrayList letterGroups)
public void printHistogram()
Please test the program using the input files : input1.txt and test.txt.
Here is the framework for the program:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* Write your information
*/
class Histogram{
private HashMap mHistogram;
public Histogram(){
this.mHistogram = new HashMap<>();
}
/*
This function counts the number of occurence of each letter group
input: letter groups
output: histogram
example: input: ["an","an","or","or","or"]
output: [<"an",2>, <"or",3>]
*/
public void generateHistogram(ArrayList letterGroups){
/*
You are going to implement body of this function
*/
}
/*
This function prints the histogram
input: histogram
output: histogram printed on the display
example: input: [<"an",2>, <"or",3>]
output:
an: **
or: ***
*/
public void printHistogram(){
/*
You are going to implement body of this function
*/
}
}
public class SentenceUtils {
private ArrayList mLetterGroups;
private int mLetterGroupLen;
public SentenceUtils(int letterGroupLen){
this.mLetterGroups = new ArrayList<>();
this.mLetterGroupLen = letterGroupLen;
}
public void addWords(String line){
String[] tokens = this.getTokens(line);
this.splitTokenstoLetterGroups(tokens);
}
public ArrayList getLetterGroups(){
return (this.mLetterGroups);
}
/*
This function splits a string into array of strings separated by space
character
input: a string
output: array of words seperated by space character
example: input: "object oriented"
output: ["object", "oriented"]
*/
private String[] getTokens(String line){
/*
You are going to implement body of this function
*/
}
/*
This function splits the tokens into letter groups according to
this.mLetterGroupLen
input: array of tokens
output: letterGroups of size this.mLetterGroupLen appended to
this.mLetterGroups
example: input: "object" "oriented"
output: this.mLetterGroups (of size 4) = this.mLetterGroups +
["obje","bjec", "ject","orie", "rien", "ient", "ente",
"nted"]
*/
private void splitTokenstoLetterGroups(String[] tokens){
/*
You are going to implement body of this function
*/
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
/*
You are going to implement to read the file and also to call
appropriate functions to
generate and print the histogram.
Ensure that if a non-existing file is input to your program,
your program do not cause
an exception and halt.
First argument is the filename, and the second argument is the
letterGroupLength.
*/
}
}
Here is input1.txt:
and and or or or or or or
Here is test.txt:
The Cat in the Hat By Dr. Seuss
The sun did not shine.
It was too wet to play.
So we sat in the house
All that cold, cold, wet day.
I sat there with Sally.
We sat there, we two.
And I said, “How I wish We had something to do!”
Too wet to go out
And too cold to play ball.
So we sat in the house.
We did nothing at all.
So all we could do was to Sit! Sit! Sit! Sit!
And we did not like it.
Not one little bit.
And then Something went BUMP!
How that bump made us jump!
We looked!
Then we saw him step in on the mat!
We looked!
And we saw him!
The Cat in the Hat!
Explanation / Answer
import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; import java.util.List; public class SentenceUtils { /* Defining Variables */ private List mLetterGroups; private int mLetterGroupLen; /* Constructor */ public SentenceUtils(int letterGroupLen){ mLetterGroups = new ArrayList(); this.mLetterGroupLen = letterGroupLen; } /* Add words: get tokens and split tokens into letter groups */ public void addWords(String line){ String[] tokens = this.getTokens(line); this.splitTokenstoLetterGroups(tokens); } /* Get letter groups */ public List getLetterGroups(){ return (this.mLetterGroups); } /* Splitting Strings using spaces as separator */ private String[] getTokens(String line){ return line.split("\s+"); } /* Splitting tokens to letter Groups */ private void splitTokenstoLetterGroups(String[] tokens){ for(String str: tokens){ for(int index = 0; indexRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.