You are on page 1of 69

List of Experiments

1) Develop a Java package with simple Stack and Queue classes.


2) Design a class for Complex numers in Java.
!) Design a Date class
") Design a simple test application to demonstrate d#namic pol#morphism.
$) Design a Java interface for %D& Stack.
') Design a file that contains D(% se)uences.
*) Develop a simple paint+like program that can draw asic graphical primitives
,) Develop a scientific calculator using even+driven programming
-) Develop a template for linked+list
1.) Design a thread+safe implementation of Queue class.
11) Develop a multi+threaded Java program to print all numers elow 1../... that are
oth prime and 0ionacci numer 1some examples are 2/ !/ $/ 1!/ etc.). Design a thread
that generates prime numers elow 1../... and writes them into a pipe. Design another
thread that generates fionacci numers and writes them to another pipe. &he main
thread should read oth the pipes to identif# numers common to oth.
12) Develop a multi+threaded 234 application of #our choice.
Ex.No : 1 Develop a Java package with simple Stack and Qee classes

!"#
&o develop a Java package with simple Stack and Queue classes. 3se JavaDoc
comments for documentation.
!L$%&"'(#
Step15 Start.
Step25 Declare the class for stack and )ueue.
Step!5 4nsert the elements to stack and )ueue.
Step"5 6erform the operation on stack and )ueue
Step$5 Stop.
S%)&*E *%DE
77package com.st.8oesph.demo.)ueue9
7::
: %rra#+ased implementation of the )ueue.
:
:7
pulic class %rra#Queue ;
private <8ect = > the%rra#9
private int currentSi?e9
private int front9
private int ack9
private static final int D@0%3A&BC%6%C4&C D 1.9
7::
: Construct the )ueue.
:7
pulic %rra#Queue1 )
;
the%rra# D new <8ect= D@0%3A&BC%6%C4&C >9
make@mpt#1 )9
E
7::
: &est if the )ueue is logicall# empt#.
: Freturn GHtrueG7H if empt#/ GcodeHfalseG7codeH otherwise.
:7
pulic oolean is@mpt#1 )
;
return currentSi?e DD .9
E
7::
: Iake the )ueue logicall# empt#.
:7
pulic void make@mpt#1 )
;
currentSi?e D .9
front D .9
ack D +19
E
7::
: Jeturn and remove the least recentl# inserted item
: from the )ueue.
: Freturn the least recentl# inserted item in the )ueue.
:7
pulic <8ect de)ueue1 )
;
if1 is@mpt#1 ) )
throw new Juntime@xception1 K%rra#Queue de)ueueK )9
currentSi?e++9
<8ect returnLalue D the%rra#= front >9
front D increment1 front )9
return returnLalue9
E
7::
: 2et the least recentl# inserted item in the )ueue.
: Does not alter the )ueue.
: Freturn the least recentl# inserted item in the )ueue.
:7
pulic <8ect get0ront1 )
;
if1 is@mpt#1 ) )
throw new Juntime@xception1 K%rra#Queue get0rontK )9
return the%rra#= front >9
E
7::
: 4nsert a new item into the )ueue.
: Fparam x the item to insert.
:7
pulic void en)ueue1 <8ect x )
;
if1 currentSi?e DD the%rra#.length )
douleQueue1 )9
ack D increment1 ack )9
the%rra#= ack > D x9
currentSi?eMM9
E
7::
: 4nternal method to increment with wraparound.
: Fparam x an# index in the%rra#Ns range.
: Freturn xM1/ or . if x is at the end of the%rra#.
:7
private int increment1 int x )
;
if1 MMx DD the%rra#.length )
x D .9
return x9
E
7::
: 4nternal method to expand the%rra#.
:7
private void douleQueue1 )
;
<8ect = > new%rra#9
new%rra# D new <8ect= the%rra#.length : 2 >9
77 Cop# elements that are logicall# in the )ueue
for1 int i D .9 i G currentSi?e9 iMM/ front D increment1 front ) )
new%rra#= i > D the%rra#= front >9
the%rra# D new%rra#9
front D .9
ack D currentSi?e + 19
EE
77package com.st.8oesph.demo.)ueue9
7::
: %rra#+ased implementation of the stack.
:
:7
pulic class %rra#Stack ;
private <8ect = > the%rra#9
private int top<fStack9
private static final int D@0%3A&BC%6%C4&C D 1.9
7::
: Construct the stack.
:7
pulic %rra#Stack1 ) ;
the%rra# D new <8ect= D@0%3A&BC%6%C4&C >9
top<fStack D +19
E

7::
: &est if the stack is logicall# empt#.
: Freturn true if empt#/ false otherwise.
:7
pulic oolean is@mpt#1 ) ;
return top<fStack DD +19
E

7::
: Iake the stack logicall# empt#.
:7
pulic void make@mpt#1 ) ;
top<fStack D +19
E

7::
: 2et the most recentl# inserted item in the stack.
: Does not alter the stack.
: Freturn the most recentl# inserted item in the stack.
:7
pulic <8ect top1 ) ;
if1 is@mpt#1 ) )
throw new Juntime@xception1 K%rra#Stack topK )9
return the%rra#= top<fStack >9
E

7::
: Jemove the most recentl# inserted item from the stack.
:7
pulic void pop1 ) ;
if1 is@mpt#1 ) )
throw new Juntime@xception1 K%rra#Stack popK )9
top<fStack++9
E

7::
: Jeturn and remove the most recentl# inserted item
: from the stack.
: Freturn the most recentl# inserted item in the stack.
:7
pulic <8ect top%nd6op1 ) ;
if1 is@mpt#1 ) )
throw new Juntime@xception1 K%rra#Stack top%nd6opK )9
return the%rra#= top<fStack++ >9
E

7::
: 4nsert a new item into the stack.
: Fparam x the item to insert.
:7
pulic void push1 <8ect x ) ;
if1 top<fStack M 1 DD the%rra#.length )
doule%rra#1 )9
the%rra#= MMtop<fStack > D x9
E

7::
: 4nternal method to extend the%rra#.
:7
private void doule%rra#1 ) ;
<8ect = > new%rra#9

new%rra# D new <8ect= the%rra#.length : 2 >9
for1 int i D .9 i G the%rra#.length9 iMM )
new%rra#= i > D the%rra#= i >9
the%rra# D new%rra#9
E

E
77package com.st.8oesph.demo.)ueue9
pulic class QueueStack&ester ;
pulic static void main1String=> args) ;
S#stem.out.println1K::::::::::::::::::::::::::::K)9
S#stem.out.println1KQueue @xampleK)9
S#stem.out.println1K::::::::::::::::::::::::::::K)9
%rra#Queue a) D new %rra#Queue1)9
a).en)ueue1new String1K1K))9
a).en)ueue1new String1K2K))9
a).en)ueue1new String1K!K))9
a).en)ueue1new String1K"K))9
S#stem.out.println1KQueue @lements +H 1/ 2/ !/ "K)9
S#stem.out.println1KQueue 040< +H KMa).get0ront1))9
S#stem.out.println1KQueue removed element +H KM a).de)ueue1))9
S#stem.out.println1KQueue 040< +H KMa).get0ront1))9
S#stem.out.println1K::::::::::::::::::::::::::::K)9
S#stem.out.println1KStack @xampleK)9
S#stem.out.println1K::::::::::::::::::::::::::::K)9
%rra#Stack arra#Stack D new %rra#Stack1)9
arra#Stack.push1new String1KaK))9
arra#Stack.push1new String1KK))9
arra#Stack.push1new String1KcK))9
arra#Stack.push1new String1KdK))9
S#stem.out.println1KStack @lements +H a/ / c/ dK)9
S#stem.out.println1KStack A40< +H KMarra#Stack.top1))9
arra#Stack.pop1)9
S#stem.out.println1K6<6 on Stack K )9
S#stem.out.println1KStack A40< +H KMarra#Stack.top1))9
E
E
%)'+)'
E:,Experiment 1-.ava QeeStack'ester
////////////////////////////
Qee Example
////////////////////////////
Qee Elements 0- 11 21 31 4
Qee 5"5% 0- 1
Qee removed element 0- 1
Qee 5"5% 0- 2
////////////////////////////
Stack Example
////////////////////////////
Stack Elements 0- a1 61 c1 d
Stack L"5% 0- d
+%+ on Stack
Stack L"5% 0- c
E:,Experiment 1-
Ex.No : 2 Design a class for *omplex nm6ers in Java
!"#
&o design a class for Complex numers in Java. 4n addition to methods for asic
operations on complex numers/ provide a method to return the numer of active o8ects
created.
!L$%&"'(#
Step15 Start.
Step25 Declare the class for complex addition/ sutraction/ multiplication/ Division.
Step!5 @nter the value for given variales.
Step"5 6erform the operation on complex numers
Step$5 Stop.
S%)&*E *%DE
pulic class Complex(umer ;
pulic static int counter D .9
private doule real6art9
private doule imaginar#6art9
7::
: Default Constructor
:7
pulic Complex(umer1);
counterMM9
E
7::
: 6arameteri?ed Constructor
: Fparam real6art
: Fparam imaginar#6art
:7
pulic Complex(umer1doule real6art/ doule imaginar#6art) ;
this1)9
this.real6art D real6art9
this.imaginar#6art D imaginar#6art9
E
7::
: 6arameteri?ed Constructor
: Fparam real6art
: Fparam imaginar#6art
:7
pulic Complex(umer1Complex(umer complex(umer) ;
this1)9
this.real6art D complex(umer.getJeal6art1)9
this.imaginar#6art D complex(umer.get4maginar#6art1)9
E
7::
: Freturn the real6art
:7
pulic doule getJeal6art1) ;
return real6art9
E
7::
: Fparam real6art the real6art to set
:7
pulic void setJeal6art1doule real6art) ;
this.real6art D real6art9
E
7::
: Freturn the imaginar#6art
:7
pulic doule get4maginar#6art1) ;
return imaginar#6art9
E
7::
: Fparam imaginar#6art the imaginar#6art to set
:7
pulic void set4maginar#6art1doule imaginar#6art) ;
this.imaginar#6art D imaginar#6art9
E
pulic Complex(umer getComplexCon8ugate1);
return new Complex(umer1this.real6art/ this.imaginar#6art : +1)9
E
7::
: Fparam multiCom(um
: Freturn
:7
pulic Complex(umer multipl#&o1Complex(umer multiCom(um);
Complex(umer result D new Complex(umer1)9
771aMi): 1cMdi) D 1ac + d) M 1ad M c) i
doule Breal D 11this.real6art : multiCom(um.getJeal6art1)) +
1this.imaginar#6art : multiCom(um.get4maginar#6art1)))9
doule Bimgr# D 11this.real6art : multiCom(um.get4maginar#6art1)) M
1this.imaginar#6art : multiCom(um.getJeal6art1)))9
result.setJeal6art1Breal)9
result.set4maginar#6art1Bimgr#)9
return result9
E
7::
: Fparam addCom(um
: Freturn
:7
pulic Complex(umer add&o1Complex(umer addCom(um);
Complex(umer result D new Complex(umer1)9
771aMi) M 1cMdi) D 1aMc) M 1Md) i
doule Breal D 1this.real6art M addCom(um.getJeal6art1))9
doule Bimgr# D 1this.imaginar#6art M addCom(um.get4maginar#6art1))9
result.setJeal6art1Breal)9
result.set4maginar#6art1Bimgr#)9
return result9
E
7::
: Fparam susCom(um
: Freturn
:7
pulic Complex(umer sus&o1Complex(umer susCom(um);
Complex(umer result D new Complex(umer1)9
771aMi) + 1cMdi) D 1a+c) M 1+d) i
doule Breal D 1this.real6art + susCom(um.getJeal6art1))9
doule Bimgr# D 1this.imaginar#6art + susCom(um.get4maginar#6art1))9
result.setJeal6art1Breal)9
result.set4maginar#6art1Bimgr#)9
return result9
E
7::
: Fparam divCom(um
: Freturn
:7
pulic Complex(umer div&o1Complex(umer divCom(um);
Complex(umer result D new Complex(umer1divCom(um)9
771aMi) 7 1cMdi) D 1aMi)1c+di) 7 1c Mdi)1c + di)
77numerator part
result D result.multipl#&o1divCom(um.getComplexCon8ugate1))9
77dr
doule dnr D divCom(um.getJeal6art1) : divCom(um.getJeal6art1) M
divCom(um.get4maginar#6art1) : divCom(um.getJeal6art1)9
result.setJeal6art1result.getJeal6art1) 7 dnr)9
result.set4maginar#6art1result.get4maginar#6art1) 7 dnr)9
return result9
E
pulic String toString1);
String img6art D 11this.imaginar#6art) G . O
String.value<f1this.imaginar#6art) 5 KMKMthis.imaginar#6art)9
return this.real6artMKKMimg6artMKiK9
E
pulic static void main1String=> args) ;
S#stem.out.println1K::::::::::::::::::::::::::::::::::::K)9
S#stem.out.println1KComplex (umer @xampleK)9
S#stem.out.println1K::::::::::::::::::::::::::::::::::::K)9
Complex(umer complex(umer D new Complex(umer1,/ ")9
Complex(umer complex(umer2 D new Complex(umer11/ 2)9
S#stem.out.println1KComplex (umer D KMcomplex(umer)9
S#stem.out.println1complex(umer MK ComplexCon8ugate D
KMcomplex(umer.getComplexCon8ugate1))9
S#stem.out.println1KPnComplex Iultiplication K)9
S#stem.out.println1K1KMcomplex(umerMK) 1KMcomplex(umer2 MK) D KM
complex(umer.multipl#&o1complex(umer2))9
S#stem.out.println1KPnComplex %ddition K)9
S#stem.out.println1K1KMcomplex(umerMK) M 1KMcomplex(umer2 MK) D
KM complex(umer.add&o1complex(umer2))9
S#stem.out.println1KPnComplex Suraction K)9
S#stem.out.println1K1KMcomplex(umerMK) + 1KMcomplex(umer2 MK) D
KM complex(umer.sus&o1complex(umer2))9
S#stem.out.println1KPnComplex Division K)9
S#stem.out.println1K1KMcomplex(umerMK) 7 1KMcomplex(umer2 MK) D
KM complex(umer.div&o1complex(umer2))9
S#stem.out.println1KPn&he numer of active o8ects created during the
asic complex numer operations + KMComplex(umer.counter)9
E
E
%)'+)'
E:,Experiment 2-.ava *omplexNm6er
////////////////////////////////////
*omplex Nm6er Example
////////////////////////////////////
*omplex Nm6er 7 8.9:4.9i
8.9:4.9i *omplex*on.gate 7 8.904.9i
*omplex #ltiplication
;8.9:4.9i< ;1.9:2.9i< 7 9.9:29.9i
*omplex !ddition
;8.9:4.9i< : ;1.9:2.9i< 7 =.9:>.9i
*omplex S6raction
;8.9:4.9i< 0 ;1.9:2.9i< 7 ?.9:2.9i
*omplex Division
;8.9:4.9i< @ ;1.9:2.9i< 7 1.>>>>>>>>>>>>>>>?:9.9i
'he nm6er of active o6.ects created dring the 6asic complex nm6er operations 0
=
E:,Experiment 2-
Ex.No : 3 Design a Date class
!"#
&o design a Date class similar to the one provided in the 8ava.util package.
!L$%&"'(#
Step15 Start.
Step25 Declare the class for Date
Step!5 Declare the variale for date class.
Step"5 6erform the operation on date class
Step$5 Stop.
S%)&*E *%DE
import 8ava.util.Date9
import 8ava.text.6arse@xception9
import 8ava.text.SimpleDate0ormat9
p6lic class Date@xample ;
private static void Date@xample1) ;
Date date D new Date1)9
S#stem.out.println1KCurrent Date and &ime is 5 K M date)9
S#stem.out.println1)9
S#stem.out.println1KDate o8ect showing specific date and timeK)9
Date particulardate1 D new Date12"A:'.A:'.A:1...A)9
Date particulardate2 D new Date1.A)9
S#stem.out.println1)9
S#stem.out.println1K0irst 6articular date 5 K M particulardate1)9
S#stem.out.println1KSecond 6articular date5 K M particulardate2)9
S#stem.out.println1)9
S#stem.out.println1KDemo of get&ime1) method returning millisecondsK)9
S#stem.out.println1)9
Date strtime D new Date1)9
S#stem.out.println1KStart &ime5 K M strtime)9
Date endtime D new Date1)9
S#stem.out.println1K@nd &ime is5 K M endtime)9
long elapsedBtime D endtime.get&ime1) + strtime.get&ime1)9
S#stem.out.println1K@lapsed &ime is5K M elapsedBtime M K millisecondsK)9
S#stem.out.println1)9
S#stem.out.println1KChanged date o8ect using set&ime1) methodK)9
S#stem.out.println1)9
Date chngdate D new Date1)9
S#stem.out.println1KDate efore change is5 K M chngdate)9
chngdate.set&ime12"A:'.A:'.A:1...A)9
S#stem.out.println1K(ow the Changed date is5 K M chngdate)9
S#stem.out.println1)9
E

p6lic static void main1String=> args) ;
S#stem.out.println1)9
Date@xample1)9
E
E
%)'+)'
P E:,Experiment 3-.ava simpledate
////////////////////////////////////
Current Date and &ime is 5 Ion Dec 1. 1,5!-52* 2I&M.$5!. 2..*
Date o8ect showing specific date and time
0irst 6articular date 5 0ri Jan .2 .$5!.5.. 2I&M.$5!. 1-*.
Second 6articular date5 &hu Jan .1 .$5!.5.. 2I&M.$5!. 1-*.
Demo of get&ime1) method returning milliseconds
Start &ime5 Ion Dec 1. 1,5!-52, 2I&M.$5!. 2..*
@nd &ime is5 Ion Dec 1. 1,5!-52, 2I&M.$5!. 2..*
@lapsed &ime is5. milliseconds
Changed date o8ect using set&ime1) method
Date efore change is5 Ion Dec 1. 1,5!-52, 2I&M.$5!. 2..*
(ow the Changed date is5 0ri Jan .2 .$5!.5.. 2I&M.$5!. 1-*.
Ex.No : 4 Design a simple test application to demonstrate dAnamic polAmorphism.
!"#
&o develop with suitale hierarch#/ classes for 6oint/ Shape/ Jectangle/ S)uare/
Circle/ @llipse/ &riangle/ 6ol#gon/ etc. Design a simple test application to demonstrate
d#namic pol#morphism.
!L$%&"'(#
Step15 Start.
Step25 Define ! functions with same name Shape1) and different arguments.
Step!5 Call the Shape1) to find the area of the rectangle
Step"5 Call the Shape1) to find the area of the S)uare
Step$5 Call the Shape1) to find the area of the circle
Step'5 Stop.
S%)&*E *%DE
pulic class Circle implements 4Shape ;
private doule radius9
pulic Circle1doule radius);
this.radius D radius9
E
pulic Doule get%reaLalue1) ;
doule area D 2 : !.1" : radius : radius9
return area9
E
pulic String get<8ect(ame1) ;
return KCircleK9
E
E
pulic class D#namic6ol#morphsim ;
pulic static void main1String=> args) ;
S#stem.out.println1K::::::::::::::::::::::::::::::::::::::::::::K)9
S#stem.out.println1KPtD#namic 6ol#morphsim @xampleK)9
S#stem.out.println1K::::::::::::::::::::::::::::::::::::::::::::K)9
Circle circle<8ect D new Circle12.")9
S#stem.out.println1KCircle o8ect is created.PnK)9
S)uare s)uare D new S)uare12)9
S#stem.out.println1KS)uare o8ect is created.PnK)9
Jectangular rectangular D new Jectangular12/ 1.)9
S#stem.out.println1KJectangular o8ect is created.PnK)9
&riangle triangle D new &riangle1!/ 2.*')9
S#stem.out.println1KJectangular o8ect is created.PnK)9
(onShape ec D new (onShape1)9
S#stem.out.println1K(onShape o8ect is created.PnK)9
S#stem.out.println1K+++++++++++++++++++++++++++++++++++++++K)9
S#stem.out.println1K Calling D#namic 6ol#morphsim methodK)9
S#stem.out.println1K+++++++++++++++++++++++++++++++++++++++K)9
getShape<8ectDetails1circle<8ect)9
getShape<8ectDetails1s)uare)9
getShape<8ectDetails1rectangular)9
getShape<8ectDetails1triangle)9
getShape<8ectDetails1ec)9
E
pulic static void getShape<8ectDetails1<8ect iShape);
if1iShape QD null);
if1iShape instanceof 4Shape);
4Shape iShape1 D 14Shape)iShape9
S#stem.out.println1K%rea of the
KMiShape1.get<8ect(ame1) MK D KMiShape1.get%reaLalue1))9
Eelse;
S#stem.out.println1KPn6assed o8ect which is not
implemented from 4Shape 4nterfaceK)9
E
E
E
E
pulic interface 4Shape ;
7::
: Freturn the G7codeH4ShapeG7codeH <8ect (ame
:7
pulic String get<8ect(ame1)9
7::
: Freturn the GH%reaG7H value of the o8ect
:7
pulic Doule get%reaLalue1)9
E
pulic class (onShape ;
pulic doule get%reaLalue1);
return ...9
E
pulic String get<8ect(ame1);
return KtestK9 EE
pulic class Jectangular implements 4Shape ;
private doule width9
private doule length9
pulic Jectangular1doule width/ doule length);
this.width D width9
this.length D length9
E
pulic Doule get%reaLalue1) ;
return width : length9
E
pulic String get<8ect(ame1) ;
return KJectangularK9
E
E
pulic class S)uare implements 4Shape ;
private doule si?e9
pulic S)uare1doule si?e);
this.si?e D si?e9
E
pulic Doule get%reaLalue1) ;
return si?e : si?e9
E
pulic String get<8ect(ame1) ;
return KS)uareK9
E
E
pulic class &riangle implements 4Shape ;
private doule ase9
private doule height9
pulic &riangle1doule ase/ doule height);
this.ase D ase9
this.height D height9
E
pulic Doule get%reaLalue1) ;
doule area D ..$ : ase : height9
return area9
E
pulic String get<8ect(ame1) ;
return K&riangleK9
E
E
%)'+)'
E:,Experiment 4-.avac /..ava
E:,Experiment 4-.ava DAnamic+olAmorphsim
////////////////////////////////////////////
DAnamic +olAmorphsim Example
////////////////////////////////////////////
*ircle o6.ect is created.
SBare o6.ect is created.
&ectanglar o6.ect is created.
&ectanglar o6.ect is created.
NonShape o6.ect is created.
000000000000000000000000000000000000000
*alling DAnamic +olAmorphsim method
000000000000000000000000000000000000000
!rea of the *ircle 7 3>.1?2?==========C
!rea of the SBare 7 4.9
!rea of the &ectanglar 7 29.9
!rea of the 'riangle 7 4.14
+assed o6.ect which is not implemented from "Shape "nterface
E:,Experiment 4-
Ex.No : C Design a Java interface for !D' Stack
!"#
&o design a Java interface for %D& Stack. Develop two different classes that
implement this interface/ one using arra# and the other using linked+list. 6rovide
necessar# exception handling in oth the implementations.
!L$%&"'(#
Step15 Start.
Step25 Declare the class for Stack using %rra#.
Step!5 Declare the class for Stack using Ainked list
Step"5 Create the interface for this class
Step$5 6erform operation on stack
Step'5 Stop.
S%)&*E *%DE
7::
: %rra#+ased implementation of the stack.
:
:7
pulic class %rra#Stack implements Stack ;
private <8ect = > the%rra#9
private int top<fStack9
private static final int D@0%3A&BC%6%C4&C D 1.9
7::
: Construct the stack.
:7
pulic %rra#Stack1 ) ;
the%rra# D new <8ect= D@0%3A&BC%6%C4&C >9
top<fStack D +19
E

7::
: &est if the stack is logicall# empt#.
: Freturn true if empt#/ false otherwise.
:7
pulic oolean is@mpt#1 ) ;
return top<fStack DD +19
E

7::
: Iake the stack logicall# empt#.
:7
pulic void make@mpt#1 ) ;
top<fStack D +19
E

7::
: 2et the most recentl# inserted item in the stack.
: Does not alter the stack.
: Freturn the most recentl# inserted item in the stack.
: Fthrows 3nderflow@xception if the stack is empt#.
:7
pulic <8ect top1 ) ;
if1 is@mpt#1 ) )
throw new 3nderflow@xception1 K%rra#Stack topK )9
return the%rra#= top<fStack >9
E

7::
: Jemove the most recentl# inserted item from the stack.
: Fthrows 3nderflow@xception if the stack is empt#.
:7
pulic void pop1 ) ;
if1 is@mpt#1 ) )
throw new 3nderflow@xception1 K%rra#Stack popK )9
top<fStack++9
E

7::
: Jeturn and remove the most recentl# inserted item
: from the stack.
: Freturn the most recentl# inserted item in the stack.
: Fthrows 3nderflow if the stack is empt#.
:7
pulic <8ect top%nd6op1 ) ;
if1 is@mpt#1 ) )
throw new 3nderflow@xception1 K%rra#Stack top%nd6opK )9
return the%rra#= top<fStack++ >9
E

7::
: 4nsert a new item into the stack.
: Fparam x the item to insert.
:7
pulic void push1 <8ect x ) ;
if1 top<fStack M 1 DD the%rra#.length )
doule%rra#1 )9
the%rra#= MMtop<fStack > D x9
E

7::
: 4nternal method to extend the%rra#.
:7
private void doule%rra#1 ) ;
<8ect = > new%rra#9

new%rra# D new <8ect= the%rra#.length : 2 >9
for1 int i D .9 i G the%rra#.length9 iMM )
new%rra#= i > D the%rra#= i >9
the%rra# D new%rra#9
E
E
77AistStack class
77
77 C<(S&J3C&4<(5 with no initiali?er
77
77 ::::::::::::::::::63RA4C <6@J%&4<(S:::::::::::::::::::::
77 void push1 x ) ++H 4nsert x
77 void pop1 ) ++H Jemove most recentl# inserted item
77 <8ect top1 ) ++H Jeturn most recentl# inserted item
77 <8ect top%nd6op1 ) ++H Jeturn and remove most recent item
77 oolean is@mpt#1 ) ++H Jeturn true if empt#9 else false
77 void make@mpt#1 ) ++H Jemove all items
77 ::::::::::::::::::@JJ<JS::::::::::::::::::::::::::::::::
77 top/ pop/ or top%nd6op on empt# stack
7::
: Aist+ased implementation of the stack.
:
:7
pulic class AinkedAistStack implements Stack ;
7::
: Construct the stack.
:7
pulic AinkedAistStack1 ) ;
top<fStack D null9
E
7::
: &est if the stack is logicall# empt#.
: Freturn true if empt#/ false otherwise.
:7
pulic oolean is@mpt#1 ) ;
return top<fStack DD null9
E
7::
: Iake the stack logicall# empt#.
:7
pulic void make@mpt#1 ) ;
top<fStack D null9
E
7::
: 4nsert a new item into the stack.
: Fparam x the item to insert.
:7
pulic void push1 <8ect x ) ;
top<fStack D new Aist(ode1 x/ top<fStack )9
E
7::
: Jemove the most recentl# inserted item from the stack.
: Fthrows 3nderflow@xception if the stack is empt#.
:7
pulic void pop1 ) ;
if1 is@mpt#1 ) )
throw new 3nderflow@xception1 KAistStack popK )9
top<fStack D top<fStack.next9
E
7::
: 2et the most recentl# inserted item in the stack.
: Does not alter the stack.
: Freturn the most recentl# inserted item in the stack.
: Fthrows 3nderflow@xception if the stack is empt#.
:7
pulic <8ect top1 ) ;
if1 is@mpt#1 ) )
throw new 3nderflow@xception1 KAistStack topK )9
return top<fStack.element9
E
7::
: Jeturn and remove the most recentl# inserted item
: from the stack.
: Freturn the most recentl# inserted item in the stack.
: Fthrows 3nderflow@xception if the stack is empt#.
:7
pulic <8ect top%nd6op1 ) ;
if1 is@mpt#1 ) )
throw new 3nderflow@xception1 KAistStack top%nd6opK )9
<8ect top4tem D top<fStack.element9
top<fStack D top<fStack.next9
return top4tem9
E
private Aist(ode top<fStack9
E
pulic class Aist(ode ;
pulic <8ect element9
pulic Aist(ode next9
77 Constructors
pulic Aist(ode1 <8ect the@lement ) ;
this1 the@lement/ null )9
E
pulic Aist(ode1 <8ect the@lement/ Aist(ode n ) ;
element D the@lement9
next D n9
E
E
pulic interface Stack ;
7::
: 4nsert a new item into the stack.
: Fparam x the item to insert.
:7
void push1 <8ect x )9
7::
: Jemove the most recentl# inserted item from the stack.
: Fexception 3nderflow@xception if the stack is empt#.
:7
void pop1 )9
7::
: 2et the most recentl# inserted item in the stack.
: Does not alter the stack.
: Freturn the most recentl# inserted item in the stack.
: Fexception 3nderflow@xception if the stack is empt#.
:7
<8ect top1 )9
7::
: Jeturn and remove the most recentl# inserted item
: from the stack.
: Freturn the most recentl# inserted item in the stack.
: Fexception 3nderflow@xception if the stack is empt#.
:7
<8ect top%nd6op1 )9
7::
: &est if the stack is logicall# empt#.
: Freturn true if empt#/ false otherwise.
:7
oolean is@mpt#1 )9
7::
: Iake the stack logicall# empt#.
:7
void make@mpt#1 )9
E
pulic class Stack&ester ;
pulic static void main1String=> args) ;
S#stem.out.println1K::::::::::::::::::::::::::::::::::::::::::K)9
S#stem.out.println1KStack using %rra# S Ainked Aist exampleK)9
S#stem.out.println1K::::::::::::::::::::::::::::::::::::::::::K)9
%rra#Stack arra#Stack D new %rra#Stack1)9
arra#Stack.push1new String1KaK))9
arra#Stack.push1new String1KK))9
arra#Stack.push1new String1KcK))9
S#stem.out.println1KStack=using arra#> elements +H a/ / cK)9
S#stem.out.println1KStack A40< and 6<6 +H KMarra#Stack.top%nd6op1))9
S#stem.out.println1KStack A40< +H KMarra#Stack.top1))9
arra#Stack.pop1)9
tr#;
arra#Stack.pop1)9
arra#Stack.top%nd6op1)9
Ecatch1Juntime@xception rte);
S#stem.err.println1K@xception occured while 6<6 operation is
happened on Stack=# using arra#>K)9
E
S#stem.out.println1KPnPn::::::::::::::::::::::::::::::K)9
S#stem.out.println1KStack using Ainked Aist exampleK)9
S#stem.out.println1K::::::::::::::::::::::::::::::K)9
AinkedAistStack linkedAistStack D new AinkedAistStack1)9
linkedAistStack.push1new 4nteger11.))9
linkedAistStack.push1new 4nteger12.))9
linkedAistStack.push1new 4nteger1!.))9
linkedAistStack.push1new 4nteger1".))9
S#stem.out.println1KStack=using linked list> elements +H 1./ 2./ !./ ".K)9
S#stem.out.println1KStack &<6 +HKMlinkedAistStack.top1))9
linkedAistStack.pop1)9
S#stem.out.println1KStack &<6 after 6<6 +HKMlinkedAistStack.top1))9
linkedAistStack.pop1)9
linkedAistStack.pop1)9
linkedAistStack.pop1)9
tr#;
linkedAistStack.pop1)9
Ecatch1Juntime@xception rte);
S#stem.err.println1K@xception occured while 6<6 operation is
happened on Stack=# using linked list>K)9
E
E
E
7::
: @xception class for access in empt# containers
: such as stacks/ )ueues/ and priorit# )ueues.
:
:7
pulic class 3nderflow@xception extends Juntime@xception ;
7::
: Construct this exception o8ect.
: Fparam message the error message.
:7
pulic 3nderflow@xception1 String message ) ;
super1 message )9
E
E
%)'+)'
E:,Experiment C-.avac /..ava
E:,Experiment C-.ava Stack'ester
//////////////////////////////////////////
Stack sing !rraA D Linked List example
//////////////////////////////////////////
StackEsing arraAF elements 0- a1 61 c
Stack L"5% and +%+ 0- c
Stack L"5% 0- 6
Exception occred while +%+ operation is happened on StackE6A sing arraAF
//////////////////////////////
Stack sing Linked List example
//////////////////////////////
StackEsing linked listF elements 0- 191 291 391 49
Stack '%+ 0-49
Stack '%+ after +%+ 0-39
Exception occred while +%+ operation is happened on StackE6A sing linked listF
Ex. No : > Design a file that contains DN! seBences
!"#
&o write a Java program to read a file that contains D(% se)uences of aritrar#
length one per line 1note that each D(% se)uence is 8ust a String). Cour program should
sort the se)uences in descending order with respect to the numer of N2CN
!L$%&"'(#
Step15 Start.
Step25 Create the file to get D(% se)uence
Step!5 0ind the word 2C from the input file.
Step"5 Sort the D(% se)uence
Step$5 Trite the sorted se)uence into output file
Step'5 Stop.
S%)&*E *%DE
import 8ava.io.Ruffered<utputStream9
import 8ava.io.RufferedJeader9
import 8ava.io.0ile9
import 8ava.io.0ile(ot0ound@xception9
import 8ava.io.0ile<utputStream9
import 8ava.io.0ileJeader9
import 8ava.io.4<@xception9
import 8ava.util.%rra#Aist9
import 8ava.util.%rra#s9
import 8ava.util.Aist9
pulic class 0ile<peration ;
7::
: suse)uence search text
:7
pulic static String S@%JCUBC<(&@(& D K2CK9
pulic static void main1String=> args) ;
String fi D K.PPfilePPinput.txtK9
String out D K.PPfilePPoutput.txtK9
S#stem.out.println1K::::::::::::::::::::::::::::::::::::::::::::::::::::K)9
S#stem.out.println1K D(% se)uence example # 0ile Jead7Trite
<perationK)9
S#stem.out.println1K:::::::::::::::::::::::::::::::::::::::::::::::::::::K)9
0ile<peration fo D new 0ile<peration1)9
String content D fo.get0ileContent1fi)9
fo.write&ext&o0ile1content/ out)9
E
pulic void write&ext&o0ile1String content/ String file6ath);
0ile out0ile D null9
0ile<utputStream fis D null9
Ruffered<utputStream os D null9
out0ile D new 0ile1file6ath)9
tr# ;
S#stem.out.println1KPnSorted contents are written into
KMout0ile.getCanonical6ath1))9
fis D new 0ile<utputStream1out0ile)9
os D new Ruffered<utputStream1fis)9
os.write1content.getR#tes1))9
os.close1)9
E catch 14<@xception e) ;
e.printStack&race1)9
E
E
pulic String get0ileContent1String file6ath) ;
String content D null9
0ile file D new 0ile1file6ath)9
0ileJeader fr D null9
RufferedJeader r D null9
AistG0ileContentH list0ileContent D new %rra#AistG0ileContentH1)9
tr# ;
S#stem.out.println1KD(% Se)uence file is located from
=KMfile.getCanonical6ath1)MK>K)9
S#stem.out.println1KPn+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++K)9
fr D new 0ileJeader1file)9
r D new RufferedJeader1fr)9
int i D .9
String lineString D r.readAine1)9
while1lineString QD null);
S#stem.out.println1lineString)9
77S#stem.out.println1K ++H KM getSe)Count1lineString/
K2CK))9
0ileContent fileContent D new 0ileContent1iMM/
getSe)Count1lineString/ S@%JCUBC<(&@(&)/ lineString)9
list0ileContent.add1fileContent)9
lineString D r.readAine1)9
E
fr.close1)9
r.close1)9
E catch 10ile(ot0ound@xception e) ;
e.printStack&race1)9
E catch 14<@xception e) ;
e.printStack&race1)9
E
S#stem.out.println1K+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++K)9
S#stem.out.println1KContents are loaded to find SuSe)uences of
NKMS@%JCUBC<(&@(&MKN for sorting the D(% contentsK)9
content D sortAines1list0ileContent)9
S#stem.out.println1KPnD(% Se)uence contents are sorted.....K)9
return content9
E
pulic String sortAines1AistG0ileContentH list0ileContents);
AistG0ileContentH Blst0ileContent D list0ileContents9
StringRuffer stringRuffer D new StringRuffer1)9
int => se)Cnt%rra# D new int =list0ileContents.si?e1)M1>9
int i D .9
for10ileContent fileContent 5 Blst0ileContent);
se)Cnt%rra#=iMM> D fileContent.getSe)Count1)9
E
7:S<J&4(2 &U@ %JJ%C :7
%rra#s.sort1se)Cnt%rra#)9
for1int itr D se)Cnt%rra#.length9 itr H . 9 itr++);
for1int 8D.9 8 G Blst0ileContent.si?e1) 9 8MM);
0ileContent fileContent D list0ileContents.get18)9
if1fileContent.getSe)Count1) DD se)Cnt%rra#=itr+1>);
77S#stem.out.println1K contents ++
=KMfileContent.getAineString1)MK> KMfileContent.getSe)Count1))9
stringRuffer.append1fileContent.getAineString1)
MKPnK)9
list0ileContents.remove1fileContent)9
E
E
E
return stringRuffer.toString1)9
E
pulic int getSe)Count1String lineString/ String text);
int se)Count D .9
if1lineString QD null SS text QD null SS lineString.contains1text));
int strAen D lineString.length1)9
String => textSplit D lineString.split1text)9
77S#stem.out.println1K length ++KMtextSplit.length)9
if1textSplit DD null);
return se)Count9
Eelse ;
se)Count D textSplit.length + 19
E
E
return se)Count9
E
E
class 0ileContent;
private int line(umer9
private int se)Count9
private String lineString9
7::
: 6arameteri?ed Consatructor
: Fparam se)Count
: Fparam lineString
:7
pulic 0ileContent1int line(umer/ int se)Count/ String lineString) ;
super1)9
this.line(umer D line(umer9
this.se)Count D se)Count9
this.lineString D lineString9
E
7::
: Freturn the se)Count
:7
pulic int getSe)Count1) ;
return se)Count9
E
7::
: Fparam se)Count the se)Count to set
:7
pulic void setSe)Count1int se)Count) ;
this.se)Count D se)Count9
E
7::
: Freturn the lineString
:7
pulic String getAineString1) ;
return lineString9
E
7::
: Fparam lineString the lineString to set
:7
pulic void setAineString1String lineString) ;
this.lineString D lineString9
E
7::
: Freturn the line(umer
:7
pulic int getAine(umer1) ;
return line(umer9
E
7::
: Fparam line(umer the line(umer to set
:7
pulic void setAine(umer1int line(umer) ;
this.line(umer D line(umer9
E
E
%)'+)'
"N+)' 5"LE
%C%%2%&2CC%&&2&CCCCC22CC&CC&2C&2C&2C&2C&C&CC2222CC%C2
2CC%CC2C&2CCC&2CC
CC&22%222&22CCCC%CC22CC2%2%C%2C2%2C%&%&2C%22%%2C22C%
22%%&%%22%%%%2C%2C
C&CC&2%C&&&CC&C2C&&22&22&&&2%2&22%CC&CCC%22CC%2&2CC22
2CCCC&C%&%22%2%22
%%2C&C222%22&22CC%22C22C%22%%22C2C%CCCCCCC%2C%%&CC2
C2C2CC222%C%2%%&2CC
C&2C%22%%C&&C&&C&22%%2%CC&&C&CC&CC&2C%%%&%%%%CC&C%CCC
%&2%%&2C&C%C2C%%2
&&&%%&&%C%2%CC&2%%
%)'+)' 5"LE
%C%%2%&2CC%&&2&CCCCC22CC&CC&2C&2C&2C&2C&C&CC2222CC%C2
2CC%CC2C&2CCC&2CC
%%2C&C222%22&22CC%22C22C%22%%22C2C%CCCCCCC%2C%%&CC2
C2C2CC222%C%2%%&2CC
CC&22%222&22CCCC%CC22CC2%2%C%2C2%2C%&%&2C%22%%2C22C%
22%%&%%22%%%%2C%2C
C&CC&2%C&&&CC&C2C&&22&22&&&2%2&22%CC&CCC%22CC%2&2CC22
2CCCC&C%&%22%2%22
C&2C%22%%C&&C&&C&22%%2%CC&&C&CC&CC&2C%%%&%%%%CC&C%CCC
%&2%%&2C&C%C2C%%2
&&&%%&&%C%2%CC&2%%
@5P@xperimrnt 'H8ava 0ile<peration
::::::::::::::::::::::::::::::::::::::::::::::::::::
D(% se)uence example # 0ile Jead7Trite <peration
:::::::::::::::::::::::::::::::::::::::::::::::::::::
D(% Se)uence file is located from =@5P@xperimrnt 'PfilePinput.txt>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%C%%2%&2CC%&&2&CCCCC22CC&CC&2C&2C&2C&2C&C&CC2222CC%C2
2CC%CC2C&2CCC&2CC
CC&22%222&22CCCC%CC22CC2%2%C%2C2%2C%&%&2C%22%%2C22C%
22%%&%%22%%%%2C%2C
C&CC&2%C&&&CC&C2C&&22&22&&&2%2&22%CC&CCC%22CC%2&2CC22
2CCCC&C%&%22%2%22
%%2C&C222%22&22CC%22C22C%22%%22C2C%CCCCCCC%2C%%&CC2
C2C2CC222%C%2%%&2CC
C&2C%22%%C&&C&&C&22%%2%CC&&C&CC&CC&2C%%%&%%%%CC&C%CCC
%&2%%&2C&C%C2C%%2
&&&%%&&%C%2%CC&2%%
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Contents are loaded to find SuSe)uences of N2CN for sorting the D(% contents
D(% Se)uence contents are sorted.....
Sorted contents are written into @5P@xperimrnt 'PfilePoutput.txt
@5P@xperimrnt 'H
Ex.No: ? Develop a simple paint0like program that can draw 6asic graphical
primitives
!"#
&o develop a simple paint+like program that can draw asic graphical primitives
in different dimensions and colors. 3se appropriate menu and uttons.
!L$%&"'(#
Step15 Start.
Step25 Create the applet program for paint
Step!5 Create utton and menu on the applet
Step"5 6erform the operation on paint
Step$5 Stop.
S%)&*E *%DE
7::
&his is the astract parent class for different shape classes/
like rectangle/ oval/ pol#gon and triangle. 4t provides an astract
method draw1).
:7
import 8ava.util.:9
import 8ava.awt.:9
pulic astract class Shapes ;
7::astract method draw1)
Freturn void
:7
pulic astract void draw18ava.util.Aist list/ 2raphics g)9
E
77different implementations of Shape class
class JectangleShape extends Shapes
;
6oint s6oint D null9
6oint e6oint D null9
pulic void draw18ava.util.Aist list/ 2raphics g)
;
4terator it D list.iterator1)9
77if the list does not contain the re)uired two points/ return.
if1list.si?e1)G2)
;
return9
E
s6oint D 16oint)it.next1)9
e6oint D 16oint)it.next1)9
if1s6oint DD null VV e6oint DD null)
;
return9
E
else
;
g.fillJect11int)s6oint.getW1)/ 1int)s6oint.getC1)/ 1int)1e6oint.getW1)+s6oint.getW1))/
1int)1e6oint.getC1)+s6oint.getC1)))9
E77end of if
list.clear1)9
E77end of draw for rectangle
E77rectangle
class <valShape extends Shapes
;
6oint s6oint D null9
6oint e6oint D null9
pulic void draw18ava.util.Aist list/ 2raphics g)
;
4terator it D list.iterator1)9
77if the list does not contain the re)uired two points/ return.
if1list.si?e1)G2)
;
return9
E
s6oint D 16oint)it.next1)9
e6oint D 16oint)it.next1)9
if1s6oint DD null VV e6oint DD null)
;
return9
E
else
;
g.fill<val11int)s6oint.getW1)/ 1int)s6oint.getC1)/ 1int)1e6oint.getW1)+s6oint.getW1))/
1int)1e6oint.getC1)+s6oint.getC1)))9
E77end of if
list.clear1)9
E77end of draw for <val
E77<valShape
class &riangleShape extends Shapes
;
pulic void draw18ava.util.Aist list/ 2raphics g)
;
6oint point D null9
4terator it D list.iterator1)9
77if the list does not contain the re)uired two points/ return.
if1list.si?e1)G!)
;
return9
E
6ol#gon p D new 6ol#gon1)9
for1int i D .9 i G !9 iMM)
;
point D 16oint)it.next1)9
p.add6oint11int)point.getW1)/ 1int)point.getC1))9
E
g.fill6ol#gon1p)9
list.clear1)9
E77end of draw for &riangle
E77&riangle
class 6ol#gonShape extends Shapes
;
pulic void draw18ava.util.Aist list/ 2raphics g)
;
6oint point D null9
4terator it D list.iterator1)9
77if the list does not contain the re)uired two points/ return.
if1list.si?e1)G!)
;
return9
E
6ol#gon p D new 6ol#gon1)9
for19it.has(ext1)9)
;
point D 16oint)it.next1)9
p.add6oint11int)point.getW1)/ 1int)point.getC1))9
E
g.fill6ol#gon1p)9
list.clear1)9
E77end of draw for 6ol#gon
E776ol#gon
import 8ava.awt.Color9
import 8ava.awt.Dimension9
import 8ava.awt.0rame9
import 8ava.awt.2raphics9
import 8ava.awt.4nsets9
import 8ava.awt.Ienu9
import 8ava.awt.IenuRar9
import 8ava.awt.Ienu4tem9
import 8ava.awt.IenuShortcut9
import 8ava.awt.6anel9
import 8ava.awt.6oint9
import 8ava.awt.event.%ction@vent9
import 8ava.awt.event.%ctionAistener9
import 8ava.awt.event.Iouse@vent9
import 8ava.awt.event.IouseAistener9
import 8ava.awt.event.Tindow%dapter9
import 8ava.awt.event.Tindow@vent9
import 8avax.swing.J<ption6ane9
pulic class SimpleDrawing&ool extends 0rame;
77constants for menu shortcuts
private static final int kControl% D '$9
private static final int kControlD D ',9
private static final int kControlC D '*9
private static final int kControlJ D ,29
private static final int kControl6 D ,.9
private static final int kControl& D ,"9
private static final int kControlW D ,,9
private JectangleShape rectangle D new JectangleShape1)9
private <valShape oval D new <valShape1)9
private 6ol#gonShape pol#gon D new 6ol#gonShape1)9
private &riangleShape triangle D new &riangleShape1)9
private Drawing6anel panel9
pulic SimpleDrawing&ool1) ;
77set frameNs title
super1KSimple Drawing &oolK)9
77add menu
addIenu1)9
77add drawing panel
add6anel1)9
77add window listener
this.addTindowAistener1new TindowUandler1))9
77set frame si?e
this.setSi?e1"../ "..)9
77make this frame visile
this.setLisile1true)9
E
pulic static void main1String=> args) ;
SimpleDrawing&ool simpleDrawing&ool D new SimpleDrawing&ool1)9
E
7::
&his method creates menu ar and menu items and then attach the menu ar
with the frame of this drawing tool.
:7
private void addIenu1)
;
77%dd menu ar to our frame
IenuRar menuRar D new IenuRar1)9
Ienu file D new Ienu1K0ileK)9
Ienu shape D new Ienu1KShapesK)9
Ienu aout D new Ienu1K%outK)9
77now add menu items to these Ienu o8ects
file.add1new Ienu4tem1K@xitK/ new
IenuShortcut1kControlW))).add%ctionAistener1new TindowUandler1))9
shape.add1new Ienu4tem1KJectangleK/ new
IenuShortcut1kControlJ))).add%ctionAistener1new TindowUandler1))9
shape.add1new Ienu4tem1KCircleK/ new
IenuShortcut1kControlC))).add%ctionAistener1new TindowUandler1))9
shape.add1new Ienu4tem1K&riangleK/ new
IenuShortcut1kControl&))).add%ctionAistener1new TindowUandler1))9
shape.add1new Ienu4tem1K6ol#gonK/ new
IenuShortcut1kControl6))).add%ctionAistener1new TindowUandler1))9
shape.add1new Ienu4tem1KDraw 6ol#gonK/ new
IenuShortcut1kControlD))).add%ctionAistener1new TindowUandler1))9
aout.add1new Ienu4tem1K%outK/ new
IenuShortcut1kControl%))).add%ctionAistener1new TindowUandler1))9
77add menus to menuar
menuRar.add1file)9
menuRar.add1shape)9
menuRar.add1aout)9
77menuRar.setLisile1true)9
if1null DD this.getIenuRar1))
;
this.setIenuRar1menuRar)9
E
E77addIenu1)
7::
&his method adds a panel to SimpleDrawing&ool for drawing shapes.
:7
private void add6anel1)
;
panel D new Drawing6anel1)9
77get si?e of SimpleDrawing&ool frame
Dimension d D this.getSi?e1)9
77get insets of frame
4nsets ins D this.insets1)9
77exclude insets from the si?e of the panel
d.height D d.height + ins.top + ins.ottom9
d.width D d.width + ins.left + ins.right9
panel.setSi?e1d)9
panel.setAocation1ins.left/ ins.top)9
panel.setRackground1Color.white)9
77add mouse listener. 6anel itself will e handling mouse events
panel.addIouseAistener1panel)9
this.add1panel)9
E77end of add6anel1)9
774nner class to handle events
private class TindowUandler extends Tindow%dapter implements %ctionAistener
;
pulic void windowClosing1Tindow@vent e)
;
S#stem.exit1.)9
E
pulic void action6erformed1%ction@vent e)
;
77check to see if the action command is e)ual to exit
if1e.get%ctionCommand1).e)uals4gnoreCase1KexitK))
;
S#stem.exit1.)9
E
else if1e.get%ctionCommand1).e)uals4gnoreCase1KJectangleK))
;
Ienu menu D getIenuRar1).getIenu11)9
for1int i D .9i G menu.get4temCount1)9menu.get4tem1i).set@naled1true)/iMM)9
getIenuRar1).getShortcutIenu4tem1new
IenuShortcut1kControlJ)).set@naled1false)9
panel.drawShape1rectangle)9
E
else if1e.get%ctionCommand1).e)uals4gnoreCase1KCircleK))
;
Ienu menu D getIenuRar1).getIenu11)9
for1int i D .9i G menu.get4temCount1)9menu.get4tem1i).set@naled1true)/iMM)9
getIenuRar1).getShortcutIenu4tem1new
IenuShortcut1kControlC)).set@naled1false)9
panel.drawShape1oval)9
E
else if1e.get%ctionCommand1).e)uals4gnoreCase1K&riangleK))
;
Ienu menu D getIenuRar1).getIenu11)9
for1int i D .9i G menu.get4temCount1)9menu.get4tem1i).set@naled1true)/iMM)9
getIenuRar1).getShortcutIenu4tem1new
IenuShortcut1kControl&)).set@naled1false)9
panel.drawShape1triangle)9
E
else if1e.get%ctionCommand1).e)uals4gnoreCase1K6ol#gonK))
;
Ienu menu D getIenuRar1).getIenu11)9
for1int i D .9i G menu.get4temCount1)9menu.get4tem1i).set@naled1true)/iMM)9
getIenuRar1).getShortcutIenu4tem1new
IenuShortcut1kControl6)).set@naled1false)9
panel.drawShape1pol#gon)9
E
else if1e.get%ctionCommand1).e)uals4gnoreCase1KDraw 6ol#gonK))
;
Ienu menu D getIenuRar1).getIenu11)9
for1int i D .9i G menu.get4temCount1)9menu.get4tem1i).set@naled1true)/iMM)9
getIenuRar1).getShortcutIenu4tem1new
IenuShortcut1kControl6)).set@naled1false)9
panel.repaint1)9
E
else if1e.get%ctionCommand1).e)uals4gnoreCase1K%outK))
;
J<ption6ane.showIessageDialog1null/ K&his small paint+like program.K/ K%outK/
J<ption6ane.6A%4(BI@SS%2@)9
E
E77action6erformed1)
E77windowUandler + 4nner Class ends here
E77SimpleDrawing&ool
class Drawing6anel extends 6anel implements IouseAistener
;
private 6oint s6oint D null9
private 6oint e6oint D null9
private Shapes shape D null9
private 8ava.util.%rra#Aist list D new 8ava.util.%rra#Aist1)9
77override panel paint method to draw shapes
pulic void paint12raphics g)
;
g.setColor1Color.green)9
shape.draw1list/ g)9
E
pulic void drawShape1Shapes shape)
;
this.shape D shape9
E
77define mouse handler
pulic void mouseClicked1Iouse@vent e)
;
77if user wants to draw triangle/ call repaint after ! clicks
if1shape instanceof &riangleShape)
;
list.add1e.get6oint1))9
if1list.si?e1) H 2)
;
repaint1)9
E
E
else if1shape instanceof 6ol#gonShape)
;
list.add1e.get6oint1))9
E
E77mouseClicked
pulic void mouse@ntered1Iouse@vent e);E
pulic void mouse@xited1Iouse@vent e);E
pulic void mouse6ressed1Iouse@vent e)
;
s6oint D e.get6oint1)9
E77mouse6ressed
pulic void mouseJeleased1Iouse@vent e)
;
e6oint D e.get6oint1)9
if1e6oint.getW1) G s6oint.getW1))
;
6oint temp D e6oint9
e6oint D s6oint9
s6oint D temp9
E
if1e6oint.getC1) G s6oint.getC1))
;
int temp D 1int)e6oint.getC1)9
e6oint.# D 1int)s6oint.getC1)9
s6oint.# D temp9
E
if1shape instanceof JectangleShape VV shape instanceof <valShape)
;
list.clear1)9
list.add1s6oint)9
list.add1e6oint)9
repaint1)9
E
E77mouseJeleased
E77Drawing6anel
Ex.No : 8 Develop a scientific calclator sing even0driven programming
!"#
&o develop a scientific calculator using even+driven programming paradigm of
Java.
!L$%&"'(#
Step15 Start.
Step25 Define the variales.
Step!5 Create the scientific calculator using even+driven
Step"5 6erform operation on scienfic calculator
Step$5 Stop.
S%)&*E *%DE
import 8ava.awt.RorderAa#out9
import 8ava.awt.Color9
import 8ava.awt.Container9
import 8ava.awt.0lowAa#out9
import 8ava.awt.0ont9
import 8ava.awt.2ridAa#out9
import 8ava.awt.Tindow9
import 8ava.awt.event.%ction@vent9
import 8ava.awt.event.%ctionAistener9
import 8ava.awt.event.Xe#@vent9
import 8ava.awt.event.Tindow%dapter9
import 8ava.awt.event.Tindow@vent9
import 8avax.swing.JRutton9
import 8avax.swing.JDialog9
import 8avax.swing.J0rame9
import 8avax.swing.JAael9
import 8avax.swing.JIenu9
import 8avax.swing.JIenuRar9
import 8avax.swing.JIenu4tem9
import 8avax.swing.J6anel9
import 8avax.swing.J&ext%rea9
import 8avax.swing.Xe#Stroke9
pulic class Calculator extends J0rame implements %ctionAistener ;
77 Lariales
final int I%WB4(63&BA@(2&U D 2.9
final int 4(63&BI<D@ D .9
final int J@S3A&BI<D@ D 19
final int @JJ<JBI<D@ D 29
int displa#Iode9
oolean clear<n(extDigit/ percent9
doule last(umer9
String last<perator9
private JIenu 8menu0ile/ 8menuUelp9
private JIenu4tem 8menuitem@xit/ 8menuitem%out9
private JAael 8l<utput9
private JRutton 8nRuttons=>9
private J6anel 8plIaster/ 8plRackSpace/ 8plControl9
7:
: 0ont1String name/ int st#le/ int si?e)
Creates a new 0ont from the specified name/ st#le and point si?e.
:7
0ont f12 D new 0ont1K&imes (ew JomanK/ ./ 12)9
0ont f121 D new 0ont1K&imes (ew JomanK/ 1/ 12)9
77 Constructor
pulic Calculator1)
;
7: Set 3p the JIenuRar.
: Uave 6rovided %ll JIenuNs with Inemonics
: Uave 6rovided some JIenu4tem components with Xe#oard
%ccelerators
:7
8menu0ile D new JIenu1K0ileK)9
8menu0ile.set0ont1f121)9
8menu0ile.setInemonic1Xe#@vent.LXB0)9
8menuitem@xit D new JIenu4tem1K@xitK)9
8menuitem@xit.set0ont1f12)9
8menuitem@xit.set%ccelerator1Xe#Stroke.getXe#Stroke1 Xe#@vent.LXBW/
%ction@vent.C&JABI%SX))9
8menu0ile.add18menuitem@xit)9
8menuUelp D new JIenu1KUelpK)9
8menuUelp.set0ont1f121)9
8menuUelp.setInemonic1Xe#@vent.LXBU)9
8menuitem%out D new JIenu4tem1K%out CalculatorK)9
8menuitem%out.set0ont1f12)9
8menuUelp.add18menuitem%out)9
JIenuRar m D new JIenuRar1)9
m.add18menu0ile)9
m.add18menuUelp)9
setJIenuRar1m)9
77Set frame la#out manager
setRackground1Color.gra#)9
8plIaster D new J6anel1)9
8l<utput D new JAael1K.K)9
8l<utput.setUori?ontal&ext6osition1JAael.J42U&)9
8l<utput.setRackground1Color.TU4&@)9
8l<utput.set<pa)ue1true)9
77 %dd components to frame
getContent6ane1).add18l<utput/ RorderAa#out.(<J&U)9
8nRuttons D new JRutton=2!>9
77 2ridAa#out1int rows/ int cols/ int hgap/ int vgap)
J6anel 8plRuttons D new J6anel1)9 77 container for Juttons
77 Create numeric Juttons
for 1int iD.9 iGD-9 iMM)
;
77 set each Jutton lael to the value of index
8nRuttons=i> D new JRutton1String.value<f1i))9
E
77 Create operator Juttons
8nRuttons=1.> D new JRutton1KM7+K)9
8nRuttons=11> D new JRutton1K.K)9
8nRuttons=12> D new JRutton1KDK)9
8nRuttons=1!> D new JRutton1K7K)9
8nRuttons=1"> D new JRutton1K:K)9
8nRuttons=1$> D new JRutton1K+K)9
8nRuttons=1'> D new JRutton1KMK)9
8nRuttons=1*> D new JRutton1Ks)rtK)9
8nRuttons=1,> D new JRutton1K17xK)9
8nRuttons=1-> D new JRutton1KYK)9
8plRackSpace D new J6anel1)9
8plRackSpace.setAa#out1new 2ridAa#out11/ 1/ 2/ 2))9
8nRuttons=2.> D new JRutton1KRackspaceK)9
8plRackSpace.add18nRuttons=2.>)9
8plControl D new J6anel1)9
8plControl.setAa#out1new 2ridAa#out11/ 2/ 2 /2))9
8nRuttons=21> D new JRutton1K C@ K)9
8nRuttons=22> D new JRutton1KCK)9
8plControl.add18nRuttons=21>)9
8plControl.add18nRuttons=22>)9
77 Setting all (umered JRuttonNs to Rlue. &he rest to Jed
for 1int iD.9 iG8nRuttons.length9 iMM) ;
8nRuttons=i>.set0ont1f12)9
if 1iG1.)
8nRuttons=i>.set0oreground1Color.lue)9
else
8nRuttons=i>.set0oreground1Color.red)9
E
77 Set panel la#out manager for a " # $ grid
8plRuttons.setAa#out1new 2ridAa#out1"/ $/ 2/ 2))9
77%dd uttons to ke#pad panel starting at top left
77 0irst row
for1int iD*9 iGD-9 iMM) ;
8plRuttons.add18nRuttons=i>)9
E
77 add utton 7 and s)rt
8plRuttons.add18nRuttons=1!>)9
8plRuttons.add18nRuttons=1*>)9
77 Second row
for1int iD"9 iGD'9 iMM)
;
8plRuttons.add18nRuttons=i>)9
E
77 add utton : and xZ2
8plRuttons.add18nRuttons=1">)9
8plRuttons.add18nRuttons=1,>)9
77 &hird row
for1 int iD19 iGD!9 iMM)
;
8plRuttons.add18nRuttons=i>)9
E
77adds utton + and Y
8plRuttons.add18nRuttons=1$>)9
8plRuttons.add18nRuttons=1->)9
770ourth Jow
77 add ./ M7+/ ./ M/ and D
8plRuttons.add18nRuttons=.>)9
8plRuttons.add18nRuttons=1.>)9
8plRuttons.add18nRuttons=11>)9
8plRuttons.add18nRuttons=1'>)9
8plRuttons.add18nRuttons=12>)9
8plIaster.setAa#out1new RorderAa#out1))9
8plIaster.add18plRackSpace/ RorderAa#out.T@S&)9
8plIaster.add18plControl/ RorderAa#out.@%S&)9
8plIaster.add18plRuttons/ RorderAa#out.S<3&U)9
77 %dd components to frame
getContent6ane1).add18plIaster/ RorderAa#out.S<3&U)9
re)uest0ocus1)9
77activate %ctionAistener
for 1int iD.9 iG8nRuttons.length9 iMM);
8nRuttons=i>.add%ctionAistener1this)9
E
8menuitem%out.add%ctionAistener1this)9
8menuitem@xit.add%ctionAistener1this)9
clear%ll1)9
77add TindowAistener for closing frame and ending program
addTindowAistener1new Tindow%dapter1) ;
pulic void windowClosed1Tindow@vent e)
;
S#stem.exit1.)9
E
E
)9
E 77@nd of Contructor Calculator
77 6erform action
pulic void action6erformed1%ction@vent e);
doule result D .9

if1e.getSource1) DD 8menuitem%out);
JDialog dlg%out D new Custom%R<3&Dialog1this/
K%out Java Swing
CalculatorK/ true)9
dlg%out.setLisile1true)9
Eelse if1e.getSource1) DD 8menuitem@xit);
S#stem.exit1.)9
E
77 Search for the utton pressed until end of arra# or ke# found
for 1int iD.9 iG8nRuttons.length9 iMM)
;
if1e.getSource1) DD 8nRuttons=i>)
;
switch1i)
;
case .5
addDigit&oDispla#1i)9
reak9
case 15
addDigit&oDispla#1i)9
reak9
case 25
addDigit&oDispla#1i)9
reak9
case !5
addDigit&oDispla#1i)9
reak9
case "5
addDigit&oDispla#1i)9
reak9
case $5
addDigit&oDispla#1i)9
reak9
case '5
addDigit&oDispla#1i)9
reak9
case *5
addDigit&oDispla#1i)9
reak9
case ,5
addDigit&oDispla#1i)9
reak9
case -5
addDigit&oDispla#1i)9
reak9
case 1.5 77 M7+
processSignChange1)9
reak9
case 115 77 decimal point
addDecimal6oint1)9
reak9
case 125 77 D
process@)uals1)9
reak9
case 1!5 77 divide
process<perator1K7K)9
reak9
case 1"5 77 :
process<perator1K:K)9
reak9
case 1$5 77 +
process<perator1K+K)9
reak9
case 1'5 77 M
process<perator1KMK)9
reak9
case 1*5 77 s)rt
if 1displa#Iode QD @JJ<JBI<D@)
;
tr#
;
if 1getDispla#String1).index<f1K+K)
DD .)
displa#@rror1K4nvalid input for functionQK)
result D Iath.s)rt1get(umer4nDispla#1))9
displa#Jesult1result)9
E
catch1@xception ex)
;
displa#@rror1K4nvalid input for functionQK)9
displa#Iode D @JJ<JBI<D@9
E
E
reak9
case 1,5 77 17x
if 1displa#Iode QD @JJ<JBI<D@);
tr#
;
if 1get(umer4nDispla#1) DD
.)
displa#@rror1KCannot divide #
?eroQK)9
result D 1 7 get(umer4nDispla#1)9
displa#Jesult1result)9
E
catch1@xception ex) ;
displa#@rror1KCannot divide #
?eroQK)9
displa#Iode D
@JJ<JBI<D@9
E
E
reak9
case 1-5 77 Y
if 1displa#Iode QD @JJ<JBI<D@);
tr# ;
result D get(umer4nDispla#1) 7 1..9
displa#Jesult1result)9
E
catch1@xception ex) ;
displa#@rror1K4nvalid input for functionQK)9
displa#Iode D
@JJ<JBI<D@9
E
E
reak9
case 2.5 77 ackspace
if 1displa#Iode QD @JJ<JBI<D@);
setDispla#String1getDispla#String1).sustring1./ getDispla#String1).length1) +
1))9
if 1getDispla#String1).length1) G 1)
setDispla#String1K.K)9
E
reak9
case 215 77 C@
clear@xisting1)9
reak9
case 225 77 C
clear%ll1)9
reak9
E
E
E
E
void setDispla#String1String s);
8l<utput.set&ext1s)9
E
String getDispla#String 1);
return 8l<utput.get&ext1)9
E
void addDigit&oDispla#1int digit);
if 1clear<n(extDigit)
setDispla#String1KK)9
String inputString D getDispla#String1)9
if 1inputString.index<f1K.K) DD .);
inputString D inputString.sustring11)9
E
if 11QinputString.e)uals1K.K) VV digit H .)
SS inputString.length1) G
I%WB4(63&BA@(2&U);
setDispla#String1inputString M digit)9
E
displa#Iode D 4(63&BI<D@9
clear<n(extDigit D false9
E
void addDecimal6oint1);
displa#Iode D 4(63&BI<D@9
if 1clear<n(extDigit)
setDispla#String1KK)9
String inputString D getDispla#String1)9
77 4f the input string alread# contains a decimal point/ donNt
77 do an#thing to it.
if 1inputString.index<f1K.K) G .)
setDispla#String1new String1inputString M K.K))9
E
void processSignChange1);
if 1displa#Iode DD 4(63&BI<D@)
;
String input D getDispla#String1)9
if 1input.length1) H . SS Qinput.e)uals1K.K))
;
if 1input.index<f1K+K) DD .)
setDispla#String1input.sustring11))9
else
setDispla#String1K+K M input)9
E
E
else if 1displa#Iode DD J@S3A&BI<D@)
;
doule numer4nDispla# D get(umer4nDispla#1)9
if 1numer4nDispla# QD .)
displa#Jesult1+numer4nDispla#)9
E
E
void clear%ll1);
setDispla#String1K.K)9
last<perator D K.K9
last(umer D .9
displa#Iode D 4(63&BI<D@9
clear<n(extDigit D true9
E
void clear@xisting1);
setDispla#String1K.K)9
clear<n(extDigit D true9
displa#Iode D 4(63&BI<D@9
E
doule get(umer4nDispla#1) ;
String input D 8l<utput.get&ext1)9
return Doule.parseDoule1input)9
E
void process<perator1String op) ;
if 1displa#Iode QD @JJ<JBI<D@)
;
doule numer4nDispla# D get(umer4nDispla#1)9
if 1Qlast<perator.e)uals1K.K))
;
tr#
;
doule result D processAast<perator1)9
displa#Jesult1result)9
last(umer D result9
E
catch 1DivideR#[ero@xception e)
;
E
E
else
;
last(umer D numer4nDispla#9
E
clear<n(extDigit D true9
last<perator D op9
E
E
void process@)uals1);
doule result D .9
if 1displa#Iode QD @JJ<JBI<D@);
tr#
;
result D processAast<perator1)9
displa#Jesult1result)9
E
catch 1DivideR#[ero@xception e) ;
displa#@rror1KCannot divide # ?eroQK)9
E
last<perator D K.K9
E
E
doule processAast<perator1) throws DivideR#[ero@xception ;
doule result D .9
doule numer4nDispla# D get(umer4nDispla#1)9
if 1last<perator.e)uals1K7K))
;
if 1numer4nDispla# DD .)
throw 1new DivideR#[ero@xception1))9
result D last(umer 7 numer4nDispla#9
E
if 1last<perator.e)uals1K:K))
result D last(umer : numer4nDispla#9
if 1last<perator.e)uals1K+K))
result D last(umer + numer4nDispla#9
if 1last<perator.e)uals1KMK))
result D last(umer M numer4nDispla#9
return result9
E
void displa#Jesult1doule result);
setDispla#String1Doule.toString1result))9
last(umer D result9
displa#Iode D J@S3A&BI<D@9
clear<n(extDigit D true9
E
void displa#@rror1String errorIessage);
setDispla#String1errorIessage)9
last(umer D .9
displa#Iode D @JJ<JBI<D@9
clear<n(extDigit D true9
E
pulic static void main1String args=>) ;
Calculator calci D new Calculator1)9
Container content6ane D calci.getContent6ane1)9
77 content6ane.setAa#out1new RorderAa#out1))9
calci.set&itle1KJava Swing CalculatorK)9
calci.setSi?e12"1/ 21*)9
calci.pack1)9
calci.setAocation1"../ 2$.)9
calci.setLisile1true)9
calci.setJesi?ale1false)9
E
E 77@nd of Swing Calculator Class.
class DivideR#[ero@xception extends @xception;
pulic DivideR#[ero@xception1)
;
super1)9
E
pulic DivideR#[ero@xception1String s)
;
super1s)9
E
E
class Custom%R<3&Dialog extends JDialog implements %ctionAistener ;
JRutton 8n<k9
Custom%R<3&Dialog1J0rame parent/ String title/ oolean modal);
super1parent/ title/ modal)9
setRackground1Color.lack)9
J6anel p1 D new J6anel1new 0lowAa#out10lowAa#out.C@(&@J))9
StringRuffer text D new StringRuffer1)9
text.append1KCalculator Demo 6rogramPnPnK)9
J&ext%rea 8t%rea%out D new J&ext%rea1$/ 21)9
8t%rea%out.set&ext1text.toString1))9
8t%rea%out.set0ont1new 0ont1K&imes (ew JomanK/ 1/ 1!))9
8t%rea%out.set@ditale1false)9
p1.add18t%rea%out)9
p1.setRackground1Color.red)9
getContent6ane1).add1p1/ RorderAa#out.C@(&@J)9
J6anel p2 D new J6anel1new 0lowAa#out10lowAa#out.C@(&@J))9
8n<k D new JRutton1K <X K)9
8n<k.add%ctionAistener1this)9
p2.add18n<k)9
getContent6ane1).add1p2/ RorderAa#out.S<3&U)9
setAocation1".,/ 2*.)9
setJesi?ale1false)9
addTindowAistener1new Tindow%dapter1) ;
pulic void windowClosing1Tindow@vent e)
;
Tindow aoutDialog D e.getTindow1)9
aoutDialog.dispose1)9
E
E
)9
pack1)9
E
pulic void action6erformed1%ction@vent e)
;
if1e.getSource1) DD 8n<k) ;
this.dispose1)9
E
E
E
Ex.No : = Develop a template for linked0list
!"#
&o develop a template for linked+list class along with its methods in Java.
!L$%&"'(#
Step15 Start.
Step25 Declare variales for linked list.
Step!5 Declare the class template
Step"5 Call insert and delete opearation on linked list
Step$5 Stop.
S%)&*E *%DE
pulic class AinkedAist ;
Aist(ode current9
7::
: Construct the list
:7
pulic AinkedAist1 ) ;
header D new Aist(ode1 null )9
E
7::
: &est if the list is logicall# empt#.
: Freturn true if empt#/ false otherwise.
:7
pulic oolean is@mpt#1 ) ;
return header.next DD null9
E
7::
: Iake the list logicall# empt#.
:7
pulic void make@mpt#1 ) ;
header.next D null9
E
7::
: Jeturn an iterator representing the header node.
:7
pulic AinkedAist4terator ?eroth1 ) ;
return new AinkedAist4terator1 header )9
E
7::
: Jeturn an iterator representing the first node in the list.
: &his operation is valid for empt# lists.
:7
pulic AinkedAist4terator first1 ) ;
return new AinkedAist4terator1 header.next )9
E
7::
: 4nsert after p.
: Fparam x the item to insert.
: Fparam p the position prior to the newl# inserted item.
:7
pulic void insert1 <8ect x/ AinkedAist4terator p ) ;
if1 p QD null SS p.current QD null )
p.current.next D new Aist(ode1 x/ p.current.next )9
E
7::
: Jeturn iterator corresponding to the first node containing an item.
: Fparam x the item to search for.
: Freturn an iterator9 iterator is not valid if item is not found.
:7
pulic AinkedAist4terator find1 <8ect x ) ;
Aist(ode itr D header.next9
while1 itr QD null SS Qitr.element.e)uals1 x ) )
itr D itr.next9
return new AinkedAist4terator1 itr )9
E
7::
: Jeturn iterator prior to the first node containing an item.
: Fparam x the item to search for.
: Freturn appropriate iterator if the item is found. <therwise/ the
: iterator corresponding to the last element in the list is returned.
:7
pulic AinkedAist4terator find6revious1 <8ect x ) ;
Aist(ode itr D header9
while1 itr.next QD null SS Qitr.next.element.e)uals1 x ) )
itr D itr.next9
return new AinkedAist4terator1 itr )9
E
7::
: Jemove the first occurrence of an item.
: Fparam x the item to remove.
:7
pulic void remove1 <8ect x ) ;
AinkedAist4terator p D find6revious1 x )9
if1 p.current.next QD null )
p.current.next D p.current.next.next9 77 R#pass deleted node
E
77 Simple print method
pulic static void printAist1 AinkedAist theAist ) ;
if1 theAist.is@mpt#1 ) )
S#stem.out.print1 K@mpt# listK )9
else ;
AinkedAist4terator itr D theAist.first1 )9
for1 9 itr.isLalid1 )9 itr.advance1 ) )
S#stem.out.print1 itr.retrieve1 ) M K K )9
E
S#stem.out.println1 )9
E
private Aist(ode header9
77 4n this routine/ AinkedAist and AinkedAist4terator are the
77 classes written in Section 1*.2.
pulic static int listSi?e1 AinkedAist theAist ) ;
AinkedAist4terator itr9
int si?e D .9
for1 itr D theAist.first1)9 itr.isLalid1)9 itr.advance1) )
si?eMM9
return si?e9
E
pulic static void main1 String = > args ) ;
AinkedAist theAist D new AinkedAist1 )9
AinkedAist4terator the4tr9
int i9
the4tr D theAist.?eroth1 )9
printAist1 theAist )9
for1 i D .9 i G 1.9 iMM ) ;
theAist.insert1 new 4nteger1 i )/ the4tr )9
printAist1 theAist )9
the4tr.advance1 )9
E
S#stem.out.println1 KSi?e was5 K M listSi?e1 theAist ) )9
for1 i D .9 i G 1.9 i MD 2 )
theAist.remove1 new 4nteger1 i ) )9
for1 i D .9 i G 1.9 iMM )
if1 1 i Y 2 DD . ) DD 1 theAist.find1 new 4nteger1 i ) ).isLalid1 ) ) )
S#stem.out.println1 K0ind failsQK )9
S#stem.out.println1 K0inished deletionsK )9
printAist1 theAist )9
E
E
pulic class AinkedAist4terator ;
Aist(ode current9
7::
: Construct the list iterator
: Fparam the(ode an# node in the linked list.
:7
AinkedAist4terator1 Aist(ode the(ode ) ;
current D the(ode9
E
7::
: &est if the current position is a valid position in the list.
: Freturn true if the current position is valid.
:7
pulic oolean isLalid1 ) ;
return current QD null9
E
7::
: Jeturn the item stored in the current position.
: Freturn the stored item or null if the current position
: is not in the list.
:7
pulic <8ect retrieve1 ) ;
return isLalid1 ) O current.element 5 null9
E
7::
: %dvance the current position to the next node in the list.
: 4f the current position is null/ then do nothing.
:7
pulic void advance1 ) ;
if1 isLalid1 ) )
current D current.next9
E
E
pulic class Aist(ode ;
pulic <8ect element9
pulic Aist(ode next9
77 Constructors
pulic Aist(ode1 <8ect the@lement ) ;
this1 the@lement/ null )9
E
pulic Aist(ode1 <8ect the@lement/ Aist(ode n ) ;
element D the@lement9
next D n9
E
E
%)'+)'
E:,Experiment =-.avac /..ava
E:,Experiment =-.ava LinkedList
EmptA list
9
9 1
9 1 2
9 1 2 3
9 1 2 3 4
9 1 2 3 4 C
9 1 2 3 4 C >
9 1 2 3 4 C > ?
9 1 2 3 4 C > ? 8
9 1 2 3 4 C > ? 8 =
SiGe was: 19
5inished deletions
1 3 C ? =
E:,Experiment =-
Ex.No : 19 Design a thread0safe implementation of Qee class.

!"#
&o design a thread+safe implementation of Queue class. Trite a multi+threaded
producer+consumer application that uses this Queue class.
!L$%&"'(#
Step15 Start.
Step25 Declare the class for producer.
Step!5 Declare the class for consumer
Step"5 Define a )ueue class
Step$5 6erform operation on producer+ consumer application
Step'5 Stop.
S%)&*E *%DE
pulic class Consumer extends &hread ;
private Cu#Uole cu#hole9
private int numer9
pulic Consumer1Cu#Uole c/ int numer) ;
cu#hole D c9
this.numer D numer9
E
pulic void run1) ;
int value D .9
for 1int i D .9 i G 1..9 iMM) ;
value D cu#hole.get1)9
S#stem.out.println1KConsumer \K M this.numer M K got5 K M value)9
E
E
E
pulic class Cu#Uole ;
private int contents9
private oolean availale D false9
pulic s#nchroni?ed int get1) ;
while 1availale DD false) ;
tr# ;
wait1)9
E catch 14nterrupted@xception e) ; E
E
availale D false9
notif#%ll1)9
return contents9
E
pulic s#nchroni?ed void put1int value) ;
while 1availale DD true) ;
tr# ;
wait1)9
E catch 14nterrupted@xception e) ; E
E
contents D value9
availale D true9
notif#%ll1)9
E
E
pulic class 6roducer extends &hread ;
private Cu#Uole cu#hole9
private int numer9
pulic 6roducer1Cu#Uole c/ int numer) ;
cu#hole D c9
this.numer D numer9
E
pulic void run1) ;
for 1int i D .9 i G 1.9 iMM) ;
cu#hole.put1i)9
S#stem.out.println1K6roducer \K M this.numer
M K put5 K M i)9
tr# ;
sleep11int)1Iath.random1) : 1..))9
E catch 14nterrupted@xception e) ; E
E
E
E
pulic class 6roducerConsumer&est ;
pulic static void main1String=> args) ;
Cu#Uole c D new Cu#Uole1)9
6roducer p1 D new 6roducer1c/ 1)9
Consumer c1 D new Consumer1c/ 1)9
p1.start1)9
c1.start1)9
E
E
%)'+)'
E:,Experiment 19-.ava +rodcer*onsmer'est
+rodcer H1 pt: 9
*onsmer H1 got: 9
+rodcer H1 pt: 1
*onsmer H1 got: 1
+rodcer H1 pt: 2
*onsmer H1 got: 2
*onsmer H1 got: 3
+rodcer H1 pt: 3
+rodcer H1 pt: 4
*onsmer H1 got: 4
+rodcer H1 pt: C
*onsmer H1 got: C
+rodcer H1 pt: >
*onsmer H1 got: >
+rodcer H1 pt: ?
*onsmer H1 got: ?
+rodcer H1 pt: 8
*onsmer H1 got: 8
+rodcer H1 pt: =
*onsmer H1 got: =
Ex.No : 11 Design a mlti0threaded Java program to print all nm6ers 6elow
1991999 that are 6oth prime and 5i6onacci nm6er
!"#
&o write a multi+threaded Java program to print all numers elow 1../... that
are oth prime and 0ionacci numer 1some examples are 2/ !/ $/ 1!/ etc.). Design a
thread that generates prime numers elow 1../... and writes them into a pipe. Design
another thread that generates fionacci numers and writes them to another pipe. &he
main thread should read oth the pipes to identif# numers common to oth.
!L$%&"'(#
Step15 Start.
Step25 Define thread for 0ionacci numer.
Step!5 Define thread for 6rime numer
Step"5 Define a main thread to execute aove threads
Step$5 Displa# the numers
Step'5 Stop.
S%)&*E *%DE
import 8ava.io.Data<utputStream9
import 8ava.io.4<@xception9
import 8ava.io.6iped<utputStream9
pulic class 0ionacci(umer2enerator extends &hread ;
private Data<utputStream dos9
pulic 0ionacci(umer2enerator1&hread2roup thread2roup
/6iped<utputStream pis/ String name);
super1thread2roup/ name)9
dos D new Data<utputStream1pis)9
E
F<verride
pulic void run1) ;
tr# ;
for1int kD.9 kG1....9 kMM);
long fionacci(um D fi1k)9
if1fionacci(um H .A SS fionacci(um G 1....A);
dos.writeAong1fionacci(um)9
dos.flush1)9
E
E
E catch 14<@xception e) ;
E
E
pulic int fi1int n) ;
int prev1D./ prev2D19
for1int iD.9 iGn9 iMM) ;
int save6rev1 D prev19
prev1 D prev29
prev2 D save6rev1 M prev29
E
return prev19
E
E
import 8ava.io.Data4nputStream9
import 8ava.io.4<@xception9
import 8ava.io.6iped4nputStream9
import 8ava.io.6iped<utputStream9
pulic class 6ipedStream&ester extends &hread ;
Data4nputStream fionic6is9
Data4nputStream prime6is9
pulic 6ipedStream&ester16iped4nputStream fionic6is/ 6iped4nputStream
prime6is);
this.fionic6is D new Data4nputStream1fionic6is)9
this.prime6is D new Data4nputStream1prime6is)9
E
pulic static void main1String=> args) ;
tr# ;
6iped<utputStream fionic6os D new 6iped<utputStream1)9
6iped4nputStream fionic6is D new 6iped4nputStream1fionic6os)9
6iped<utputStream prime6os D new 6iped<utputStream1)9
6iped4nputStream prime6is D new 6iped4nputStream1prime6os)9
&hread2roup tg D new &hread2roup1K6iped&hreadsK)9
0ionacci(umer2enerator f D new
0ionacci(umer2enerator1tg/ fionic6os/ K0ionacci(umer2eneratorK)9
6rime(umer2enerator p D new 6rime(umer2enerator1tg/
prime6os/ K6rime(umer2eneratorK)9
6ipedStream&ester main&ester D new
6ipedStream&ester1fionic6is/ prime6is)9
main&ester.start1)9
f.start1)9
p.start1)9
E catch 14<@xception e) ;
e.printStack&race1)9
E
E
7: 1non+Javadoc)
: Fsee 8ava.lang.&hread\run1)
:7
F<verride
pulic void run1) ;
oolean canJun D true9
oolean can2o6rimeAoop D true9
oolean can2o0ionicAoop D true9
Aong prime(umer D +1A9
Aong fionacci(umer D +1A9
while1canJun);
if1fionic6is QD null SS can2o0ionicAoop);
tr# ;
fionacci(umer D fionic6is.readAong1)9
S#stem.out.println1K 0ionic (umer
\HKMfionacci(umer)9
E catch 14<@xception e) ;
77S#stem.err.println1K@xception occurred while
reading from fionacci numer/ KMe)9
can2o0ionicAoop D false9
E
E
if1prime6is QD null SS can2o6rimeAoop);
tr# ;
prime(umer D prime6is.readAong1)9
S#stem.out.println1K 6rime (umer
\HKMprime(umer)9
E catch 14<@xception e) ;
77S#stem.err.println1K@xception occurred while
reading from prime numer/ KMe)9
can2o6rimeAoop D false9
E
E
E
E
E
import 8ava.io.Data<utputStream9
import 8ava.io.4<@xception9
import 8ava.io.6iped<utputStream9
pulic class 6rime(umer2enerator extends &hread ;
private Data<utputStream dos9
pulic 6rime(umer2enerator 1&hread2roup thread2roup/ 6iped<utputStream
pos/ String name);
super1thread2roup/ name)9
dos D new Data<utputStream1pos)9
E
F<verride
pulic void run1) ;
tr# ;
int x/ #/ c D .9
for1 x D 29 x G 1....9 xMM );
if1 x Y 2 QD . VV x DD 2 );
for1 # D 29 # GD x 7 29 #MM );
if1 x Y # DD . );
reak9
E
E
if1 # H x 7 2 );
if1x G 1....);
dos.writeAong1x)9
dos.flush1)9
E
E
E
E
E catch 14<@xception e) ;
E
E
E
%)'+)'
E:,Experiment 11-.avac /..ava
E:,Experiment 11-.ava +ipedStream'ester
5i6onic Nm6er H-1
+rime Nm6er H-2
5i6onic Nm6er H-1
+rime Nm6er H-3
5i6onic Nm6er H-2
+rime Nm6er H-C
5i6onic Nm6er H-3
+rime Nm6er H-?
5i6onic Nm6er H-C
+rime Nm6er H-11
5i6onic Nm6er H-8
+rime Nm6er H-13
5i6onic Nm6er H-13
+rime Nm6er H-1?
5i6onic Nm6er H-21
+rime Nm6er H-1=
5i6onic Nm6er H-34
+rime Nm6er H-23
5i6onic Nm6er H-CC
+rime Nm6er H-2=
5i6onic Nm6er H-8=
+rime Nm6er H-31
5i6onic Nm6er H-144
+rime Nm6er H-3?
5i6onic Nm6er H-233
+rime Nm6er H-41
5i6onic Nm6er H-3??
+rime Nm6er H-43
5i6onic Nm6er H->19
+rime Nm6er H-4?
5i6onic Nm6er H-=8?
+rime Nm6er H-C3
5i6onic Nm6er H-1C=?
+rime Nm6er H-C=
5i6onic Nm6er H-2C84
+rime Nm6er H->1
5i6onic Nm6er H-4181
+rime Nm6er H->?
5i6onic Nm6er H->?>C
+rime Nm6er H-?1
+rime Nm6er H-?3
+rime Nm6er H-?=
+rime Nm6er H-83
+rime Nm6er H-8=
+rime Nm6er H-=?
+rime Nm6er H-191
+rime Nm6er H-193
+rime Nm6er H-19?
+rime Nm6er H-19=
+rime Nm6er H-113
+rime Nm6er H-12?
+rime Nm6er H-131
+rime Nm6er H-13?
+rime Nm6er H-13=
+rime Nm6er H-14=
+rime Nm6er H-1C1
+rime Nm6er H-1C?
+rime Nm6er H-1>3
+rime Nm6er H-1>?
+rime Nm6er H-1?3
+rime Nm6er H-1?=
+rime Nm6er H-181
+rime Nm6er H-1=1
+rime Nm6er H-1=3
+rime Nm6er H-1=?
+rime Nm6er H-1==
+rime Nm6er H-211
+rime Nm6er H-223
+rime Nm6er H-22?
+rime Nm6er H-22=
+rime Nm6er H-233
+rime Nm6er H-23=
+rime Nm6er H-241
+rime Nm6er H-2C1
+rime Nm6er H-2C?
+rime Nm6er H-2>3
+rime Nm6er H-2>=
+rime Nm6er H-2?1
+rime Nm6er H-2??
+rime Nm6er H-281
+rime Nm6er H-283
+rime Nm6er H-2=3
+rime Nm6er H-39?
+rime Nm6er H-311
+rime Nm6er H-313
+rime Nm6er H-31?
+rime Nm6er H-331
+rime Nm6er H-33?
Ex.No : 12 Develop a mlti0threaded $)" application of Aor choice.
!"#
&o develop a multi+threaded 234 application of #our choice.
!L$%&"'(#
Step15 Start.
Step25 Define a thread for adding two numers using 234.
Step!5 Define a thread for multipl# two numers using 234.
Step"5 @xecute oth the thread simultaneousl#
Step$5 Displa# the numers
Step'5 Stop.
S%)&*E *%DE
import org.eclipse.swt.ST&9
import org.eclipse.swt.events.Selection@vent9
import org.eclipse.swt.events.SelectionAistener9
import org.eclipse.swt.graphics.Jectangle9
import org.eclipse.swt.widgets.Rutton9
import org.eclipse.swt.widgets.Displa#9
import org.eclipse.swt.widgets.Shell9
7::
: 4llustrates multithread 34 programming issues.
:7
p6lic class 64Calculator ;
Displa# displa# D new Displa#1)9
Shell shell D new Shell1displa#)9
Rutton utton&hread D new Rutton1shell/ ST&.63SU)9
Rutton utton%s#nc@xec D new Rutton1shell/ ST&.63SU)9
p6lic 64Calculator16oolean as#nc@xec@naled) ;
final 6oolean as#nc D as#nc@xec@naled9
shell.set&ext1K64 CalculatorK)9
shell.setSi?e1"../ ,.)9
Jectangle client%rea D shell.getClient%rea1)9
utton&hread.set&ext1
KClick here to calculate 64 =(on+34 thread 34 3pdate>K)9
utton&hread.setRounds1
client%rea.x/
client%rea.#/
client%rea.width/
client%rea.height 7 2)9
utton&hread.addSelectionAistener1new SelectionAistener1) ;
p6lic void widgetDefaultSelected1Selection@vent e) ;
E
p6lic void widgetSelected1Selection@vent e) ;
utton&hread.set&ext1KCalculation in progress ...K)9
get&ask1utton&hread).start1)9
E
E)9

