Section B : 2014 Solved Theory
Question :8
A class Mixer………………enable the task.
import
java.util.*;
public
class Mixer
{
intarr[]=new
int[100];
int
n;
Mixer(intnn)
{
n=nn;
}
void accept()
{
Scanner in=new Scanner(System.in);
inti;
System.out.println("Enter
"+n+" elements in array in ascending order : ");
for(i=0;i<n;i++)
{
arr[i]=in.nextInt();
}
}
Mixer mix(Mixer A)
{
Mixer B=new Mixer(n+A.n);
inti,j=0;
for(i=0;i<n;i++)
{
B.arr[j++]=arr[i];
}
for(i=0;i<A.n;i++)
{
B.arr[j++]=A.arr[i];
}
return (B);
}
void display()
{
inti;
System.out.println("Mixed
Array : ");
for(i=0;i<n;i++)
{
System.out.print(arr[i]+"
");
}
}
public static void main()
{
Scanner in=new Scanner(System.in);
inta,b;
System.out.println("Enter
the size of first array : ");
a=in.nextInt();
System.out.println("Enter
the size of second array : ");
b=in.nextInt();
Mixer ob=new Mixer(a);
Mixer ob1=new Mixer(b);
Mixer ob2=new Mixer(a+b);
ob.accept();
ob1.accept();
ob2=ob.mix(ob1);
ob2.display();
}
}
Question : 9 (a)
A
class SeriesSum is designed……………. enable the task.
import
java.util.*;
public
class SeriesSum
{
intx,n;
double sum;
SeriesSum(intxx,intnn)
{
x=xx;
n=nn;
}
double findfact(int m)
{
if(m==1)
{
return (1);
}
else
{
return (m*findfact(m-1));
}
}
double findpower(intx,int y)
{
if(y==0)
{
return (1);
}
else
{
return (x*findpower(x,(y-1)));
}
}
void calculate()
{
inti;
for(i=2;i<=n;i=i+2)
{
sum=sum+findpower(x,i)/findfact(i-1);
}
}
void display()
{
System.out.println("Sum
of series : "+sum);
}
public static void main()
{
Scanner in=new Scanner(System.in);
inti,j;
System.out.println("Enter
the value of 'X' : ");
i=in.nextInt();
System.out.println("Enter
the number of terms : ");
j=in.nextInt();
SeriesSumob=new
SeriesSum(i,j);
ob.calculate();
ob.display();
}
}
Question : 9 (b)
State
two difference between iteration and recursion.
Iteration
:i. It is a fast process.
ii. It takes less memory.
Recursion
:i. Is is a slow process.
ii. It takes more memory.
Question : 10
A
sequence of Fibonacci strings…..enable the task.
Sample
Output :a, b, ab, bab, abbab,……n terms
import
java.util.*;
public
class FiboString
{
String x,y,z;
int
n;
FiboString()
{
x="a";
y="b";
z="ba";
}
void accept()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter
the number of terms : ");
n=in.nextInt();
}
void generate()
{
inti;
System.out.println("Fibonacci
String : ");
System.out.print(x+"
, "+y+" , "+z);
for(i=4;i<=n;i++)
{
System.out.print("
, "+(z+y));
x=y;
y=z;
z=y+x;
}
}
public static void main()
{
FiboStringob=new
FiboString();
ob.accept();
ob.generate();
}
No comments:
Post a Comment