Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
6.3k views
in Computer by (49.9k points)
closed by

Input a sentence from the user and count the number of times, the words “an” and “and” are present in the sentence. Design a class Frequency using the description given below: 

Class name: Frequency 

Data members/variables: 

text: stores the sentence 

countand: to store the frequency of the word “and” 

countan: to store the frequency of the word “an” 

len: stores the length of the string Member functions/methods:

Frequency(): constructor to initialize the instance variables 

void accept(String n): to assign n to text, where the value of the parameter n should be in lower case. 

void checkandfreq(): to count the frequency of “and” 

void checkanfreq(): to count the frequency of “an” 

void display(): to display the number of “and” and “an” with appropriate messages. 

Specify the class Frequency giving details of the constructor(), void accepts(String), void checkandfreq(), 

void checkanfreq() and void display(). Also, define the main() function to create an object and call methods accordingly to enable the task.

1 Answer

+1 vote
by (49.0k points)
selected by
 
Best answer

Class frequency 

String text; 

Public int countand; 

Public int countan: 

Public int len; Frequency() 

{ text = "countand = 0; 

countan = 0: len = 0; 

void accept(String n) 

n = text:

void check and freq() 

String S = " "; 

countand = 0 ; 

len = n. length(); 

for (int i = 0; i < len; i++) 

char b = n. charAt(i); 

if(b = = ' ') 

if (S = = "and'') 

{

countand = countand + 1; 

S = " "; 

else S = S+b; 

void checkanfreq() 

{ String S1 = " "; 

countan = 0; 

len = n. length ( ) 

for (int i = 0; i < n; i++) 

{ char b = n.charAt(i); 

if (b = = ' '); 

{ if (S1 == "an") 

countan = countan + 1; 

S1 = " "; 

else 

S1 = S1 +b; 

}

void display() 

System.out pri nl n(' 'Frequency of'and' in the sentence is'' + countand); 

System.out.println(''Frequency of 'an' in the sentence is'' + countan); 

}

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...