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

You are to create a program that extends the Echo class to read in any external

ID: 3625504 • Letter: Y

Question

You are to create a program that extends the Echo class to read in any external text file, and then report the relative frequencies of the letters of the English alphabet, plus the blank, or space symbol (convert letters to all lower case, all non-letters except blank (space) are to be ignored). Generally, blank actually appears about 18% of the time, more frequently even than e, which appears 10% of the time. Using printf to format your fractional answers to 5 decimal places (blank included).

For example, on this text:

Goodbye old paint!
Goodbye old paint!

The correct output would look like this:

a 0.05882
b 0.05882
c 0.00000
d 0.11765
e 0.05882
f 0.00000
g 0.05882
h 0.00000
i 0.05882
j 0.00000
k 0.00000
l 0.05882
m 0.00000
n 0.05882
o 0.17647
p 0.05882
q 0.00000
r 0.00000
s 0.00000
t 0.05882
u 0.00000
v 0.00000
w 0.00000
x 0.00000
y 0.05882
z 0.00000
blanks: 0.11765

One significant caveat. Your programs must count blocks of blanks just once (that is, if many blanks (spaces) appear in a row, this should only count for one blank). Thus the two example sentences above (both lines end at the !) should have the same number of blanks, namely 2.
Called one driver FreqDriver as principal class - the one that extends Echo - called Frequencies. Convert all text to lower case before proceeding with your analysis.


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

public class Echo{
String fileName; // external file name
Scanner scan; // Scanner object for reading from external file

public Echo(String f) throws IOException
{
fileName = f;
scan = new Scanner(new FileReader(fileName));
}

public void readLines(){ // reads lines, hands each to processLine
while(scan.hasNext()){
processLine(scan.nextLine());
}
scan.close();
}

public void processLine(String line){ // does the real processing work
System.out.println(line);
}
}

Explanation / Answer

public class LetterCount extends Echo
{
// letter counts
private int[] letters;
private int blanks;
private int total;

public LetterCount(String file)
{
super(file);
letters = new int[26];
blanks = 0;
total = 0;
}

public void processLine(String line)
{
// replace blocks of blanks with 1 blank
line.replaceAll(" ", " ");

for(char c : line.toCharArray())
{
if(Character.isLetter(c))
{
letters[Character.toLowerCase(c)-'a']++;
total++;
}
else if(c == ' ')
{
blanks++;
total++;
}
}
}

public String toString()
{
String output = "";
for(int i = 0; i < 26; i++)
{
output += String.format((char)('a'+i)+": %.5f ", letters[i]/total);
}
output += String.format("blanks: %.5f ", blanks/total);
return output;
}
}


public static void main(String[] args)
{
LetterCount test = new LetterCount("input.txt");
test.readLines();
System.out.println(test);
}

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