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

Design a program named CharDistribution.java, within this program, implements me

ID: 3590733 • Letter: D

Question

 Design a program named CharDistribution.java, within this program, implements methods as specified below:  public int[] charCounting(String str) - this method counts number of times each of alphabetic characters appears in the string. The first element of the int array represents the frequency of A, the 2nd is for B, and so on.  public int[] charCounting(File file) - which counts how many times each of the alphabetic characters appears in the data file.  public void print(int[] c) -print the int array in a histogram format.  Note: The overloaded charCounting methods do not differentiate the difference between lower and upper cases.    Then print the result as a histogram as specified as follows:           *                                                    *                                                    *                                                    *                                                    *                   *                                *                   *                        *       *                   *                        *       *                   *     * * *              *       *                 * *     * * *              *       *       *         * *     * * *              *       *       *         * *     * * *              *       *       *     *   * *     * * *              *       *       *     *   * *     * * *              *       *     * *     *   * * *   * * *              *   *   *     * *     *   * * *   * * *              *   * * *     * *     * * * * *   * * *              *   * * *   * * *     * * * * *   * * * *            *   * * *   * * *     * * * * *   * * * *            *   * * * * * * *     * * * * *   * * * * * *        * * * * * * * * *     * * * * *   * * * * * *   *    ----------------------------------------------------  A B C D E F G H I J K L M N O P Q R S T U V W X Y Z   How to run testing cases: Click on the CharDistributionJUnit.class file, then click on the Run JUnit button To examine the histogram output, click on the CharDistribtionT.class to select the file, then type *data1.txt in the Cmd argument list then click on the Run button 
  Design a program named CharDistribution.java, within this program, implements methods as specified below:    public int[] charCounting(String str) - this method counts number of times each of alphabetic characters appears in the string. The first element of the int array represents the frequency of A, the 2nd is for B, and so on.    public int[] charCounting(File file) - which counts how many times each of the alphabetic characters appears in the data file.    public void print(int[] c) -print the int array in a histogram format.    Note: The overloaded charCounting methods do not differentiate the difference between lower and upper cases.       Then print the result as a histogram as specified as follows:              *                                                     *                                                     *                                                     *                                                     *                   *                                 *                   *                         *       *                   *                         *       *                   *     * * *               *       *                 * *     * * *               *       *       *         * *     * * *               *       *       *         * *     * * *               *       *       *     *   * *     * * *               *       *       *     *   * *     * * *               *       *     * *     *   * * *   * * *               *   *   *     * *     *   * * *   * * *               *   * * *     * *     * * * * *   * * *               *   * * *   * * *     * * * * *   * * * *             *   * * *   * * *     * * * * *   * * * *             *   * * * * * * *     * * * * *   * * * * * *         * * * * * * * * *     * * * * *   * * * * * *   *     ----------------------------------------------------   A B C D E F G H I J K L M N O P Q R S T U V W X Y Z     How to run testing cases:  Click on the CharDistributionJUnit.class file, then click on the Run JUnit button  To examine the histogram output, click on the CharDistribtionT.class to select the file, then type *data1.txt in the Cmd argument list then click on the Run button  

Explanation / Answer

CharDistribution.java

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.util.Scanner;

public class CharDistribution {

public int[] charCounting(File file) throws Exception{

StringBuffer sbContent=new StringBuffer(100);

String content=null;

int[] ex=new int[26];

FileReader fr=new FileReader(file);

BufferedReader br=new BufferedReader(fr);

String line;

while(( line=br.readLine())!=null)

{

sbContent.append(line);

}

content=sbContent.toString().replaceAll("/[^A-Za-z]/", "").toUpperCase();

System.out.println("content::"+content);

ex=charCounting(content);

return ex;

}

public int[] charCounting(String str){

int[] ex=new int[26];

String initStr=str.replaceAll("/[^A-Za-z]/", "").toUpperCase();

for(int i=0;i<initStr.length();i++){

Character c=initStr.charAt(i);

switch(c){

case 'A':

ex[0]++;

break;

case 'B':

ex[1]++;

break;

case 'C':

ex[2]++;

break;

case 'D':

ex[3]++;

break;

case 'E':

ex[4]++;

break;

case 'F':

ex[5]++;

break;

case 'G':

ex[6]++;

break;

case 'H':

ex[7]++;

break;

case 'I':

ex[8]++;

break;

case 'J':

ex[9]++;

break;

case 'K':

ex[10]++;

break;

case 'L':

ex[11]++;

break;

case 'M':

ex[12]++;

break;

case 'N':

ex[13]++;

break;

case 'O':

ex[14]++;

break;

case 'P':

ex[15]++;

break;

case 'Q':

ex[16]++;

break;

case 'R':

ex[17]++;

break;

case 'S':

ex[18]++;

break;

case 'T':

ex[19]++;

break;

case 'U':

ex[20]++;

break;

case 'V':

ex[21]++;

break;

case 'W':

ex[22]++;

break;

case 'X':

ex[23]++;

break;

case 'Y':

ex[24]++;

break;

case 'Z':

ex[25]++;

break;

}

}

return ex;

}

public void print(int[] c){

int max = c[0];

int i,j;

for(i = 0; i < c.length; i++)

{

if(max < c[i])

{

max = c[i];

}

}

System.out.println("MAX value::"+max);

char alphabets[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

for(j=max;j>=1;j--){ //For each possible height of the histogram.

for(i=0;i<c.length;i++) //Check height of each element

if(c[i]>=j)

System.out.print(" *"); //Print * if the k-th element has at least a height j.

else

System.out.print(" "); //Else, do print blank space

System.out.println(); //Newline after every row.

}

for (int k = 0; k < alphabets.length; k++) {

System.out.print(" "+alphabets[k]);

}

}

public static void main(String[] args) throws Exception{

//This is the test code for displaying histogram if input string is given

Scanner sc=new Scanner(System.in);

CharDistribution cd=new CharDistribution();

System.out.println("Please enter a string");

String str=sc.nextLine();

cd.print(cd.charCounting(str));

//This is the test code for displaying histogram if input file name is given

System.out.println("Please enter file name");

String fileName=sc.nextLine();

File f=new File(fileName);

System.out.println(f.exists());

cd.print(cd.charCounting(f));

}

}

Output

Please enter a string
HELLO world
MAX value::3
*   
* *   
* * * * * * *   
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Please enter file name
D:program_workspaceCharCountsrcbc.txt
true
content::HI HELLO
MAX value::2
* *   
* * * * *   
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

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