Answer to Question #262388 in Java | JSP | JSF for deee

Question #262388

Create a program with a method that takes a single character and returns true if the character is a vowel otherwise return false.


1
Expert's answer
2021-11-07T14:49:23-0500


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







Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS