Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class with a constructor that accepts a String object as its argument. T

ID: 3631139 • Letter: W

Question

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; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote