4. Vowels and Consonants Write a class with a constructor that accepts a String
ID: 3529060 • Letter: 4
Question
4. Vowels and Consonants Write a class with a constructor that accepts a String object as its argument. The class should have a method that returns the number of vowels in the string, and another method that returns the number of consonants in the string. Demonstrate the class in a program that performs the following steps: 1. The user is asked to enter a string. 2. The program displays the following menu: a. Count the number of vowels in the string b. Count the number of consonants in the string c. Count both the vowels and consonants in the string d. Enter another string e. Exit the program 3. The program performs the operation selected by the user and repeats until the user selects e, to exit the program.Explanation / Answer
import java.util.StringTokenizer; /** FileName: VowelsAndConsonants.java The VowelsAndConsonants class accepts a String object as its argument. The class should have a method that returns the number of vowels in the string, and another method that returns the number of consonants in the string. */ public class VowelsAndConsonants { private String consonants = "bcdfghjklmnpqrstvwxz" + "BCDFGHJKLMNPQRSTVWXZ"; private String string; int i = 0; int a = 0; int vowels = 0; int con = 0; /** Constructor @param in The input to analyze. */ public VowelsAndConsonants(String str) { string = str; } /** The getVowels method returns the number of vowels in the string. @return The number of vowels in the string. */ public int getVowels(String str) { a = str.length(); for (i=0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.