Here goes the Group B questions on core Java language. I am sure you know you need to attempt any two questions . Flowcharts , algorithms nothing is required you just have to write the program and that is it. So let's start .
Question 8 :
A class Admission contains the admission numbers of 100 students . Some of the data members / member functions are given below :
Class Name Admission
Data member/Instance variable
Adno[] : Integer array to store admission numbers
Member Functions/Methods :
Admission() : Constructor to initialize array elements
void fillArray() : to accept elements of array in descending order
int binSearch(int l , int u , int v) : to search for particular admission number (v) using binary search and recursive techniques and returns 1 if found otherwise returns -1
Specify the class Admission giving details of constructor , fillArray() and binSearch(int,int,int). Define a main function to create an object and call functions accordingly to accomplish the task.
Sol :
import java.util.*;
public class Admission
{
int Adno[] = new int [100];
Admission()
{}
void fillArray()
{
Scanner s = new Scanner(System.in);
System.out.println("enter elements");
for (int i = 0 ; i <100 ; i++)
Adno[i]= s.nextInt();
}
int binSearch(int l,int u, int v)
{
int mid = (l+u)/2 ;
if(Adno[mid] == v)
return 1;
else if(l>u)
return -1;
else if (Adno[mid]>v)
return binSearch(l,mid-1,v);
else
return binSearch(mid+1, u , v);
}
public static void main(String args[])
{
Admission obj = new Admission();
obj.fillArray();
System.out.println("Enter value to be searched :");
Scanner s = new Scanner(System.in);
int v = s.nextInt();
int p = obj.binSearch(0,obj.Adno.length-1,v);
if(p==-1)
System.out.println("Object not found!");
else
System.out.println("Object found");
}
}
Please note that the above question asks for implementing binary search using recursive technique . If you do not follow this instruction you will be penalized . Otherwise a simple program to write .
Question 10 :
A class Merger concatenates two positive integers that are greater than 0 and produces a new merged integer.
Example : if the first number is 23 and second is 764 , the new number is 23764.
Some of the members of the class are given below
CLASS Merger
Data Members/ Instance Variables :
n1 : long integer to store first number
n2 : long integer to store second number
mergNum : long integer to store merged number
Member Functions :
Merger() : Constructor to initialize data members
void readnum() : Function to accept values of numbers n1 and n2
void JoinNum() : To concatenate numbers n1 and n2 to store it in mergNum
void show() : To display the original numbers and merged number with appropriate messages .
Specify the class Merger , giving details of constructor , void readNum() , void JoinNum() and void show() . Define main () function to create an object and call the functions accordingly to enable the task .
Sol :
import java.util.*;
public class Merger
{
long n1 , n2 , mergNum;
Merger(){}
void readNum()
{
Scanner s = new Scanner(System.in);
System.out.println("Enter First Number:");
n1 = s.nextLong();
System.out.println("Enter Second Number");
n2 = s.nextLong();
}
void JoinNum()
{
if(n1>0 && n2>0)
{
String s1 = Long.toString(n1);
String s2 = Long.toString(n2);
String s3 = s1 + s2;
mergNum = Long.valueOf(s3);
}
else
System.out.println("One or both the numbers are 0 or less than that , concatenation not possible !");
// It is better to use this if-else loop , since in the question it is mentioned that the class concatenates the string only if it finds out that the numbers are greater than 0 .
}
void show()
{
System.out.println("First Number : "+n1);
System.out.println("Second Number : "+n2);
System.out.println("Merged Number : "+mergNum);
}
public static void main(String[] args)
{
Merger obj = new Merger();
obj.readNum();
obj.JoinNum();
obj.show();
}
}
Question 10 :
A class TheString accepts a string of a maximum 100 characters with only a blank space between the words .
Some of the members of the class are given below
Class Name : TheString
Data Member/Instance Variables :
str : To store the string
len : integer to store length of string
wordcount : integer to store number of words
cons : integer to store number of consonants
Member Functions / Methods
TheString() : default constructor to initialize data members
TheString( String ds) : parameterized constructor to assign str = ds
void countFreq() : to count the number of words and 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 constructor , countFreq() , Display() . Define the main function . create an object of the class and call the member functions accordingly to enable the task .
import java.util.*
public class TheString
{
String str;
int len, wordcount, cons;
TheString(){}
TheString(String ds)
{
str = ds;
len = str.length();
}
void countFreq()
{
char c ;
String str1 = str.toLowerCase(); //lower case conversion simplifies the string handling to find the consonants. You can change to uppercase also . But semantics will change a bit to count the consonants.
for (int i = 0 ; i<len; i++)
{
c = str1.charAt(i);
if(c==32 || i==len-1) // 32 denotes space, || i=len-1 is used to mark the end of string input
wordcount++;
if(c!='a' && c!='e' && c!='i' && c!='o' && c!='u' && c!=32) //if c!32 is not mentioned blank spaces will be counted as consonants
cons++;
}
}
void Display()
{
System.out.println("Original String : "+str);
System.out.println("Number of words : "+wordcount);
System.out.println("Number of consonants : "+cons);
}
public static void main(String[] args)
{
TheString obj = new TheString("Today is Republic Day.");
obj.countFreq();
obj.Display();
}
}
Question 8 :
A class Admission contains the admission numbers of 100 students . Some of the data members / member functions are given below :
Class Name Admission
Data member/Instance variable
Adno[] : Integer array to store admission numbers
Member Functions/Methods :
Admission() : Constructor to initialize array elements
void fillArray() : to accept elements of array in descending order
int binSearch(int l , int u , int v) : to search for particular admission number (v) using binary search and recursive techniques and returns 1 if found otherwise returns -1
Specify the class Admission giving details of constructor , fillArray() and binSearch(int,int,int). Define a main function to create an object and call functions accordingly to accomplish the task.
Sol :
import java.util.*;
public class Admission
{
int Adno[] = new int [100];
Admission()
{}
void fillArray()
{
Scanner s = new Scanner(System.in);
System.out.println("enter elements");
for (int i = 0 ; i <100 ; i++)
Adno[i]= s.nextInt();
}
int binSearch(int l,int u, int v)
{
int mid = (l+u)/2 ;
if(Adno[mid] == v)
return 1;
else if(l>u)
return -1;
else if (Adno[mid]>v)
return binSearch(l,mid-1,v);
else
return binSearch(mid+1, u , v);
}
public static void main(String args[])
{
Admission obj = new Admission();
obj.fillArray();
System.out.println("Enter value to be searched :");
Scanner s = new Scanner(System.in);
int v = s.nextInt();
int p = obj.binSearch(0,obj.Adno.length-1,v);
if(p==-1)
System.out.println("Object not found!");
else
System.out.println("Object found");
}
}
Please note that the above question asks for implementing binary search using recursive technique . If you do not follow this instruction you will be penalized . Otherwise a simple program to write .
Question 10 :
A class Merger concatenates two positive integers that are greater than 0 and produces a new merged integer.
Example : if the first number is 23 and second is 764 , the new number is 23764.
Some of the members of the class are given below
CLASS Merger
Data Members/ Instance Variables :
n1 : long integer to store first number
n2 : long integer to store second number
mergNum : long integer to store merged number
Member Functions :
Merger() : Constructor to initialize data members
void readnum() : Function to accept values of numbers n1 and n2
void JoinNum() : To concatenate numbers n1 and n2 to store it in mergNum
void show() : To display the original numbers and merged number with appropriate messages .
Specify the class Merger , giving details of constructor , void readNum() , void JoinNum() and void show() . Define main () function to create an object and call the functions accordingly to enable the task .
Sol :
import java.util.*;
public class Merger
{
long n1 , n2 , mergNum;
Merger(){}
void readNum()
{
Scanner s = new Scanner(System.in);
System.out.println("Enter First Number:");
n1 = s.nextLong();
System.out.println("Enter Second Number");
n2 = s.nextLong();
}
void JoinNum()
{
if(n1>0 && n2>0)
{
String s1 = Long.toString(n1);
String s2 = Long.toString(n2);
String s3 = s1 + s2;
mergNum = Long.valueOf(s3);
}
else
System.out.println("One or both the numbers are 0 or less than that , concatenation not possible !");
// It is better to use this if-else loop , since in the question it is mentioned that the class concatenates the string only if it finds out that the numbers are greater than 0 .
}
void show()
{
System.out.println("First Number : "+n1);
System.out.println("Second Number : "+n2);
System.out.println("Merged Number : "+mergNum);
}
public static void main(String[] args)
{
Merger obj = new Merger();
obj.readNum();
obj.JoinNum();
obj.show();
}
}
A class TheString accepts a string of a maximum 100 characters with only a blank space between the words .
Some of the members of the class are given below
Class Name : TheString
Data Member/Instance Variables :
str : To store the string
len : integer to store length of string
wordcount : integer to store number of words
cons : integer to store number of consonants
Member Functions / Methods
TheString() : default constructor to initialize data members
TheString( String ds) : parameterized constructor to assign str = ds
void countFreq() : to count the number of words and 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 constructor , countFreq() , Display() . Define the main function . create an object of the class and call the member functions accordingly to enable the task .
import java.util.*
public class TheString
{
String str;
int len, wordcount, cons;
TheString(){}
TheString(String ds)
{
str = ds;
len = str.length();
}
void countFreq()
{
char c ;
String str1 = str.toLowerCase(); //lower case conversion simplifies the string handling to find the consonants. You can change to uppercase also . But semantics will change a bit to count the consonants.
for (int i = 0 ; i<len; i++)
{
c = str1.charAt(i);
if(c==32 || i==len-1) // 32 denotes space, || i=len-1 is used to mark the end of string input
wordcount++;
if(c!='a' && c!='e' && c!='i' && c!='o' && c!='u' && c!=32) //if c!32 is not mentioned blank spaces will be counted as consonants
cons++;
}
}
void Display()
{
System.out.println("Original String : "+str);
System.out.println("Number of words : "+wordcount);
System.out.println("Number of consonants : "+cons);
}
public static void main(String[] args)
{
TheString obj = new TheString("Today is Republic Day.");
obj.countFreq();
obj.Display();
}
}
Synopsis
Nothing much needs to be mentioned . The first two questions are pretty straight forward and should not consume much time to write . The third one has two points to keep in mind :
1) To count the number of words , you have to consider the spaces . But what about the sentence that does not end with a space ? So you have to check where the string ends too otherwise you will end up getting a word less than the actual number of words in original string.
2) We usually check if a character is a vowel to count the consonants , but here the string contains spaces , so if we do not mention it in the condition checking not to consider the space as a consonant , the number of spaces present also gets added to the number of consonants.
So in my opinion in the examination hall you might not get adequate time to think on these points and also you cannot avail yourself of the compiler to find out where you have gone wrong so it is better to avoid the third program or Question no 10.