Professional Documents
Culture Documents
INDEX
ARRAY
STRING MANUPULATION
STACK
QUEUE
DEQUE
LINKED LIST
RECURSION
INHERITANCE
PATTERN
MISCELLANEOUS
FILES
ARRAY
QUESTION:
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.
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-
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
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));
}
}
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.
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]);
}
}
{
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
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);
}
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 .
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;
}
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;
import java.io.*;
class project
{
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;
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);
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
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
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
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.
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.
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:
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
{
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-
ALGORITHM
STEP 1:START
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
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.
}
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:
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.
}
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);
}
}
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
ALGORITHM
STEP 1:START
STEP 2:import java.io.*;
STEP 3:Declare class Link
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));
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:
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);
}
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;
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:
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.
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:
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.
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
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:
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);
}
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)
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
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 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:
ALOGORITHM:
STEP 1:START
STEP 2:import java.io.*;
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));
}
}
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
ALOGORITHM:
STEP 1:START
STEP 2:import java.io.*;
subject
{
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]);
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
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
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
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
QUESTION :
Write a program to calculate the duration of time logged
in by the user.
import java.io.*;
public class log
{
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)
class Hanoi
{
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:
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.
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)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
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.
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];
}
{
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();
}
}
}