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

Write a Java program called Histogram.java that displays a list of distinct char

ID: 3741918 • Letter: W

Question

Write a Java program called Histogram.java that displays a list of distinct characters in an input file and the occurrence of each character. Your program should read an input file name from a user. After that, your program should read characters in the file and display a list of distinct characters and their occurrences. Finally, your program should draw a vertical bar for the occurrences.

In the assignment, you can assume that the number of characters in the input file is less than 200. You can also assume that each line has only one character and there’s no extra blank space after each character. Furthermore, you can assume that all characters are capitals and from ‘A’ to ‘K’.

A sample run of your program MUST be like below:

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 maximum value of “Occurrence”. And also, the characters in the vertical bar should come in the 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 their occurrences are 1. Finally, ‘B’ is displayed because its occurrence is 3.

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

Histogram.java


//File imports----
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.lang.StringIndexOutOfBoundsException;
//----------------

public class Histogram {
  

   //Faster println call
   public static void println(String line) {
       System.out.println(line);
   }
  
   //Delete repeats in string for use in counting occurrences
   public static String deleteRepeats(String str) {
       int [] count = new int[256];
      
       for (int i = 0 ; i < str.length(); i++) {
           char c = str.charAt(i);
           count[c]++;
       }
      
       StringBuilder strBld = new StringBuilder(count.length);
       for (int i = 0; i < count.length; i++) {
           if (count[i] > 0) {
               strBld.append((char)i)
;           }
       }
       return strBld.toString();
   }
  
  
   //Build HashMap to hold all letters and default 0 instances of values.
   public static void buildHashMap(HashMap<Character, Integer> tr) {
       tr.put('a', 0);
       tr.put('b', 0);
       tr.put('c', 0);
       tr.put('d', 0);
       tr.put('e', 0);
       tr.put('f', 0);
       tr.put('g', 0);
       tr.put('h', 0);
       tr.put('i', 0);
       tr.put('j', 0);
       tr.put('k', 0);
   }
  
  
   //----------------------
   public static void main(String[] args) {
       //Initialize variables
       Scanner keyboard;
       String keyIn;
       Scanner fileIn;
       String fileString = "";
       char [] letters;
       int [] count = new int[256];
       String cutString;
       int maxCount = 0;
       HashMap<Character,Integer> tracker = new HashMap<Character, Integer>();
      
      
       System.out.print("Input filename: ");
       keyboard = new Scanner(System.in);
       keyIn = new String(keyboard.nextLine());
      
       //Attempt to open file
       try {
          
           //Note: file must exist outside of src
           fileIn = new Scanner(new FileInputStream(keyIn));
          
           //Fill string with text in file by each line of data in text file
           while(fileIn.hasNextLine()) {
               String tempStr = fileIn.nextLine();
               fileString = fileString + tempStr;
           }
          
//           Testing String concatenation
//           println(fileString); //Successful test
//           --------------------------
          
          
//           Set frame above occurrence marker
           println("Char    Occurrence");
          
           //Convert string to char array and sort
           letters = fileString.toCharArray();
           for(int i = 0; i < letters.length-1; i++) {
               for (int j = i+1; j < letters.length; j++) {
                   if ((int)letters[i] > (int)letters[j]) {
                       char temp = letters[j];
                       letters[j] = letters[i];
                       letters[i] = temp;
                   }
               }
           }
          
          
           cutString = deleteRepeats(fileString);
           buildHashMap(tracker);
          
           //Fill count array according to letter appearance
           for (int i = 0; i < cutString.length(); i++) {
               for (int j = 0; j < letters.length;j ++) {
                   if (cutString.charAt(i) == letters[j]) {
                       count[i]++;
                       //Increment value in the hash map          
                   }
               }
           }
          
           //Print Char and Occurrence data
           for (int i = 0; i < cutString.length(); i++) {
               System.out.println(cutString.charAt(i) + "       " + count[i]);
           }
          
           maxCount = 0;
           for (int i = 0; i < cutString.length();i++) {
               if (count[i] > maxCount) {
                   maxCount = count[i];
               }
           }
          
       }
       catch (FileNotFoundException e) {
           println("File not found.");
           System.exit(0);
       }
      
      
       println("");
       println("============== Vertical Bar Graph ===============");
       for (int i = maxCount; i > 0; i--) {
           System.out.println("| " + i + " |");
       }
       System.out.println("=================================================");
       System.out.print("| No.|");
      
       //println("Success");
      
   }

}

t1.txt


B
A
C
B
B

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