In: Computer Science
In Java please:
3.39 Lab 6e: CharacterOps
Write a program that prompts the user to enter their first name.
Say hello to the person. Then display whether their name begins
with a vowel or consonant.
If it is neither, perhaps a special character or a number digit,
then print neither a vowel nor a consonant.
Example 1:
Enter your first name: Barbara Hello, Barbara! The first letter of your name, 'B', is a consonant.
Example 2:
Enter your first name: Elizabeth Hello, Elizabeth! The first letter of your name, 'E', is a vowel.
Example 3:
Enter your first name: !Sam Hello, !Sam! The first letter of your name, '!', is a is neither a vowel nor a consonant.
Java's Character class provides a method isAlphabetic() which may assist your coding effort.
Suppose we have char ch = 'm'; then Character.isAlphabetic(ch)
would return true.
Likewise, Character.isAlphabetic('&') would return false.
import java.util.Scanner; public class CharacterOps { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter your first name: "); String name = in.nextLine(); char ch = name.charAt(0); System.out.println("Hello, " + name + "!"); if (Character.isAlphabetic(ch)) { ch = Character.toLowerCase(ch); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { System.out.println("The first letter of your name, '" + name.charAt(0) + "', is a vowel."); } else { System.out.println("The first letter of your name, '" + name.charAt(0) + "', is a consonant."); } } else { System.out.println("The first letter of your name, '" + ch + "', is a is neither a vowel nor a consonant."); } } }