icse-promo

Question

A class Capital has been defined to check whether a sentence has words beginning with a capital letter or not.
Some of the members of the class are given below:
Class name:Capital
Data member/instance variable:
sent:to store a sentence
freq :stores the frequency of words beginning with a capital letter

Member functions/methods:
Capital( ):default constructor

void input():to accept the sentence

boolean isCap(String w):checks and returns true if word begins with a capital letter, otherwise returns false

void display ():displays the sentence along with the frequency of the words beginning with a capital letter

Specify the class Capital, giving the details of the constructor( ), void input( ), boolean isCap(String) and void display( ). Define the main( ) function to create an object and call the functions accordingly to enable the task.

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class Capital
{
    String sent;
    int freq;
    Capital()
    {
        sent=""; 
        freq=0;
    }

    void input()
    {
        Scanner sc=new Scanner(System.in); 
        System.out.println("Enter the sentence");
        sent=sc.nextLine();
        sent=sent+" ";
    }

    boolean isCap(String w)
    {
        char ch=w.charAt(0) ;
        if (Character.isUpperCase(ch)==true)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    void display()
    { 
        System.out.println("Original sentence="+sent); 
        String wd="";
        char ch=' ';
        for (int i=0;i< sent.length();i++)
        { 
            ch=sent.charAt(i);
            if(ch==' ')
            {
                if(isCap(wd)==true)
                {
                    freq=freq+1;
                }
                wd="";
            }
            else
            {
                wd=wd+ch;
            }
        }
    

        System.out.println("Freqency of  word with first letter a Capital letter ="+freq) ;
    }


    public static void main()
    {
        Capital ob1=new Capital(); 
        ob1.input();
        ob1.display() ;
    }
}

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *