The following Java program prompts the user and reads in a l
The following Java program prompts the user and reads in a line of text, then displays the text. The class name is A9. You should not change the class name.
This assignment involves reading a line of text from the keyboard, and generating a histogram of the vowels that occur in the text. You will edit the program supplied above, adding the code to generate the histogram.
For example, if the user entered
your program would display:
Notice that it is case-insensitive.
If the user entered
your program would display:
You must write the code to generate the histogram in the printHistogram method (not in the main method).
Call printHistogram from the main method, passing the text (entered by the user) as an argument.
You may add more methods if you wish.
Do not use arrays, or any other concepts not yet covered in the course.
You only need to submit your A9.java file.
Start early - there is opportunity for extra points (see below) but it requires more work!! Make sure your program is perfect before attempting the extra credit.
Add an extra method \"printVerticalHistogram\" to the class A9, to print the histogram in the following orientation:
Example 1: Alphabet soup is my favorite soup in the whole world.
Example 2: Tomato TOO.
Call both methods, printHistogram and printVerticalHistogram, from your main, to show both orientations in the output.
Solution
/**
* A simple program that prompts the user
* for a line of text.
*/
import java.util.Scanner;
class A9 {
public static String getPhrase () {
Scanner input = new Scanner(System.in);
System.out.print (\"Enter a phrase: \");
String phrase = input.nextLine();
return phrase;
}
public static void reportPhrase (String phrase) {
System.out.println (phrase);
}
public static void printHistogram (String phrase)
{
int counta,counte,counti,counto,countu;
counta = counte = counti = counto = countu =0;
for(int i=0;i<phrase.length();i++) //count the number of each of vowels
{
if(phrase.charAt(i)==\'a\'||phrase.charAt(i)==\'A\' ) counta++;
else if(phrase.charAt(i)==\'e\'|| phrase.charAt(i)==\'E\') counte++;
else if(phrase.charAt(i)==\'i\'|| phrase.charAt(i)==\'I\') counti++;
else if(phrase.charAt(i)==\'o\'|| phrase.charAt(i)==\'O\') counto++;
else if(phrase.charAt(i)==\'u\'|| phrase.charAt(i)==\'U\') countu++;
}
System.out.print(\"a:\");
for(int i=0;i<counta;i++)System.out.print(\"*\"); //print * for the number of each of vowels
System.out.println();
System.out.print(\"e:\");
for(int i=0;i<counte;i++)System.out.print(\"*\");
System.out.println();
System.out.print(\"i:\");
for(int i=0;i<counti;i++)System.out.print(\"*\");
System.out.println();
System.out.print(\"o:\");
for(int i=0;i<counto;i++)System.out.print(\"*\");
System.out.println();
System.out.print(\"u:\");
for(int i=0;i<countu;i++)System.out.print(\"*\");
}
public static void printVerticalHistogram (String phrase)
{
int counta,counte,counti,counto,countu;
counta = counte = counti = counto = countu =0;
for(int i=0;i<phrase.length();i++) //count the number of each of vowels
{
if(phrase.charAt(i)==\'a\'||phrase.charAt(i)==\'A\' ) counta++;
else if(phrase.charAt(i)==\'e\'|| phrase.charAt(i)==\'E\') counte++;
else if(phrase.charAt(i)==\'i\'|| phrase.charAt(i)==\'I\') counti++;
else if(phrase.charAt(i)==\'o\'|| phrase.charAt(i)==\'O\') counto++;
else if(phrase.charAt(i)==\'u\'|| phrase.charAt(i)==\'U\') countu++;
}
int[] asterisks = {counta,counte,counti,counto,countu};
int rows = asterisks[0];
for (int i = 1; i < asterisks.length; i++) {
if (asterisks[i] > rows)
{
rows = asterisks[i];
}
}
System.out.println(\"\ \ \");
while(rows > 0)
{
for(int i = 0; i < asterisks.length; i++){
if(i == 0){
if(asterisks[i] < rows)
System.out.printf(\" %-7s\",\" \");
else System.out.printf(\" %-7s\",\"*\");
}
else{
if(asterisks[i] < rows) System.out.printf(\"%-7s\",\" \");
else System.out.printf(\"%-7s\",\"*\");
}
}
System.out.println();
rows--;
}
System.out.printf(\"%-7s%-7s%-7s%-7s%-7s\",\"a\",\"e\",\"i\",
\"o\",\"u\");
}
public static void main(String args[]) {
String phrase;
phrase = getPhrase ();
reportPhrase (phrase);
printHistogram(phrase); // Call the method printHistogram here
printVerticalHistogram(phrase);
}
}
output:


