Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a file with the downloaded contents of the Green Eggs and Ham masterpiece

ID: 3857387 • Letter: C

Question

Create a file with the downloaded contents of the Green Eggs and Ham masterpiece.
Only words appearing at least 20 times will be reported.

When you're done: sort your output, both descending and ascending.

public class Frequency {

private String fruitName;

private int freq;

public Frequency(String fruitName, int freq) {

this.fruitName = fruitName;

this.freq = freq;

}


public void addOne() {

this.freq++;

}


public String toString() {

return "(" + fruitName + ", " + freq + ")";

}

public class ListOfFrequency{
private ArrayList data;
  
public ListOfFrequency(){
data = new ArrayList();
}
  
public void add(Frequency obj){
data.add(obj);
}
  
public String toString(){
int i;
String str;
  
if ( data.size() == 0 ){
return "[]";
}
else{
str = "[";
for(i=0; i str = str + data.get(i).toString() + ", ";
}
  
str += data.get(i).toString() + "]";
}
  
return str;
}

public class ListOfStrings {
private ArrayList list;

public ListOfStrings() {
list=new ArrayList();
}
  
public void add(String s){
list.add(s);
}

public String toString() {
String s="[";
for(int i=0;i s=s+list.get(i);
if(i!=list.size()-1){
s=s+",";
}
}
s=s+"]";
return s;
}

import java.io.*;
import java.util.*;

class Utilities {
public static void countWord(ListOfFrequency lof, String word){
for (Frequency f : lof){
if (f.word.equals(word)){
f.addOne();
return;
}
lof.add(new Frequency(word,1));
}

public static ListOfFrequency countAllWords(ListOfStrings los){
ListOfFrequency lof = new ListOfFrequency();
for (String word : los)
Utilities.countWord(lof, word);
return lof;
}

public static ListOfStrings readFile(File file) { // new procedure, given
ListOfStrings los = new ListOfStrings();
try {
Scanner in = new Scanner(file);
while (in.hasNext()) {
los.add(in.next());
}
} catch (FileNotFoundException e) {
  
}
return los;
}

// you need to write/define filter here

public static void main(String[] args) { // combined unit test code
ListOfFrequency lof = new ListOfFrequency();
Utilities.countWord( lof, "mango" );
Utilities.countWord( lof, "apple" );
Utilities.countWord( lof, "banana" );
Utilities.countWord( lof, "mango" );
Utilities.countWord( lof, "apple" );
Utilities.countWord( lof, "apple" );
System.out.println( lof );
ListOfStrings los = new ListOfStrings();
los.add("mango");
los.add("apple");
los.add("banana");
los.add("mango");
los.add("apple");
los.add("apple");
ListOfFrequency alof = Utilities.countAllWords(los);
System.out.println( alof );
ListOfStrings alos = Utilities.readFile(new File("geah.txt"));
System.out.println( Utilities.filter( 20, Utilities.countAllWords( alos ) ) );
}
}

The code above (note it's a cumulative unit test) when compiled and run will produce:

Explanation / Answer

public class Frequency {

private String fruitName;

private int freq;

/**

* @param fruitName

* @param freq

*/

public Frequency(String fruitName, int freq) {

this.fruitName = fruitName;

this.freq = freq;

}

/**

* @return the fruitName

*/

public String getFruitName() {

return fruitName;

}

/**

* @return the freq

*/

public int getFreq() {

return freq;

}

/**

* @param fruitName

* the fruitName to set

*/

public void setFruitName(String fruitName) {

this.fruitName = fruitName;

}

/**

* @param freq

* the freq to set

*/

public void setFreq(int freq) {

this.freq = freq;

}

/**

* adds one o freq member

*/

public void addOne() {

this.freq++;

}

/*

* (non-Javadoc)

*

* @see java.lang.Object#toString()

*/

@Override

public String toString() {

return "(" + fruitName + ", " + freq + ")";

}

}

import java.util.ArrayList;

public class ListOfFrequency {

private ArrayList<Frequency> data;

public ListOfFrequency() {

data = new ArrayList();

}

/**

* @return the data

*/

public ArrayList<Frequency> getData() {

return data;

}

/**

* @param data

* the data to set

*/

public void setData(ArrayList<Frequency> data) {

this.data = data;

}

public void add(Frequency obj) {

data.add(obj);

}

public String toString() {

int i;

String str;

if (data.size() == 0) {

return "[]";

} else {

str = "[";

for (i = 0; i < data.size(); i++) {

str = str + data.get(i).toString() + ", ";

}

str += "]";

}

return str;

}

}

import java.util.ArrayList;

public class ListOfStrings {

private ArrayList<String> list;

public ListOfStrings() {

list = new ArrayList();

}

public void add(String s) {

list.add(s);

}

/**

* @return the list

*/

public ArrayList<String> getList() {

return list;

}

/**

* @param list

* the list to set

*/

public void setList(ArrayList<String> list) {

this.list = list;

}

public String toString() {

String s = "[";

for (int i = 0; i < list.size(); i++) {

s = s + list.get(i);

if (i != list.size() - 1) {

s = s + ",";

}

}

s = s + "]";

return s;

}

}

import java.io.*;

import java.util.*;

class Utilities {

public static void countWord(ListOfFrequency lof, String word) {

ArrayList<Frequency> freqs = lof.getData();

for (Frequency f : freqs) {

if (f.getFruitName().equals(word)) {

f.addOne();

return;

}

}

lof.add(new Frequency(word, 1));

}

public static ListOfFrequency countAllWords(ListOfStrings los) {

ListOfFrequency lof = new ListOfFrequency();

ArrayList<String> words = los.getList();

for (String word : words)

Utilities.countWord(lof, word);

return lof;

}

public static ListOfStrings readFile(File file) { // new procedure, given

ListOfStrings los = new ListOfStrings();

try {

Scanner in = new Scanner(file);

while (in.hasNext()) {

los.add(in.next());

}

} catch (FileNotFoundException e) {

}

return los;

}

}

public class TestUtilities {

public static void main(String[] args) { // combined unit test code

ListOfFrequency lof = new ListOfFrequency();

Utilities.countWord(lof, "mango");

Utilities.countWord(lof, "apple");

Utilities.countWord(lof, "banana");

Utilities.countWord(lof, "mango");

Utilities.countWord(lof, "apple");

Utilities.countWord(lof, "apple");

System.out.println(lof);

ListOfStrings los = new ListOfStrings();

los.add("mango");

los.add("apple");

los.add("banana");

los.add("mango");

los.add("apple");

los.add("apple");

ListOfFrequency alof = Utilities.countAllWords(los);

System.out.println(alof);

// for this please provide geah.txt text file

// ListOfStrings alos = Utilities.readFile(new File("geah.txt"));

// System.out.println( Utilities.filter( 20, Utilities.countAllWords(

// alos ) ) );

}

}

OUTPUT:

[(mango, 2), (apple, 3), (banana, 1), ]
[(mango, 2), (apple, 3), (banana, 1), ]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote