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

Lab 11 Vowel Counter and Sorter write a program that takes a phrase and t encoun

ID: 1766081 • Letter: L

Question

Lab 11 Vowel Counter and Sorter write a program that takes a phrase and t encounts the number of vowels AEIOU) as the vowels. ase does not matter n the phrase. It then should display an of he vowels in sort dasend gon rac ding to the rou to t may be a good idea to keep track and sort two arrays: Has the vowels in alphabetic order 2. Has the number of said vowels Whenever one would swap then it swaps the values in both arrays Welcome to the vowel counter and sorter! Enter a phrase! The vowels and their count u 1 o 2 i 3 Welcome to the vowel counter and sorter! Facetious is the only word in English that contains all vowela in order! The vowels and their count u 1

Explanation / Answer

Answer:

program

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.HashMap;

import java.util.Map.Entry;

import java.util.Scanner;

public class VowelCounter {

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("Welcome to the vowel counter and sorter!");

System.out.println("Enter a phrase");

Scanner sc = new Scanner(System.in);

String str = sc.nextLine();

HashMap<Character,Integer> hm = new HashMap<Character, Integer>();

hm = countVowels(str);

ArrayList<Entry<Character,Integer>> al = new ArrayList<Entry<Character,Integer>>();

for(Entry e : hm.entrySet()){

al.add(e);

}

// to print in acsending order

Collections.sort(al, new SortAscendingOrder());

System.out.println("The vowels and their count");

for(Entry<Character, Integer> e : al){

System.out.println(e.getKey()+ " "+ e.getValue());

}

// to print in Descending order

Collections.sort(al, new SortDescendingOrder());

System.out.println("The vowels and their count");

for(Entry<Character, Integer> e : al){

System.out.println(e.getKey()+ " "+ e.getValue());

}

}

private static HashMap<Character,Integer> countVowels(String str) {

HashMap<Character,Integer> hm = new HashMap<Character,Integer>();

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

if(str.charAt(i)=='a' || str.charAt(i)=='A' || str.charAt(i)=='e' || str.charAt(i)=='E' || str.charAt(i)=='i'

|| str.charAt(i)=='I' || str.charAt(i)=='o' || str.charAt(i)=='O' || str.charAt(i)=='u' || str.charAt(i)=='U'){

if(hm.get(str.charAt(i))==null)

hm.put(str.charAt(i),1); // to capture the count of individual character

else

hm.put(str.charAt(i), hm.get(str.charAt(i))+1); // to increase the count of individual character

}

}

return hm;

}

}

class SortAscendingOrder implements Comparator <Entry<Character, Integer>> {

public int compare(Entry<Character, Integer> obj1, Entry<Character, Integer> obj2){

return obj1.getValue()-obj2.getValue();

}

}

class SortDescendingOrder implements Comparator <Entry<Character, Integer>>{

public int compare(Entry<Character, Integer> obj1, Entry<Character, Integer> obj2){

return obj2.getValue()-obj1.getValue();

}

}