You are on page 1of 254

Acknowledgement

With regards to my project I would like to thank


everyone who offered help, guideline and support
whenever required.
First and foremost I would like to express gratitude
to my computer teacher for her support and
guidance in my project work. I am extremely grateful
to my guardians for their guidance and their
suggestions.
I would like to give special thanks to my friends who
have supported me to complete my project. And
lastly I would like to express my gratitude to my
parents for helping me in all my queries regarding my
project work.

INDEX
ARRAY
STRING MANUPULATION
STACK
QUEUE
DEQUE
LINKED LIST
RECURSION
INHERITANCE
PATTERN
MISCELLANEOUS
FILES

ARRAY

QUESTION:

Write a program to input names by the user in an array


and arrange these names in ascending order using
insertion sort method.

ALGORITHM:
STEP 1: START
STEP 2:import java.io.*;
STEP 3:Declare class Insertion
STEP 4:Declare int n, int i, int j
STEP 5:Input number of names to be
entered by the

user.

STEP 6:Initialise the array nm[ ]


STEP 7:Input the names by the user.
STEP 8: Sort the array nm[ ] using
insertion sort.

STEP 9:Display the sorted names in the


array.
STEP 10:Close of class Insertion.
STEP 11:STOP
import java.io.*;
class Insertion
{
public static void main(String args[])throws
IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int n,i,j;
System.out.println("Enter the number of
names");
n=Integer.parseInt(br.readLine());

String[]nm=new String[n];
for(i=1;i<=n-1;i++)
{
System.out.println("Enter a name");
nm[i]=br.readLine();
}
for(i=1;i<=n-1;i++)
{
String t;
t=nm[i];
for(j=i-1;j>=0 &&
t.compareTo(nm[j])<0;j--)
{
nm[j+1]=nm[j];
}
nm[j+1]=t;
}
System.out.println("Sorted Array");

for(i=0;i<=n-1;i++)
{
System.out.print(nm[i]+" ");
}
}
}

OUTPUT:
Enter the number of names
3
Enter a name
Ankita
Enter a name

Aayush
Enter a name
Swasti
Sorted Array
Aayush Ankita Swasti

QUESTION:
Define a class named as Sorting having following
members:
Class - Sorting
Data Members-

Array a,b,c of size 5,5,10 respectively(each of integer type)


Member Functions

i) input1()-enter elements of first array(a[]) by the user.


ii) input2()-enter elements of second array(b[]) by the
user.
iii) sort1()-arrange elements of first array in descending
order. using selection sort method.
iv) sort2()-arrange elements of second array in descending
order using insertion sort method.

v) merge()-apply merge sort method to merge a[] and b[]


in c[] in ascending order.
vi) disp()-display elements of the merged array.

ALGORITHM:
STEP 1:START
STEP 2:import java.io.*;
STEP 3:Declare class Merge
STEP 4:Declare a[ ] of size 5,b[ ] of size
5 and c[ ]
of size 10.
STEP 5:Declare input1( ) to input
numbers in a[ ] STEP 6: Declare input2(
) to input numbers in b[ ]
STEP 7:Sort the elements of a[ ] in
descending order
sort in sort1( ).

using selection

STEP 8: Sort the elements of b[ ] in


descending order using insertion sort in
sort2( ).
STEP 9:Declare merge( ) to merge array
a and b in c and sort the elements of c[
] in ascending order.
STEP 10:Declare disp( ) to display
elements of c[ ].
STEP 11:STOP

import java.io.*;
class Merge

{
int a[]=new int[5];
int b[]=new int[5];
int c[]=new int[10];
void input1()throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the elements in array ");
for(int i=0;i<=5;i++)
{
a[i]=Integer.parseInt(br.readLine());
}
}
void input2()throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));

System.out.println("Enter the elements in array ");


for(int i=0;i<=5;i++)
{
b[i]=Integer.parseInt(br.readLine());
}
}
void sort1()
{
for(int i=0;i<=3;i++)
{
int s=a[i];
int p=i;
for(int j=1;j<=4;j++)
{
if(a[j]>s)
{
s=a[j];
p=j;

}
}
int t=a[i];
a[i]=a[p];
a[p]=t;
}
}
void sort2()
{
int i,j,t;
for(i=1;i<=4;i++)
{
t=b[i];
for( j=i-1;j>=0 && t>b[j];j--)
{
b[j+1]=b[j];
}
b[j+1]=t;

}
}
void merge()
{
int i,j,k;
i=4;
j=4;k=0;
while(i>=0 && j>=0)
{
if(a[i]<b[j])
c[k++]=a[i--];
else if(a[i]>b[j])
c[k++]=b[j--];
else
{
c[k++]=a[i--];
c[k++]=b[j--];
}

}
if(i<0)
{
while(j>=0)
{
c[k++]=b[j--];
}
}
if(j<0)
{
while(i>=0)
{
c[k++]=a[i--];
}
}
}
void disp( )
{

System.out.println("Sorted array:");
for(int k=0;k<=9;k++)
{
System.out.print(c[k]+" ");
}
}
public static void main(String args[])throws
IOException
{
Merge ob=new Merge();
ob.input1();
ob.input2();
ob.sort1();
ob.sort2();
ob.merge();
ob.disp();
}

OUTPUT:
Enter the elements in the array
3
4
2
9
1
Enter the elements in the array
5
7
6

10
8
Sorted array:
1 2 3 4 5 6 7 8 9 10

QUESTION:
Write a program to input 20 names in an array using
bubble sort and then search a name in the array using
binary search.

ALGORITHM
STEP 1:START
STEP 2:import java.io.*;
STEP 3:Declare class names
STEP 4:Declare String[ ] st , int i, j, k
STEP 5: Declare input( ) to input 20 names
STEP 6: Declare sort( ) to sort the array using
bubble sort and display the sorted array.

STEP 7:Declare search( ) to search a name and


display "Found at index" if found in the given
array.
STEP 8: Declare main() and create object of
class.
STEP 9:Call the methods of the class
STEP 10:Close of class names.
STEP 11:STOP

import java.io.*;
class names
{

//class

begins
String[] st=new String[20];
//integer array

int i,j,k;
String t,x;
void input()throws IOException
//for input of names
{
BufferedReader I=new BufferedReader(new
InputStreamReader (System.in));
for(i=0;i<=19;i++)
{
System.out.println("Enter a name");
st[i]=I.readLine();
}
}
void sort()
perform sorting
{
for(i=0;i<=18;i++)
{

//to

for(j=0;j<=18-i;j++)
{
if(st[j].compareTo(st[j+1])>0)
{
t=st[j];
st[j]=st[j+1];
st[j+1]=t;
}
}
}
System.out.println("SORTED ARRAY");

for(i=0;i<=19;i++)
{
System.out.println(st[i]);
}
}

void search()throws IOException


//for searching
{
BufferedReader I=new BufferedReader(new
InputStreamReader (System.in));
System.out.println("Enter a name to be
searched");
x=I.readLine();
i=0;
j=19;
while(i<j)
{
k=(i+j)/2;
if(st[k].compareTo(x)>0)
{
i=k+1;
}
else if(st[k].compareTo(x)<0)

{
j=k-1;
}
else
{
System.out.println("Found at index:"+k);
}
}
}
}

//class

ends

class q14
{

//class

begins
public static void main(String args[])throws
IOException

//main

function begins
name ob=new name();
ob.input();
ob.sort();
ob.search();
}
}
//main function ends

OUTPUT:
Enter a name
Swasti
Enter a name
Gargi

Enter a name
Niharika
Enter a name
Alisha
Enter a name
Riya
Enter a name
Stuti
Enter a name
Prachi
Enter a name
Monika
Enter a name
Vanshika
Enter a name
Muskan
Enter a name
Mehek

Enter a name
Manan
Enter a name to be searched
Niharika
SORTED ARRAY
Alisha
Ananya
Ansh
Anshul
Deepika
Gargi
Manan
Mehek
Monika
Muskan
Niharika
Prachi

Preeti
Radhika
Riya
Saumya
Stuti
Swasti
Vanshika
Varalika

QUESTION:
Write a program to illustrate quick sort method in an
array.

ALGORITHM
STEP 1:START
STEP 2:import java.io.*;
STEP 3:Declare class QSort

STEP 4:Declare int left, int right,int pivot


STEP 5:Declare function sort(int a[ ],int l, int r)
STEP 6: Sort the values till l is less than r.
STEP 7: Arrange the values of left,right and
pivot and recursively call function sort( )
according to if l is less than pivot or r is greater
than pivot.
STEP 8:Declare main( ) and create object of the
class and call sort( ) and display the elements of
sorted array.
STEP 9:Close of class QSort.
STEP 10:STOP

class QSort
{
int left,right,pivot;
void sort(int[] a,int l,int r)
{
left=l;
right=r;
pivot=a[l];
while(l<r)
{
while(a[r]>=pivot && l<r)
r--;
if(l!=r)
{
a[l]=a[r];
l++;

}
while(a[l]<=pivot && l<r)
l++;
if(l!=r)
{
a[r]=a[l];
r--;
}
}
a[l]=pivot;
pivot=l;
l=left;
r=right;
if(l<pivot)
sort(a,l,pivot-1);
if(r>pivot)
sort(a,pivot+1,r);
}

public static void main(String args[])


{
QSort ob=new QSort();
int[] x={4,2,1,6,3,5};
ob.sort(x,0,5);
for(int i=0;i<=5;i++)
System.out.print(x[i]+" ");
}
}

OUTPUT:
123456

STRING
MANUPULATION

QUESTION:
Write a program to input a number by the user and
display its digit in the original order separated with
comma.

