Design a programming named CharDistribution.java, within this program, implement
ID: 3590605 • Letter: D
Question
Design a programming 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
Below is your code.
CharDistribution.java
public class CharDistribution {
public int[] charCounting(String str) {
int[] counts = new int[26];
for (int i = 0; i < str.length(); ++i) {
if (Character.isAlphabetic(str.charAt(i))) {
counts[Character.toUpperCase(str.charAt(i)) - 'A']++;
}
}
return counts;
}
public int[] charCounting(File file) {
int counts[] = new int[26], temp[];
try {
Scanner fin = new Scanner(file);
String line;
while (fin.hasNextLine()) {
line = fin.nextLine();
temp = charCounting(line);
for (int i = 0; i < counts.length; ++i) {
counts[i] += temp[i];
}
}
fin.close();
} catch (FileNotFoundException e) {
System.out.println(file.getAbsolutePath() + " does not exist");
}
return counts;
}
public void print(int[] c) {
int max = 0;
for (int i = 0; i < c.length; ++i) {
if (c[i] > max) {
max = c[i];
}
}
for (int i = 0; i < max; ++i) {
for (int j = 0; j < c.length; ++j) {
if (c[j] >= max - i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
System.out.println("----------------------------------------------------");
for (char ch = 'A'; ch <= 'Z'; ch++) {
System.out.print(ch);
}
System.out.println();
}
public static void main(String[] args) {
CharDistribution charDistribution = new CharDistribution();
int[] counts = charDistribution.charCounting(new File("data1.txt"));
charDistribution.print(counts);
}
}
data1.txt
The 22-member Greater Richmond Solar Co-op today issued a request for proposals (RFP) from area solar installers.
The group members created the co-op to save money and make going solar easier, while building a network of solar supporters.
Drive Electric RVA, the Richmond Temple, RVA HUB and VA SUN are the co-op sponsors.
Local installers interested in serving the group can download the RFP here and response template here.
Richmond-area residents interested in joining the co-op can sign up at www.vasun.org/richmond.
Joining the co-op is not a commitment to purchase panels. Co-op members will select a single company to complete all of the installations.
They will then have the option to purchase panels individually based on the installer?s group rate. By going solar as a group and
choosing a single installer, participants can save up to 20% off the cost of their system.
Sample Output
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* *
* *
* *
* *
* *
* *
* * *
* * *
* * *
* * *
* * * ***
* * * ***
* * * ***
* * ** ***
* * ** ***
* * ** ***
* * ** ***
* * ** ***
* * ** ***
* * ** ***
* * * ** ***
* * * ** ***
* * * ** ***
* * * ** ***
* * * ** ***
* * * ** ***
* * * * ** ***
* * * * ** ***
* * * * ** ***
* * * * ** ***
* * * * ** ***
* * * * ** ***
* * * * ** ***
* * * * *** ***
* * * * *** ***
* * * * *** ***
* * ** * *** ***
* * ** * *** ***
* * * ** * *** ***
* * * ** * *** ***
* * * ** * *** ***
* * * ** * *** ***
* * * ** * *** ***
* *** ** ***** ***
* *** ** ***** ***
* *** ** ***** ***
* *** *** ***** ***
* *** *** ***** ***
* *** *** ***** ****
* *** *** ***** ****
* *** *** ***** ****
* *** *** ***** ****
* *** *** ***** ****
* *** *** ***** ****
* *** *** ***** *****
* ******* ***** *****
* ******* ***** ******
********* ***** ****** *
********* ***** ****** *
********* ***** ****** *
********* ***** ****** *
********* ***** ****** *
**************** ****** *
*********************** *
----------------------------------------------------
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.