| 1. |
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 nameCapitalData member/instance variables :sentto store a sentence freqstores the frequency of words beginning with a capital letterMember functions / methods :Capital( )default constructorvoid input( )to accept the sentenceboolean isCap(String w)checks and returns true if word begins with a capital letter, otherwise returns falsevoid display( )displays the sentence along with the frequency of the words beginning with a capital letterSpecify 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. |
|
Answer» import java. io. * ; class Capital { String sent; int freq; Capital ( ) { sent = " "; freq = 0; } void input ( ) { BufferedReader br = new BufferedReader (new InputStreamReader (System. in)); system. out. println ("Enter one sentence"); sent = br. readLine ( ); String [ ] word = sent. split (" \\ s"); for (int i = 0; i < word. length; i ++) { if (isCap (word [i]) = = true) freq ++; } } boolean isCap (String w) { if (Character. isUpperCase (w. charAt (0)) = = true) return true; else return false; } void display ( ) } System. out. println (sent); System. out. println (" the frequency of the words beginning with a capital letter is "+ freq); } public static void main ( ) { Capital Ob = new Capital ( ); Ob. input ( ); Ob. display ( ); } } |
|