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

You will write a program, Project1.java, which repeatedly asks the user for inpu

ID: 668179 • Letter: Y

Question

You will write a program, Project1.java, which repeatedly asks the user for input until the user enters “DONE” and then outputs a chart showing the relative frequencies of the letters a-z and the letter with the highest frequency.

Capital letters A-Z are treated as if they were lower case letters. Any other characters are ignored. Frequencies are rounded to the nearest integer (e.g.: 3.333 % becomes 3 % and 3.666 % becomes 4 %). The chart and output of your program should be exactly as shown in the sample runs below:

Hints:

–   This program is really simple and is meant as a warm up exercise and to ensure we are all on the same page before we get started with the course.
–   Use a 26 element array of ints to store the counts for each character.
–   Figure out a simple way to test whether a character is between a and z or between A and Z and ignore any other characters.
–   To calculate the index for a letter (e.g. 'a' and 'A' go to index 0, 'b' and 'B' to index 1, etc.) recognize that you can treat a char as a number and do math with it (e.g. 'a' – 'a' = 0)
–   Use the Scanner class to get input from the user. E.g.:
import java.util.Scanner;
...
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a line of text");
String text = scanner.nextLine();
System.out.println("You entered " + text);

scanner.close();
–   Think about breaking the problem down into separate functions; for example one function that processes a line of text and updates the counts of the letters, another one that calculates the relative frequencies from the counts array, and another one that prints the output and the chart. Here is a possible solution outline: for every line the user enters, count all the letters and store their counts in the counts array. Once you have all the counts, calculate the relative frequency for each letter by dividing by the total number of letters (round the result to the nearest integer). You can then calculate the most frequent letter and output the frequencies chart.

OUTPUT::

Explanation / Answer

import java.util.*;
import java.io.*;
public class freq
{
public static void main(String[] args)
{
try
{
Hashtable<String, Double> freq = new Hashtable<String, Double>();
String set = "abcdefghijklmnopqrstuvwxyz";
int count = 0;
for ( int i = 0; i < set.length(); i++)
{
freq.put(set.substring(i, i+1), 0.0);
}
BufferedReader in = new BufferedReader(new FileReader(args[0]));
String line;
while ( (line = in.readLine()) != null)
{
for (int i = 0; i < line.length(); i++)
{
String c = line.substring(i, i+1);
if (c.matches("[a-zA-Z]"))
{
c = c.toLowerCase();
freq.put(c, freq.get(c) + 1.0);
count++;
}
}
}
for (int i = 0; i < set.length(); i++)
{
String key = set.substring(i, i + 1);
double perc = freq.get(key) / count * 100.0;
System.out.println(key + ": " + String.format("%2.2f", perc));
}
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}

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