In this section you are required to answer two questions out of three . You are to create a class where the data members to be stored and methods/member functions to be defined are clearly instructed in the question. You just have to implement the same using Java syntax and have to define a main which creates the class object and calls the required function/s. In this section you will be tested on your basic programming skill in Java like constructor , recursion etc . Many of the functions will require you to program mathematical series like the factorial series , arithmetic or geometric progression sequence , sum of squares or cubes of n natural numbers and so on. Here is a sample of the section.
Question 8.
A perfect square is an integer which is the square of another integer. For example, 4,9,16… are
perfect squares. Design a class Perfect with the following description:
Class Name : Perfect
Data members
n : stores an integer number
Member functions
Perfect() : default constructor
Perfect(int) : parameterized constructor to assign value
to n
void perfect_sq() : to display the first 5 perfect squares larger
than
‘n’ (if n=15, the next 3 perfect squares are
16,25,36).
void sum_of() : to display all combinations of consecutive
integers whose sum is = n. (The number
n=15 can be expressed as :
1 2 3 4 5
4 5 6
7 8
Specify the class Perfect giving details of the constructors, void perfect_sq() and void sum_of(). Also
define the main function to create an object and call methods accordingly to enable the task.
[ 10 ]
Sol.
import java.util.*;
class Perfect
{
int n;
Perfect() //Default Constructor
{
n=0;
}
Perfect (int n) //Parameterised constructor
{
this.n=n; // Since I assigned the same name of variable as the member function , this operator is used . You are free to use any other name like num
}
void perfect_sq()
{
int k ;
for(int i=1; i<=n/2; i++)
{
if(i*i==n)
{
k=i+1;
break;
}
else if(i*i>n)
{
k=i;
break;
}
else
continue;
}
System.out.println("The next five perfect nos are as follows");
StringBuilder sb= new StringBuilder();
for (int i=k;i<=k+5;i++)
{
sb.append(i*i).append("");
}
System.out.println( sb.ToString());
}
void sum_of()
{
System.out.println("The combination of the consecutive integers whose value is "+n);
for (int i = 1; i < n; i++)
{
StringBuffer sb = new StringBuffer();
int sum = i;
sb.append(i).append(" ");
for (int j = i + 1; sum < n; j++)
{
sb.append(j).append(" ");
sum = sum + j;
if (sum == n)
System.out.println(sb.toString());
}
}
}
}
class call_main
{
public static void main(String args[])
{
Perfect obj = new Perfect(); //Default constructor is called here. n value set to 1.
obj.perfect_sq();
obj.sum_of();
Perfect ob = new Perfect ob(15); //Calls the parameterized constructor
obj.perfect_sq();
obj.sum_of();
}
}
Dry Run :
The next five perfect nos are as follows
16 25 36 49 64
Question 9.
A class RecFact defines a recursive function to find the factorial of a number. The details of the class
are given below:
Class Name : RecFact
Data Members
n : stores the number whose factorial is
required.
r : stores an integer
Member functions
RecFact() : default constructor
voidreadnum() : to enter values for n and r.
int factorial(int) : returns the factorial of the number using
the
recursive technique.
voidfactseries() : to calculate and display the value of
n!
-----------------
r! * (n-r)!
Specify the class RecFact giving the details of the constructor and member functions void readnum(),
int factorial(int) and void factseries(). Also define the main function to create an object and call
methods accordingly to enable the task. [10]
Sol.
import java.util.*;
class RecFact
{
int n;
int r;
RecFact()
{
n=0;
r=0;
}
void readnum()
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Input an integer whose factorial is to be computed");
n = Integer.ParseInt(br.readLine());
System.out.println("Input an integer");
r= Integer.ParseInt(br.readLine());
}
int factoral(int fact)
{
int retfact=1;
int ifact=fact;
if(ifact==0 || ifact==1)
{
return retfact;
}
else
{
retfact=ifact*factorial(ifact-1);
}
}
void factseries()
{
int numerator= factorial(n);
int factr = factorial(r);
int diffact= factorial(n-r);
int denom= factr*diffact;
float result= (float) numerator/denom ;
System.out.println("The value of the series is : " +result);
}
}
class FactMain
{
public static void main(String args[])
{
RecFact obj = new RecFact();
obj.readnum();
obj.factseries();
}
}
Output :
Input an integer whose factorial is to be computed
5
Input an integer
3
The value of the series is 10.
(I am sure you must have observed it basically computes the value of nCr or possible combinations of r objects from a set of n objects.)
Question 10.
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
txt : to store a word
len : to store the length
Member functions
PigLatin() : constructor to initialize data members
voidreadstring() : to accept the word input in uppercase
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 details of the constructor, void readstring(), void convert() and void
consonant(). Also define a main function to create an object and call methods accordingly to enable
the task.
[ 10 ]
Sol.
class Piglatin
{
String txt;
int len;
Piglatin() // It is a good programming practice to provide a default constructor when you create another constructor . Though this step is completely optional.
{
}
Piglatin(string txt)
{
this.txt = txt;
len = txt.length();
}
void readstring()
{
InputStreamReader rd = new InputStreamReader();
BufferedReader br = new BufferedReader(rd);
System.out.println("Enter any word");
String str= br.readline();
str= str.toUpperCase();
Piglatin pgobj = new Piglatin(str);
}
void convert()
{
int pos=-1;
char ch ;
for(int i = 0; i<len ; i++)
{
ch = txt.charAt(i)'
if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
pos = i;
break;
}
}
if(pos!= -1)
{
String s1 = txt.substring(pos);
String s2 = txt.substring(0,pos);
String pglatin = s1+s2+"AY";
System.out.println("The piglatin conversion is "+pglatin);
}
else
System.out.println("Piglatin conversion not possible for the input");
}
void consonant()
{
int num = 0;
char ch;
for (int i = 0; i<len ; i++)
{
ch= txt.charAt(i);
if(ch!='A' || ch!='E' || ch!='I' || ch!='O' || ch!='U')
num=num+1;
}
System.out.println("The number of consonants in "+txt+" is "+num);
}
}
class showmain()
{
public static void main(String args[])
{
Piglatin obj = new Piglatin();
obj.readnum();
obj.convert();
obj.consonant();
}
}
Output
Enter any word
king
The piglatin conversion is INGKAY
The number of consonants in KING is 3
Question 8.
A perfect square is an integer which is the square of another integer. For example, 4,9,16… are
perfect squares. Design a class Perfect with the following description:
Class Name : Perfect
Data members
n : stores an integer number
Member functions
Perfect() : default constructor
Perfect(int) : parameterized constructor to assign value
to n
void perfect_sq() : to display the first 5 perfect squares larger
than
‘n’ (if n=15, the next 3 perfect squares are
16,25,36).
void sum_of() : to display all combinations of consecutive
integers whose sum is = n. (The number
n=15 can be expressed as :
1 2 3 4 5
4 5 6
7 8
Specify the class Perfect giving details of the constructors, void perfect_sq() and void sum_of(). Also
define the main function to create an object and call methods accordingly to enable the task.
[ 10 ]
Sol.
import java.util.*;
class Perfect
{
int n;
Perfect() //Default Constructor
{
n=0;
}
Perfect (int n) //Parameterised constructor
{
this.n=n; // Since I assigned the same name of variable as the member function , this operator is used . You are free to use any other name like num
}
void perfect_sq()
{
int k ;
for(int i=1; i<=n/2; i++)
{
if(i*i==n)
{
k=i+1;
break;
}
else if(i*i>n)
{
k=i;
break;
}
else
continue;
}
System.out.println("The next five perfect nos are as follows");
StringBuilder sb= new StringBuilder();
for (int i=k;i<=k+5;i++)
{
sb.append(i*i).append("");
}
System.out.println( sb.ToString());
}
void sum_of()
{
System.out.println("The combination of the consecutive integers whose value is "+n);
for (int i = 1; i < n; i++)
{
StringBuffer sb = new StringBuffer();
int sum = i;
sb.append(i).append(" ");
for (int j = i + 1; sum < n; j++)
{
sb.append(j).append(" ");
sum = sum + j;
if (sum == n)
System.out.println(sb.toString());
}
}
}
}
class call_main
{
public static void main(String args[])
{
Perfect obj = new Perfect(); //Default constructor is called here. n value set to 1.
obj.perfect_sq();
obj.sum_of();
Perfect ob = new Perfect ob(15); //Calls the parameterized constructor
obj.perfect_sq();
obj.sum_of();
}
}
Dry Run :
The next five perfect nos are as follows
16 25 36 49 64
Question 9.
A class RecFact defines a recursive function to find the factorial of a number. The details of the class
are given below:
Class Name : RecFact
Data Members
n : stores the number whose factorial is
required.
r : stores an integer
Member functions
RecFact() : default constructor
voidreadnum() : to enter values for n and r.
int factorial(int) : returns the factorial of the number using
the
recursive technique.
voidfactseries() : to calculate and display the value of
n!
-----------------
r! * (n-r)!
Specify the class RecFact giving the details of the constructor and member functions void readnum(),
int factorial(int) and void factseries(). Also define the main function to create an object and call
methods accordingly to enable the task. [10]
Sol.
import java.util.*;
class RecFact
{
int n;
int r;
RecFact()
{
n=0;
r=0;
}
void readnum()
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Input an integer whose factorial is to be computed");
n = Integer.ParseInt(br.readLine());
System.out.println("Input an integer");
r= Integer.ParseInt(br.readLine());
}
int factoral(int fact)
{
int retfact=1;
int ifact=fact;
if(ifact==0 || ifact==1)
{
return retfact;
}
else
{
retfact=ifact*factorial(ifact-1);
}
}
void factseries()
{
int numerator= factorial(n);
int factr = factorial(r);
int diffact= factorial(n-r);
int denom= factr*diffact;
float result= (float) numerator/denom ;
System.out.println("The value of the series is : " +result);
}
}
class FactMain
{
public static void main(String args[])
{
RecFact obj = new RecFact();
obj.readnum();
obj.factseries();
}
}
Output :
Input an integer whose factorial is to be computed
5
Input an integer
3
The value of the series is 10.
(I am sure you must have observed it basically computes the value of nCr or possible combinations of r objects from a set of n objects.)
Question 10.
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
txt : to store a word
len : to store the length
Member functions
PigLatin() : constructor to initialize data members
voidreadstring() : to accept the word input in uppercase
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 details of the constructor, void readstring(), void convert() and void
consonant(). Also define a main function to create an object and call methods accordingly to enable
the task.
[ 10 ]
Sol.
class Piglatin
{
String txt;
int len;
Piglatin() // It is a good programming practice to provide a default constructor when you create another constructor . Though this step is completely optional.
{
}
Piglatin(string txt)
{
this.txt = txt;
len = txt.length();
}
void readstring()
{
InputStreamReader rd = new InputStreamReader();
BufferedReader br = new BufferedReader(rd);
System.out.println("Enter any word");
String str= br.readline();
str= str.toUpperCase();
Piglatin pgobj = new Piglatin(str);
}
void convert()
{
int pos=-1;
char ch ;
for(int i = 0; i<len ; i++)
{
ch = txt.charAt(i)'
if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
pos = i;
break;
}
}
if(pos!= -1)
{
String s1 = txt.substring(pos);
String s2 = txt.substring(0,pos);
String pglatin = s1+s2+"AY";
System.out.println("The piglatin conversion is "+pglatin);
}
else
System.out.println("Piglatin conversion not possible for the input");
}
void consonant()
{
int num = 0;
char ch;
for (int i = 0; i<len ; i++)
{
ch= txt.charAt(i);
if(ch!='A' || ch!='E' || ch!='I' || ch!='O' || ch!='U')
num=num+1;
}
System.out.println("The number of consonants in "+txt+" is "+num);
}
}
class showmain()
{
public static void main(String args[])
{
Piglatin obj = new Piglatin();
obj.readnum();
obj.convert();
obj.consonant();
}
}
Output
Enter any word
king
The piglatin conversion is INGKAY
The number of consonants in KING is 3