utton%s#nc@xec.set&ext1KClick here to calculate 64 =as#n@xec method 34 3pdate>K)9
utton%s#nc@xec.setRounds1
client%rea.x/
client%rea.# M client%rea.height 7 2/
client%rea.width/
client%rea.height 7 2)9
utton%s#nc@xec.addSelectionAistener1new SelectionAistener1) ;
p6lic void widgetDefaultSelected1Selection@vent e) ;
E
p6lic void widgetSelected1Selection@vent e) ;
utton%s#nc@xec.set&ext1KCalculation in progress ...K)9
get&ask21utton%s#nc@xec).start1)9
E
E)9
shell.open1)9
while 1Qshell.isDisposed1)) ;
if 1Qdispla#.read%ndDispatch1)) ;
displa#.sleep1)9
E
E
displa#.dispose1)9
E
p6lic static void main1String=> args) ;
77 new Calculate641false)9
new 64Calculator1tre)9
E
p6lic &hread get&ask1Rutton utton) ;
final Rutton theRutton D utton9
retrn new &hread1) ;
p6lic void run1) ;
do6le pi D calculate641-------)9
theRutton.set&ext1K64 D K M pi)9 77 3pdate 34.
E
E9
E
p6lic &hread get&ask21Rutton utton) ;
final Rutton theRutton D utton9
retrn new &hread1) ;
p6lic void run1) ;
final do6le pi D calculate641-------)9

displa#.as#nc@xec1new Junnale1) ;
p6lic void run1) ;

77 3pdate 34.

theRutton.set&ext1K64 D K M pi)9

E
E)9
E
E9
E
7::
: Calculate value of 64 using LietaNs formula.
: Fparam nestedAevel +
: level of nested s)uare roots in LietaNs formula.
: Freturn value of 64
:7
p6lic static do6le calculate641int nestedAevel) ;
do6le product D 19
do6le lastS)rtLalue D .9
for 1int i D .9 i G nestedAevel9 iMM) ;
do6le s)rt D get(extS)rtLalue1lastS)rtLalue)9
product :D ..$ : s)rt9
lastS)rtLalue D s)rt9
E
retrn 2 7 product9
E
7::
: Jeturn the s)uare root item value.
:
: Fparam lastLalue +
: last s)uare root item value.
: Freturn
:7
p6lic static do6le get(extS)rtLalue1do6le lastLalue) ;
retrn Iath.s)rt12 M lastLalue)9
E
E

You might also like