Section B : 2012 Solved Theory
Question : 8
A class Combine contains ..........................
Sample Input :
1 2 3 4 5 and 8 9 10 11 45 98
Sample Output : 1 2 3 4 5 8 9 10 11 45 98
import java.util.*;
public class Combine
{
Scanner in=new Scanner(System.in);
int size;
int com[]=new int[100];
Combine(int nn)
{
size=nn;
}
void inputarray()
{
System.out.println("Enter "+size+" elements in array :");
int i;
for(i=0;i<size;i++)
{
com[i]=in.nextInt();
}
}
void sort()
{
int i,j,max=0;
for(i=0;i<size;i++)
{
for(j=0;j<size-1;j++)
{
if(com[j]>=com[j+1])
{
max=com[j];
com[j]=com[j+1];
com[j+1]=max;
}
}
}
}
void mix(Combine A,Combine B)
{
int i;
for(i=0;i<A.size;i++)
{
com[i]=A.com[i];
}
for(i=0;i<B.size;i++)
{
com[i+A.size]=B.com[i];
}
}
void display()
{
int i;
for(i=0;i<size;i++)
{
System.out.print(com[i]+"\t");
}
}
public static void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of 1st array :");
int n=in.nextInt();
System.out.println("Enter the size of 2nd array :");
int m=in.nextInt();
Combine C=new Combine(n);
Combine D=new Combine(m);
Combine E=new Combine(m+n);
C.inputarray();
D.inputarray();
E.mix(C,D);
E.sort();
E.display();
}
}
public class Combine
{
Scanner in=new Scanner(System.in);
int size;
int com[]=new int[100];
Combine(int nn)
{
size=nn;
}
void inputarray()
{
System.out.println("Enter "+size+" elements in array :");
int i;
for(i=0;i<size;i++)
{
com[i]=in.nextInt();
}
}
void sort()
{
int i,j,max=0;
for(i=0;i<size;i++)
{
for(j=0;j<size-1;j++)
{
if(com[j]>=com[j+1])
{
max=com[j];
com[j]=com[j+1];
com[j+1]=max;
}
}
}
}
void mix(Combine A,Combine B)
{
int i;
for(i=0;i<A.size;i++)
{
com[i]=A.com[i];
}
for(i=0;i<B.size;i++)
{
com[i+A.size]=B.com[i];
}
}
void display()
{
int i;
for(i=0;i<size;i++)
{
System.out.print(com[i]+"\t");
}
}
public static void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of 1st array :");
int n=in.nextInt();
System.out.println("Enter the size of 2nd array :");
int m=in.nextInt();
Combine C=new Combine(n);
Combine D=new Combine(m);
Combine E=new Combine(m+n);
C.inputarray();
D.inputarray();
E.mix(C,D);
E.sort();
E.display();
}
}
Question : 9
Design a class VowelWord to accept ............................
Sample Input : You are learning at Sharma Tutorials Jamshedpur
Sample Output : Number of words starts with vowel = 2
import java.util.*;
public class VowelWord
{
String str;
int freq;
VowelWord()
{
str="";
freq=0;
}
void readstr()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter your sentence : ");
str=in.nextLine();
}
void freq_vowel()
{
int i;
String w[];
w=str.split(" ");
for(i=0;i<w.length;i++)
{
if((w[i].charAt(0)=='A')||(w[i].charAt(0)=='E')||(w[i].charAt(0)=='I')||(w[i].charAt(0)=='O')||(w[i].charAt(0)=='U')||(w[i].charAt(0)=='a')||(w[i].charAt(0)=='e')||(w[i].charAt(0)=='i')||(w[i].charAt(0)=='o')||(w[i].charAt(0)=='u'))
{
freq++;
}
}
}
void display()
{
System.out.println("Original Sentence : "+str);
System.out.println("Number of words starts with vowel : "+freq);
}
public static void main()
{
VowelWord ob=new VowelWord();
ob.readstr();
ob.freq_vowel();
ob.display();
}
}
public class VowelWord
{
String str;
int freq;
VowelWord()
{
str="";
freq=0;
}
void readstr()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter your sentence : ");
str=in.nextLine();
}
void freq_vowel()
{
int i;
String w[];
w=str.split(" ");
for(i=0;i<w.length;i++)
{
if((w[i].charAt(0)=='A')||(w[i].charAt(0)=='E')||(w[i].charAt(0)=='I')||(w[i].charAt(0)=='O')||(w[i].charAt(0)=='U')||(w[i].charAt(0)=='a')||(w[i].charAt(0)=='e')||(w[i].charAt(0)=='i')||(w[i].charAt(0)=='o')||(w[i].charAt(0)=='u'))
{
freq++;
}
}
}
void display()
{
System.out.println("Original Sentence : "+str);
System.out.println("Number of words starts with vowel : "+freq);
}
public static void main()
{
VowelWord ob=new VowelWord();
ob.readstr();
ob.freq_vowel();
ob.display();
}
}
Question : 10
A happy number.................................
Sample Input : 28 = 2² + 8² = 4 + 64 = 68
68 = 6² + 8² = 36 + 64 = 100
100 = 1² + 0² + 0² = 1 + 0 + 0 = 1
Sample Output : Hence 28 is a happy number.
import java.util.*;
public class Happy
{
int n;
Happy()
{
n=0;
}
void getnum(int nn)
{
n=nn;
}
int sum_sq_digits(int x)
{
if(x==0)
{
return (0);
}
else
{
return (((x%10)*(x%10))+sum_sq_digits(x=x/10));
}
}
void ishappy()
{
int t=n;
while(t>9)
{
t=sum_sq_digits(t);
}
if(t==1)
{
System.out.println(n+" is a Happy Number");
}
else
{
System.out.println(n+"is not a Happy Number");
}
}
public static void main()
{
Scanner in=new Scanner(System.in);
Happy ob=new Happy();
int m;
System.out.println("Enter your number to check if it is Happy Number : ");
m=in.nextInt();
ob.getnum(m);
ob.ishappy();
}
}
public class Happy
{
int n;
Happy()
{
n=0;
}
void getnum(int nn)
{
n=nn;
}
int sum_sq_digits(int x)
{
if(x==0)
{
return (0);
}
else
{
return (((x%10)*(x%10))+sum_sq_digits(x=x/10));
}
}
void ishappy()
{
int t=n;
while(t>9)
{
t=sum_sq_digits(t);
}
if(t==1)
{
System.out.println(n+" is a Happy Number");
}
else
{
System.out.println(n+"is not a Happy Number");
}
}
public static void main()
{
Scanner in=new Scanner(System.in);
Happy ob=new Happy();
int m;
System.out.println("Enter your number to check if it is Happy Number : ");
m=in.nextInt();
ob.getnum(m);
ob.ishappy();
}
}
No comments:
Post a Comment