Question

In: Computer Science

JAVA PLEASE!! Write a value-returning method, isVowel, that returns the value true if a given character...

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.)

Solutions

Expert Solution

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.


Related Solutions

Please write a Java method contains that checks whether the second given character array is contained...
Please write a Java method contains that checks whether the second given character array is contained in the first given character array. We require that both of the arrays are partially filled.
Use a switch statement to write a function that returns TRUE if a character is a...
Use a switch statement to write a function that returns TRUE if a character is a consonant and returns FALSE otherwise.
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
Write a Java method that computes the future investment value at a given interest rate for...
Write a Java method that computes the future investment value at a given interest rate for a specified number of years. Your method should return the future investment value after calculation. The future investment is determined using the formula below:     futureInvestmentValue = investmentAmount * (1+monthlyInterestRate)numberOfYears*12 You can use the following method header: public static double futureInvestmentValue (double investmentAmount, double monthlyInterestRate, int years);
Java please. Write a static method sqrt()that takes a double argument and returns the square root...
Java please. Write a static method sqrt()that takes a double argument and returns the square root of that number using newton's method to compute result.
Code with Java and no imports, Method sandwiched returns true if num is in the element...
Code with Java and no imports, Method sandwiched returns true if num is in the element before and after an element that is not equal to num sandwiched([4,5,4,6,7,3], 4) returns true sandwiched([2,1,2], 2) returns true sandwiched([3,3,3], 3) returns false sandwiched([4,5,6,4], 4) returns false sandwiched([1,1,2,3,1,4,1], 1) returns true @param nums Integer ArrayList @param num integer @return true if a single number is between elements equal to num */ public static boolean sandwiched(ArrayList nums, int num) { return false; }//end sandwiched
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
java program: Please save the program with the name ‘Palindrome.java’ Write a method which return true...
java program: Please save the program with the name ‘Palindrome.java’ Write a method which return true if the string parameter is a palindrome. This is the output example: Lets check if a string is a palindrom Please give me a string: abc No, its not a Palindrom Lets check if a string is a palindrom Please give me a string: noon Yes, its a Palindrom
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
Write a method in JAVA, called binarySearch, that returns the index of the key element if...
Write a method in JAVA, called binarySearch, that returns the index of the key element if found in the array, otherwise it will return the value of minus(insertion point +1). In main, use an initializer list create an array of ints called nums holding the following values: 1, 4, 13, 43, -25, 17, 22, -37, 29. Test your binarySearch method on nums with a key value number in it (e.g. 4) and one that is not (e.g. 100). Ex. Given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT