Create a program with a method that takes a single character and returns true if the character is a vowel otherwise return false.
SOLUTION TO THE ABOVE QUESTION
SOLUTION CODE
package com.company;
import java.io.*;
import java.util.*;
public class Main {
//lets define a method thats returns true if the character
// passed to it is a vowel otherwise false
public static boolean character_is_a_vowel(char c)
{
Set<Character> vowels = new LinkedHashSet<Character>();
//add the five vowels to the set
vowels.add('a');
vowels.add('e');
vowels.add('i');
vowels.add('o');
vowels.add('u');
//check if the cahracter passed is a vowel
return vowels.contains(c);
}
public static void main(String[] args) {
//Lets prompt the user to enter a character and call our method
Scanner sc = new Scanner(System.in);
System.out.print("Enter a character: ");
String my_String_char = sc.next();
char c=my_String_char.charAt(0);
//Now call our method
if(character_is_a_vowel(c)==true)
{
System.out.println("The character '"+c+"' is a vowel");
}
else
{
System.out.println("The character '"+c+"' is not a vowel");
}
}
}
SAMPLE PROGRAM OUTPUT
Comments
Leave a comment