Question

In: Computer Science

using Dr java Objective: Write a program that takes a phrase and then counts the number...

using Dr java

Objective:

Write a program that takes a phrase and then counts the number of vowels (case does not matter) in the phrase. It then should display all of the vowels in sorted ascending order according to their count. Only consider {AEIOU} as the vowels.

Hint:

It may be a good idea to keep track and sort two arrays:

Has the vowels in alphabetic order
Has the number of said vowels

Whenever one would swap then it swaps the values in both arrays.

Example Dialog:

Welcome to the vowel counter and sorter!

Enter a phrase!

aaaaaeeeeiiioou

The vowels and their count

u 1

o 2

i 3

e 4

a 5

Another Example

Welcome to the vowel counter and sorter!

Enter a phrase!

Facetious is the only word in English that contains all vowels in order!

The vowels and their count

u 1

a 4

e 5

i 6

o 6

Yet another Example

Welcome to the vowel counter and sorter!

Enter a phrase!

I have a pen, I have an apple. Uh! Apple Pen I have a pen, I have pineapple. Uh! Pineapple Pen Apple pen... Pineapple pen... uh! Pen Pineapple Apple Pen! Pen Pineapple Apple Pen

The vowels and their count

o 0

u 3

i 9

a 17

e 29

Solutions

Expert Solution

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication3;

import java.util.Arrays;
import java.util.Scanner;

/**
*
* @author Namburi Ramesh
*/
public class VowelCounter {
public static void main(String [] args){
int[] count=new int[5]; //to store the count of vowels
char[] vowels=new char[]{'a','e','i','o','u'}; // to store the associated vowels
String phrase; //to store the entered phrase
Scanner scanner = new Scanner(System.in); //to take the input
System.out.println("Welcome to the vowel counter and sorter!\n" +"Enter a phrase!");
phrase=scanner.nextLine(); // stores input string into phrase variable
/*
Run a loop to count the number of vowels in the entered string and store it in the count array
*/
for(int j=0;j<phrase.length();j++){
if(phrase.charAt(j) == 'a' || phrase.charAt(j)=='A'){
count[0]++;
}else if(phrase.charAt(j)=='e' || phrase.charAt(j)=='E'){
count[1]++;
}else if(phrase.charAt(j)=='i' || phrase.charAt(j)=='I'){
count[2]++;
}else if(phrase.charAt(j)=='o' || phrase.charAt(j)=='O'){
count[3]++;
}else if(phrase.charAt(j)=='u' || phrase.charAt(j)=='U'){
count[4]++;
}
}
  
System.out.println("The vowels and their count");
int maximum=count[findmax(count)]; // find the maximum element in the count array
/*
loop to print the number of vowels in ascending order
*/
for(int j=0;j<5;j++){
int nextmin=findmin(count); // find the position of minimum element in array count
System.out.println(vowels[nextmin] +" "+ count[nextmin]); //print the associated vowel and count
count[nextmin]=maximum+1; // set the minimum element to more than maximum so that it wont be returned when we next call the finfmin method
}
}
//Below function returns the position of minimum element in the array a
public static int findmin(int[] a){
  
int minposition=0;
for(int i=1;i<a.length;i++){
if(a[minposition]>a[i]){
minposition=i;
}
}
return minposition;
  
}
//Below function returns the position of maximum element in the array a
public static int findmax(int[] a){
  
int maxposition=0;
for(int i=1;i<a.length;i++){
if(a[maxposition]<a[i]){
maxposition=i;
}
}
return maxposition;
  
}
}


Related Solutions

Write a C program that counts the number of repeated characters in a phrase entered by...
Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them. If none of the characters are repeated, then print “No character is repeated” For example: If the phrase is “full proof” then the output will be Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10. ###Note: the output should print exactly as it is stated in the example if...
Write a Java program that takes in a string and a number and prints back the...
Write a Java program that takes in a string and a number and prints back the string from the number repeatedly until the first character... for example Pasadena and 4 will print PasaPasPaP. Ask the user for the string and a number Print back the string from the number repeatedly until the first character For both programs please utilize: methods arrays loops Turn in screenshots
Java - Firstly, write a method, using the following header, that counts the number of prime...
Java - Firstly, write a method, using the following header, that counts the number of prime numbers between from and to (inclusive). public static int countPrimes(int from, int to) For example, countPrimes(11,19) returns 4 since 11, 13, 17 and 19 are primes. You can assume that someone has already written the following function to determine is an integer is prime or not. public static boolean isPrime(int i) // returns true if i is a prime number Secondly, write a program...
Java - Firstly, write a method, using the following header, that counts the number of prime...
Java - Firstly, write a method, using the following header, that counts the number of prime numbers between from and to (inclusive) . public static int countPrimes(int from, int to ) For example, countPrimes(11,19) returns 4 since 11, 13, 17 and 19 are primes. You can assume that someone has already written the following function to determine is an integer is prime or not. public static boolean isPrime(int i) // returns true if i is a prime number Secondly, write...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of miles, and the class of journey (1,2, or 3, for first, second, and third class respectively), for a train journey. The program should then calculate and display the fare of journey based on the following criteria: Note: Use Switch...case and if...else construct First (1) Class Second (1) Class Third (3) Class First 100 mile $ 3 per mile $ 2 per mile $ 1.50...
in this java program, list sentence(s) with the max. number of occurences of the phrase “but...
in this java program, list sentence(s) with the max. number of occurences of the phrase “but the” in the entire file and also list the corresponding frequency. -cant use hashmaps -instances like “but there” still counts as an occurence -input file will be a couple paragraphs long.
write a java program that takes three numbers from the user and print the greatest number...
write a java program that takes three numbers from the user and print the greatest number (using if-statement). sample output: input the first number:35 input the second number:28 input the third number:87 the greatest number:87
***Using Java Using the switch/case construct, write a program that takes a character from the user...
***Using Java Using the switch/case construct, write a program that takes a character from the user and classifies it as a number (‘1’,’2’, ‘3’, …), a letter from the alphabet (‘A’, ‘a’, ‘B’, ‘b’, …, ‘Z’, ‘z’), an arithmetic operator (‘+’, ‘-‘, ‘/’, ‘*’, ‘%’), a comparison operator (‘<’, ‘>’, ‘=’), punctuation (‘!’, ‘?’, ‘,’, ‘,’, ‘:’, ‘;’), and all other characters as a Special Character, and informs the user of the same. If the user entered the character A,...
Using Java, write a program that takes in two integers from the keyboard called m and...
Using Java, write a program that takes in two integers from the keyboard called m and n, where m > n. Your program should print the first m natural numbers (m..1) downwards in n rows.
Add a new function that takes a phrase as an argument and counts each unique word...
Add a new function that takes a phrase as an argument and counts each unique word in the phrase. The function should return a list of lists, where each sub-list is a unique [word, count] pair. Hint: A well-written list comprehension can solve this in a single line of code, but this approach is not required.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT