Write a Java program called Histogram.java that displays a list of distinct char
ID: 3741912 • Letter: W
Question
Write a Java program called Histogram.java that displays a list of distinct characters in an input tile and the occurrence of each eharacte. Your iogram should 1ead an input file name from a use. After that, your program should read characters in the file and display a list of distinct characters and their occurTeces. Finally, your program should draw a veril bafo the occuences For the assignment, your program has.to display the result exactly as the sample run. For instance, when you display a list of character occurrences, your program should display the characters with more than zero occurrence in the ascending order. The height of the vertical bar should be the same as the maxium value of"Occurrence" And aksa the characters in th vertical bar should cone i te order of occurrences. In other words, since the characters from 'D' to 'K have occurrence 0, it comes first. After that, characters . A' and .C' come next because ther occurrences are Finally, 'Bs displayed because ts occurrence is 3. In the assignment, you can assume that the number of characters in the input file is less than 200. Yau can also assume that each line has only one character and there's no extra blank Furthermore, you can assume that all characters are capitals and from 'A' to 'K space after each character This is another sample run of your progranm: Input filenamei Ctmpt2.txt A sample run of your program MUST be like below: Input tilename: C:\tnp l.txt Char Occurrence vertical Bar 12 I 12 For the sample un, tl.txt has the following context: For the sample t2. txt has the following context:Explanation / Answer
Code to copy:
import java.io.*;
import java.util.Arrays;
//Define the class Histogram.
public class Histogram
{
//Define the class to evaluate character frequency.
class CFrequency implements Comparable<CFrequency>
{
//Define the member variables for
final char alphabet;
int counter;
//Create the constructor.
public CFrequency(char alphabet)
{
//Set alphabet and counter.
this.alphabet = alphabet;
this.counter = 0;
}
//Define the method to increase the counter.
void increment()
{
counter++;
}
//Define the method for getting the counter value.
@Override
public int compareTo(CFrequency obj)
{
return counter - obj.counter;
}
}
//Define the character array to store the characters.
CFrequency[] chararr;
//Define the method to sort the array.
void sortCharArray()
{
//Sort array by using the sort function.
Arrays.sort(chararr);
}
//Define the method to read the file.
void readFile(File file) throws IOException
{
//Create an object of buffered reader.
BufferedReader brobj = new BufferedReader
(new FileReader(file));
//Define a variable to get the file name.
String readline;
//Read the file line by line.
while ((readline = brobj.readLine())!=null)
{
int ch = readline.charAt(0) - 'A';
chararr[ch].increment();
}
}
//Define the method to display the histogram.
void displayHistogram()
{
int maxLength = chararr[10].counter;
//Print the upper part.
System.out.println();
System.out.println("=============Vertical Bar"
+ "==============");
//Print the number, characters and * as per the
//conditions.
for (int iterI = maxLength; iterI >= 1; iterI--)
{
if (iterI < 10)
System.out.print("| ");
else
System.out.print("| ");
System.out.print(iterI+ " | ");
System.out.print(" ");
for (int iterJ = 0; iterJ < 11; iterJ++)
if (chararr[iterJ].counter >= iterI)
System.out.print(" * ");
else
System.out.print(" ");
System.out.println();
}
System.out.println("====================="
+ "==================");
System.out.print("| No | ");
for (int jIter = 0; jIter < 11; jIter++)
System.out.print(" " + chararr[jIter].alphabet
+ " ");
System.out.println();
//Print the lower part.
System.out.println("======================="
+ "================");
}
//Define a method to display the character frequency.
void displayCharFrequency()
{
System.out.println("Char Occurrence");
for (int iIter = 0; iIter < 11; iIter++)
if (chararr[iIter].counter > 0)
System.out.println(" " +
chararr[iIter].counter + ""
+ " " + chararr[iIter].counter);
}
//Create the constructor of the class histogram.
public Histogram()
{
chararr = new CFrequency[11];
//Add the character in the character array.
for (int iIter = 0; iIter < 11; iIter++)
chararr[iIter] = new CFrequency
((char)('A' + iIter));
}
//Define the main method.
public static void main(String[] args) throws IOException
{
//Define the variable to get filename.
String fName;
//Create an object of buffered reader.
BufferedReader brobj = new BufferedReader
(new InputStreamReader(System.in));
System.out.print("Input filename: ");
//Read the filename.
fName = brobj.readLine();
System.out.println();
//Create an object of the file.
File Nfile = new File(fName);
//Create an object of the class histogram.
Histogram hobj = new Histogram();
//Read the file.
hobj.readFile(Nfile);
//Display character frequency.
hobj.displayCharFrequency();
//Sort the character array.
hobj.sortCharArray();
//Display the histogram.
hobj.displayHistogram();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.