Computer Applications

Write a program by using a class with the following specifications:

Class name: Vowel

Data MembersPurpose
String sTo store the string
int cTo count vowels
Member MethodsPurpose
void getstr()to accept a string
void getvowel()to count the number of vowels
void display()to print the number of vowels

Java

Java Classes

17 Likes

Answer

import java.util.Scanner;

public class Vowel
{
    private String s;
    private int c;
    
    public void getstr() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the string: ");
        s = in.nextLine();
    }
    
    public void getvowel() {
        String temp = s.toUpperCase();
        c = 0;
        for (int i = 0; i < temp.length(); i++) {
            char ch = temp.charAt(i);
            if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O'
             || ch == 'U')
                c++;
        }
    }
    
    public void display() {
        System.out.println("No. of Vowels = " + c);
    }
    
    public static void main(String args[]) {
        Vowel obj = new Vowel();
        obj.getstr();
        obj.getvowel();
        obj.display();
    }
}

Variable Description Table

Program Explanation

Output

Answered By

9 Likes


Related Questions