Vous êtes sur la page 1sur 26

Problem 1

A financial institution has only two kinds of accounts, Simple and Compound. Consider
the class structure as given:-
class account
{
acc_number; // tostore account number
prin_Amount // to store principal amount
account( ) // constructor
void input( ) // to read values of Acc_number and Prin_Amount
}
Another class acc_Type inherits the above class having data members- type(to store
simple or compound) , rate(to store rate of interest) , Time (to store time in yrs).
It has member functions:
accType( ) // constructor
void input( ) // to read details
void display( ) // to print all the details

SOLUTION :-

import java.io.*;
class account
{
protected int acc_number;
protected double prin_amount;
account()
{
acc_number=0;
prin_amount=0;
}
void input() throws Exception
{
BufferedReader ds=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter account number:- ");
acc_number=Integer.parseInt(ds.readLine());
System.out.print("Enter principal amount:- ");
prin_amount=Double.parseDouble(ds.readLine());
}
}

class acc_type extends account


{
char type;
double rate;
double time;
acc_type()
{
type='g';
rate=0.0;
time=0.0;
}
void input() throws Exception
{
super.input();
BufferedReader ds=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter account type(S//C) :- ");
type=(ds.readLine()).charAt(0);
System.out.print("Enter the rate:- ");
rate=Double.parseDouble(ds.readLine());
System.out.print("Enter the time:- ");
time=Integer.parseInt(ds.readLine());
}
void display()
{
double i=0.0;
if(type=='S' || type=='s')
i=(super.prin_amount*rate*time)/100;
else
i=(super.prin_amount*(Math.pow((1+(rate/100.0)),time)))-super.prin_amount;
System.out.println("\n\n\tAccount Number ="+super.acc_number);
System.out.println( "\tPrincipal ="+super.prin_amount);
System.out.println( "\tRate ="+rate);
System.out.println( "\tTime ="+time);
System.out.println( "\tInterest ="+i);
}
}

class runner
{
public static void main(String args[]) throws Exception
{
acc_type x=new acc_type();
x.input();
x.display();
}
}
Problem 2

Design a program which creates a binary search tree. It should have the following
functions.
 Numbers can be added in it
 Traverse the tree in inorder
 Traverse the tree in preorder
 Traverse the tree in postorder

SOLUTION :

import java.io.*;
class btree
{
int a;
btree L;
btree R;
btree(int k)
{
a=k;
R=L=null;
}
}

class bstree
{
btree B;
bstree()
{
B=null;
}
void add(int k)
{
B=add(B,k);
}
btree add(btree M,int k)
{
if(M==null)
M=new btree(k);
else
{
if(k==M.a)
return M;
else
{
if(k<M.a)
M.L=add(M.L,k);
else
M.R=add(M.R,k);
}
}
return M;
}
void printer(int x)
{
if(x==1)
inprint(B);
if(x==2)
preprint(B);
if(x==3)
postprint(B);
}
void inprint(btree M)
{
if(M==null)
return;
else
{
inprint(M.L);
System.out.println(M.a);
inprint(M.R);
}
}
void preprint(btree M)
{
if(M==null)
return;
else
{
System.out.println(M.a);
preprint(M.L);
preprint(M.R);
}
}
void postprint(btree M)
{
if(M==null)
return;
else
{
postprint(M.L);
postprint(M.R);
System.out.println(M.a);
}
}
}

class bstree1
{
public static void main(String agrs[])throws Exception
{
DataInputStream ds=new DataInputStream(System.in);
bstree x1=new bstree();
System.out.println("How many no.s do you want to add?");
int m=Integer.parseInt(ds.readLine());
for(int i=0;i<m;i++)
{
System.out.println("Enter the no.-");
int n=Integer.parseInt(ds.readLine());
x1.add(n);
}
int ch=5;
while(ch!=1 || ch!=2 || ch!=3)
{
System.out.println("How do you want to print your btree-");
System.out.println("\n 1.Inorder \n 2.Preorder \n 3.Postorder");
System.out.println("Enter your choice");
ch=Integer.parseInt(ds.readLine());
x1.printer(ch);
}
}
}
Problem 3

Find out the lucky number in a circular link list.

SOLUTION :

import java.io.*;
class dlltravel
{
int a;
dlltravel N;
dlltravel P;
}

class dlltravel1
{
public static void main(String agrs[])throws Exception
{
DataInputStream ds=new DataInputStream(System.in);
dlltravel l1;
dlltravel l2;
l1=null;
l2=null;
System.out.println("Enter the no. of nodes.");
int n=Integer.parseInt(ds.readLine());
for(int i=0;i<n;i++)
{
if(l1==null)
{
l1=new dlltravel();
System.out.println("Enter a no. ");
l1.a=Integer.parseInt(ds.readLine());
l1.N=l1.P=null;
l2=l1;
}
else
{
while(l1.N!=null)
l1=l1.N;
l1.N=new dlltravel();
l1.N.P=l1;
l1=l1.N;
System.out.println("Enter a no. ");
l1.a=Integer.parseInt(ds.readLine());
l1.N=null;
}
}
l1.N=l2;
l2=l1;
while(l1.P!=null)
l1=l1.P;
l1.P=l2;
int g=1;
for(int i=1;i<n;i++)
{
int b=l1.a;
if(g>1)
l1.a=0;
g++;
int p=1;
int s;
if(b<0)
s=-b;
else
s=b;
while(p<=s)
{
if(b<0)
l1=l1.P;
else
l1=l1.N;
if(l1.a!=0)
p++;
}
}
for(int i=0;i<n;i++)
{
if(l1.a!=0)
System.out.println(l1.a);
l1=l1.N;
}
}
}

Problem 4
Write a program do design a double link list. It should implement the following
functions:
 Adding a node
 Printing the list
 Inserting a node
 Deleting a node
 Clearing the link list

SOLUTION :

import java.io.*;
class doublell
{
int a;
doublell P;
doublell N;
}

class doublell1
{
public static void main(String agrs[])throws Exception
{
BufferedReader ds=new BufferedReader(new InputStreamReader(System.in));
doublell l1;
l1=null;
int ch=0;
while(ch!=6)
{
System.out.println("What will you like to do?");
System.out.println("1. Add a node \n2. Print the list \n3. Clear linklist \n4.
Insert a node \n5. Delete a node \n6. Exit");
ch=Integer.parseInt(ds.readLine());
if(ch==1)
{
if(l1==null)
{
l1=new doublell();
System.out.println("Enter a no. ");
l1.a=Integer.parseInt(ds.readLine());
l1.N=l1.P=null;
}
else
{
while(l1.N!=null)
l1=l1.N;
l1.N=new doublell();
l1.N.P=l1;
l1=l1.N;
System.out.println("Enter a no. ");
l1.a=Integer.parseInt(ds.readLine());
l1.N=null;
}
}
if(ch==2)
{
if(l1==null)
System.out.println("The linklist is empty.");
else
{
while(l1.P!=null)
l1=l1.P;
while(l1.N!=null)
{
System.out.print(l1.a +"\t");
l1=l1.N;
}
System.out.println(l1.a);
}
}
if(ch==3)
{
l1=null;
}
if(ch==4)
{
if(l1==null)
{
l1=new doublell();
System.out.println("Enter a no. ");
l1.a=Integer.parseInt(ds.readLine());
l1.N=l1.P=null;
}
else
{
while(l1.P!=null)
l1=l1.P;
System.out.println("Enter the position where you want to insert the
node");
int n=Integer.parseInt(ds.readLine());
int k=1;
while(l1.N!=null)
{
k++;
l1=l1.N;
}
if(n>k)
{
l1.N=new doublell();
l1.N.P=l1;
l1=l1.N;
System.out.println("Enter a no. ");
l1.a=Integer.parseInt(ds.readLine());
l1.N=null;
}
else
{
while(l1.P!=null)
l1=l1.P;
if(n==1)
{
doublell l2;
l2=new doublell();
System.out.println("Enter a no. ");
l2.a=Integer.parseInt(ds.readLine());
l2.P=null;
l2.N=l1;
l1.P=l2;
}
else
{
for(int i=1;i<(n-1);i++)
l1=l1.N;
doublell l2;
l2=new doublell();
System.out.println("Enter a no. ");
l2.a=Integer.parseInt(ds.readLine());
l2.P=l1;
l2.N=l1.N;
l1.N=l2;
}
}
}
}
if(ch==5)
{
System.out.println("Enter the position of the node you want to delete.");
int n=Integer.parseInt(ds.readLine());
int k=1;
while(l1.P!=null)
l1=l1.P;
while(l1.N!=null)
{
k++;
l1=l1.N;
}
if(n>k || n<1)
System.out.println("No node can be deleted .");
else
{
while(l1.P!=null)
l1=l1.P;
if(n==1)
{
l1=l1.N;
l1.N.P=null;
}
else
{
for(int i=1;i<(n+1);i++)
l1=l1.N;
doublell l2;
l2=new doublell();
l2=l1;
while(l1.P!=null)
l1=l1.P;
for(int i=1;i<(n-1);i++)
l1=l1.N;
l1.N=l2;
l2.P=l1;
}
}
}
if(ch==6)
break;
}
}
}

Problem 5
Write program to covert an infix expression into a postfix expression.

SOLUTION :

import java.io.*;
class inpostfix
{
void play() throws Exception
{
DataInputStream ds=new DataInputStream(System.in);
System.out.println("Enter the algeraic expression:- ");
String s1=ds.readLine();
s1=s1+")";
int z=s1.length();
String s2="";
char B[]=new char[z];B[0]='(';
int m=0,n=1;
for(int i=0;i<z;i++)
{
char x=s1.charAt(i);
if(x>='A' && x<='Z' || x>='a' && x<='z')
{
s2=s2+x;
}
else
{
B[n]=x;
if(B[n]==')')
{
B[n]=' ';
n--;
int s=n;
while(B[s]!='(')
{
if(B[s]==' ')
{ s--;n--; }
else
{
s2=s2+B[s];
B[s]=' ';
s--;
n--;
}
}
B[s]=' ';
n--;
}
if(x=='+' || x=='-')
{
int s=n-1;
while(B[s]!='(')
{
if(B[s]==' ')
s--;
else
{
if(B[s]=='*' || B[s]=='/' || B[s]=='^' || B[s]=='+' || B[s]=='-')
{
s2=s2+B[s];
B[s]=' ';
s--;
}
else
s--;
}
}
}
if(x=='*' || x=='/')
{
int s=n-1;
while(B[s]!='(')
{
if(B[s]==' ')
s--;
else
{
if(B[s]=='^' || B[s]=='*' || B[s]=='/')
{
s2=s2+B[s];
B[s]=' ';
s--;
}
else
s--;
}
}
}
n++;
}
}
System.out.println(s2);
}
}
class inpostfix1
{
public static void main(String agrs[]) throws Exception
{
inpostfix x1=new inpostfix();
x1.play();
}
}
Problem 6
Write a program to find the value of cos(x) without using inbuilt functions.
The value of x should be given in radians.

cos(x)= 1- x2/2! + x4/4! – x6/6! ………… ∞

Also find the value of sec (x).

SOLUTION :

import java.io.*;
class mymath
{
double x;
void read() throws Exception
{
BufferedReader ds=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value of x in radians:- ");
x=Double.parseDouble(ds.readLine());
}
void process() throws Exception
{
try
{
double s1=0;
double tot=0;
boolean g=true;
double num=0;
double fac=1;
for(int i=0;i<100;i++)
{
num=Math.pow(x,(i*2));
for(int gh=(i*2);gh>=1;gh--)
fac=fac*gh;
s1=num/fac;
if(g==true)
{
tot+=s1;
g=false;
}
else
{
tot-=s1;
g=true;
}
fac=1;
}
String s2=Double.toString(tot);
s2=s2.Substring(0,8);
System.out.println("The value of cos(x) is :- "+s2);
System.out.println("The value of sec(x) is :- "+(1/tot));
}
catch(Exception E)
{
System.out.println("ERROR!!!!!!");
}
}
}
class runner7
{
public static void main(String args[]) throws Exception
{
mymath x=new mymath();
x.read();
x.process();
}
}
Problem 7

Write the program a grid of nXn . 1-indicates that a path exists. 0- indicates that a path
does not exists.
The elements of right diagonal are by default given a value 1.
Elements written above the right diagonal are mirrored the other side.
In the input we need to give the starting and final location.
We need to find out whether a path exists or not.

SOLUTION :

import java.io.*;
class path
{
static int arr[][];
static int l;
static boolean g;
public static void main(String args[]) throws Exception
{
BufferedReader ds=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the length of array:- ");
try
{
l=Integer.parseInt(ds.readLine());
arr=new int[l][l];
System.out.println("PLS fill up the array:- ");
for(int i=0;i<l;i++)
{
for(int j=0;j<l;j++)
{
if(j>i)
{
System.out.print("arr["+(i+1)+"]["+(j+1)+"]:- ");
arr[i][j]=Integer.parseInt(ds.readLine());
arr[j][i]=arr[i][j];
}
if(j==i)
arr[i][i]=1;
}
}
System.out.print("Enter initial position:- ");
int p=Integer.parseInt(ds.readLine());
System.out.print("Enter final position:- ");
int q=Integer.parseInt(ds.readLine());
p--;
q--;
int r=-1;
g=false;
g=rec(p,q,r);
if(arr[0][p]==0)
{
System.out.println("SORRY!!! NO PATH CAN BE
DEFINED\n\t\t\tANNIHILATED");
System.exit(0);
}
if(g==true)
System.out.println("\nTHE PATH EXISTS.. ");
else
System.out.println("\nPATH DOES NOT EXIST... SORRY!!!");
}
catch(Exception E)
{
System.out.println("\t\t\tERROR!!!!!!");
}
}
static boolean rec(int p,int q,int r)
{
if(arr[p][q]==1)
{
g=true;
return g;
}
for(int i=0;i<l;i++)
{
if(g==true)
return g;
if((i==p)||(i==r))
continue;
if(arr[p][i]==1)
{
if(i==q)
g=true;
else
{
if(p!=i)
{
arr[p][i]=0;
arr[i][p]=0;
}
rec(i,q,p);
}
}
}
return g;
}
}
Problem 8

Write a program to design a link list using recursive functions. It should have the
following functions:-
 Adding numbers by stack method
 Adding numbers by queue method
 Printing the link list
 Inserting a number in between
 Deleting a number
 Clearing the list

SOLUTION :

import java.io.*;
class reclist2
{
int a;
reclist2 n;
reclist2(int k)
{
a=k;
n=null;
}
}
class listing2
{
reclist2 l1;
listing2()
{
l1=null;
}
void clearer()
{
l1=null;
System.out.println("\nLINK LIST CLEARED ");
}
void add(int k)
{
l1=add(l1,k);
}
reclist2 add(reclist2 m,int k) // for queue
{
if(m==null)
m=new reclist2(k);
else
m.n=add(m.n,k);
return m;
}
void print(reclist2 m) throws Exception
{
if(m==null)
return;
else
{
System.out.print(m.a+" ");
Thread.sleep(200);
print(m.n);
}
}
void printer() throws Exception
{
System.out.println("\nThe link list is as:- ");
print(l1);
Thread.sleep(2000);
}
reclist2 adds(reclist2 m,int k) // for stack
{
reclist2 x2;
x2=new reclist2(k);
x2.n=m;
return x2;
}
void adds(int k)
{
l1=adds(l1,k);
}
void deleter(int k) throws Exception
{
if(l1.a==k)
l1=l1.n;
for(int i=0;i<1;i++)
dels(l1,k);
System.out.println("\nNUMBER DELETED ");
Thread.sleep(1000);
}
void dels(reclist2 m,int k) throws Exception
{
try
{
if(m.n.a==k)
{
m.n=m.n.n;
}
else
dels(m.n,k);
}
catch(Exception E)
{
System.out.println("COULDN'T DELETE\nERROR!!!!!!");
}
}
void inserter1(int k,int m) throws Exception
{
l1=inserts1(l1,l1,l1,k,m);
System.out.println("\nNUMBER INSERTED ");
Thread.sleep(1000);
}
void inserter(int k,int m) throws Exception
{
l1=inserts(l1,l1,l1,k,m);
System.out.println("\nNUMBER INSERTED ");
Thread.sleep(1000);
}
reclist2 inserts1(reclist2 m,reclist2 pr,reclist2 l,int k,int p)
{
reclist2 x2=new reclist2(k);
x2.n=m;
return x2;
}
reclist2 inserts(reclist2 m,reclist2 pr,reclist2 l,int k,int p) throws Exception
{
try
{
if(p==1)
{
reclist2 x2=new reclist2(k);
x2.n=m;
pr.n=x2;
return l;
}
else
{
inserts(m.n,m,l,k,p-1);
}
return m;
}
catch(Exception E)
{
System.out.println("COULDN'T INSERT\nERROR!!!!!!");
return l;
}
}
}
class runner11
{
public static void main(String args[]) throws Exception
{
BufferedReader ds=new BufferedReader(new InputStreamReader(System.in));
listing2 x=new listing2();
int ch=3436;
for(int i=0;i<27;i++)
System.out.println();
System.out.print("Loading ");Thread.sleep(500);
System.out.print(".");Thread.sleep(500);
System.out.print(".");Thread.sleep(500);
System.out.print(".");Thread.sleep(500);
while(ch!=7)
{
for(int i=0;i<27;i++)
System.out.println();
System.out.print("\n\n\t\t###\t\tRECURSIVE LINK-LIST\t\t###\n\nWhat do you
want to do:- \n\n\t1.> Add numbers by queue.\n\t2.> Add numbers by a
stack.\n\t3.> Print the list.\n\t4.> Clear the link list.\n\t5.> Delete a number.\n\t6.>
Insert number.\n\t7.> Exit.\n\nEnter choice:- ");
ch=Integer.parseInt(ds.readLine());
if(ch==1)
{
System.out.print("\nEnter a number:- ");
x.add(Integer.parseInt(ds.readLine()));
System.out.println("NUMBER ADDED");Thread.sleep(1000);
}
if(ch==2)
{
System.out.print("\nEnter a number:- ");
x.adds(Integer.parseInt(ds.readLine()));
System.out.println("NUMBER ADDED");
Thread.sleep(1000);
}
if(ch==3)
x.printer();
if(ch==4)
{
x.clearer();
Thread.sleep(1000);
}
if(ch==5)
{
x.printer();
System.out.print("\nEnter a number to delete:- ");
x.deleter(Integer.parseInt(ds.readLine()));
}
if(ch==6)
{
x.printer();
System.out.print("\nEnter a position:- ");
int pos=Integer.parseInt(ds.readLine());
System.out.print("\nEnter a number:- ");
int num=Integer.parseInt(ds.readLine());
if(pos==1)
x.inserter1(num,pos);
else
x.inserter(num,pos);
}
}
}
}

Problem 9

A input is given in the form of a string which contains a password.


By knowing the length of the password it can be located easily within the string.
A password with size N can be found by searching the text for the most frequent
substring with N characters. After finding the password, all the substrings that coincide
with the password are removes from the encoded text. Now the password can be used.

Sample input:-
Size of password 3
Text baababacb

Sample output
Password aba

SOLUTION :

import java.io.*;
class summ
{
public static void main(String ar[])throws Exception
{
DataInputStream r = new DataInputStream(System.in);
int size=0;
String text="";

while(true)
{
try
{
System.out.print("Size of password : ");
size=Integer.parseInt(r.readLine());

System.out.print("Text : ");
text=r.readLine();
}
catch(Exception E)
{
System.out.println("Wrong message !");
break;
}

if(text.equals("") || size==0 || size>text.length())


{
System.out.println("Wrong message !");
break;
}
String arr[]=new String[text.length()+1-size];
int freq[]=new int[arr.length];

for(int i=0; i<arr.length; i++)


{
arr[i]="";
freq[i]=0;
}

for(int i=0; i<arr.length; i++)


arr[i]=text.substring(i, i+size);

for(int i=0; i<arr.length; i++)


{
for(int j=i+1; j<arr.length; j++)
{
if(arr[i].equals(arr[j]))
{
freq[i]++;
arr[j]="";
}
}
}

int max=0;

for(int i=0; i<arr.length; i++)


{
if(freq[max]<freq[i])
max=i;
}

int flag=0;
for(int i=0; i<arr.length; i++)
{
if(freq[max]==freq[i])
flag++;
}

if(flag==1)
System.out.println("Password " +arr[max]);
else
{
System.out.println("Wrong message !");
break;
}
break;
}
}
}
Problem 10

Write a program to check the validity of UPC (Universal Product Code).


Maximum five inputs can be given at a time.

SOLUTION :

import java.io.*;
class UPS
{
String A[];
int n;
void read()throws Exception
{
DataInputStream ds=new DataInputStream(System.in);
n=7;
while(n<0 || n>5)
{
System.out.println("Enter the no. of UPS code you want to input");
System.out.println("The no. of inputs should be maximum 5");
n=Integer.parseInt(ds.readLine());
}
A=new String[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter a UPS code of 12 digits");
try
{
A[i]=ds.readLine();
}
catch(Exception E)
{
System.out.println("Invalid Input");
i--;
continue;
}
int k=A[i].length();
if(k!=12)
{
System.out.println("Invalid Input");
i--;
}
}
}
void check()
{
System.out.println("INPUT -");
for(int i=0;i<n;i++)
System.out.println(A[i]);
for(int i=0;i<n;i++)
{
int k=0;
int ev=0;
int dd=0;
String z=A[i];
int p=z.length();
while(p!=0)
{
String s2="";
char d=z.charAt(p-1);
int m=(int)(d-'0');
for(int j=0;j<(p-1);j++)
{
s2=s2+z.charAt(j);
}
z=s2;
p=z.length();
k++;
if((k%2)==0)
dd=dd+m;
else
ev=ev+m;
}
int a=ev+(3*dd);
int x=A[i].length();
char e=A[i].charAt(x-1);
int b=(int)(e-'0');
int c=a%10;
int d=10-c;
if(c!=0)
{
if((b-c)>=0)
b=b-c;
else
{
if((b+d)<10)
b=b+d;
}
}
char f=(char)(b+'0');
String s3="";
for(int j=0;j<(x-1);j++)
s3=s3+A[i].charAt(j);
s3=s3+f;
A[i]=s3;
}
System.out.println("OUTPUT -");
for(int i=0;i<n;i++)
System.out.println(A[i]);
}
}
class UPS1
{
public static void main(String agrs[])throws Exception
{
UPS x1=new UPS();
x1.read();
x1.check();
}
}

Vous aimerez peut-être aussi