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: 3821995 • 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.

The user is asked to enter a string

The program displays the following menu

Count the number of vowels in the string

Count the number of consonants in the string

Count both the vowels and consonants in the string

Enter another string

Exit the program

The program performs the operation selected by the user and repeats until the user selects e, to exit the program

This is what I have so far:

import java.util.Scanner;
public class vowelsConsonants {
   private static String sentence;
   public vowelsConsonants(String str) {
       sentence=str;
   }
   public static int vowelCount()
   {
       int count=0;
       for (int i=0;i<sentence.length(); i++)
       if (sentence.charAt(i)=='a'||
           sentence.charAt(i)=='A'||sentence.charAt(i)=='e'
           ||sentence.charAt(i)=='E'||sentence.charAt(i)=='i'
           ||sentence.charAt(i)=='I'||sentence.charAt(i)=='o'
           ||sentence.charAt(i)=='O'||sentence.charAt(i)=='u'
                                   ||sentence.charAt(i)=='U')
       {
           count++;
       }
       return count;
   }
   public static int consonantCount() {
       int count=0;
       for (int i=0;i<sentence.length(); i++)
       if (sentence.charAt(i)!='a'&&
           sentence.charAt(i)!='A'&&sentence.charAt(i)!='e'
           &&sentence.charAt(i)!='E'&&sentence.charAt(i)!='i'
           &&sentence.charAt(i)!='I'&&sentence.charAt(i)!='o'
           &&sentence.charAt(i)!='O'&&sentence.charAt(i)!='u'
                                   &&sentence.charAt(i)!='U')
       {
           count++;
       }
       return count;
   }
}
class vowelsTest
{
   public static void main (String[]args)
   {
       char ch;
       Scanner keyboard= new Scanner(System.in);
       System.out.print("Enter String: ");
       do
       {
           System.out.println("Menu:");
           System.out.println("a) Count Vowels");
           System.out.println("b) Count Consonants");
           System.out.println("c) Count Vowels and Consonants");
           System.out.println("d)Enter Another String");
           System.out.println("e) Exit Program");
           System.out.println("Enter Choice");
           String choice = keyboard.nextLine();
           ch=choice.charAt(0);
           switch(ch)
           {
           case 'a':
               System.out.println("Number of Vowels: "+vowelsConsonants.vowelCount());
               break;
           case 'b':
               System.out.println("Number of Consonants: "+vowelsConsonants.consonantCount());
               break;
           case 'c':
               System.out.println("Number of Vowels: "+vowelsConsonants.vowelCount());
               System.out.println("Number of Consonants: "+vowelsConsonants.consonantCount());
               break;
           case 'd':
               System.out.print("Enter String: ");
               choice=keyboard.nextLine();
               break;
           case 'e':
               System.exit(0);
               break;  
              
           }
       }while (ch=='d');
       System.exit(0);
   }
}

Explanation / Answer

Hi, Please find my implementation.

Please let me know in case of any issue.

public class VowelsConsonants {

   private static String sentence;

   public VowelsConsonants(String str) {

       sentence=str;

   }

   private boolean isVowel(char c){

       c = Character.toLowerCase(c);

       if (c=='a'||c=='e'||c=='i' || c=='o' || c=='u')

           return true;

       return false;

   }

   public int vowelCount()

   {

       int count=0;

       for (int i=0;i<sentence.length(); i++)

           if (isVowel(sentence.charAt(i)))

           {

               count++;

           }

       return count;

   }

   public int consonantCount() {

       int count=0;

       for (int i=0;i<sentence.length(); i++)

           if (!isVowel(sentence.charAt(i)))

           {

               count++;

           }

       return count;

   }

}

##############################################

import java.util.Scanner;

public class VowelsTest

{

   public static void main (String[]args)

   {

       char ch;

       Scanner keyboard= new Scanner(System.in);

       System.out.print("Enter String: ");

       String str = keyboard.nextLine();

       VowelsConsonants vowelsConsonants = new VowelsConsonants(str);

       do

       {

           System.out.println("Menu:");

           System.out.println("a) Count Vowels");

           System.out.println("b) Count Consonants");

           System.out.println("c) Count Vowels and Consonants");

           System.out.println("d)Enter Another String");

           System.out.println("e) Exit Program");

           System.out.println("Enter Choice");

           String choice = keyboard.nextLine();

           ch=choice.charAt(0);

           switch(ch)

           {

           case 'a':

               System.out.println("Number of Vowels: "+vowelsConsonants.vowelCount());

               break;

           case 'b':

               System.out.println("Number of Consonants: "+vowelsConsonants.consonantCount());

               break;

           case 'c':

               System.out.println("Number of Vowels: "+vowelsConsonants.vowelCount());

               System.out.println("Number of Consonants: "+vowelsConsonants.consonantCount());

               break;

           case 'd':

               System.out.print("Enter String: ");

               str=keyboard.nextLine();

               vowelsConsonants = new VowelsConsonants(str);

               break;

           case 'e':

               System.exit(0);

               break;

           default:

               System.out.println("Wrong option!!!");

           }

       }while (ch!='e');

       System.exit(0);

   }

}

/*

Sample run:

Enter String: Pravesh Kumar

Menu:

a) Count Vowels

b) Count Consonants

c) Count Vowels and Consonants

d)Enter Another String

e) Exit Program

Enter Choice

a

Number of Vowels: 4

Menu:

a) Count Vowels

b) Count Consonants

c) Count Vowels and Consonants

d)Enter Another String

e) Exit Program

Enter Choice

b

Number of Consonants: 9

Menu:

a) Count Vowels

b) Count Consonants

c) Count Vowels and Consonants

d)Enter Another String

e) Exit Program

Enter Choice

d

Enter String: Apple

Menu:

a) Count Vowels

b) Count Consonants

c) Count Vowels and Consonants

d)Enter Another String

e) Exit Program

Enter Choice

a

Number of Vowels: 2

Menu:

a) Count Vowels

b) Count Consonants

c) Count Vowels and Consonants

d)Enter Another String

e) Exit Program

Enter Choice

e

*/

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