Design a program named CharDistribution.java, within this program, implements me
ID: 3590708 • 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
Explanation / Answer
import java.io.BufferedReader;
public int[] charCounting(String str){
int[] counts = new int[(int) Character.MAX_VALUE];
// If you are certain you will only have ASCII characters, use `new int[256]` instead
for (int i = 0; i < input.length(); i++) {
char charAt = input.charAt(i);
counts[(int) charAt]++;
}
}
{
//to count characters in a string
System.out.println(“Enter the string”);
int c=charCounting(input);
System.out.println(Arrays.toString(c));
// to count characters in a file
int[] f=charCounting("filename.txt");
//print histogram of astrisks
print(arrays.toString(f));
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.