135k views
5 votes
Write a program named CountVowelsModularized that passes a string to a method that returns the number of vowels in the string.

Note: For testing purposes, Y will not be counted as a vowel.

2 Answers

4 votes

Final answer:

The CountVowelsModularized program is a Java application that counts the number of vowels in a string via a dedicated method. The program treats 'a', 'e', 'i', 'o', and 'u' as vowels and does not count 'y'. The solution demonstrates a modular design in which functionality could be easily modified or expanded.

Step-by-step explanation:

The question asks to write a program named CountVowelsModularized which includes a method that counts the number of vowels in a provided string. In this case, the vowels to be counted are a, e, i, o, and u. The letter y will not be considered a vowel for this particular task. Below is an example code written in Java that demonstrates how you could implement such a program:

public class CountVowelsModularized {

public static void main(String[] args) {
String inputString = "Example string";
System.out.println("Number of vowels: " + countVowels(inputString));
}

public static int countVowels(String str) {
int vowelCount = 0;
for(int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
vowelCount++;
}
}
return vowelCount;
}
}

This modularized approach provides a clear path to expand or modify the program, should the definitions of vowels change or additional functionality be required.

User Zuallauz
by
5.1k points
7 votes

Answer:

import java.util.Scanner;

public class CountVowelsModularized {

public static void main(String[] args) {

Scanner in = new Scanner (System.in);

//Prompt user to enter a string, receive it and assign to a variable

System.out.println("Enter a string Value");

String word =in.nextLine();

//Calling the method numVowels and passing the string

System.out.println("The Number of vowels are " +numVowels(word.toLowerCase()));

}

}

The method definition and complete program is given in the explanation section

Step-by-step explanation:

import java.util.Scanner;

public class CountVowelsModularized {

public static void main(String[] args) {

Scanner in = new Scanner (System.in);

System.out.println("Enter a string Value");

String word =in.nextLine();

System.out.println("The Number of vowels are " +numVowels(word.toLowerCase()));

}

public static int numVowels(String string) {

int counter = 0;

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

if (string.charAt(i) == 'a' || string.charAt(i) == 'e' || string.charAt(i) == 'i'

|| string.charAt(i) == 'o' || string.charAt(i) == 'u') {

counter++;

}

}

return counter;

}

}

User Geekoder
by
4.6k points