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

Assignment must be written in java and eclipse. Must use ArrayList, Interface an

ID: 3779341 • Letter: A

Question

Assignment must be written in java and eclipse. Must use ArrayList, Interface and Generics. I cannot use HashMap and TreeMap.

Write a program to verify Zipf s law with your favourite text. Specifically, your program must ask the user for an input text file (please handle potential I/O errors properly), and count how many words the file contains and display the following statistics Display each word in the text along with its rank and frequency You can assume that a word is defined as anything that the method scanner.next returns and only contains letters. For example "U2", data-base" and "hi should not be counted as words. Your program does not have to be case-sensitive. For example, the words "hi" and "Hi" can be considered as same words The list of rank/word/frequency must be displayed in descending order of frequency, and all words with the same frequency must be displayed in alphabetical order (uppercases before lowercases). Display the total number of word tokens and word types. The number of word tokens refers to the total number of words in the text (the number of times the method Scanner .next returned a string), whereas the number of word types refers to the number of different words in the text. For example, if the word "the" appears 30 times, it will count as 30 word tokens, but only 1 word type.

Explanation / Answer

SOURCE CODE:

Ziph.java

package ziph_law;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;


public class Ziph {
private ArrayList<word> a=new ArrayList<>();

public static void main(String[] args) throws IOException {
Ziph z = new Ziph();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the filename: ");
String filename = br.readLine();
FileInputStream fis = new FileInputStream(filename);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br1 = new BufferedReader(isr);
String feed = br1.readLine();
while (feed!=null) {
String t[]=feed.split(" ");
for(int i=0;i<t.length;i++)
{
z.add(t[i]);
}
feed = null;
feed = br1.readLine();
}
  
z.printList();
  
br1.close();
isr.close();
fis.close();
  
}
public void printList()
{
int c = 0;
int j = 1;
System.out.println("------------------------------------------------------------------");
System.out.println("RANK FREQ WORD");
System.out.println("------------------------------------------------------------------");
Collections.sort(this.a, new wordCompare());
for(int i=this.a.size()-1;i>=0;i--)
{
c = c + this.a.get(i).getCount();
System.out.println(j+" "+a.get(i).getCount()+" "+a.get(i).getWord());
j++;
}
}
public void add(String s)
{
int i=0;
for(i=0;i<this.a.size();i++)
{
word w = new word();
String temp = this.a.get(i).getWord();
if(s.equalsIgnoreCase(temp))
{
w = new word(s,this.a.get(i).getCount()+1);
this.a.remove(i);
this.a.add(i, w);
}
  
}
if(i==this.a.size())
this.a.add(new word(s));
}
}

---------------------------------------------------------------------------------------------------------

word.java


package ziph_law;


public class word implements Comparable{

private String word;
private int count;

public word(String word, int count) {
this.word = word;
this.count = count;
}

public word(String word) {
this.word = word;
this.count = 1;
}

public word() {
this.word = "";
this.count = 1;
}
  
public void increment()
{
this.count++;
}
  
public int getCount() {
return count;
}

public String getWord() {
return word;
}

@Override
public int compareTo(Object t) {
word w = (word)t;
int ret=0,c=w.count;
if(this.getCount()<c)
ret = -1;
else if(this.getCount()>c)
ret = 1;
return ret;
}

}

---------------------------------------------------------------------------------------------------------

wordCompare.java


package ziph_law;

import java.util.Comparator;


public class wordCompare implements Comparator{


@Override
public int compare(Object t, Object t1) {
word w1=(word) t,w2=(word) t1;
int ret=0;
if(w1.getCount()<w2.getCount())
ret = -1;
else if(w1.getCount()>w2.getCount())
ret = 1;
return ret;
}
  
}

---------------------------------------------------------------------------------------------

SAMPLE OUTPUT:

a.txt:

124
324
567
234
123 234 567
124
324
1 2 3 0

OUTPUT:

Enter the filename: D:.txt
------------------------------------------------------------------
RANK       FREQ       WORD
------------------------------------------------------------------
1       2       234
2       2       567
3       2       324
4       2       124
5       1       0
6       1       3
7       1       2
8       1       1
9       1       324
10       1       124
11       1       567
12       1       234
13       1       123

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