ALGORITHM:
STEP 1:START
STEP 2:import java.io.*;
STEP 3:Declare class Number
STEP 4:Declare int st[ ], int top, int size
STEP 5:Declare parameterized
constructor(int x) to assign size=x and
top=-1.
STEP 6: Declare function push(int y) to push
the number in the stack.
STEP 7: Declare function char pop( ) to pop
elements.
STEP 8:Declare main( ) and create object of
the class .

STEP 9:Input the number by a user .


STEP 10:Declare int m=n and int c=0 .
STEP 11:Extract each digit of the number
and push in the stack.
STEP 12:Now display the popped digit
separated from the comma.
STEP 13:Close of class Number
STEP 14:STOP

import java.io.*;
class Number
{
int []st;
int top,size;
Number(int z)
{

size=z;
st=new int[size];
top=-1;
}
void push (int c)
{
if(top==size-1)
System.out.println("Overflow");
else
st[++top]=c;
}
int pop()
{
if(top==-1)
{
System.out.println("Underflow");
return(-1);
}

else
return(st[top--]);
}
public static void main (String args[])throws
IOException
{
BufferedReader br=new BufferedReader (new
InputStreamReader(System.in));
int n,ch=0;
System.out.println("Enter a no.");
n=Integer.parseInt(br.readLine());
int m=n;
while(m>0)
{
ch++;
m=m/10;
}

Number S=new Number(ch);


while(n>0)
{
S.push(n%10);
n=n/10;
}
for(int i=1;i<=ch;i++)
{
if(i==ch)
System.out.println(S.pop());
else
System.out.println(S.pop()+",");
}
}
}

OUTPUT:
Enter a number
234
2,3,4

QUESTION:
Write a program to calculate the date of submission of
project by a student.

ALGORITHM:
STEP 1:START
STEP 2:import java.io.*;
STEP 3: Declare class Project.
STEP 4:Declare main();
STEP 5: Declare int d,int m,int y,int
nu,int d1,int m1,int y1,int n,int l and int
f;

STEP 6:Input the date of starting the


project and the number of days given
to complete it from the user.
STEP 7:Calculate the date of submission.
STEP 8:Display the initial date,number
of days and date of submission.
STEP 9:STOP

import java.io.*;
class project
{

public static void main (String args[]) throws


IOException
{
int d,m,y,nu, d1,m1,y1,n,l,f;
BufferedReader i = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter the date of starting the
project in

DD/MM/YY format:");

d= Integer.parseInt(i.readLine());
m= Integer.parseInt(i.readLine());
y= Integer.parseInt(i.readLine());
d1=d;
m1=m;
y1=y;

System.out.println("enter number of days given


to complete the project:");
nu= Integer.parseInt(i.readLine());
n=nu;
while(n!=0)
{
if(m1==1||m1==3||m1==5||m1==7||m1==8||m1==
10||m1==12)
l=31;
else if(m1==4||m1==6||m1==9||m1==11)
l=30;
else
{
if(y1%4==0)

l=29;
else
l=28;
}
f=l-d1;
if(n<=f)
{
d1=d1+n;
n=0;
}
else
{
d1=1;
n=n-(f+1);

m1=m1+1;
if(m1==13)
{
m1=1;
y1=y1+1;
}
}
}
System.out.println("The date of starting the project
is:
+d+"/"+m+"/"+y);
System.out.println("number of days given to
complete the project are:"+nu);

System.out.println("The project submission date


is: +d1+"/"+m1+"/"+y1);
}
}

OUTPUT:
enter the date of starting the project in
DD/MM/YY format
30
10
2013
enter number of days given to complete the
project
15
The date of starting the project is 30/10/2013

number of days given to complete the project


are15
The project submission date is 14/11/2013

QUESTION:
Write a program to accept any three letters and print all
the probable combinations(no combinations should be
repeated).

import java.io.*;

class Combination
{
public static void main(String args[])throws
IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
char ch[]=new char[3];
System.out.println("Enter the 3 letters");
for (int i=0;i<3;i++)
{
char x=(char)br.read();
}
System.out.println("THE PROBABLE
COMBINATION IS:");
System.out.println(ch[0]+ch[1]+ch[2]);

System.out.println(ch[0]+ch[2]+ch[1]);
System.out.println(ch[1]+ch[2]+ch[0]);
System.out.println(ch[1]+ch[0]+ch[2]);
System.out.println(ch[2]+ch[1]+ch[0]);
System.out.println(ch[2]+ch[0]+ch[1]);
}
}

OUTPUT:
Enter the 3 letters
A
B
C

THE PROBABLE COMBINATION IS:


ABC
BCA
BAC
CAB
CBA

QUESTION:
Write a program to calculate the day of the date input by
the user.

import java.io.*;
public class calender
{
public static void main(String args[])throws
IOException

{
InputStreamReader read=new
InputStreamReader(System.in);
BufferedReader in= new BufferedReader(read);
int dd,mm,tt,k;tt=0;
System.out.println("Enter the date of which
you want see the day of week in 2012");
dd=Integer.parseInt(in.readLine());
System.out.println("Enter the month of the
date of which you want see the day of week in
2012");
mm=Integer.parseInt(in.readLine());
if (dd<=0||dd>31||mm<=0||mm>12)
{
System.out.println("Invalid date");
}

else
{
if(mm==1)
{
tt=dd;
}
else if(mm==2)
{
tt=31+dd;
}
else if(mm==3)
{
tt=60+dd;
}
else if(mm==4)
{

tt=91+dd;
}
else if(mm==5)
{
tt=121+dd;
}
else if(mm==6)
{
tt=152+dd;
}
else if(mm==7)
{
tt=182+dd;
}
else if(mm==8)
{

tt=213+dd;
}
else if(mm==9)
{
tt=244+dd;
}
else if(mm==10)
{
tt=274+dd;
}
else if(mm==11)
{
tt=305+dd;
}
else if(mm==12)
{

tt=335+dd;
}
k=tt%7;
switch(k)
{
case 0:
System.out.println("SATURDAY");
break;
case 1:
System.out.println("SUNDAY");
break;
case 2:
System.out.println("MONDAY");
break;
case 3:
System.out.println("TUESDAY");

break;
case 4:
System.out.println("WEDNESDAY");
break;
case 5:
System.out.println("THUSDAY");
break;
case 6:
System.out.println("FRIDAY");
break;
default:
System.out.println("INVALID CHOICE");
break;
}
}
}

OUTPUT:
Enter the date of which you want see the day of
week in 2012
30
Enter the month of the date of which you want
see the day of week in 2012
10
TUESDAY

QUESTION:
Write a program to calculate the age of a person.

import java.io.*;
class age
{
public static void main (String args[]) throws
IOException
{
int ddb,mmb,yyb,ddc, mmc,yyc,d,m,y,ds;
BufferedReader i = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter your date of birth and
current date in DD/MM/YY format");
ddb=Integer.parseInt(i.readLine());
mmb= Integer.parseInt(i.readLine());
yyb= Integer.parseInt(i.readLine());

ddc= Integer.parseInt(i.readLine());
mmc= Integer.parseInt(i.readLine());
yyc= Integer.parseInt(i.readLine());
if(mmb==1||mmb==3||mmb==5||mmb==7||mmb=
=8||mmb==10||mmb==12)
ds=31;
if(mmb==4||mmb==6||mmb==9||mmb==11)
ds=30;
else
{
if(yyb%4==0)
ds=29;
else
ds=28;
}
if(mmc>=mmb)

{
y=yyc-yyb;
if(ddc>=ddb)
{
m=mmc-mmb;
d=ddc-ddb;
}
else
{
m=mmc-mmb-1;
d=ds-ddb+ddc;
}
}
else
{
y=yyc-yyb-1;

m=12-mmb+mmc;
if(ddc>=ddb)
d=ddc-ddb;
else
{
m=12-mmb+mmc-1;
d=ds-ddb+ddc;
}
}
System.out.println("Your date of birth is"+"
"+ddb+"/"+mmb+"/"+yyb);
System.out.println("current date is"+" "+
ddc+"/"+mmc+"/"+yyc);
System.out.println("Your age is"+" "+y+"
"+"years"+" "+m+" "+"months"+" "+d+"
"+"days");

}
}

OUTPUT:
Enter your date of birth and current date in
DD/MM/YY format
9
11
1995

Your date of birth is 9/11/1995


current date is 30/10/2013
Your age is 17 years 11 months 21 days

STACK

QUESTION:
Write a program to input a postfix expression and
evaluate this expression using stack.

ALGORITHM
STEP 1:START
STEP 2:import java.io.*;
STEP 3:Declare class Postfix
STEP 4:Declare int st[ ], int top, int size
STEP 5:Declare parameterized constructor(int z)
to assign size=z and top=-1.
STEP 6: Declare function push(int c)to push the
number in the stack.
STEP 7: Declare function int pop( ) to pop the
elements.
STEP 8:Declare main( ) and create object of the
class .
STEP 9:Declare int P1,P2,result , l ,i
STEP 10:Input the postfix expression by the user
and calculate its length.

STEP 11:Extract each element of the expression


from the beggining and push in the stack if it is
a digit.
STEP 12: Else pop P1 and P2 and perform the
necessary calculation according to the operand.
STEP 13:Now the push the result it the
expression is evaluated.
STEP 14:Now pop the result and display it.
STEP 15:Close of class Postfix.
STEP 16:STOP
import java.io.*;
class Postfix
{
int [] st;
int top,size;
Postfix(int z)
{

size =z;
st=new int[size];
top=-1;
}
void push (int c)
{
if(top==size-1)
System.out.println("Overflow");
else
st[++top]=c;
}
int pop()
{
if(top==-1)
{
System.out.println("Underflow");
return(-1);

}
else
return(st[top--]);
}
public static void main (String args[])throws
IOException
{
BufferedReader br=new BufferedReader (new
InputStreamReader(System.in));
int P1,P2;
int result;
String post;
System.out.println("Enter a postfix expression");
post=br.readLine();
int l=post.length();
int i;
Postfix ob=new Postfix(l);
for(i=0;i<=l-1;i++)

{
char c=post.charAt(i);
if(Character.isDigit(c))
ob.push((int)(c-'0'));
else
{
P2=ob.pop();
P1=ob.pop();
switch(c)
{
case'+':
result=P1+P2;
break;
case'-':
result=P1-P2;
break;
case'*':
result=P1*P2;

break;
case'/':
result=P1/P2;
break;
case'^':
result=(int)Math.pow(P1,P2);
break;
}
ob.push(result);
}
}
result=ob.pop();
System.out.println(result);
}
}

OUTPUT:

47+23^!^2^-1-54

QUESTION:
Write a program to input a string and display its letters in
reverse order using stack.

ALGORITHM:
STEP 1:START
STEP 2:import java.io.*;
STEP 3:Declare class Reverse
STEP 4:Declare char st[ ], int top, int
size
STEP 5:Declare parameterized
constructor(int z) to assign size=z and
top=-1.

STEP 6: Declare function push(char c) to


push the
characters in the stack.
STEP 7: Declare function char pop( ) to
pop the
elements.
STEP 8:Declare main( ) and create object
of the class.
STEP 9:Input the string by a user and
claculate its length STEP 10:Push each
character of the string one by one.
STEP 11:Now pop each character from
the stack.
STEP 12:Close of class Reverse
STEP 13:STOP

import java.io.*;
class Reverse
{
char []st;
int top,size;
Reverse(int z)
{
size=z;
st=new char[size];
top=-1;
}
void push(char c)
{
if(top==size-1)
System.out.println("Overflow");
else
st[++top]=c;

}
char pop()
{
if(top==-1)
{
System.out.println("Underflow");
return(' ');
}
else
return(st[top--]);
}
public static void main (String args[])throws
IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String str;
System.out.println("Enter a string");

str=br.readLine();
int l=str.length();
Reverse S=new Reverse(l);
for (int i=0;i<=l-1;i++)
{
char c=str.charAt(i);
S.push(c);
}
for (int i=0;i<=l-1;i++)
{
System.out.print(S.pop());
}
}
}

INPUT:
Enter a string
reverse

OUTPUT:
esrever

QUESTION:

Write a program to input a prefix


expression and evaluate this expression
using stack.

ALGORITHM
STEP 1:START
STEP 2:import java.io.*;
STEP 3:Declare class Prefix
STEP 4:Declare int st[ ], int top, int size
STEP 5:Declare parameterized constructor(int z)
to assign size=z and top=-1.
STEP 6: Declare function push(int c)to push the
number in the stack.
STEP 7: Declare function int pop( ) to pop the
elements.
STEP 8:Declare main( ) and create object of the
class .
STEP 9:Declare int P1,P2,result , l ,i

STEP 10:Input the postfix expression by the user


and calculate its length.
STEP 11:Extract each element from the end of
the expression of the expression and push in
the stack if it is a digit.
STEP 12: Else pop P1 and P2 and perform the
necessary calculation according to the operand.
STEP 13:Now the push the result it the
expression is evaluated.
STEP 14:Now pop the result and display it.
STEP 15:Close of class Prefix.
STEP 16:STOP
import java.io.*;
class Prefix
{
int [] st;
int top,size;
Prefix(int z)

{
size =z;
st=new int[size];
top=-1;
}
void push (int c)
{
if(top==size-1)
System.out.println("Overflow");
else
st[++top]=c;
}
int pop()
{
if(top==-1)
{
System.out.println("Underflow");

return(-1);
}
else
return(st[top--]);
}
public static void main (String args[])throws
IOException
{
BufferedReader br=new BufferedReader (new
InputStreamReader(System.in));
int P1,P2;
int result;
String pre;
System.out.println("Enter a prefix expression");
pre=br.readLine();
int l=pre.length();
int i;
Prefix ob=new Prefix(l);

for(i=l-1;i>=0;i--)
{
char c=pre.charAt(i);
if(Character.isDigit(c))
ob.push((int)(c-'0'));
else
{
P1=ob.pop();
P2=ob.pop();
switch(c)
{
case'+':
result=P1+P2;
break;
case'-':
result=P1-P2;
break;
case'*':

result=P1*P2;
break;
case'/':
result=P1/P2;
break;
case'^':
result=(int)Math.pow(P2,P1);
break;
}
ob.push(result);
}
}
result=ob.pop();
System.out.println(result);
}

OUTPUT:
//^232^-4*212
1

QUESTION:
Class-Repeat
Data Members-

i) st[]-stack array to hold maximum 100 integers.


ii) cap-capacity of the array.
iii) f-front
iv) r-rear
Member Functions-

i) Repeat(int m)-constructor to initialize


cap=m
f=r=-1
ii) void pushvalue(int v)-to add integers from rear index if
possible also display "Overflow".
iii) int popvalue()-to remove and return from front index
if
queue is empty then return -9999.
iv) void disp()-displays the elements present in the queue.

ALGORITHM
STEP 1:START

STEP 2:import java.io.*;


STEP 3:Declare class Repeat
STEP 4:Declare int st[ ], int f,int r, int cap
STEP 5:Declare parameterized constructor(int m)
to assign size=m and f=-1 and r=-1.
STEP 6: Declare function pushvalue(int v)to
push the number in the queue if possible.
STEP 7: Declare function int popvalue( ) to
remove the number if possible.
STEP 8: Declare function display( )
STEP 9:Declare main( ) and create object of the
class .
STEP 10:Call the methods of the class.
STEP 11:Close of class Repeat
STEP 12:STOP

import java.io.*;
class Repeat
{
int st[]=new int[100];
int cap,f,r;
Repeat(int m)
{
cap=m;
f=r=-1;
}
void pushvalue(int v)

{
if((f==0 && r==cap-1)||(r+1==f))
System.out.print("Overflow");
else
{
if(f==-1)
f=r=0;
else if(r==cap-1)
r=0;
else
r=r+1;
st[r]=v;
}
}
int popvalue()
{
if(f==-1)
{

System.out.print("Underflow");
return(-9999);
}
else
{
int a=st[f];
if(f==r)
f=r=-1;
else if(f==cap-1)
f=0;
else
f++;
return(a);
}
}
public void display()
{

for(int i=0;i<=cap;i++)
System.out.println(st[i]);
}
public static void main(String args[])throws
IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
Repeat ob=new Repeat(10);
for(int i=0;i<=9;i++)
{
System.out.println("Enter a value");
i=Integer.parseInt(br.readLine());
ob.pushvalue(i);
}
int y=ob.popvalue();

ob.display();
}
}

OUTPUT:
Enter a value
45
Enter a value
23
Enter a value
56

QUEUE

QUESTION:
Write a program to input a string and insert only those
words of the string in a queue which has odd number of

letters. Display all the inserted words in the same line


separated by a tab gap.

ALGORITHM
STEP 1:START
STEP 2:import java.io.*;
STEP 3:Declare class Queue
STEP 4:Declare String qu[ ], int f,int r, int size
STEP 5:Declare parameterized constructor(int z)
to assign size=z and f=-1 and r=-1.
STEP 6: Declare function insert (String c)to push
the string in the queue.
STEP 7: Declare function String remove( ) to
remove the

elements.

STEP 8:Declare main( ) and create object of the


class .
STEP 9:Declare int x=0 and int N
STEP 10:Claculate the N=number of tokens and
start a loop fom 1 to N and calculate if the

length of the word is odd or not if it is a digit.


If the length is odd then insert the word in the
queue.
STEP 11:Now remove the inserted words and
display them.
STEP 12:Close of class Queue.
STEP 13:STOP
import java.io.*;
class Queue
{
String[] qu;
int f,r,size;
Queue(int z)
{
size=z;
qu=new String[size];
f=r=-1;

}
void insert(String c)
{
if(r==size-1)
System.out.print(" Underflow");
else
qu[++r]=c;
}
String remove()
{
if(f==r)
{
System.out.println("Underflow");
return(" ");
}
else
return(qu[++f]);

}
public static void main(String args[])throws
IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int n,ch,m,x=0;
System.out.println("Enter a String");
String str=br.readLine();
StringTokenzier st=new StringTokenzier(str);
n=str.countToken();
Queue Q=new Queue(n);
for(int i=1;i<=n;i++)
{
String s=str.nextToken();
if(s.lenght()%2!=0)
{
Q.insert(s);

x++;
}
}
for(int i=1;i<=x;i++)
{
System.out.print(Q.remove()+" ");
}
}
}
For example:

INPUT:
I am a girl.

OUTPUT:
Ia

QUESTION:

Create a circular queue of size 13.Insert all 26 alphabets


(capital) in the queue..Remove all the letters which has a
ASCII
value which is divisible by 11.

ALGORITHM
STEP 1:START
STEP 2:import java.io.*;
STEP 3:Declare class Cqueue
STEP 4:Declare char qu[ ], int f,int r, int size
STEP 5:Declare parameterized constructor(int z)
to assign size=z and f=-1 and r=-1.
STEP 6: Declare function insert (char c)to push
the string in the queue if possible.
STEP 7: Declare function char remove( ) to
remove the character if possible.
STEP 8:Declare main( ) and create object of the
class .
STEP 9:Declare two loops. First loop from 1 to 2
and second from 1 to 13.

STEP 10:Now insert each alphabet and increase


x by 1.
STEP 11:Now remove each ASCII code of each
alphabet and if it is divisible by 11 then display
the alphabet.
STEP 12:Close of class Cqueue
STEP 13:STOP
import java.io.*;
class Cqueue
{
char[] qu;
int f,r,size;
Cqueue(int z)
{
size=z;
qu=new char[size];
f=r=-1;

}
void insert(char c)
{
if((f==0 && r==size-1)||(r+1==f))
System.out.println("Overflow");
else
{
if(f==-1)
f=r=0;
else if(r==size-1)
r=0;
else
r=r+1;
qu[r]=c;
}
}
char remove()
{

if(f==-1)
{
System.out.println("Underflow");
return(' ');
}
else
{
char a=qu[f];
if(f==r)
f=r=-1;
else if(f==size-1)
f=0;
else
f++;
return(a);
}
}

public static void main(String args[])throws


IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
Cqueue cq=new Cqueue(13);
int x=65;
for(int i=1;i<=2;i++)
{
for(int j=1;j<=13;j++)
{
cq.insert((char)x);
x++;
}
for(int j=1;j<=13;j++)
{
int y=cq.remove();
if(y%11==0)

System.out.println("Alphabets removed
are:"+(char)y);
}
}
}
}

OUTPUT:
Alphabets removed are:B
Alphabets removed are:M
Alphabets removed are:X

DEQUE

LINKED-LIST

QUESTION:
Link is an entity which can hold a maximum of 100
integers. Link enables the user to add elements from the
rear end and remove integer from the front end of the
entity. Define a class Link with following details:
Data Members

i) lnk[]-entity to hold the integer elements.


ii)max- stores the maximum capacity of the entity.
iii) begin-point to the index of the front end
iv) end-to point to the index of the rear end.
Member Functions
i) Link(int mm)-constructor to initialise
max=mm,begin=0,end=0
ii) void addLink(int v)-to add an element from the rear
index if
possible otherwise display "Out of
size".
iii)int delLink()-to remove and return an element from
the index if possible otherwise display "Empty" and
return
-99.
iv) void display()-displays the elements of the entity.

ALGORITHM
STEP 1:START
STEP 2:import java.io.*;
STEP 3:Declare class Link

STEP 4:Declare int st[ ], int begin,int end, int


max
STEP 5:Declare parameterized constructor(int
mm)to assign size=m and begin=0 and end=0.
STEP 6: Declare function addLink(int v)to push
the number in the queue if possible.
STEP 7: Declare function int delLink( ) to
remove the number if possible.
STEP 8: Declare function display( )
STEP 9:Declare main( ) and create object of the
class .
STEP 10:Call the methods of the class.
STEP 11:Close of class Link
STEP 12:STOP
import java.io.*;
class Link
{

int lnk[]=new int[100];


int max,begin,end;
Link(int mm)
{
max=mm;
begin=end=0;
}
void addLink(int v)
{
if((begin==1 && end==max1)||(end+1==begin))
System.out.println("Out of size");
else
{
if(begin==0)
begin=end=1;
else if(end==max-1)
end=1;

else
end=end+1;
lnk[end]=v;
}
}
int delLink()
{
if(begin==0)
{
System.out.println("Empty");
return(-99);
}
else
{
int a=lnk[begin];
if(begin==end)
begin=end=0;
else if(begin==max-1)

begin=1;
else
begin++;
return(a);
}
}
void display()
{
for(int i=0;i<=max;i++)
{
System.out.println(lnk[i]);
}
}
public static void main(String args[])throws
IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));

System.out.println("Enter the maximum


capacity");
int mm=Integer.parseInt(br.readLine());
Link ob=new Link(mm);
for(int i=0;i<=9;i++)
{
System.out.println("Enter a value");
i=Integer.parseInt(br.readLine());
ob.addLink(i);
}
ob.delLink();
ob.display();
}
}

OUTPUT:
Enter the maximum capacity
5
Enter a value
1
Enter a value
2
Enter a value
3
Enter a value
4
Enter a value
5
Out of size
Enter a value
2
Out of size

QUESTION:

Write a program to illustrate singly sorted Linked-List.

Algorithm
STEP 1:START
STEP 2:Function Create
STEP3:Node pointer,n,ptr,prev
STEP 4:CHAR x
STEP 5:INT CH
STEP 6:DO
STEP 7:DISPLAY "Enter info"
STEP 8:READ x
STEP 9:n=New Node(x,NULL)
STEP 10:IF START=NULL THEN
START=n
STEP 11:ELSE
FOR ptr=START TO ptr!=NULL AND
x>ptr.DATA STEP prev=ptr,ptr=ptr.Next
STEP 12: END FOR
STEP 13:IF ptr=START THEN

START=N
n.NEXT=ptr
STEP 14:ELSE IF ptr=NULL THEN
prev.NEXT=n
n.NEXT=ptr
STEP 15:END IF
STEP 16:END IF
STEP 17:DISPLAY "Enter 1 to continue"
STEP 18: READ CH
STEP 19: WHILE CH=1
STEP 20:STOP

import java.io.*;

class Node
{
char c;
Node next;
Node(char x,Node add)
{
c=x;
next=add;
}
void setData(char x)
{
c=x;
}
char getData()
{
return (c);
}
void setNext(Node add)

{
next=add;
}
Node getData1()
{
return(next);
}
}
class SLL
{
Node start;
BufferedReader br=new BufferedReader (new
InputStreamReader(System.in));
SLL()
{
start=null;
}
void create()throws IOException

{
Node n,ptr;
Node prev;
char a;
int ch;
do
{
System.out.println("Enter info");
a=(char)br.read();
n=new Node (a,null);
if(start==null)
start=n;
else
{
for(ptr=start;ptr!=null&&
a>ptr.getData();prev=ptr,ptr=ptr.getData1());
if (ptr==start)
{

start=n;
n.setNext(ptr);
}
else if(ptr==null)
{
prev.setNext(n);
}
else
{
prev.setNext(n);
n.setNext(ptr);
}
}
System.out.println("enter 1 to continue");
ch=Integer.parseInt(br.readLine());
}while(ch==1);
}

public static void main(String args[])


{
SLL ob=new SLL();
ob.create();
}
}

OUTPUT:
Enter info
A
Enter 1 to continue
Enter info
B

Enter 1 to continue
0

QUESTION:
Write a program to illustrate doubly sorted Linked-List.

Algorithm
STEP 1:START
STEP 2:Function Create
STEP 3:Node pointer,n,ptr,prev
STEP 4:CHAR n
STEP 5:INT CH
STEP 6:DO
STEP 7:DISPLAY "Enter info"
STEP 8:READ n
STEP 9:n=New Node(n,NULL)
STEP 10:IF START=NULL THEN
START=n

LAST=n
STEP 11:ELSE
FOR ptr=START TO ptr!=NULL AND
n>ptr.DATA STEP prev=ptr,ptr=ptr.Next
STEP 12: END FOR
STEP 13:IF ptr=START THEN
START=n
n=NEXT(ptr)
ptr.PRIOR(n)
STEP 14:ELSE IF ptr=NULL THEN
prev.NEXT=n
n.PRIOR=prev
LAST=n
STEP 15: ELSE

prev.NEXT=n

n.PRIOR=prev
n.NEXT=ptr
ptr.PRIOR=n
STEP 16:END IF

STEP 17:END IF
STEP 18:DISPLAY "Enter 1 to continue"
STEP 19: READ CH
STEP 20: WHILE CH=1
STEP 21:STOP

import java.io.*;
class Node
{
char c;
Node next;
Node prior;
Node(char x,Node add)
{

c=x;
next=add;
prior=add;
}
void setData(char x)
{
c=x;
}
char getData()
{
return (c);
}
void setNext(Node add)
{
next=add;
}
Node getData1()

{
return(next);
}
void setprior(Node add)
{
prior=add;
}
Node getprior()
{
return(prior);
}
}
class DLL
{
Node start;
Node last;

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));
DLL()
{
start=null;
last=null;
}
void create()throws IOException
{
Node n,ptr;
Node prev;
char a;
int ch;
do
{
System.out.println("Enter info");
a=(char)br.read();
n=new Node(a,null);

if(start==null)
start=n;
else
{
for(ptr=start;ptr!=null &&
a>ptr.getData();prev=ptr,ptr=ptr.getData1());
if(ptr==start)
{
start=n;
n.setNext(ptr);
ptr.setprior(n);
}
else if(ptr==null)
{
last=n;
n.setprior(prev);
prev.setNext(n);

}
else
{
prev.setNext(n);
n.setprior(prev);
n.setNext(ptr);
ptr.setprior(n);
}
}
System.out.println("Enter 1 to continue");
ch=Integer.parseInt(br.readLine());
}while(ch==1);
}
public static void main(String args[])
{
DLL ob=new DLL();
ob.create();
}

OUTPUT:
Enter info
A
Enter 1 to continue
Enter info
B
Enter 1 to continue O

QUESTION:

Define a class named as Change having following


members:
Class : Change
Data Members:

i) str-stores the word input by the user


ii) newstr-stores the changed word
iii)length-stores the length of the word
Member Functions

i) Change()-default constructor
ii)void inputword():To accept a word input by the user.
iii)char caseConvert(char ch)-converts the case of the
character and return it.
iv) void reckchange(int)-extracts characters using
recursive technique and changes its case using
caseConvert() and forms a new word.
v) void display()-displays both the words.

ALGORITHM
STEP 1:START
STEP 2:import java.io.*;
STEP 3:Declare class Change
STEP 4:Declare String str,newstr int length
STEP 5: Declare default constructor to assign
values to data members.
STEP 6:Declare function inputword( ) to input a
string by the user.
STEP 7: Declare function caseConvert( ) to
change the case of the character.
STEP 8: Declare function reckchange( ) to form
a new string using caseConvert( ).
STEP 9:Declare function display( ) to display the
two strings.
STEP 10:Declare main( ) and create object of the
class and call the methods.
STEP 11:Close of class Change.

STEP 12:STOP

import java.io.*;
class Change
{
String str,newstr;
int length;
Change()
{
str=null;

newstr=null;
length=0;
}
void inputword()throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter a word");
str=br.readLine();
length=str.length();
}
char caseConvert(char ch)
{
if(ch==65 && ch<=90)
{
ch=(char)(ch+32);
}
else if(ch==97 && ch<=122)

{
ch=(char)(ch-32);
}
return(ch);
}
void reckchange(int i)
{
if(i==length)
display();
else
{
char c=str.charAt(i);
newstr=" "+caseConvert(c);
reckchange(i+1);
}
}

void display()
{
System.out.println("Original Word is:-"+str);
System.out.println("New Word is:-"+newstr);
}
public static void main(String args[])throws
IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
Change ob=new Change();
ob.inputword();
ob.reckchange(0);
}
}

OUTPUT:
Enter a word
CoNcAt
Original Word is:-CoNcAt
New Word is:- cOnCaT

RECURSION

QUESTION:
Write a program to print fibonacci series using recursion
up to
term input by the user.

ALOGORITHM:
STEP 1: START
STEP 2: import java.io.*;
STEP 3: Declare class fibonacci
STEP 4: Declare function series(int f,int s,int N)
STEP 5: If (f+s)>N then return.
STEP 6: Else Display the term f+s and call
function series recursively
STEP 7: Declare main() and input the term by
the user.
STEP 8: Close of class fibonacci.
STEP 9: STOP

import java.io.*;
class fibonacci
{
void series(int f,int s,int N)
{
if((f+s)>N)
return;
else
{
System.out.print((f+s)+" ");
series(s,f+s,N);
}

}
public static void main(String args[])throws
IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
fibonacci ob=new fibonacci();
int x;
System.out.println("Enter a no.");
x=Integer.parseInt(br.readLine());
ob.series(1,0,x);
}
}

OUTPUT:
Enter a no.

8
112358

QUESTION:
Define a recursive function to search to search a number
input by the user in a given array using linear search
method.

ALGORITHM:
STEP 1:START
STEP 2:import java.io.*;
STEP 3:Declare class Linear
STEP 4:Declare function search(int[ ] a,int x,int
i)
STEP 5: If i is equal to length then display "Not
found
STEP 6: Else if a[i] is equal to x then display
"Found"
STEP 7: Else recursively call function search( )
STEP 8: Declare main() and create object of
class.
STEP 9:Input a number by the user to be
searched in the given array.

STEP 10:Close of class Linear.


STEP 11:STOP

import java.io.*;
class Linear
{
void search(int[] a,int x,int i)
{
if(i==a.length)
System.out.println("Not found");
else if(a[i]==x)
System.out.println("Found at position:"+i);
else
search(a,x,i+1);

}
public void main(String args[])throws
IOException
{
BufferedReader br=new
BufferedReader(new
InputStreamReader(System.in));
Linear ob=new Linear();
int[] b={3,7,56,4,9,1};
int y;
System.out.println("Enter a number to be
searched");
y=Integer.parseInt(br.readLine());
ob.search(b,y,0);
}
}

OUTPUT:
Enter a number to be searched
9
Found at position:5

QUESTION:
Class Highfact has been defined to find HCF of two
numbers using recursion. This H.C.F. is used to find
L.C.M.
Class-Highfact
Data Members-a,b,hcf,lcm(integer type)
Methods:

i)Highfact()-constructor to assign intial values to data


members.
ii)void getData()-to input a and b
iii)void change()-to swap a and b if a>b
iv)int reckhcf(int, int)-find HCF using recursion
v)int fnlcm(int,int,int)-find LCM using a,b and hcf
vi)void result()-to invoke fnlcm and reckhcf and to print
HCF and LCM of a and b.

ALGORITHM:
STEP 1:START
STEP 2:import java.io.*;
STEP 3:Declare class Highfact
STEP 4:Declare int a, int b, int hcf, int lcm
STEP 5: Declare default constructor to assign
initial values to data members.
STEP 6:Declare function getData( ) to input two
numbers by the user.
STEP 7: Declare function change ( ) to swap and
b if a is greater than b.

STEP 8: Declare function reckhcf(int c,int d ) to


find HCF of two numbers using recursion.
STEP 9:Declare function fnlcm( int c,int d,int hcf)
to find LCM.
STEP 10:Declare function result( ) to print HCF
and LCM of a and b.
STEP 11: Declare main( ) and create object of
the class and call the methods.
STEP 12:Close of class Highfact.
STEP 13:STOP

import java.io.*;

class HighFact
{
int a,b,hcf,lcm;
HighFact()
{
a=0;
b=0;
hcf=0;
lcm=0;
}
void getData()throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter two numbers");
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
}

void change()
{
if(a>b)
{
int t;
t=a;
a=b;
b=t;
}
}
int reckhcf(int c,int d)
{
if(c==d)
return(1);
else
return(d,c%d);
}
int fnlcm(int c,int d,int h)

{
if(c==d)
return(c);
else
int l=(c/h)*(d/h)*h;
return(lcm);
}
void result()
{
hcf=reckhcf(a,b);
lcm=fnlcm(a,b,hcf);
System.out.println("H.C.F ="+hcf);
System.out.println("L.C.M ="+lcm);
}
public static void main(String args[])throws
IOException
{

BufferedReader br=new
BufferedReader(new
InputStreamReader(System.in));
HighFact ob=new HighFact();
ob.getdata();
ob.change();
ob.result();
}
}

OUTPUT:
Enter two numbers
6

8
H.C.F=2
L.C.M=24

QUESTION:
Define a recursive function to search a number input by
the user in a given array using binary search method.

ALGORITHM:
STEP 1:START
STEP 2:import java.io.*;
STEP 3:Declare class Bsearch

STEP 4:Declare function search(int[ ] a,int x,int


i,int j)

STEP 5: If i is greater than j

then display Not found


STEP 6: Else int k=(i+j)/2
STEP 7:If x is greater than a[k] then recursively
call search(a,x,k+1,j)
STEP 8:else If x is less than a[k] then recursively
call search(a,x,i,k+1)
STEP 9:Else dispay"Found"
STEP 10: Declare main() and create object of
class.
STEP 11:Input a number by the user to be
searched in the given array.
STEP 12:Close of class Bsearch.
STEP 11:STOP

import java.io.*;
class Bsearch
{
void search(int a[],int x,int i,int j)
{
if(i>j)
System.out.println("Not found");
else
{
int k=(i+j)/2;
if(x>a[k])
search(a,x,k+1,j);
else if(x<a[k])
search(a,x,i,k-1);
else
System.out.println("Found at position:"+i);

}
}
public void main(String args[])throws
IOException
{
BufferedReader br=new
BufferedReader(new
InputStreamReader(System.in));
Bsearch ob=new Bsearch();
int[] b={3,4,8,13,15};
int y;
System.out.println("Enter a number to be
searched");
y=Integer.parseInt(br.readLine());
ob.search(b,y,0,4);
}

OUTPUT:
Enter a number to be searched
13
Found at position:3

QUESTION:

Define a recursive function to search to search a number


input by the user in a given array using linear search
method.

ALGORITHM
STEP 1:START
STEP 2:import java.io.*;
STEP 3:Declare class Linear
STEP 4:Declare function search(int[ ] a,int x,int
i)
STEP 5: If i is equal to length then display "Not
found"
STEP 6: Else if a[i] is equal to x then display
"Found"
STEP 7: Else recursively call function search( )
STEP 8: Declare main() and create object of
class.
STEP 9:Input a number by the user to be
searched in the given array.
STEP 10:Close of class Linear.

STEP 11:STOP

import java.io.*;
class Linear
{
void search(int[] a,int x,int i)
{
if(i==a.length)
System.out.println("Not found");
else if(a[i]==x)
System.out.println("Found at position:"+i);
else
search(a,x,i+1);
}

public void main(String args[])throws


IOException
{
BufferedReader br=new
BufferedReader(new
InputStreamReader(System.in));
Linear ob=new Linear();
int[] b={3,7,56,4,9,1};
int y;
System.out.println("Enter a number to be
searched");
y=Integer.parseInt(br.readLine());
ob.search(b,y,0);
}
}

OUTPUT:
Enter a number to be searched
9
Found at position:5

QUESTION:
Define a recursive function to search a number input by
the user in a given array using binary search method.

ALGORITHM
STEP 1:START
STEP 2:import java.io.*;
STEP 3:Declare class Bsearch
STEP 4:Declare function search(int[ ] a,int x,int
i,

int j)

STEP 5: If i is greater than j then display "Not


found"
STEP 6: Else
int k=(i+j)/2

STEP 7:If x is greater than a[k] then recursively


call search(a,x,k+1,j)
STEP 8:else If x is less than a[k] then recursively
call search(a,x,i,k+1)
STEP 9:Else dispay"Found"
STEP 10: Declare main() and create object of
class.
STEP 11:Input a number by the user to be
searched in the given array.
STEP 12:Close of class Bsearch.
STEP 11:STOP
import java.io.*;
class Bsearch
{
void search(int a[],int x,int i,int j)
{

if(i>j)
System.out.println("Not found");
else
{
int k=(i+j)/2;
if(x>a[k])
search(a,x,k+1,j);
else if(x<a[k])
search(a,x,i,k-1);
else
System.out.println("Found at position:"+i);
}
}
public void main(String args[])throws
IOException
{

BufferedReader br=new
BufferedReader(new
InputStreamReader(System.in));
Bsearch ob=new Bsearch();
int[] b={3,4,8,13,15};
int y;
System.out.println("Enter a number to be
searched");
y=Integer.parseInt(br.readLine());
ob.search(b,y,0,4);
}
}

OUTPUT:
Enter a number to be searched

13
Found at position:3

INHERITANCE

QUESTION:
Class-personal
Data members

i)name-to store name of employee


ii)pan-to store pan number
iii)basic_pay-to store basic pay
iv)acc_no-to store account number
Member Functions

i)parameterized constructor to assign values to data


members.
ii)void display()-to display employee details
Class-Retire
Data Members

i)yrs-to store years of service


ii)pf-to store provident fund
iii)grat-to store gratuity amount
Member Functions

i)parameterized constructor to assign values to data


members
ii)void provident()-to calculate provident fund pf=2%of
basic pay*years of service
vi)void gratuity()-to calculate gratuity amount which is
equal to annual pay if years of service is equal to or more
than 10 years else equal to zero.
vii)display()-to display provident fund and gratuity
amount.

class personal
{
String name;
int pan;
double basic_pay;
int acc_no;
public personal(String x,int y,double z,int a)
{
name=x;
pan=y;
basic_pay=z;
acc_no=a;
}

public void display()


{
System.out.println("Name-"+name);
System.out.println("PAN number-"+pan);
System.out.println("Basic pay-"+basic_pay);
System.out.println("Account number-"+acc_no);
}
}
class Retire extends personal
{
int yrs;
double pf;
double grat;
Retire(String x,int y,double z,int a,int b)
{
super(x,y,z,a);
yrs=b;

}
public void provident()
{
pf=0.02*basic_pay*yrs;
}
public void gratuity()
{
if(yrs>=10)
{
grat=basic_pay*12;
}
else
grat=0;
}
public void display1()
{
display();
System.out.println("Provident fund-"+pf);

System.out.println("Gratuity fund-"+grat);
}
}

Question:

Class : Number
Data Members: N, R(integer type)
Member Functions:

i) input()-Enter N and R.
ii) output()-display N and R.
Class : Series1
Data Members: Sum(integer type)
Member Functions:

i) int fact(int)-return factorial of recieved number.


ii) void output()-calculate sum where sum is calculated as
follows:
sum=
iii) void output()-display sum.
Class Name-Series 2
Data Members-NCR(float type)
Member Functions:
i) int fact(int)-return factorial of received number.
ii) void compute()-calculate NCR which can be calculated
as follows:
NCR= (
)
iii) void output()-display NCR.

ALOGORITHM:
STEP 1:START
STEP 2:import java.io.*;

STEP 3:Declare class Number


STEP 4:int N,R
STEP 5: Declare function input( ) to enter N,R
STEP 6:Declare function output( ) to display N,R
STEP 7:Close of class Number
STEP 8:Declare class Series1
STEP 9:int sum
STEP 10:Declare function int fact(int x) which
returns factorial.
STEP 11:Declare function compute( ) to
calculate sum.
STEP 12:Declare function output( ) to display
sum.
STEP 13:Close of class Series1
STEP 14:Declare class Series2
STEP 15:float NCR
STEP 16:Declare function int fact(int y) which
returns factorial.

STEP 17:Declare function compute( ) to


calculate NCR.
STEP 18:Declare function output( ) to display
NCR.
STEP 19:Close of class Series2
STEP 20:Declare class Prog1
STEP 21:Declare main( )
STEP 22:int ch
STEP 23: Enter choice of user.
STEP 24:Calculate sum or NCR respectively as
per user's choice.
STEP 25:Close of class Prog1
STEP 26:STOP

import java.io.*;
class Number
{

int N,R;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
public void input()throws IOException
{
System.out.println("Enter value of N");
N=Integer.parseInt(br.readLine());
System.out.println("Enter value of R");
R=Integer.parseInt(br.readLine());
}
public void output()
{
System.out.println("N="+N);
System.out.println("R="+R);
}
}
class Series1 extends Number
{

int sum;
public int fact(int x)
{
int f=1;
for(int i=1;i<=x;i++)
f=f*i;
return(f);
}
void compute()throws IOException
{
input();
sum=0;
for(int i=1;i<=N;i++)
{
sum+=Math.pow(R,i)/fact(i);
}
}
public void output()

{
super.output();
System.out.println("Sum is="+sum);
}
}
class Series2 extends Number
{
float NCR;
public int fact(int y)
{
int f=1;
for(int i=1;i<=y;i++)
f=f*i;
return(f);
}
void compute()throws IOException
{

input();
NCR+=fact(N)/(fact(R)*fact(N-R));
}
public void output()
{
super.output();
System.out.println("NCR="+NCR);
}
}
class Prog1
{
public static void main(String
args[])throws IOException
{
BufferedReader br=new
BufferedReader(new
InputStreamReader(System.in));

System.out.println("Enter 1 for sum or


2 for NCR");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:Series1 ob=new Series1();
System.out.println("Enter a
no.");
ob.compute();
ob.output();
break;
case 2:Series2 ob1=new Series2();
System.out.println("Enter a
no.");
ob1.compute();
ob1.output();
break;
}

}
}

OUTPUT:
Enter 1 for sum or 2 for NCR
1
Enter a no.
Enter value of N
4
Enter value of R
5
N=4
R=5
Sum is=63

QUESTION:
Class -Super

Data Members-a[5][3](integer type array)


Member Functions

i) Super(int)-parameterized constructor to initialize


marks of 5 students in 3 subjects.
ii) int topper1()-return index(row) of topper of first
subject.
iii) int topper2()-return index(row) of topper of second
subject.
iv) int topper3()-return index(row) of topper of third
subject.
Class -Sub
Data Members-nm[5](String type array)
Member Functions-

i) Sub(String)-parameterized constructor to initialize


names of five students.
ii) void MAX()-display highest marks of each subject
along with student's name.

ALOGORITHM:
STEP 1:START
STEP 2:import java.io.*;

STEP 3:Declare class Super


STEP 4:int a[ ][ ] of size 5X3
STEP 5: Declare parameterized constructor
super(int[ ][ ] x) to enter values of the array a[
][ ]
STEP 6:Declare function int topper1( ) which
return index of topper of first subject
STEP 7:Declare function int topper2( ) which
return index if topper of second subject
STEP 8: Declare function int topper3( ) which
return index if topper of third

subject

STEP 9:Close of Super


STEP 10:Declare class Sub
STEP 11:String nm of size 5
STEP 12:Declare parameterized constructor Sub
to initialise names of 5 students.

STEP 13:Declare function MAX( ) to display


highest marks of each subject along with
student's name.
STEP 14:Close of class
STEP 15:Declare class Prog
STEP 16:Declare main( )
STEP 17:Create object of class Sub
STEP 18:Close of class Prog
STEP 19:STOP
class Super
{
int a[][]=new int[5][3];
Super(int x[][])
{
int i,j;
for(i=0;i<=4;i++)
{
for(j=0;j<=2;j++)

{
a[i][j]=x[i][j];
}
}
}
int topper1()
{
int i,index=0,h;
h=a[0][0];
for(i=0;i<=4;i++)
{
if(a[i][0]>h)
{
h=a[i][0];
index=i;
}
}
return(index);

}
int topper2()
{
int i,index,h;
h=a[0][1];
index=0;
for(i=0;i<=4;i++)
{
if(a[i][1]>h)
{
h=a[i][0];
index=i;
}
}
return(index);
}
int topper3()
{

int i,index,h;
h=a[0][2];
index=0;
for(i=0;i<=4;i++)
{
if(a[i][2]>h)
{
h=a[i][2];
index=i;
}
}
return(index);
}
}
class Sub extends Super
{
String[] nm=new String[5];
Sub(String[] y,int x[][])

{
super(x);
int i;
for(i=0;i<=4;i++)
{
nm[i]=y[i];
}
}
void MAX()
{
int x=topper1();
int y=topper2();
int z=topper3();
System.out.println("Name of topper of first
subject:"+nm[x]+"\t"+"Marks="+a[x][0]);
System.out.println("Name of topper of second
subject:"+nm[y]+"\t"+"Marks="+a[y][1]);

System.out.println("Name of topper of second


subject:"+nm[z]+"\t"+"Marks="+a[z][2]);
}
}
class Prog
{
public static void main(String args[])
{
int[][] m={
{70,80,90},
{60,30,90},
{56,25,20},
{90,97,95},
{45,67,89}};
String
n[]={"Alisha","Aliena","Gargi","Medha","Ritika"};
Sub ob=new Sub(n,m);

ob.MAX();
}
}

OUTPUT:
Name of topper of first subject:Ritika Marks=90
Name of topper of second subject:Ritika Marks=97
Name of topper of third subject:Ritika Marks=95

PATTERN

QUESTION:
Write a program to print the following pattern:
123454321
1234 4321
123
321
12
21
1
1
class Pattern

{
int i,j,k,l,sp,st;
public void main()
{
sp=1;
st=5;
for(i=1;i<=5;i++)
{
for(j=1;j<=st;j++)
System.out.print(j);
for(k=1;k<=sp;k=k+2)
System.out.print( " ");
if(i==1)
{
for(l=st-1;l>=1;l--)
System.out.print(l);

}
else
{
for(l=st;l>=1;l--)
System.out.print(l);
}
System.out.println();
sp+=2;st--;
}
}
}

OUTPUT:
12345 4321
1234
123

4321
321

12

21

QUESTION:
Write a program to print the following series:
1/1!,(1+2+/2!,....(1+2+3+...+n)/n!
import java.io.*;
class series3
{
private int s,n,i,j,l;
void inputdata()throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the value of n;");
n=Integer.parseInt(br.readLine());
}

void display()
{
for(i=1;j<=n;i++)
{
l=1;
for(j=1;j<=i;j++)
{
l=l*j;
s=s+j;
}
System.out.print((s/l)+"");
}
}
public static void main(String []args)throws
IOException
{
series3 obj=new series3();

obj.inputdata();
obj.display();
}
}

OUTPUT:
For example:
Enter the value of n;
4
1210

QUESTION:
Write a program to print the following series:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
import java.io.*;
class pattern2
{
int sp,st,i,j,k,l;
public void main()
{
sp=4;
st='a';
for(i=1;i<=5;i++)

{
for(k=1;k<=sp;k++)
System.out.print("");
for(j='A';j<=st;j++)
System.out.print((char)j);
if(i>=2)
{
st--;
for(l=st;l>='A';l--)
System.out.print((char)l);
st++;
}
System.out.println();
sp--;st++;
}
}

OUTPUT:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA

MISCELLANEOUS

QUESTION:
A class Telcall calculates the monthly phone bill of a
consumer. Some of the members of the class are given
below:
Class name :Telcall
Data members:
phno
:phone number
name
:name of consumer
n
:number of calls made

amt

:bill amount

Member functions:

Telcall()
values to data

:parameterized constructor to assign

members.
void compute() :to calculate the phone bill amount based
on the slabs given
below.
void dispdata() :to display the details in the specified
format
Number of calls
Rate
1-100
Rs. 500/-rental
charge only
101-200
Rs.1.00/-per call+
rental charge
201-300
Rs.1.20/-per
call+rental charge
above 300
Rs.1.50/-per
call+rental charge
The calculations need to be done as per the slabs.
Specify the class Telcall, giving the details of the
constructor, void compute() and void dispdata().
In the main function , create an object of type Telcall and
display the phone bill in the following format:
Phone Number
Amount
XXXXXXXXX
XXXX

Name
XXXX

Total Calls
XXXXXX

import java.io.*;
class Telcall
{
String phno;
String name;
int n;
double amt;
Telcall(String ph,String na,int nc)
{
phno=ph;
name=na;

n=nc;
amt=0.0;
}
void compute()
{
if(n<=100)
{
amt=500.0;
}
else if(n>100 && n<=200)
{
amt=(n-100)*1+500;
}
else if(n>200 && n<=300)
{
amt=500+100+(n-200)*1.20;
}
else

{
amt=500+100+100*1.20+(n-300)*1.50;
}
}
void dispdata()
{
System.out.println("Phone
number\tName\tTotal calls\tAmount");
System.out.println(phno+"\t"+name+"\t"+n+"\t
"+amt);
}
public

void main(String args[])throws

IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));

System.out.println("Enter phone
number,name and no. of calls");
phno=br.readLine();
name=br.readLine();
n=Integer.parseInt(br.readLine());
Telcall ob=new Telcall(phno,name,n);
ob.compute();
ob.dispdata();
}
}

OUTPUT:
For example:
Enter phone number,name and no. of calls
01352654232
Divya

432
Phone number Name Total calls Amount
01352654232

Divya

432 918.0

QUESTION:
A ticket at a ticket selling booth at a fair is priced at Rs

2.50/-.The booth keeps track of the total number of people


who have visited the booth, the number of people who
have actually purchased a ticket and the total amount of
money collected. Design a class called Ticbooth which
includes the following Members:
Class Name :Ticbooth
Data members:

no_people :number of people who have visited the booth.


amount : total amount of money collected.
np : no of tickets sold.
Member functions:

void initial():To assign initial values of data members


void notsold():Increment total number of people only
visiting the booth and not purchasing a ticket.
void sold():Increment total number of people purchasing a
ticket and the amount collected when a ticket is sold.
void disp_totals():To display the total number of people
visiting the booth(the total number of people purchasing
the
ticket as well as those not purchasing a ticket).
void disp_ticket():To display the total number of tickets
sold and the amount collected.

Specify the class Ticbooth giving details of the functions


void initial(),void sold(),void notsold(),void _totals and
void disp_ticket

import java.io.*;
class Ticbooth
{
int no_people;
double amount;
int np;
void intial()
{
np=0;
no_people=0;
amount=0.0;
}
void notsold()
{
no_people++;
}
void sold()

{
no_people++;
amount=amount+2.50;
np++;
}
void disp_totals()
{
System.out.println("Total no. of people who
visited the booth: "+no_people);
}
void disp_ticket()
{
System.out.println("No. of tickets sold"+np);
System.out.println("Amount
collected=Rs."+amount);
}
public static void main(String args[])throws
IOException

{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
Ticbooth ob=new Ticbooth();
System.out.println("Press 1 for visiting the
booth and not buying a ticket");
System.out.println("Press 2 for visiting the
booth and buying a ticket");
System.out.println("Enter your choice");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:{
ob.notsold();
ob.disp_totals();
}
case 2:
{

ob.sold();
ob.disp_totals();
ob.disp_ticket();
}
}
}
}

OUTPUT:
Press 1 for visiting the booth and not buying a
ticket

Press 2 for visiting the booth and buying a ticket


Enter your choice
1
Total no. of people who visited the booth: 1
No. of tickets sold0
Amount collected=Rs0.0

QUESTION :
Write a program to calculate the duration of time logged
in by the user.
import java.io.*;
public class log
{

public static void main(String args[ ])throws


IOException
{
int h1,m1,s1,h2,m2,s2;
int h,m,s;h=m=s=0;
InputStreamReader read=new
InputStreamReader(System.in);
BufferedReader in= new
BufferedReader(read);
System.out.println("Enter log in hour");
h1=Integer.parseInt(in.readLine());
System.out.println("Enter log in minute");
m1 =Integer.parseInt(in.readLine());
System.out.println("Enter log in second");
s1 =Integer.parseInt(in.readLine());
System.out.println("Enter log out hour");
h2 =Integer.parseInt(in.readLine());
System.out.println("Enter log out min");

m2 =Integer.parseInt(in.readLine());
System.out.println("Enter log out second");
s2 =Integer.parseInt(in.readLine());
if(s2<s1)
{
s=s2+60-s1;
m2=m2-1;
}
else
{
s=s2-s1;}
if(m2<m1)
{
m=m2+60-m1;
h2=h2-1;
}
else
{

m=m2-m1;
}
h=h2-h1;
System.out.println(Duration is);
System.out.println(h);
System.out.println(m);
System.out.println(s);
}
}

QUESTION :
Write a program to check if the given numbers are Twin
Primes or not.
import java.io.*;
class Twin
{

int k,l,m,x,i,n,j,y;
int flag=0;
void inputdata()throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter 2 numbers");
k=Integer.parseInt(br.readLine());
l=Integer.parseInt(br.readLine());
}
void calculate()
{
if(k>l)
n=k-l;
else
n=l-k;
if(n==2)
{

x=0;
for(i=1;i<=k;i++)
{
if(k%i==0)
x++;
}
if(x==2)
{
y=0;
for(j=1;j<=l;l++)
{
if(l%j==0)
y++;
}
if(y==2)
flag=1;
}
}

}
void display()
{
if(flag==1)
System.out.println("They are Twin Primes");
else
System.out.println("They are not Twin Primes");
}
public static void main(String args[])throws
IOException
{
Twin ob=ob=new Twin();
ob.inputdata();
ob.calculate();
ob.display();
}
}

OUTPUT:
Enter 2 numbers
17
19
They are Twin Primes

QUESTION :
Write a program to generate fibonacci series till n and
print only the prime numbers.

import java.io.*;
class FibPrime
{
int k,l,x,i,n,j,y;
int flag;
void inputdata()throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the limit of
Fibonacci series");

n=Integer.parseInt(br.readLine());
}
void calculate()
{
System.out.println("The required fibonacci
series is:");
k=0;l=1;
for(i=1;i<n;i++)
{
x=l=k;
flag=0;
for(j=1;j<=x;j++)
{
if(x%j==0)
flag++;
}
if(flag==2)

System.out.print(x+" ");
k=l;
l=x;
}
}
public void main(String args[])throws
IOException
{
FibPrime ob=new FibPrime();
ob.inputdata();
ob.calculate();
}
}

OUTPUT:
Enter the limit of the fibonacci series
10
The required fibonacci series is:
2
3
5
13

QUESTION:
Write a program to illustrate tower of Hanoi.

ALGORITHM:
STEP 1:START
STEP 2:Declare class Hanoi
STEP 3:Declare function TOH( int N, char S,
char I, char D)
STEP 4: If N is equal to zero then return
STEP 5: Else TOH(N-1,S,D,I)
STEP 6:Display "Move 5 From L To R"
STEP 7: TOH(N-1,I,S,D)

STEP 8: Declare main() and create object of


class.
STEP 9:Call function TOH(5,'L','M','R')
STEP 10:Close of class Hanoi
STEP 12:STOP

class Hanoi
{

void TOH(int N,char S,char I,char D)


{
if(N==0)
return;
else
{
TOH(N-1,S,D,I);
System.out.println("Move "+N+" From
"+S+" To "+D);
TOH(N-1,I,S,D);
}
}
public void main(String args[])
{
Hanoi ob=new Hanoi();
ob.TOH(5,'L','M','R');
}
}

Output:
Move 1 From L To R
Move 2 From L To M
Move 1 From R To M
Move 3 From L To R
Move 1 From M To L
Move 2 From M To R
Move 1 From L To R
Move 4 From L To M
Move 1 From R To M
Move 2 From R To L
Move 1 From M To L
Move 3 From R To M
Move 1 From L To R
Move 2 From L To M
Move 1 From R To M

Move 5 From L To R
Move 1 From M To L
Move 2 From M To R
Move 1 From L To R
Move 3 From M To L
Move 1 From R To M
Move 2 From R To L
Move 1 From M To L
Move 4 From M To R
Move 1 From L To R
Move 2 From L To M
Move 1 From R To M
Move 3 From L To R
Move 1 From M To L
Move 2 From M To R
Move 1 From L To R

QUESTION:

Write a program to input an infix expression and check


the validity of the expression in terms of use of the
brackets.

ALGORITHM
STEP 1:START
STEP 2:import java.io.*;
STEP 3:Declare class Infix
STEP 4:Declare char st[ ], int top, int size
STEP 5:Declare parameterized constructor(int z)
to assign size=z and top=-1.
STEP 6: Declare function push(char c)to push
the characters in the stack.
STEP 7: Declare function char pop( ) to pop the
elements.
STEP 8:Declare main( ) and create object of the
class .
STEP 9:Input the string and calculate its length.
STEP 10:Extract each opening bracket of the
expression and push in the stack.

STEP 11: Extract each closing bracket of the


expression and pop .
STEP 12:Now check the opening and closing
bracket.
STEP 13:Else display "Invalid expression"
STEP 14:If i==l then display "Valid Expression"
STEP 15:Close of class Infix
STEP 16:STOP

import java.io.*;
class Infix
{
char []st;
int top,size;
Infix(int z)
{
size=z;

st=new char[size];
top=-1;
}
void push (char c)
{
if(top==size-1)
System.out.println("Overflow");
else
st[++top]=c;
}
char pop()
{
if(top==-1)
{
System.out.println("Underflow");
return(' ');
}
else

return(st[top--]);
}
public static void main (String args[])throws
IOException
{
BufferedReader br=new BufferedReader (new
InputStreamReader(System.in));
String str;
System.out.println("Enter a string");
str=br.readLine();
int l=str.length();
int i;
Infix ob=new Infix(l);
for( i=0;i<=l-1;i++)
{
char ch=str.charAt(i);
if(ch=='['||ch=='{'||ch=='(')
ob.push(ch);

else if(ch==']'||ch=='}'||ch==')')
{
char d=ob.pop();
if((d=='[' && ch==']')||(d=='{' &&
ch=='}')||(d=='('&& ch==')'))
continue;
else
{
System.out.println("Invalid
expression");
break;
}
}
}

if(i==l)
{

if(ob.top==-1)
System.out.println("Valid Expression");
}
}
}

OUTPUT:
Enter a string
{(a+b)-c]
Invalid expression

QUESTION:
Class Account
Data members

i) an(int)-to store account number


ii) p(double)-to store principal
Member Functions
i)default constructor
ii) parameterized constructor
iii)display()-to display account number and principal
Class-simple
Data members

i) r(int)-to input rate of interest


ii) t(int)-to input time
iii)SI(double)-to store simple interest.
Member Functions

i)default constructor
ii) parameterized constructor
iii)double interest()-to calculate simple interest
iv)display()-to display rate of interest, time and simple
interest
Class-compound
Data members

i) r(int)-to input rate of interest


ii) t(int)-to input time
iii)CI(double)-to store compound interest

Member Functions

i)default constructor
ii) parameterized constructor
iii)double interest()-to calculate compound interest
iv)display()-to display rate of interest, time and
compound interest

ALGORITHM:
STEP 1:START
STEP 2:Declare class Account.

STEP 3:Declare int an and double p.


STEP 4:Declare default constructer() to initialize
an=0 and p=0.0.
STEP 5:Declare parameterised constructer(int
x,double y) to initialize an=x and p=y.
STEP 6:Declare function display() to display the
account number and principal amount.
STEP 7:Close of class account.
STEP 8:Declare class simple extends account.
STEP 9:Declare int i,int t and double SI.
STEP 10:Declare default constructer() to call the
default constructer of account and initialize t=0
and r=0.
STEP 11:Declare parameterised constructer(int
x,double y,int n,int m) to call the parameterised
constructer of account and initialize t=n and r=m.
STEP 12:Declare function interest() to calculate
interest.

STEP 13:Declare function display() to display all


the details of both the classes.
STEP 14:Close of class simple.
STEP 15:Declare class compound extends class
account.
STEP 16:Declare int t,int r and double CI.
STEP 17:Declare default constructer() to call
default constructer of account and initialize t=0
and r=0:
STEP 18:Declare parameterised constructer(int
x,double y,int n,int m) to call the parameterised
constructer of account and initialize t=n and r=m.
STEP 19:Declare function interest() to calculate the
compound interest.
STEP 20:Declare function display() to display all
details of both the classes.

STEP 21:Declare function main() and input account


number,principle,time and rate of interest from the
user.
STEP 22: call all the functions of class simple and
compound.
STEP 23:Close of main().
STEP 24:STOP

import java.io.*;
class account
{
protected int an;
protected double p;
account()
{
an=0;
p=0.0;
}
account(int x,double y)

{
an=x;
p=y;
}
void display()
{
System.out.println("Account number="+an);
System.out.println("principal="+p);
}
}
class simple extends account
{
int r,t;
double SI;
simple()
{
super();
t=0;

r=0;
}
simple(int x,double y,int n,int m)
{
super(x,y);
t=n;
r=m;
}
double interest()
{
SI=(p*r*t)/100;
return(SI);
}
void display()
{
super.display();
System.out.println("rate="+r);
System.out.println("time="+t);

double v= intrest();
System.out.println("Interest="+v);
System.out.println();
System.out.println();
}
}
class compound extends account
{
double CI;
int t,r;
compound()
{
super();
t=0;
r=0;
}
compound(int x,double y,int n,int m)
{

super(x,y);
t=n;
r=m;
}
double interest()
{
double h =(Math.pow(((100+r)/100),t));
CI=(h);
return(CI);
}
void display()
{
super.display();
System.out.println("rate="+r);
System.out.println("time="+t);
double v= intrest();
System.out.println("Interest="+v);
System.out.println();

System.out.println();
}
}
public class inherit
{
public static void main(String args[])throws
IOException
{
InputStreamReader read= new
InputStreamReader(System.in);
BufferedReader in =new BufferedReader(read);
System.out.println("Enter account no,principal,time
and rate");
double b;
int a,c,d;
a=Integer.parseInt(in.readLine());
b=Double.parseDouble(in.readLine());
c=Integer.parseInt(in.readLine());

d=Integer.parseInt(in.readLine());
simple ob1=new simple();
simple ob2=new simple(a,b,c,d);
compound ob3= new compound();
compound ob4= new compound(a,b,c,d);
ob1.display();
ob2.display();
ob3.display();
ob4.display();
}}

OUTPUT:
Enter account no,principal,time and rate
2345
25000
2
10
Account number=0

principal=0.0
rate=0
time=0
Interest=0.0
Account number=2345
principal=25000.0
rate=10
time=2
Interest=5000.0

Account number=0
principal=0.0
rate=0
time=0
Interest=1.0

Account number=2345
principal=25000.0
rate=10
time=2
Interest=1.0
Found at index:10

FILES

Question:
Write a program using the following class:
Class: deque
Data members: ar[],front,rear,size
Methods: deque(int cap)
void addrear(int)
void addfront(int)
int delfront()
int delrear()
void display(0
no need of main.

Class deque
{
int ar[],front,rear,size;
public deque(int cap)
{
size=cap;
front=rear=0;
ar[]=new int[size];
}

public void addfromt(int num)


{
if (front==0)
System.out.println(Overflow from front);
Else
ar[front--]=num;
}
public void addrear(int num)
{
if (rear==size-1)
System.out.println(overflow from rear);
else
ar[++rear]=num;
}
public int delfront()
{
If (front==rear)

{
System.out.println(underflow);
return -999;
}
else
return ar[++front];
}
public int delrear()
{
if(front==rear)
{
System.out.ptintln(underflow);
return -999
}
else
return ar[rear--];
}
public void display()

{
if(front==rear)
System.out.println(no array);
else
{
for(int i=front+1;i<=rear;++i)
System.out.println(ar[i]);
}
}

Question:
Write a program to read three files NAMES.TXT,
HIST.TXT, GEOG.TXT which contain names ,history
marks ,geography marks respectively. Open a new file
SST.TXT and store all the names along with their
average.

import java.io.*;
Class copy
{
public static void main(String args[])throws
IOException
{

InputStreamReader isr=new
InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
FileReader obj1=new FileReader(NAMES.TXT);
BufferedReader obj2=new BufferedReader(obj1);
FileReader obj3=new FileReader(HIST.TXT);
BufferedReader obj4=new BufferedReader(obj3);
FileReader obj5=new FileReader(GEOG.TXT);
BufferedReader obj6=new BufferedReader(obj5);
FileReader obj7=new FileReader(SST.TXT);
BufferedReader obj8=new BufferedReader(obj7);
PrintWriter obj9=new Printwriter(obj8);
String n=obj2.readLine();
while(n!=null)
{
String h=obj4.readLine();
String g=obj.readLine();
double hh=Double.parseDouble(h);

double gg=Double.parseDouble(g);
double avg=(hh+gg)/2;
obj9.println(n+\n+avg);
n=obj2.readLine();
}
}
}

You might also like