Java Lab Manual CSE

download Java Lab Manual CSE

of 45

Transcript of Java Lab Manual CSE

  • 8/13/2019 Java Lab Manual CSE

    1/45

    Ex No1: Rational Numbers

    AIM To write a Java Program to develop a class for Rationalnumbers.

    ALGORITHM:

    Step 1:-Declare a class called Rational and invoke a functioncalled gcda!b".

    Step #:-$ind out t%e reminder w%en a is divided b& b and pass itas a parameter to t%e function.

    Step ':-(reate an ob)ect for t%e class and declare t%e re*uiredstring and integer variables.

    Step +:-(reate an ob)ect of class Data,nputStream .Read t%enumbers t%roug% t%e Readine" met%od into t%e ob)ect.

    Step :-(onvert t%e input accepted into ,ntegers t%roug% t%eparse,nt met%od and store t%em in variables a and b.

    Step /:-store t%e value returned b& t%e function 0(D in variablel.

    Step :-Divide a b& l and b b& l and store t%em in variables 2and &.

  • 8/13/2019 Java Lab Manual CSE

    2/45

    Program:-

    import )ava.io.34class rational1

    5 public rational1"56

    public long gcdlong a!long b"5

    ifb778"return a4

    elsereturn gcdb!a9b"4

    66public class rational5

    public static void mainString args;"t%rows ,=nter t%e value for ?>"4str7in.readine"4a7,nteger.parse,ntstr"4S&stem.out.println>=nter t%e value for @>"4str7in.readine"4b7,nteger.parse,ntstr"4

    long l7r.gcda!b"4

    S&stem.out.println"4S&stem.out.println>T%e 0(D of t%e number is:>Al"427aBl4&7bBl4S&stem.out.println"4S&stem.out.println>T%e resultant value is: >A2A>B>A&"4

    66

    Output

    =nter t%e value for ?88=nter t%e value for @1888

    T%e 0(D of t%e number is:88

  • 8/13/2019 Java Lab Manual CSE

    3/45

    T%e resultant value is: 1B#Ex No : !ate "lass in #a$a

    AIM

    To design a Date class in Java .

    ALGORITHM:-

    %tep 1:Declare a class called Datee2ample and create an ob)ectcalled date.

    %tep :-Displa& t%e Date and Time t%roug% t%ese ob)ects wit% t%eCet%ods in Date (lass.

    %tep &:-Declare two ob)ects called starttime and endtime for t%isclass .

    %tep ':-(reate anot%er ob)ect called c%anged ob)ect and displa&t%e c%anged time wit% t%e calculation #+3/83/831888.

    %tep (:-,n t%e main function create ob)ect for t%e class Date anddispla& t%e time and date accordingl&.

  • 8/13/2019 Java Lab Manual CSE

    4/45

    %O)R"E "O!E

    import )ava.util.Date4

    import )ava.te2t.Parse=2ception4import )ava.te2t.SimpleDate$ormat4public*lass Date=2ample 5

    pri$ate stati* $oi+ Date=2ample" 5

    Date date 7 ne, Date"4 S&stem.out.println>(urrent Date and Time is : > A date"4 S&stem.out.println"4

    S&stem.out.println>Date ob)ect s%owing specific date and time>"4 Date particulardate1 7 ne, Date#+3/83/831888"4

    Date particulardate# 7 ne, Date8"4 S&stem.out.println"4 S&stem.out.println>$irst Particular date : > A particulardate1"4 S&stem.out.println>Second Particular date: > A particulardate#"4 S&stem.out.println"4

    S&stem.out.println>Demo of getTime" met%od returning milliseconds>"4 S&stem.out.println"4 Date strtime 7 ne, Date"4 S&stem.out.println>Start Time: > A strtime"4

    Date endtime 7 ne, Date"4 S&stem.out.println>=nd Time is: > A endtime"4 long elapsedtime 7 endtime.getTime" - strtime.getTime"4 S&stem.out.println>=lapsed Time is:> A elapsedtime A > milliseconds>"4 S&stem.out.println"4

    S&stem.out.println>(%anged date ob)ect using setTime" met%od>"4 S&stem.out.println"4 Date c%ngdate 7 ne, Date"4 S&stem.out.println>Date before c%ange is: > A c%ngdate"4 c%ngdate.setTime#+3/83/831888"4 S&stem.out.println>Eow t%e (%anged date is: > A c%ngdate"4 S&stem.out.println"4 6

    publi* stati* $oi+ mainString; args" 5 S&stem.out.println"4 Date=2ample"4 6

  • 8/13/2019 Java Lab Manual CSE

    5/45

    6

    O)TP)T

    (urrent Date and Time is : Con Dec 18 1F:'G:# 0CTA8:'8#88

    Date ob)ect s%owing specific date and time

    $irst Particular date : $ri Jan 8# 8:'8:88 0CTA8:'8 1G8Second Particular date: T%u Jan 81 8:'8:88 0CTA8:'8 1G8

    Demo of getTime" met%od returning milliseconds

    Start Time: Con Dec 18 1F:'G:#F 0CTA8:'8 #88=nd Time is: Con Dec 18 1F:'G:#F 0CTA8:'8 #88=lapsed Time is:8 milliseconds

    (%anged date ob)ect using setTime" met%od

    Date before c%ange is: Con Dec 18 1F:'G:#F 0CTA8:'8 #88Eow t%e (%anged date is: $ri Jan 8# 8:'8:88 0CTA8:'8 1G8

  • 8/13/2019 Java Lab Manual CSE

    6/45

    Ex' : Implementation o %ta*. A!T

    AIM

    To write a Java Program to design an interface for Stack?DT.and implement Stack ?DT using bot% ?rra& and inked ist.

    ALGORITHM

    Step 1:Declare an arra& for storing t%e stack elements andinitialise t%e capacit& of t%e arra&.

    Step #:-Declare functions makeempt&"!isempt&" to c%eck w%et%er

    t%e stack is empt& to insert an element.

    Step ':?fter inserting an element!increment t%e counter b& 1 toincrease t%e number of elements in stack.

    Step +:,nitiate anot%er arra& to e2tend t%e siHe of t%e arra&w%en t%e number of elements e2ceed be&ond t%e limit.

    Step :-,nvoke met%ods and constructors to implement t%e stackusing linked list.

    Step /:-Test if t%e stack is logicall& empt&.t%en return true orelse false.

    Step :To delete an element from stack!c%eck if stack is empt&!ifso t%row an e2ception or else move to t%e ne2t element in t%estack.

    Step F:To return t%e most recentl& inserted element use t%emet%od top

  • 8/13/2019 Java Lab Manual CSE

    7/45

    %O)R"E "O!E

    B33

    3 ?rra&-based implementation of t%e stack.3Bpublic class ?rra&Stack implements Stack 5

    private ?rra&Stack top> "4 return t%e?rra& top

  • 8/13/2019 Java Lab Manual CSE

    8/45

    if is=mpt& " " t%row new Inderflow=2ception >?rra&Stack pop> "4 top

  • 8/13/2019 Java Lab Manual CSE

    9/45

    BBistStack classBBBB (

  • 8/13/2019 Java Lab Manual CSE

    10/45

    3Bpublic void pop " 5

    if is=mpt& " "t%row new Inderflow=2ception >istStack pop> "4

    top

  • 8/13/2019 Java Lab Manual CSE

    11/45

    ne2t 7 n46

    6

    public interface Stack 5

    B333 ,nsert a new item into t%e stack.3 Kparam 2 t%e item to insert.3Bvoid pus%

  • 8/13/2019 Java Lab Manual CSE

    12/45

    public static void mainString; args" 5

    S&stem.out.println>333333333333333333333333333333333333333333>"4S&stem.out.println>Stack using ?rra& N inked ist

    e2ample>"4

    S&stem.out.println>333333333333333333333333333333333333333333>"4

    ?rra&Stack arra&Stack 7 new ?rra&Stack"4arra&Stack.pus%new String>a>""4arra&Stack.pus%new String>b>""4arra&Stack.pus%new String>c>""4S&stem.out.println>Stackusing arra&; elements -M a! b!

    c>"4S&stem.out.println>Stack ,$< and P

    Aarra&Stack.top?ndPop""4

    S&stem.out.println>Stack ,$< -M >Aarra&Stack.top""4arra&Stack.pop"4tr&5

    arra&Stack.pop"4arra&Stack.top?ndPop"4

    6catc%Runtime=2ception rte"5S&stem.err.println>=2ception occured w%ile P"46S&stem.out.println>OnOn333333333333333333333333333333>"4S&stem.out.println>Stack using inked ist e2ample>"4S&stem.out.println>333333333333333333333333333333>"4

    inkedistStack linkedistStack 7 new inkedistStack"4linkedistStack.pus%new ,nteger18""4linkedistStack.pus%new ,nteger#8""4linkedistStack.pus%new ,nteger'8""4linkedistStack.pus%new ,nteger+8""4S&stem.out.println>Stackusing linked list; elements -M

    18! #8! '8! +8>"4S&stem.out.println>Stack T

    AlinkedistStack.top""4linkedistStack.pop"4S&stem.out.println>Stack T

  • 8/13/2019 Java Lab Manual CSE

    13/45

    66

    B333 =2ception class for access in empt& containers

    3 suc% as stacks! *ueues! and priorit& *ueues.3 Kaut%or Ramkumar3Bpublic class Inderflow=2ception e2tends Runtime=2ception 5

    B333 (onstruct t%is e2ception ob)ect.3 Kparam message t%e error message.3Bpublic Inderflow=2ception String message " 5

    super message "46

    6

    O)TP)T

    %ta*. using Arra/ 0 Lin.e+ List example

    %ta*.2using arra/3 elements -4 a5 b5 *

    %ta*. LI6O an+ POP -4 *%ta*. LI6O -4 b

    Ex*eption o**ure+ ,7ile POP operation is 7appene+ on %ta*.2b/using arra/3

    %ta*. using Lin.e+ List example

    %ta*.2using lin.e+ list3 elements -4 185 85 &85 '8

    %ta*. TOP -4'8%ta*. TOP ater POP -4&8

    Ex*eption o**ure+ ,7ile POP operation is 7appene+ on %ta*.2b/using lin.e+ list3

  • 8/13/2019 Java Lab Manual CSE

    14/45

    Ex no:( Pol/morp7ism

    Aim:- To develop a ve%icle class %ierarc%& in Java todemonstrate t%e concept of pol&morp%ism.

    Algorit7m:-

    Step 1:-Declare a super class called ve%icle wit% dataelements doors!w%eels and seats.

    Step #:-Derive anot%er class called car and invoke afunction tostring" to displa& t%e variables.

    Step ':-Derive anot%er class called motorc&cle wit% samedata and met%od called setseats" .

    Step +:-Declare anot%er sub class called Truck wit% #constructors and finall& assign values to variables.

    Step :-,n t%e main function! create an ob)ect for classmotorc&cle and displa& all details of sub classes t%roug%ob)ect.

    %our*e*o+e:-

    BBT%is is t%e class t%at will be in%eritedpublic class e%icle5 public int doors4 public int seats4 public int w%eels4 e%icle"

    5 w%eels7+4 doors7+4

    seats7+4 66BBT%is class in%erits e%icle.)avapublic class (ar e2tends e%icle5 public String toString"

  • 8/13/2019 Java Lab Manual CSE

    15/45

    5return >T%is car %as >AseatsA> Seats! >AdoorsA> Doors >A >and >Aw%eelsA> w%eels.>4

    66BBT%is class in%erits e%icle.)avapublic class Cotor(&cle e2tends e%icle5 Cotor(&cle" 5

    w%eels7#4 doors784

    seats714 6 void setSeatsint num" 5

    seats7num4 6 public String toString" 5

    return >T%is motorc&cle %as >AseatsA> Seats! >AdoorsA> Doors >A >and >Aw%eelsA> w%eels.>4

    66BBT%is class in%erits e%icle.)avapublic class Truck e2tends e%icle5 boolean isPickup4 Truck" 5

    isPickup7true4 6 Truckboolean aPickup" 5

    t%is"4isPickup7aPickup4

    6 Truckint doors! int seats! int inQ%eels! boolean isPickup" 5

    t%is.doors7doors4t%is.seats7seats4w%eels7inQ%eels4t%is.isPickup7isPickup4

    6 public String toString" 5

    return >T%is >AisPickup>pickup>:>truck>"A

    > %as >AseatsA> Seats! >AdoorsA> Doors >A>and >Aw%eelsA>w%eels.>4 66BBT%is class tests t%e classes t%at in%erit e%icle.)avapublic class e%iclesTest5 public static void mainString args;" 5

    Cotor(&cle mine 7 new Cotor(&cle"4

  • 8/13/2019 Java Lab Manual CSE

    16/45

    S&stem.out.printlnmine"4(ar mine# 7 new (ar"4S&stem.out.printlnmine#"4mine#.doors7#4S&stem.out.printlnmine#"4Truck mine' 7 new Truck"4S&stem.out.printlnmine'"4Truck mine+ 7 new Truckfalse"4mine+.doors7#4S&stem.out.printlnmine+"4

    66

    Output

    T%is motorc&cle %as 1 Seats! 8 Doors and # w%eelsT%is car %as + Seats! + Doors and + w%eelsT%is car %as + Seats! # Doors and + w%eels

    T%is pickup %as + Seats! + Doors and + w%eelsT%is truck %as + Seats! # Doors and + w%eels

    Ex No:-6 Object Serialization

    Aim:-To write a Java Program to randomly generate objects and write them into a fileusing concept of Object Serialization.

    Algorithm-

    Step !"eclare a class called #urrency .Open a file in output mode with a name.

    Step $-%rite new data into the object using writeobject&' method.

    Step (-Similarly create an input stream object .)ead it both in terms of "ollars and)upees.close the output object.

    Step *-derive a class called "ollar which implements serializable interface.+nvo,e aconstructor and function to display the data.

    Step Similarly declare a class called )upee with private variables and use print functionto display the variables.

  • 8/13/2019 Java Lab Manual CSE

    17/45

    Step terminate the e/ecution.The output is displayed as dollar to rupee conversion andvice versa.

    Sourcecode-

    00 #urrency conversionimport java.io.12public class #urrency3 public static void main&String args45' 3 "ollar dr6new "ollar&7879*:'2 dr.print"ollar&'2 )upee re6new )upee&;)s.;9:'2

    re.print)upee&'2 try 3

  • 8/13/2019 Java Lab Manual CSE

    18/45

    public "ollar&char sm9float doll' 3 sym6sm2 dol6doll2 =

    void print"ollar&' 3 System.out.print&sym'2 System.out.println&dol'2 ==class )upee implements Serializable3 private String sym2 private float rs2 public )upee&String sm9float rup'

    3 sym6sm2 rs6rup2 = void print)upee&' 3 System.out.print&sym'2 System.out.println&rs'2 =

    =

    Output:-

    >?java@java #urrency8*:.:)s.:.:8*:.:)s.:.:

  • 8/13/2019 Java Lab Manual CSE

    19/45

    Ex No:9 E$ent-!ri$en Programming

    AIM

    To develop a scientific calculator using even-drivenprogramming paradigm of Java.

    ALGORITHM:

    %tep 1:-,mport all t%e packages re*uired for designing t%egrap%ical elements in t%e applet window.

    %tep :-,mplement t%e istener files for e&board! mouse eventsin t%e class defined.

    %tep &:-,mport t%e swing package in t%e program for using swingcomponents for t%r grida&out design for menu!$rame!Dialog!Te2t?rea!abel and ot%er components for alignment in t%e appletwindow.

    %tep ':-(reate ob)ects from t%e main classes forJabel!Jte2tbo2!JPanel !$ont and Cenu items.

    %tep (:?ssign t%e font items to all t%e menu items and add t%em to t%epanel.

    %tep :-(reate t%e 0rid@aga&out and add all t%e buttons to it usingt%e swing class.

    %tep 9:-?dd all t%e J@uttons to t%e panel and also t%e colourcomponents to t%e 0I, controls.

    %tep ;:-(all adddig%itstodispla&" to activate t%e re*uired arit%meticoperations w%en t%e buttons are pressed.

    %tep

  • 8/13/2019 Java Lab Manual CSE

    20/45

    Step 1#:-,nvoke t%e actionPerformed" met%od t%roug% t%e ?ction=ventclass and write code for eac% operator or number being pressed.

    %O)R"E "O!E:=-

    import )ava.awt.@ordera&out4import )ava.awt.(olor4import )ava.awt.(ontainer4import )ava.awt.$lowa&out4import )ava.awt.$ont4import )ava.awt.0rida&out4import )ava.awt.Qindow4import )ava.awt.event.?ction=vent4import )ava.awt.event.?ctionistener4

    import )ava.awt.event.e&=vent4import )ava.awt.event.Qindow?dapter4import )ava.awt.event.Qindow=vent4

    import )ava2.swing.J@utton4import )ava2.swing.JDialog4import )ava2.swing.J$rame4import )ava2.swing.Jabel4import )ava2.swing.JCenu4import )ava2.swing.JCenu@ar4import )ava2.swing.JCenu,tem4import )ava2.swing.JPanel4import )ava2.swing.JTe2t?rea4import )ava2.swing.e&Stroke4

    public class (alculator e2tends J$rame implements ?ctionistener 5

    BB ariables

    final int C?U,EPIT=E0T 7 #84final int ,EPITC

  • 8/13/2019 Java Lab Manual CSE

    21/45

    private JPanel )plCaster! )pl@ackSpace! )pl(ontrol4

    B33 $ontString name! int st&le! int siHe"

    (reates a new $ont from t%e specified name! st&le and pointsiHe.

    3B

    $ont f1# 7 new $ont>Times Eew Roman>! 8! 1#"4$ont f1#1 7 new $ont>Times Eew Roman>! 1! 1#"4

    BB (onstructorpublic (alculator"5

    B3 Set Ip t%e [email protected] ave Provided ?ll JCenuVs wit% Cnemonics3 ave Provided some JCenu,tem components wit% e&board

    ?ccelerators3B

    )menu$ile 7 new JCenu>$ile>"4)menu$ile.set$ontf1#1"4)menu$ile.setCnemonice&=vent.$"4

    )menuitem=2it 7 new JCenu,tem>=2it>"4)menuitem=2it.set$ontf1#"4

    )menuitem=2it.set?cceleratore&Stroke.gete&Stroke e&=vent.U!

    ?ction=vent.(TRC?S""4)menu$ile.add)menuitem=2it"4

    )menuelp 7 new JCenu>elp>"4)menuelp.set$ontf1#1"4)menuelp.setCnemonice&=vent."4

    )menuitem?bout 7 new JCenu,tem>?bout (alculator>"4)menuitem?bout.set$ontf1#"4)menuelp.add)menuitem?bout"4

    JCenu@ar mb 7 new JCenu@ar"4mb.add)menu$ile"4mb.add)menuelp"4setJCenu@armb"4

    BBSet frame la&out manager

    set@ackground(olor.gra&"4

    )plCaster 7 new JPanel"4

    )lb8>"4)lb

  • 8/13/2019 Java Lab Manual CSE

    22/45

    )lbB>"4)bn@uttons1+; 7 new J@utton>3>"4)bn@uttons1; 7 new J@utton>->"4)bn@uttons1/; 7 new J@utton>A>"4)bn@uttons1; 7 new J@utton>s*rt>"4)bn@uttons1F; 7 new J@utton>1B2>"4)bn@uttons1G; 7 new J@utton>9>"4

    )pl@ackSpace 7 new JPanel"4

    )[email protected]&outnew 0rida&out1! 1! #! #""4

    )bn@uttons#8; 7 new J@utton>@ackspace>"4)[email protected])bn@uttons#8;"4

    )pl(ontrol 7 new JPanel"4)pl(ontrol.seta&outnew 0rida&out1! #! # !#""4

    )bn@uttons#1; 7 new J@utton> (= >"4)bn@uttons##; 7 new J@utton>(>"4

    )pl(ontrol.add)bn@uttons#1;"4)pl(ontrol.add)bn@uttons##;"4

    BB Setting all Eumbered J@uttonVs to @lue. T%e rest to Redfor int i784 iL)[email protected]%4 iAA" 5

    )bn@uttonsi;.set$ontf1#"4

    if iL18")bn@uttonsi;.set$oreground(olor.blue"4

    else

  • 8/13/2019 Java Lab Manual CSE

    23/45

    )bn@uttonsi;.set$oreground(olor.red"46

    BB Set panel la&out manager for a + b& grid)[email protected]&outnew 0rida&out+! ! #! #""4

    BB?dd buttons to ke&pad panel starting at top leftBB $irst rowforint i74 iL7G4 iAA" 5

    )[email protected])bn@uttonsi;"46

    BB add button B and s*rt)[email protected])bn@uttons1';"4)[email protected])bn@uttons1;"4

    BB Second rowforint i7+4 iL7/4 iAA"5

    )[email protected])bn@uttonsi;"46

    BB add button 3 and 2W#)[email protected])bn@uttons1+;"4)[email protected])bn@uttons1F;"4

    BB T%ird rowfor int i714 iL7'4 iAA"5

    )[email protected])bn@uttonsi;"46

    BBadds button - and 9)[email protected])bn@uttons1;"4)[email protected])bn@uttons1G;"4

    BB$ourt% RowBB add 8! AB-! .! A! and 7)[email protected])bn@uttons8;"4)[email protected])bn@uttons18;"4)[email protected])bn@uttons11;"4)[email protected])bn@uttons1/;"4)[email protected])bn@uttons1#;"4

    )plCaster.seta&outnew @ordera&out""4

    )plCaster.add)pl@ackSpace! @ordera&out.Q=ST"4)plCaster.add)pl(ontrol! @ordera&out.=?ST"4)plCaster.add)pl@uttons! @ordera&out.S

  • 8/13/2019 Java Lab Manual CSE

    24/45

    for int i784 iL)[email protected]%4 iAA"5)bn@uttonsi;.add?ctionistenert%is"4

    6

    )menuitem?bout.add?ctionistenert%is"4)menuitem=2it.add?ctionistenert%is"4

    clear?ll"4

    BBadd Qindowistener for closing frame and ending programaddQindowistenernew Qindow?dapter" 5

    public void window(losedQindow=vent e"5

    S&stem.e2it8"46

    6"4

    6 BB=nd of (ontructor (alculator

    BB Perform actionpublic void actionPerformed?ction=vent e"5

    double result 7 84

    ife.getSource" 77 )menuitem?bout"5 JDialog dlg?bout 7 new (ustom?@?bout Java Swing(alculator>! true"4

    dlg?bout.setisibletrue"46else ife.getSource" 77 )menuitem=2it"5

    S&stem.e2it8"46

    BB Searc% for t%e button pressed until end of arra& orke& found

    for int i784 iL)[email protected]%4 iAA"5

    ife.getSource" 77 )bn@uttonsi;"5

    switc%i"5

    case 8:addDigitToDispla&i"4break4

    case 1:addDigitToDispla&i"4break4

    case #:addDigitToDispla&i"4break4

    case ':

  • 8/13/2019 Java Lab Manual CSE

    25/45

    addDigitToDispla&i"4break4

    case +:addDigitToDispla&i"4break4

    case :addDigitToDispla&i"4break4

    case /:addDigitToDispla&i"4break4

    case :addDigitToDispla&i"4break4

    case F:addDigitToDispla&i"4break4

    case G:addDigitToDispla&i"4break4

    case 18: BB AB-processSign(%ange"4break4

    case 11: BB decimal point

    addDecimalPoint"4break4

    case 1#: BB 7process=*uals"4break4

    case 1': BB divideprocessB>"4break4

    case 1+: BB 3process3>"4

    break4

    case 1: BB -process->"4break4

    case 1/: BB AprocessA>"4break4

  • 8/13/2019 Java Lab Manual CSE

    26/45

    case 1: BB s*rtif displa&Code X7 =RR" 77 8"

    displa&=rror>,nvalid input for functionX>"4

    result 7 Cat%.s*rtgetEumber,nDispla&""4displa&Resultresult"4

    6

    catc%=2ception e2"5

    displa&=rror>,nvalid input for functionXY"4displa&Code 7 =RR

  • 8/13/2019 Java Lab Manual CSE

    27/45

    catc%=2ception e2" 5displa&=rror>,nvalid

    input for functionX>"4displa&Code 7

    =RR>"4

    String inputString 7 getDispla&String"4

    if inputString.inde28>" 77 8"5inputString 7 inputString.substring1"4

    6

    if XinputString.e*uals>8>" ZZ digit M 8"

  • 8/13/2019 Java Lab Manual CSE

    28/45

    NN inputString.lengt%" LC?U,EPIT=E0T"5

    setDispla&StringinputString A digit"46

    displa&Code 7 ,EPITC>"4

    String inputString 7 getDispla&String"4

    BB ,f t%e input string alread& contains a decimal point!

    donVtBB do an&t%ing to it.if inputString.inde2.>" L 8"

    setDispla&Stringnew StringinputString A >.>""46

    void processSign(%ange"5if displa&Code 77 ,EPITC4lastEumber 7 84

  • 8/13/2019 Java Lab Manual CSE

    29/45

    displa&Code 7 ,EPITC""5

    tr&5

    double result 7 processast

  • 8/13/2019 Java Lab Manual CSE

    30/45

    catc% Divide@&[ero=2ception e" 5displa&=rror>(annot divide b& HeroX>"4

    6

    last8>46

    6

    double processast""5

    if number,nDispla& 77 8"t%row new Divide@&[ero=2ception""4

    result 7 lastEumber B number,nDispla&46

    if last3>""result 7 lastEumber 3 number,nDispla&4

    if last->""result 7 lastEumber - number,nDispla&4

    if lastA>""result 7 lastEumber A number,nDispla&4

    return result46

    void displa&Resultdouble result"5setDispla&StringDouble.toStringresult""4lastEumber 7 result4displa&Code 7 R=SITC

  • 8/13/2019 Java Lab Manual CSE

    31/45

    calci.setisibletrue"4calci.setResiHablefalse"4

    6

    6 BB=nd of Swing (alculator (lass.

    class Divide@&[ero=2ception e2tends =2ception5public Divide@&[ero=2ception"5

    super"46

    public Divide@&[ero=2ceptionString s"5

    supers"46

    6

    class (ustom?@(alculator Demo ProgramOnOn>"4

    JTe2t?rea )t?rea?bout 7 new JTe2t?rea! #1"4)t?rea?bout.setTe2tte2t.toString""4)t?rea?bout.set$ontnew $ont>Times Eew Roman>! 1! 1'""4)t?rea?bout.set=ditablefalse"4

    p1.add)t?rea?bout"4p1.set@ackground(olor.red"4get(ontentPane".addp1! @ordera&out.(=ET=R"4

    JPanel p# 7 new JPanelnew$lowa&out$lowa&out.(=ET=R""4

    )bn "4)bn

  • 8/13/2019 Java Lab Manual CSE

    32/45

    5Qindow aboutDialog 7 e.getQindow"4aboutDialog.dispose"466"4

    pack"46

    public void actionPerformed?ction=vent e"5ife.getSource" 77 )bn

  • 8/13/2019 Java Lab Manual CSE

    33/45

    E> NO:-; Multit7rea+ing

    Aim:-

    To write a multi-t%readed Java program to print all numbersbelow 188!888 t%at are bot% prime and $ibonacci numbersome e2amples are #! '! ! 1'! etc.". Design a t%readt%at generates prime numbers below 188!888 and writes t%eminto a pipe. Design anot%er t%read t%at generatesfibonacci numbers and writes t%em to anot%er pipe.T%e maint%read s%ould read bot% t%e pipes to identif& numberscommon to bot%.

    ALGORITHM:

    %tep 1:-,nclude Dataoutputstream and piped

  • 8/13/2019 Java Lab Manual CSE

    34/45

    numbers common to bot% an dfinall& displa& t%em.

    %O)R"E "O!E

    import )ava.io.Data

  • 8/13/2019 Java Lab Manual CSE

    35/45

    import )ava.io.Data,nputStream4import )ava.io.,PipedT%reads>"4

    $ibonacciEumber0enerator f 7 new$ibonacciEumber0eneratortg! fibonicPos!>$ibonacciEumber0enerator>"4

    PrimeEumber0enerator p 7 newPrimeEumber0eneratortg! primePos! >PrimeEumber0enerator>"4

    PipedStreamTester mainTester 7 newPipedStreamTesterfibonicPis! primePis"4

    mainTester.start"4f.start"4p.start"4

    6 catc% ,

  • 8/13/2019 Java Lab Manual CSE

    36/45

    3BK $ibonic Eumber

    \M>AfibonacciEumber"4

    6 catc% , Prime Eumber

    \M>AprimeEumber"4

    6 catc% ,

  • 8/13/2019 Java Lab Manual CSE

    37/45

    K

  • 8/13/2019 Java Lab Manual CSE

    38/45

    6iboni* Number ?4;