In: Computer Science
JAVA PLEASE!! Write a value-returning method, isVowel, that returns the value true if a given character is a vowel, and otherwise returns false.
Also write a program to test your method.
2) Write a program that prompts the user to input a sequence of characters and outputs the number of vowels. (Use the method isVowel written in Programming Exercise 1.)
Dear Learner,
Here is the code for the solution,
import java.io.*;
import java.util.*;
public class Main
{ //the following function is to check if a single character is a
vowel or not
public static boolean isVowel(char ch) {
return
ch=='A'||ch=='a'||ch=='E'||ch=='e'||ch=='I'||ch=='i'||ch=='O'||ch=='o'||ch=='U'||ch=='u';
}
//now the second part begins which is to count the number of
vowels
public static int countVowel(char [] s){
int count = 0; //initializing the final count as 0
for (int i=0;i<s.length;i++) //loop to traverse through the
characters
if (isVowel(s[i])) //calling the function isVowel function with the
current character of array
count++;
return count;
}
public static void main(String[] args) {
boolean result = isVowel('a');
//calling the function and storingthe bool value in a
variable
System.out.println(result);
//printing the result returned
Scanner sc=new Scanner(System.in);
//initializing a Scanner object to take user input
System.out.println("Please enter elements :- ");
char[] array=sc.next().toCharArray(); //taking user input
int count = countVowel(array); //calling the countVowel function
with array as argument
System.out.println(count); //printing the number of vowels
}
}
SCREENSHOT OF THE CODE:-
CORRESPONDING OUTPUT:-
I hope I have answered the question the way you were expecting. If you still have any doubts or want any other explanation, feel free to ask us in the comment section.
Please leave a like if this was helpful.
Thanks
Happy Studying.