JAVA: Write a class with a constructor that accepts a String object as its argum
ID: 3854910 • Letter: J
Question
JAVA:
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.
OUTPUT like:
Enter·a·string:Supercalifragilisticexpialidocious
Enter·a·number
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:1
Vowels:·16
Enter·a·number
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:2
Consonants:·18
Enter·a·number
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:4
Enter·a·string:A·spoonful·of·sugar·makes·the·medicine·go·down
Enter·a·number
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:3
Vowels:·16
Consonants:·22
Enter·a·number
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:5
Explanation / Answer
CharactersStats.java
import java.util.Scanner;
public class CharactersStats {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a string: ");
String s = scan.nextLine();
CharacterUtil u = new CharacterUtil(s);
System.out
.println("Enter·a·number 1 ·Count the number of vowels in the string 2 Count the number of consonants in the string 3. Count the number of vowels and consonants in the string 4. Enter another string 5. Exit the program ");
int choice = scan.nextInt();
while (choice != 5) {
if (choice == 1) {
System.out.println("Vowels: " + u.getVowelCount());
} else if (choice == 2) {
System.out.println("Consonants: " + u.getConsonantCount());
} else if (choice == 3) {
System.out.println("Vowels: " + u.getVowelCount());
System.out.println("Consonants: " + u.getConsonantCount());
} else if (choice == 4) {
System.out.println("Enter a string: ");
s = scan.nextLine();
u = new CharacterUtil(s);
}
System.out
.println("Enter·a·number 1 ·Count the number of vowels in the string 2 Count the number of consonants in the string 3. Count the number of vowels and consonants in the string 4. Enter another string 5. Exit the program ");
choice = scan.nextInt();
}
}
}
CharacterUtil.java
public class CharacterUtil {
private String s;
public CharacterUtil(String s) {
this.s = s;
}
public int getVowelCount() {
int count = 0;
for(int i=0;i<s.length();i++) {
if(isVowel(s.charAt(i))) {
count++;
}
}
return count;
}
public int getConsonantCount() {
int count = 0;
for(int i=0;i<s.length();i++) {
if(s.charAt(i) != ' ' && !isVowel(s.charAt(i))) {
count++;
}
}
return count;
}
public boolean isVowel(char ch) {
String vowels = "aeiouAEIOU";
if(vowels.contains(ch+"")) {
return true;
}
return false;
}
}
Output:
Enter a string:
Supercalifragilisticexpialidocious
Enter·a·number
1 ·Count the number of vowels in the string
2 Count the number of consonants in the string
3. Count the number of vowels and consonants in the string
4. Enter another string
5. Exit the program
1
Vowels: 16
2
Consonants: 18
3
Vowels: 16
Consonants: 18
5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.