JAVA PROJECT Write a class with a constructor that accepts a String object as it
ID: 665805 • 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:
a. The user is asked to enter a string .
b. 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
c. The program performs the operation selected by the user and repeats until
the user selects 5 to exit the program .
SAMPLE OUTPUT(do as wriiten):
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
package vowels.and.consonants; import java.util.Scanner; import javax.swing.JOptionPane; public class VowelsAndConsonants { public static void main(String[] args) { Counter c = new Counter(getString()); char choice; do{ choice = getChoice(); if(choice=='a'){ System.out.println("Vowels: "+c.getVowels()); } if(choice=='b'){ System.out.println("Consonants: "+c.getConsonants()); } if(choice=='c'){ System.out.println("Vowels: "+c.getVowels()); System.out.println("Consonants: "+c.getConsonants()); } if(choice=='d'){ c = new Counter(getString()); } if(choice=='e'){ System.out.println("Exiting program.."); System.exit(0); } }while(choice!='e'); } public static char getChoice(){ char choice; Scanner s = new Scanner(System.in); System.out.println("Enter a choice: "); do{ System.out.println("a. Count the number of vowels in the string"); System.out.println("b. Count the number of consonants in the string"); System.out.println("c. Count both the vowels and consonants in the string."); System.out.println("d. Enter another string"); System.out.println("e. Exit the program"); choice = s.nextLine().toLowerCase().charAt(0); }while(choice!='a'&&choice!='b'&&choice!='c'&&choice!='d'&&choice!='e'); return choice; } public static String getString(){ String str = JOptionPane.showInputDialog("Enter a string to count."); return str; } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.