Java Class Design for Date and Word Manipulation
Java Class Design for Date and Word Manipulation
1 QUESTION 7
[10] 2020
Design a class Convert to find the date and the month from a given day number for a particular year.
Example : If day number is 64 and the year is 2020, then the corresponding data would be:
March 4, 2020i.e. (31 + 29 + 4 = 64)
The details of the class are given below:
Class name Convert
Data members/instance variables :
n : integer to store day number.
d : integer to store the day of the month (date).
m : integer to store the month.
y : integer to store the year.
Member functions/methods :
Convert( ) : constructor to initialize the data members with legal initial
values.
void accept( ) : to accept the day number and the year from the user.
void day_to_date( ) : convert the day number to its corresponding date for a
particular year and stores the date in ‘d’ and the month in ‘m’.
void display( ) : displays the month name, date and year.
Specify the class Convert giving the details of constructor and methods void accept( ), void day_to_date( ) and
void display( ). Define a main( ) function to create an object and call the functions accordingly to enable the task.
ANSWER :
// The following program is successfully compiled and executed
import [Link].*;
import [Link].*;
class Convert
{
int n, d, m, y ; //data members required in the class
Convert( ) //this is default constructor
{
n = 0; d = 0; m = 0; y = 0; //initializing data members of class
}//end of the default constructor
void accept( ) // function to accept data
{
Scanner br = new Scanner( [Link] );
[Link]("Input day number : ");
n = br . nextInt( );
[Link]("Input year : ");
y = br . nextInt( );
}//end of the function accept ( )
void day_to_date( ) // function to convert day number to the date
{
int c = 0; // this variable will be used as counter
m = 1; //assign month number to 1
int days_of_month[ ] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // array of days of
month
if( y % 4 == 0 ) // check for leap year
days_of_month[ 2 ] = 29;
else
days_of_month[ 2 ] = 28;
while( c < n ) //finding actual date using input number of days ‘n’
{
c++;
d++; // increase date
if( d > days_of_month[ m ] ) //check if the current day is more than number of days of current month
{
ISC String Questions 42
Specify the class Capital giving details of the constructor( ), and functions void input( ), boolean isCap(String
) and void display( ). Define a main( ) function to create an object and call the methods accordingly to enable
the task.
ANSWER:
// The following program is successfully compiled and executed
import [Link].*;
import [Link].*;
class Capital
{
String sent; int freq; // declaration of data members
Capital( ) // this is default or non-parameterized constructor
{
sent = ""; freq = 0; // initialize data members
}//end of the non-parameterized constructor
void input( ) // function definition to input a sentence
{
Scanner br = new Scanner( [Link] );
[Link]("Enter a sentence = " );
sent = br . nextLine( ); // scanner function to accept a sentence and stores in variable ‘sent’
}//end of the function input( )
boolean isCap( String w ) // function to check and return true if first letter is capital else false
{
char ch = [Link]( 0 ); // extracting the character at 0th index from string ‘w’
if( ch >= 'A' && ch <= 'Z' ) // check for capital letter
return true;
else
return false;
}//end of the function isChar( )
void display( ) // function to perform extracting word, check the first letter and count frequency
{
[Link]("The original sentence = " + sent ); // print the input sentence
StringTokenizer str = new StringTokenizer( sent ); // making tokenizer object ‘str’ to extract words
while( str . hasMoreTokens( ) ) // this loop runs till the tokens or words are present in the object ‘str’
{
String word = str . nextToken( ); // extract a token or word from tokenizer object ‘str’
boolean res = isCap( word ); // calling function by passing value in ‘word’ to function argument ‘w’
if ( res == true ) // if res=true, it means first letter of the word is capital
freq ++; // increase value of variable ‘freq’ by 1 if first letter is capital
}//end while loop
[Link]("The frequency of words starting with capital letter are = " + freq ); // print frequency
}//end of the function display( )
public static void main( String args[ ] ) //main function starts
{
Capital obj = new Capital( ); // making object ‘obj’ of class
obj . input( ); //calling function to input a sentence
obj . display( ); //calling function to print results
}//end of the main( ) function
}//end of the class
2018
4
QUESTION 9. [10]
A class SwapSort has been defined to perform string related operations on a word input.
Some of the members of the class are as follows :
Class name SwapSort
Data members/instance variables:
wrd : stores a word.
len : stores the length of the word.
swapwrd : to store the swapped word.
ISC String Questions 45
sortwrd : to store the sorted word.
Member functions/methods:
SwapSort( ) : default constructor to initialize data members with legal
initial values.
void readword( ) : accepts a word in UPPER CASE.
void swapchar( ) : to interchange/swap the first and last characters of the
word in ‘wrd’ and stores the new word in ‘swapwrd’.
void sortword( ) : sorts the characters of the original word in alphabetical
order and stores it in ‘sortwrd’.
void display( ) : displays the original word, swapped word and the sorted
word.
Specify the class SwapSort giving details of the constructor( ), functions void readword( ), void swapchar( ),
void sortword( ) and void display( ). Define a main( ) function to create an object and call the methods
accordingly to enable the task.
ANSWER:
// The following program is successfully compiled and executed
import [Link].*;
import [Link].*;
class SwapSort
{
String wrd, swapwrd, sortwrd; int len; // declaration of data members
SwapSort( ) // this is default or non-parameterized constructor
{
wrd = ""; swapwrd = ""; sortwrd = ""; len = 0; // initialize data members
}//end of the non-parameterized constructor
void readword( ) throws IOException // function to input a word in lowercase
{
Scanner br = new Scanner( [Link] );
[Link]("Enter a word in upper case or capital letters = " );
wrd = br . next( ); // the function next( ) will accept only one word and ignores others
len = [Link]( ); // find length of the word, if not here, then use in both the functions
}//end of the function readword( )
void swapchar( ) // function to swap first and last letters of the word
{
if( len == 1) // check if word contains only one letter then no change
swapwrd = wrd; //store original word and the new word
else if( len == 2) // check if word contains only two letters then swap 1st and 2nd
{
swapwrd = wrd . charAt(1) + wrd . charAt(0); //swap 0th and 1st index letters
}//else-if block closes
else // check if word contains more than two letters
{
swapwrd = wrd . charAt(len-1) + wrd . substring( 1, len-1 ) + wrd . charAt(0);
}//else block closes
}//end of the function swapchar( )
void sortword( ) // function to sort the characters of original word in alphabetical order
{
for( char ch = 'A' ; ch <= 'Z' ; ch++ ) //character loop from A to Z
{
for( int i = 0 ; i < len; i++ ) // inner loop from 0th index to length of the word
{
char ch1 = wrd . charAt( i ); // extract a character at ith index
if( ch == ch1 ) // if the extracted character ‘ch1’ and loop character ‘ch’ are same
{
sortwrd = sortwrd + ch1; //add the character to form sorted word
}//end of if block
}// end of for ‘i’ block
}// end of for ‘ch’ block i.e. outer loop
}//end of the function sortword( )
ISC String Questions 46
void display( ) // function to print the results
{
[Link]("The original word = " + wrd ); // print the input word
[Link]("The swapped word = " + swapwrd ); // print the swapped word
[Link]("The sorted word = " + sortwrd); // print the input word
}//end of the function display( )
public static void main( String args[ ] ) throws IOException //main function starts
{
SwapSort obj = new SwapSort( ); // making object of class
obj . readword( ); //calling function to input a word in uppercase
obj . swapchar( ); //calling function to swap first and last character
obj . sortword( ); //calling function to sort the letters of the word
obj . display( ); //calling function to print results
}//end of the main( ) function
}//end of the class
2017
5
QUESTION 9. [10]
A class ConsChange has been defined with the following details::
Class name ConsChange
Data members/instance variables:
word : stores the word.
len : stores the length of the word.
Member functions/methods:
ConsChange( ) : default constructor to initialize data members with legal
initial values.
void readword( ) : accepts the word in lowercase.
void shiftcons( ) : shifts all the consonants of the word at the beginning
followed by the vowels (e.g. spoon becomes spnoo).
void changeword( ) : changes the case of all occurring consonants of the shifted
word to uppercase (e.g. spnoo becomes SPNoo).
void show( ) : displays the original word, shifted word and the changed
word.
Specify the class ConsChange giving details of the constructor( ), functions void readword( ), void shiftcons(
), void changeword() and void show( ). Define a main( ) function to create an object and call the methods
accordingly to enable the task.
ANSWER:
// The following program is successfully compiled and executed
import [Link].*;
import [Link].*;
class ConsChange
{
String word; int len; // declaration of data members of the class
ConsChange( ) // this is default or non-parameterized constructor
{
word = ""; len = 0; // assign null to variable ‘word’ and 0 to variable ‘len’
}//end of the non-parameterized constructor
void readword( ) throws IOException // function to input a word in lowercase
{
Scanner br = new Scanner( [Link] );
[Link]("Enter a word in lowercase or small letters = " );
word = [Link]( ); // the function next( ) will accept only one word and ignores others
len = [Link]( ); // find length of the word, if not here, then use in both the functions
}//end of the function readword( )
void shiftcons( ) // function to shifts all the consonants at the starting of the word
{ // followed by vowels
String temp = ""; // temporary local string variable that will store consonants followed
// by vowels
char ch;
ISC String Questions 47
for( int i = 0 ; i < len; i++ ) // within this loop only consonants are extracted and stored
{
ch = word . charAt( i ); // extract a character at ith index from the string stored in word
if( ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u' ) // consonants check
{
temp = temp + ch; // add consonant stored in ‘ch’ to string variable ‘temp’
}// end of if block
}// end of for ‘i’ block
for( int i = 0 ; i < len; i++ ) // within this loop only vowels are extracted and stored in the {
// same variable ‘temp’
ch = word . charAt( i ); // extract a character at ith index from the string stored in word
if( ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ) // check for vowel
{
temp = temp + ch; // add vowel stored in ‘ch’ to string variable ‘temp’ that
}// end of if block // already contains consonants
}// end of for ‘i’ block
[Link]("The new word with consonants followed by vowels = " + temp ); // print
word = temp ; // update global variable ‘word’ by the new word stored in ‘temp’
}//end of the function shiftcons( )
void changeword( ) // function that converts only consonants into uppercase, remember,
{ // variable ‘word’ is already updated
String temp = ""; // local string variable to store consonants in uppercase then vowel
char ch;
for( int i = 0 ; i < len; i++ ) // within this loop only consonants are extracted and stored
{
ch = word . charAt( i ); // extract a character at ith index from the UPDATED word
if( ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u' ) // consonants check
temp = temp + (char) (ch-32); // add consonant in ‘ch’ to string variable ‘temp’
// after converting in CAPITAL
else
temp = temp + ch; // otherwise add value in ‘ch’ as it is, because this is a vowel
}// end of for ‘i’ block
[Link]("The new word with consonants in capitals followed by vowels = " + temp );
}//end of the function changeword( )
void show( ) // function to print the results by calling other functions in a proper order
{
[Link]("The original word = " + word ); // print the input word
shiftcons( ); // calling function to print word with consonants follwed by vowels
changeword( ); // calling function to print consonants in CAPITAL follwed by vowels
}//end of the function show( )
public static void main( String args[ ] ) throws IOException //main function starts
{
ConsChange obj = new ConsChange( ); // making object of class
obj . readword( ); //calling function to input a word in lowercase
obj . show( ); //calling function to print results by calling other functions in a sequence
}//end of the main( ) function
}//end of the class
2016
6 A class TheString accepts a string of a maximum of 100 characters with only one blank space between the words.
Some of the members of the class are as follows:
Class name TheString
Data members/instance variables:
str : to store a string.
len : integer to store length of the string.
wordcount : integer to store number of words.
cons : integer to store number of consonants.
Member functions/methods:
TheString( ) : default constructor to initialize the data members.
TheString( String ds) : parameterized constructor to assign str=ds.
ISC String Questions 48
void countFreq( ) : to count the number of words and the number of
consonants and store them in wordcount and cons
respectively.
void Display( ) : to display the original string, along with number of words
and number of consonants.
Specify the class TheString, giving the details of the constructors and the functions void
countFreq() and void Display( ). Define the main( ) function to create an object and call the
functions accordingly to enable the task. 2015
ANSWER:
// The following program is successfully compiled and executed
import [Link].*;
import [Link].*;
class TheString
{
String str; //declaration of data members or variables
int wordcount, cons, len;
TheString( ) //default constructor definition
{
str= ""; //assign null to string variable ‘str’
wordcount=0;
cons=0; len=0;
}//end of default constructor
TheString( String ds) //parameterized constructor definition
{
str=ds; //assign 'ds' to 'str'
}//end of parameterized constructor
void countFreq( ) //function to count number of words and consonants
{
str = str + " "; //adding a blank at the end of the string. If you are not adding blank then
// assign 1 to wordcount or if wordcount =0 then at the time of printing, // do
"wordcount+1"
len = [Link]( ); //finding the length of the string in 'str'
for( int j = 0; j < len ; j++ )
{
char ch = str. charAt( j ); //extract a character at jth index from 'str' and store in 'ch'
if( ch== ' ' ) //check for blank space
wordcount++; //counting words that stores in variable ‘wordcount’
else if( ch!='A' && ch!='E' && ch!='I' && ch!='O' && ch!='U' && ch!='a' && ch!='e' && ch!='i' && ch!='o' &&
ch!='u' ) //checking if character in ‘ch’is not a vowel
cons++; //counting consonants that stores in variable ‘cons’
}//for loop closes here
}//end of function countFreq( )
void Display( ) //function to print the final results
{
[Link]("The string = " +str );
[Link]("Number of words = " +wordcount );
[Link]("Number of consonants = " +cons );
}//end of function Display( )
public static void main( String args[ ] ) //main function starts here
{
Scanner cx = new Scanner( [Link] );
[Link]("Enter a string ");
String st = [Link]( );
TheString obj= new TheString( st ); //making object of the class and that passes string
// argument 'st' to string parameter 'ds' in the constructor
ISC String Questions 49
[Link]( );
[Link]( );
}//end of main( ) function
}//end of the class
7 A sequence of fibonacci strings is generated as follows:
S0 = "a", S1 = "b", Sn = S(n-1) + S(n-2) where '+' denotes concatenation. Thus the sequence is:
a, b, ba, bab, babba, babbabab..............n terms
Design a class FiboString to generate fibonacci strings. Some of the members of the class are given below:
Class name : FiboString
Data members / instance variables
x : to store the first string
y : to store the second string
z : to store the concatenation of the previous two strings
n : to store the number of terms
Member functions :
FiboString ( ) : constructor to assign x="a", y="b" and z="ba"
void accept( ) : to accept the number of terms 'n'
void generate( ) : to generate and print the fibonacci strings. The sum of ( '+' ie
concatenation) first two strings is the third string. Eg. "a" is
first string, "b" is second string then the third will be "ba", and
fourth will be"bab" and so on.
Specify the class FiboString, giving details of the constructor( ), void accept( ), and void generate( ). Define the main(
) function to create an object and call the functions accordingly to enable the task.
[ISC 2014]
ANSWER (a):
// The following program is successfully compiled and executed
import [Link].*;
import [Link].*;
class FiboString
{
String x, y, z ; int n; //data members as required in the class
FiboString( ) //this is default or non-parameterized constructor
{
x = "a"; y = "b"; z = "ba"; //assiging values to string variables x, y, z as required
}
void accept ( ) //function definition to input number of terms of the series
{
Scanner Sc = new Scanner ( [Link] ); //making object ‘Sc’of scanner class
[Link]("Enter number of terms of the fibonacci series: " ) ;
n = [Link]( ); //input value of ‘n’ from user
}//end of function accept( )
void generate( ) //function definition to generate and print string fibonacci series
{
[Link]( x+","+y ); //printing first two terms stored in ‘x’ and ‘y’ i.e. ‘a’, ‘b’
for( int i=0; i<=n-2; i++ ) //printing other terms of the string fibonacci series
{
[Link](","+z);
//interchanging ‘y’ & ‘z’ because as the output in question the values of ‘y’ & ‘z’ are used
x = y; // value in ‘y’ assigns to ‘x’
y = z; // value of ‘z’ assigns to ‘y’
z = y + x; // you may also write z= [Link]( x );
}//end for loop
}//end of function generate( )
public static void main( String args[ ] ) throws IOException //main function starts
{
FiboString obj=new FiboString( ); //making object ‘obj’ of class
[Link]( ); //calling function to input number of terms
[Link]( ); //calling function to print the string fibonacci terms
}//end of main( ) function
}//end of class
ISC String Questions 50
8 Design a class Exchange to accept a sentence and interchange the first alphabet with the last alphabet for each word in
the sentence, with single letter word remaining unchanged. The words in the input sentence are separated by a single
blank space and terminated by a full stop.
Example: Input : It is a warm day.
Output: tI si a marw yad
Some of the data members and member functions are given below:
Class name : Exchange
Data members / instance variables
sent : stores the sentence
rev : to store the new sentence
size : stores the length of the sentence
Member functions :
Exchange ( ) : default constructor
void readsentence( ) : to accept the sentence
void exfirstlast( ) : extract each word and interchange the first and last alphabet of
the word and form a new sentence rev using the changed words
void display( ) : display the original sentence along with the new changed
sentence
Specify the class Exchange, giving details of the constructor( ), void readsentence( ), void exfirstlast( ) and void
display( ). Define the main( ) function to create an object and call the functions accordingly to enable the task.
[ISC 2013]
ANSWER :
// The following program is successfully compiled and executed
import [Link].*;
import [Link].*;
class Exchange
{
String sent, rev; int size;
Exchange( ) // the default or non-parameterized constructor
{
sent = ""; rev= ""; size = 0; //store null to string variables 'sent', 'rev' and 0 to ‘size’
}// constructor closes here
void readsentence( ) // function to input a string
{
Scanner obj = new Scanner( [Link] ); // making memory buffer
[Link]("Enter a sentence ");
sent = [Link]( ); // this Scanner command inputs a string
}// function readsentence( ) closes here
void exfirstlast( ) // function to find frequency of vowel words
{
String sent1 = [Link](0, [Link]( )-1); // extract sentence except the last full stop,
// and store in string variable ‘sent1’
StringTokenizer st = new StringTokenizer( sent1 ); // here "st" is tokenizer object
int w = [Link]( ); // count total number of words from sent through object ‘st’
for(int j=1; j<=w; j++) //this loop goes from 1 to the number of words (w)
{
String ss = [Link]( ); // extract a word from ‘sent’ using tokenizer object ‘st’ and
// store in string variable ‘ss’
size = [Link]( ); // finding length of the exteacted word in ‘ss’
if( size == 1 ) // means it is a single letter word
rev = rev + ss; // add or concatenate or join the word in ‘ss’ as it is into ‘rev’
else if( size == 2 ) // means it is a two letters word
rev = rev + [Link]( 1 ) + [Link]( 0 ); // extract 1, 0 index letters and add to ‘rev’
else //control comes here if the word contains more then two letters
rev = rev + [Link]( size- 1) + [Link]( 1, size-1 ) + [Link]( 0 ); // extract last
// letter, 0 index letter and the middle (sub) string except 0, last letter and
// add to variable ‘rev’
rev = rev + " "; // now add or cancatenate a blank at the end of the word
}// for j loop closes here
}// end of function exfirstlast( )
ISC String Questions 51
void display( ) //function to print results
{
[Link]("ORIGINAL SENTENCE = " + sent );
[Link]("NEW SENTENCE = " + rev );
}//end of function display( )
// starting of the main( ) function
public static void main( String args[ ]) throws IOException
{
Exchange Fx = new Exchange( ); // making object 'Fx' of the class Exchange
Fx . readsentence( ); // calling function to input a sentence
Fx . exfirstlast( ); // calling function to form a new word a desired & given in example
Fx . display( ); // calling function to print results
}// main( ) function closes here
}//end of class
Important : The function void exfirstlast( ) can also be defined (made) as follows:
void exfirstlast( )
{ char c, ch= ' '; int start = 0; String word = "";
size = [Link]( ); //find the length of the sentence
for(int i=0; i<size; i++)
{ c = [Link]( i ); // extract a character at ith index and store in character variable ‘c’
if( c == ' ' || c == '.' ) // check for blank or full stop (.)
{
word = [Link](start, i ); // extract word from start index to just before the index of space
if( [Link]( ) == 1 )
rev = rev + word; // add or concatenate the word in ‘word’ as it is to variable ‘rev’
else
{
rev = rev + [Link]( [Link]( ) -1) +[Link](1,[Link]( )-1)+[Link](0);
// the above statement extract last letter, 0 index letter and the middle (sub) string except // 0
index and last letter then add or concatenate to variable ‘rev’
}//end of else block
rev = rev + " "; // now add or cancatenate a blank at the end of the word
start = i+1; // now change the position of variable ‘start’ to help the extraction of new word
} // outer if block closes here
} // for i loop closes here
}// end of the function
9 Design a class VowelWord to accept a sentence and calculate the frequency of words that begin with a vowel. The
words in the input string are separated by a single blank space and terminated by a full stop. The description of the class
is given below:
Class name : VowelWord
Data members / instance variables:
str : to store a sentence
freq : store the frequency of the words beginning with a vowel
Member functions :
VowelWord( ) : constructor to initialize data members with initial value
void readstr( ) : to accept a sentence
void freq_vowel ( ) : counts the frequency of the words that begin with a vowel
void display( ) : to display the original string and the frequency of the
words that begin with a vowel
Specify the class VowelWord giving details of the constructor( ), void readstr( ),
void freq_vowel( ) and void display( ). Also define a main( ) function to create an object and call the methods
accordingly to enable the task.
[2012]
import [Link].*;
class VowelWord
{
String str;
int freq;
public VowelWord()
{
ISC String Questions 52
str="";
freq=0;
}
public void readstr()
{
Scanner ob = new Scanner([Link]);
[Link]("Enter a sentence ");
str=[Link]();
}
public void freqvowel()
{
StringTokenizer word = new StringTokenizer(str);
int i,len;
String st;
len=[Link]();
for(i=1;i<=len; i++)
{
st=[Link]();
if("AEIOUaeiou".indexOf([Link](0))!= -1) freq++;
}
}
public void display()
{
[Link]("Original string is = " + str);
[Link]("Frequency of words starting with vowel ="+freq);
}
public static void main(String args[ ])
{
VowelWord obj = new VowelWord();
[Link]();
[Link]();
[Link]();
}
}
10 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 / instance variables :
text : stores the sentence
countand : to store the frequency of the word “and”
countan : to store the frequency of the word “an”
len : store the length of the string
Member functions :
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 displays the number of “and” and “an” with
appropriate messages
Specify the class Frequency giving details of the constructor( ), void accept (String), void checkandfreq( ),
void checkanfreq( ) and void display( ). Also define main( ) function to create an object and call methods accordingly
to enable the task.
[2011]
ANSWER :
// The following program is successfully compiled and executed
import [Link].*;
import [Link].*;
class Frequency
{
ISC String Questions 53
String text; int countand, countan, len;
Frequency( ) // constructor definiton begins
{
text=null; countand=0; countan=0; // initializing instance variables
}// end of contructor
void accept(String n) // function that assigns 'n' to 'text'
{
text=n; // assign or copy 'n' to 'text'
}//end function accept( ) // function to find frequency of "and"
void checkandfreq( )
{
StringTokenizer na = new StringTokenizer( text ); // making tokenizer object 'na'
int p=0; String tempStr="";
while( [Link]( ) ) //the loop runs till tokens are present in 'na' i.e 'text'
{
tempStr = [Link]( ); // extract a word or token from object ‘na’
if( [Link]("and"))
countand++; //counting frequency of word "and"
}// end of while loop
}//end function checkandfreq( )
void checkanfreq( ) // function to find frequency of "an". In the above function
{ // you have seen the word extraction using StringTokenizer but
// in this function the words are extracted using simple method
text = text +" "; // add a blank at the end of the string stored in variable ‘text’
len=[Link]( );
int p=0; char ch ; String tempStr=""; // taking some extra variables
for(int i=0; i < len; i++)
{
ch = [Link](i); // to get or extract a character present at ith index in text
if( ch==' ' || ch=='.' )
{
tempStr=[Link]( p, i ); // extract the word or substring from p to i-1
if([Link]( "an" ))
countan++; //counting frequency of word "an"
p=i+1; // update the index position to get the starting index to extract the new word
}//end outer if
}//end of for 'i' loop
}//end function checkanfreq( )
void display( ) // function to print final results
{
[Link]("Number of and's =" + countand);
[Link]("Number of an's =" + countan);
} //end of function display( )
public static void main( String args[ ] ) throws IOException // main function begins here
{ BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter a sentence in Lower case : ");
String sent= [Link]( );
Frequency bbr= new Frequency( ); // making object of class
[Link](sent); // calling function by passing 'sent' to 'n'
[Link]( ); // calling function to find frequency of "and"
[Link]( ); // calling function to find frequency of "an"
[Link]( ); // calling function to print results
}//end of main( ) function
}//class closes here
11 import [Link].*;
class data
{
protected String str;
data()
{
str=" ";
ISC String Questions 54
}
public void acceptstr()
{
Scanner ob = new Scanner([Link]);
[Link]("enter string");
str = [Link](); str=str+" ";
}
public void Print()
{
[Link](" string="+str);
}
}
class process extends data
{
int l;
public void removeDuplicate()
{
String s1=" ";String s=" ",s2=" "; int y=0;
l=[Link]();
for(int i=0;i<l;i++)
{
char ch=[Link](i);
if(ch==' ')
{
s=[Link](y,i);[Link](" s="+s);
y=i;
int l1=[Link]();
int p=0;
for(int r=0;r<l1;r++)
{
for(int j=r+p;j<l1-1;j++)
{
if([Link](j)==[Link](j+1))
{p++;[Link](" p="+p);continue;}
else
s1=s1+[Link](j);
}
s2=s1;[Link](" new="+s2);
}s1="";
}
//[Link](" new="+s2);
}}
public static void main(String args[])
{
Scanner ob = new Scanner([Link]);
process p = new process();
[Link]();
[Link]();
[Link]();
}
}
12 Input a word in uppercase and check for the position of the first occurring vowel and perform the following operations.
(i) Words that begin with a vowel are concatenated with “Y”
For example, EUROPE becomes EUROPEY
(ii) Words that contain a vowel in-between should have the first part from the position of the vowel till end,
followed by the part of the string from beginning till position of the vowel and is concatenated by “C”
For example, PROJECT becomes OJECTPRC
(iii) Words which do not contain a vowel are concatenated with “N”
For example, SKY becomes SKYN
Design a class Rearrange using the description of the data members and member functions given below:
Class name : Rearrange
Data Members/ instance variables:
ISC String Questions 55
Txt : to store a word
Cxt : to store the rearranged word
len : to store the length of the word
Member functions:
Rearrange( ) : constructor to initialize the instance variables
void readword( ) : to accept the word input in UPPER CASE
void convert( ) : converts the word into its changed form and stores it in string Cxt
void display : displays the original and the changed word
Specify the class Rearrange giving details of the constructor( ), void readword( ), void convert( ) and void display( ).
Define a main( ) function to create an object and call the function accordingly to enable the task.
[ISC 2010]
import [Link].*;
class Rearrange
{
String txt,cxt;
int len;
public Rearrange()
{
txt="";
cxt="";
len=0;
}
public void readword()throws IOException
{
BufferedReader ob=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter a word in uppercase only");
txt=[Link]();
}
public void convert()
{
int i=0;
String s="";
len=[Link]();
for(i=0;i<len;i++)
{
char x=[Link](i);
if(x=='A'||x=='E'||x=='I'||x=='O'||x=='U')
break;
}
cxt=cxt+[Link](i)+[Link](0,i)+"C";
}
char ch=[Link](1);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
cxt=txt+"Y";
else
{
for(i=0;i<len;i++)
{
char z=[Link](i);
if(z!='A'||z!='E'||z!='I'||z!='O'||z!='U')
break;
}
cxt=cxt+txt+"N";
}
}
public void display()
{
[Link]("Original string="+txt);
[Link]("Rearranged String="+cxt);
}
public static void main(String args[])throws IOException
ISC String Questions 56
{
Rearrange obj=new Rearrange();
[Link]();
[Link]();
[Link]();
}
}
13 In “Piglatin” a word such as KING is replaced by INGKAY, while TROUBLE becomes OUBLETRAY and so on . The
first vowel of the original word becomes the start of the translation, any preceding letters being shifted towards the end
and followed by AY. Words that begin with a vowel or which do not contain any vowel are left unchanged.
Design a class Piglatin using the description of the data members and member functions given below :
Class name : Piglatin
Data members /instance variables :
Txt : to store a word
len : to store the length
Member functions :
Piglatin( ) : constructor to initialize the data mrmbers
void readstring( ) : to accept the word input in UPPER CASE
void convert ( ) : converts the word into its piglatin form and displays
the word (changed or unchanged)
void consonant( ) : counts and displays the number of consonants present
in the given word.
Specify the class Piglatin giving the details of the constructor, void readstring( ), void convert( ) and void consonant( ).
Also define the main function to create an object and call methods accordingly to enable the task.
[SP 2013]
14 Design a class Alpha which enables a word to be arranged in ascending order according to its alphabets.
The details of the members of the class are given below :
Class name : Alpha
Data members / instance variables:
Str : to store a word
Member functions :
Alpha( ) : default constructor
void readword( ) : to accept the inputted word
void arrange ( ) : to arrange the word in alphabetical order using any
standard sorting technique.
void disp(( ) : displays the word.
Specify the class Alpha giving details of the constructor and the member functions void readword( ), void arrange(
), void disp ( ) and defining the main ( ) function to create an object and call the function in order to execute the class by
displaying the original word and the changed word with proper message.
[SP 2010]
Specify the class SortWord giving details of the functions constructor, void readTxt(), void sortTxt( ), void
changeTxt( ) and void disp( ). You need not write the main function.
[2009]
Specify the class Modify giving details of the functions void read( ), void putin(int, char), void takeout(int) and void
change( ). The main function need not be written.
[2008]
17 A class modistring has been defined for the following methods/functions:
Class name: : modistring
Data members:
str[ ] : to store a string.
len : length of the given string.
Member functions:
Mystring( ) : constructor
void readstring( ) : reads the given string from the input.
int code(int index) : returns ASCII code for the character at position index.
void word( ) : displays longest word in the string.
Specify the class modiString giving the details of the constructor, void readstring( ), int code(int index), void word( )
only. The main function need not be written.
[2007]
ANSWER:
// The following program is successfully compiled and executed
import [Link].*;
import [Link];
class Mystring
{
String str; int len ;
Mystring( ) // non-parameterized constructor
{
str="";
len=0;
}// end constructor
void readstring( ) throws IOException // member function_1 to input a string
{
BufferedReader inp = new BufferedReader(new InputStreamReader([Link]));
[Link]("Input a string : ");
str= [Link]( );
}// end function readstring( )
int code(int index) // member function_2 to return ASCII code
{
int cod = [Link](index);
return cod;
}// end function code( )
void word( ) // member function_3 to find longest word
{
String largeWord="";
StringTokenizer st = new StringTokenizer(str); // making StringTokenizer object ‘st’
int g = 0;
while ([Link]( )) // this loop runs till the tokens or words are present in ‘st’
ISC String Questions 58
{
String wd= [Link]( ); // this statement extract a word from object ‘st’
len= [Link]( );
if(len>g)
{
largeWord=wd; // store the longest word in variable ‘largeWord’
g=len; // store the length of the largest word
}
}// end while
[Link]("Longest word = "+largeWord);
}// end function word( )
/* The main function is defined below */
public static void main(String args[ ]) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader([Link]));
Mystring obj = new Mystring( ); // creation of object of class
[Link]( ); // function calling to input string
[Link]("Input an index number: ");
int id=[Link]([Link]( ));
int cd=[Link](id); // function calling by passing id to index
[Link]("The ASCII of character at the "+ id +" index is= "+cd);
[Link]( ); // function calling to find and print longest word
} // end main( ) function
}// end class
Specify the class modiString giving the details of the functions void getstr( ), void change( ), void next( ) and void
print( ). The main function need not be written.
[2007R]
19 Design a class Stringfun to perform various operations on strings without using built-in functions except for finding the
length of the string. Some of the member functions/methods are given below:
Class name: : Stringfun
Data members:
str[ ] : store a string.
Member functions:
void input( ) : to accept the string.
Specify the class Stringfun giving the details of the functions void input( ), void words( ) and void frequency( ). The
main function need not be written.
[2006]
ISC String Questions 59
20 You are given a piece of text that contains words, blank spaces and tabs only.
A Word is defined as a group of contiguous non blank characters.
A white space is a tab ('\t') or a blank spaces(' ').
For example
Text No. of words No. of white spaces
" " 0 10
" This is beautiful " 3 15
"blessed" 1 0
A class Text is designed to handle text related operations. Some functions of class Text are as follows:
Class name : Text
Data members/instance variables:
txt - to store the given string. You may assume that it has 200 characters at most.
Member functions/methods:
Text() : constructor
void readText() : read the given string from the input
char charAt(int i) : returns the character at position I of the string
int length() : returns the length of the string
int noOfWhiteSpaces() : returns the total number of white space in the text
int noOfWords() : returns the number of words in the text
Specify the class Text giving details of int noOfWhiteSpaces() and int noOfWords() only. You may assume that the
other functions/methods are written for you. You do not need to write the main function.
[2003]
21 Class ChaArray contains an array of n characters (n<=100). You may assume that the array contains only the letters of
the English alphabet. Some of the member functions/ methods of ChaArray are given below:
Class name : ChaArray
Data members/instance variables:
char ch[ ] : an array of characters
int size : the size of array of characters
Members functions/methods:
ChaArray( ) : constructor to initialize instance variables to null
ChaArray( char c[ ]) : constructor to assign character array c to the instance variable
void displayArray( ) : to display the list of n characters
void move( ) : to move all the upper case letters to the right side of the
array and the lower case letters to the left side of the array
without using any standard sorting technique.
Input : t D f s X v d Output: t d f s v X D
Specify the class ChaArray giving details of the two constructors and the functions void displayArray( ), and void
move( ) only. You do not need to write the main function.
[2004]
22 A class stringop is designed to handle string related operations. Some members of the class are given below:
Class name : stringop
Data members/ instance variables :
Txt : to store the given string of maximum length 100
Member functions/methods
stringop( ) : constructor.
void readstring( ) : to accept the string
void caseconvert(int, int) : to convert the letter to other case
void circulardecode( ) : to decode the string by replacing each letter by converting it to
opposite case and then by the next character in a circular way.
Hence “AbZ” will be decoded as “bCa”.
Specify the class giving the details of the char caseconvert(int) and void circulardecode( ) only. You may assume that
other functions are written for you. You do not need to write the main( ).
[2005]
23 S is a string of capital alphabets, parts of which may be enclosed in brackets ( ). S is an invalid string if the brackets are
nested. For example : AXB (YPT (COM)FTY) XUP is not a valid string. Write a program to
(1) Check the validity of the input string.
(2) If the string is valid, output the given string omitting the portion enclosed in brackets.
ISC String Questions 60
For example : Input string : COM(IP)PUTER IS JUNK (MONK) MACHINE
Output string : COMPUTER IS JUNK MACHINE
If the string is not valid, print the string and a message “Input string is not valid” and stop.
[1996]