You are on page 1of 6

Solved Programs:

1. A class Data has been defined to perform string operations. Some of the
members of the class are given below:

Class name : Data

Data member/instance variable:

str : string variable to store string or sentence

Member functions/methods:

Data( ) : constructor to initialize null to str

void accept() : to read a sentence from the user in str

void computePrint() : to print the original sentence, total number of

words in the sentence and also print each word of

the sentence in different lines along with number of

characters in each word.

Print the output in two columns.

Specify the class Data, giving the details of the constructor( ), void accept( )

and void computePrint( ). Define the main( ) function to create an object and

call the functions accordingly to enable the task.


import java.util.*;
class Data
{
String str;
Data()
{
str = "";
}
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string or sentence:");
str=sc.nextLine();
}
void computePrint()
{
System.out.println("The original sentence: “+str);
int c = 0;
int len;
str = str + " ";
String w = "";
System.out.println("Words \t Number of characters");
for(int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
if(ch != ' ')
w = w + ch;
else
{
len = w.length();
System.out.println(w+"\t"+len);
c++;
w = "";
}
}
System.out.println("Total number of words: "+c);
}
public static void main(String arg[])
{
Data ob = new Data();
ob.accept();
ob.computePrint();
}
}
2. Design a class WordWise to separate words from a sentence and find the
frequency of the vowels in each word.

Some of the members of the class are given below:

Class name : WordWise

Data members/instance variables:

str : to store a sentence

Member functions/methods:

WordWise( ) : default constructor

void readsent( ) : to accept a sentence

int freq_vowel(String w) : returns the frequency of vowels in the

parameterized string w

void arrange( ) : displays each word of the sentence in a separate line

along with the frequency of vowels for each word

by invoking the function freq_vowel( )

Define the class WordWise giving details of the constructor( ), void

readsent(), int freq_vowel(String) and void arrange(). Define the main( )

function to create an object and call the functions accordingly to enable the

task.
import java.util.*;

class WordWise

String str;

WordWise()

{
str = "";
}
void readsent()

Scanner sc=new Scanner(System.in);

System.out.println("Enter a string or sentence:");

str=sc.nextLine();

int freq_vowel(String w)

int c = 0;

w = w.toUpperCase();

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

char ch = w.charAt(i);

if(ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')

c++;

}
return c;
}
void arrange()

System.out.println("The original sentence: ”+str);

str = str + " ";

String w = "";

System.out.println("Words \t Frequency of vowels");

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

char ch = str.charAt(i);

if(ch != ' ')

w = w + ch;

else

int v = freq_vowel(w);

System.out.println(w+"\t"+v);

w = "";

}
}
}
public static void main(String arg[])

WordWise ob = new WordWise();

ob.readsent();

ob.arrange();

}
}

You might also like