Java Project Write a class with a constructor that accepts a String object as it
ID: 663586 • Letter: J
Question
Java Project
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 .
(Spaces count as neither vowels nor consonants and should be ignored.)
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:
1. Count the number of vowels in the string
2. Count the number of consonants in the string
3. Count both the vowels and consonants in the string
4. Enter another string
5. Exit the program
The user can select options by inputting the appropriate number.
If option 1, 2, or 3 is selected, the result should be printed to the screen.
When displaying the number of vowels, do it in the format:
Vowels: 3
When displaying consonants, do it in the format:
Consonants: 5
When displaying both, do it in the format:
Vowels: 3
Consonants: 5
3. The program performs the operation selected by the user and repeats until
the user selects 5 to exit the program .
Explanation / Answer
Given below is the Java code for the question along with output. Please do rate the answer if it helped. Thank you.
MyString.java
public class MyString {
private String value;
private int vowelCount, consonantCount;
public MyString(String str)
{
value = str;
count();
}
public int getVowelCount()
{
return vowelCount;
}
public int getConsonantCount()
{
return consonantCount;
}
private void count()
{
char[] arr = value.toCharArray();
char ch;
vowelCount = 0;
consonantCount = 0;
for(int i = 0; i < arr.length; i++)
{
ch = Character.toLowerCase(arr[i]);
if(ch == 'a' || ch == 'e' || ch == 'i' || ch =='o' || ch == 'u')
vowelCount++;
else if(ch >= 'a' && ch <= 'z')
consonantCount++;
}
}
}
MyStringDemo.java
import java.util.Scanner;
public class MyStringDemo {
public static void main(String[] args) {
Scanner keybd = new Scanner(System.in);
int choice = 4;
MyString mystr = null;
while(choice != 5)
{
switch(choice)
{
case 1:
System.out.println(" Vowels: " + mystr.getVowelCount());
break;
case 2:
System.out.println(" Consonants: " + mystr.getConsonantCount());
break;
case 3:
System.out.println(" Vowels: " + mystr.getVowelCount());
System.out.println("Consonants: " + mystr.getConsonantCount());
break;
case 4:
System.out.print(" Enter a string: ");
String line = keybd.nextLine();
mystr = new MyString(line);
case 5:
break;
}
System.out.println();
System.out.println("1. Count number of vowels");
System.out.println("2. Count number of consonants");
System.out.println("3. Count both vowels and consonants");
System.out.println("4. Enter another string");
System.out.println("5. Exit the program");
System.out.print("Enter your choice: ");
choice = keybd.nextInt();
keybd.nextLine(); //flush out newline
}
}
}
output
Enter a string: good morning
1. Count number of vowels
2. Count number of consonants
3. Count both vowels and consonants
4. Enter another string
5. Exit the program
Enter your choice: 1
Vowels: 4
1. Count number of vowels
2. Count number of consonants
3. Count both vowels and consonants
4. Enter another string
5. Exit the program
Enter your choice: 2
Consonants: 7
1. Count number of vowels
2. Count number of consonants
3. Count both vowels and consonants
4. Enter another string
5. Exit the program
Enter your choice: 3
Vowels: 4
Consonants: 7
1. Count number of vowels
2. Count number of consonants
3. Count both vowels and consonants
4. Enter another string
5. Exit the program
Enter your choice: 4
Enter a string: hello everybody
1. Count number of vowels
2. Count number of consonants
3. Count both vowels and consonants
4. Enter another string
5. Exit the program
Enter your choice: 1
Vowels: 5
1. Count number of vowels
2. Count number of consonants
3. Count both vowels and consonants
4. Enter another string
5. Exit the program
Enter your choice: 2
Consonants: 9
1. Count number of vowels
2. Count number of consonants
3. Count both vowels and consonants
4. Enter another string
5. Exit the program
Enter your choice: 3
Vowels: 5
Consonants: 9
1. Count number of vowels
2. Count number of consonants
3. Count both vowels and consonants
4. Enter another string
5. Exit the program
Enter your choice: 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.