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

(Intro to Java help?) Write a static method named vowelCount that accepts a Stri

ID: 3699328 • Letter: #

Question

(Intro to Java help?)

Write a static method named vowelCount that accepts a String as a parameter and produces and returns an array of integers representing the counts of each vowel in the String. The array returned by your method should hold 5 elements: the first is the count of As, the second is the count of Es, the third is the count of Is, the fourth is the count of Os, and the fifth is the count of Us. You may assume that the string contains no uppercase letters.

      For example, the call of vowelCount("black banana republic boots") should return an array containing {4, 1, 1, 2, 1}.

Explanation / Answer

PROGRAM

class VowelTest
{
// Create and implement static method vowelCount() and return integer array elements
public static int[] vowelCount(String s)
{
int a[]=new int[5]; // Declare integer Array of size 5
int a1=0,i1=0,o=0,e=0,u=0; // Declare integer variables of vowels

for(int i=0;i<s.length();i++) // create for loop until length of the string
{

switch(s.charAt(i)) // Create switch statement for accessing each character in the givent string
{
case 'a': a1++; a[0]=a1; break; // count 'a' vowel and store into array of 0th location
case 'i': i1++; a[1]=i1; break; // count 'i' vowel and store into array of 1st location
case 'e': e++; a[2]=e; break; // count 'e' vowel and store into array of 2nd location
case 'o': o++; a[3]=o; break; // count 'o' vowel and store into array of 3rd location
case 'u': u++; a[4]=u; break; // count 'u' vowel and store into array of 4th location
}
}
return a; // return array element
}
public static void main(String args[])
{

int a[]=new int[5]; // Declare integer array a of size 5
a=vowelCount("black banana republic boots"); // call vowelCount() method with string parameters
System.out.println(" Vowel Count Array is ");
for(int i=0;i<5;i++) // create for loop until 5 elements
System.out.print(a[i]+" "); // display array elements
}
}

OUTPUT


F:>javac VowelTest.java

F:>java VowelTest

Vowel Count Array is

4 1 1 2 1