Sie sind auf Seite 1von 167

Department of Information Technology OOPS THROUGH JAVA Lab Manual for the Academic Year 2007-2008 II B.

Tech

Guru Nana "ollege

!ngineering

I#rahimpatnam$ R R Di%trict & '() '(* +A, P,-

In.charge Principal Prepare/ #y0

HOD

NAV !"N

Appro1e/ I%%ue/ 2 #y0 Re1ie3e/ #y0 NAVEEN NAVEEN DON DON

3,e,f Date0

Guru Nanak Engineering College Ibrahimpatnam, R R District 501 506 (A. P.)

!e#artment of Information Technolo$% Lab Manual for the Academic Year 2007-08 (In accordance with JNT !"llabu!#

$ %J&'T

( JA)A LA%

$ %J&'T '*+& ( '$ (0,--8# $&M&$T&. STREAM ( II - II : Information Technolo/"

IN$T. 'T*. ( +0 .AMA .A* ( 10 2.A%3A4A. .AJ 2.*1.AMM&.$ ( 5# 10 $.INI)A$ L

2# M0%0 N&&LA4ANT&$36A.A .&++Y

&ead IT

Guru Nanak Engineering College Ibrahimpatnam, R R District 501 506 (A. P.)

!e#artment of Information Technolo$% Lab Manual for the Academic Year 2007-08 'In accordance (ith )NT* +%llabu+, -u$$e+tion+ from .rinci#al/

nhancement if an%/

0omment+/

$0 No 5 2 7

'ontent! Lab *b8ecti9e Introduction About Lab 1uideline! to $tudent! Li!t of Lab &;erci!e!

2a/e No 7 , : 7-8

705 $"llabu! 2ro/ram! (JNT # 702 Additional and Ad9anced 2ro/ram! , : 7 8 +e!cri<tion about *o<! 'once<t! $olution! for 2ro/ram! $olution! for Additional 2ro/ram! .eference! =-57 58-,= :0-80 85

Lab *b8ecti9e 1) Introduction to ob8ect oriented <ro/rammin/ conce<t!- 8a9a a! an ob8ect oriented <ro/rammin/ lan/ua/e0 Introduction to 8a9a a<<lication and a<<let!-control !tructure!-method!-arra"!0 2) *b8ect ba!ed and ob8ect oriented <ro/rammin/ creatin/ <ac>a/e!-u!in/ o9erloaded con!tructor!-!tatic cla!! 9ariable!-data ab!traction and information hidin/-relation between !u<er cla!! ob8ect! and !ubcla!! ob8ect! com<o!ition 9er!e! inheritance-<ol"mor<hi!md"namic method bindin/ ab!tract !u<er cla!!e! and concrete !u<er cla!!e! ?inheritin/ interface-u!e of inner cla!!e! and wra<<er cla!!e!-$trin/ to >eni@er and $trin/ $uffer cla!!e!0

3) .ole of ob8ect oriented <ro/rammin/ in de!i/nin/ 1 I ?1ra<h! and Ja9a20o9er9iew of !win/- e9ent handlin/A ada<ter cla!!e! and la"out mana/er!0 Ad9ance 1 I com<onent!- J2o<u< Menu!- J+e!>to<2ane- ad9ance la"out mana/er!0 4) &;ce<tion handlin/ and multithreadin/ in ob8ect oriented <ro/rammin/- 6hen e;ce<tion handlin/ !hould be u!ed-8a9a e;ce<tion handlin/ ? e;ce<tion! and inheritance-multithreadin/ in 8a9a-thread !"nchroni@ation-daemon thread! .unnable interface- Bile! and !tream! in 8a9a0 5) Networ> and +ataba!e handlin/ throu/h ob8ect oriented <ro/rammin/ ?u!in/ J*$' ?<roce!!in/ Cuerie!-o9er9iew of !er9let ? introduction to networ>in/ ?e!tabli!hin/ a !im<le !er9er and a client ? introduction to .MI ? im<lementin/ the remote interface0

INT1"!*0TI"N AB"*T LAB There are :: !"!tem! ( 'om<aC 2re!ario # in!talled in thi! Lab0 Their confi/uration! are a! follow! ( 2roce!!or ( AM+ Athelon D 50:7 13@ .AM ( 2,: M% 3ard +i!> ( 70 1% Mou!e ( *<tical Mou!e Networ> Interface card ( 2re!ent $oftware 5 All !"!tem! are confi/ured in + AL %**T mode i0eA $tudent! can boot from 6indow!
50

E2 or Linu; a! <er their lab reCuirement0 Thi! i! 9er" u!eful for !tudent! becau!e the" are familiar with different *<eratin/ $"!tem! !o that the" can e;ecute their <ro/ram! in different <ro/rammin/ en9ironment!0 2 &ach !tudent ha! a !e<arate lo/in for databa!e acce!! *racle =i client 9er!ion i! in!talled in all !"!tem!0 *n the !er9erA account for each !tudent ha! been created0 Thi! i! 9er" u!eful becau!e !tudent! can !a9e their wor> ( !cenario!FA <lG!Cl <ro/ram!A data related <ro8ect! Aetc# in their own account!0 &ach !tudent wor> i! !afe and !ecure from other !tudent!0 - Late!t Technolo/ie! li>e +*T N&T and J2&& are in!talled in !ome !"!tem!0 %efore !ubmittin/ their final <ro8ectA the" can !tart doin/ mini <ro8ect from 2nd "ear onward!0 7 MA$M ( Macro A!!embler # i! in!talled in all the !"!tem!

55

$tudent! can e;ecute their a!!embl" lan/ua/e <ro/ram! u!in/ MA$M0 MA$M i! 9er" u!eful !tudent! becau!e when the" e;ecute their <ro/ram! the" can !ee content! of 2roce!!or .e/i!ter! and how each in!truction i! bein/ e;ecuted in the '2 0 .ational .o!e $oftware i! in!talled in !ome !"!tem! !in/ thi! !oftwareA !tudent! can de<ict ML dia/ram! of their <ro8ect!0 $oftware in!talled ( 'A 'HHA J+450,A MA$MA *BBI'&-E2A J2&& and +*T N&TA .ational .o!e0 $"!tem! are <ro9ided for !tudent! in the 5(5 ratio0 $"!tem! are a!!i/ned number! and !ame !"!tem i! allotted for !tudent! when the" do the lab0

2*I! LIN - T" -T*! NT-/

52

5 &Cui<ment in the lab for the u!e of !tudent communit"0 $tudent! need to maintain a <ro<er decorum in the com<uter lab0 $tudent! mu!t u!e the eCui<ment with care0 An" dama/e i! cau!ed i! <uni!hable0 2 $tudent! are reCuired to carr" their ob!er9ation G <ro/ram! boo> with com<leted e;erci!e! while enterin/ the lab0 - $tudent! are !u<<o!ed to occu<" the machine! allotted to them and are not !u<<o!ed to tal> or ma>e noi!e in the lab0 The allocation i! <ut u< on the lab notice board0 7 Lab can be u!ed in free time G lunch hour! b" the !tudent! who need to u!e the !"!tem! !hould ta>e <rior <ermi!!ion from the lab inchar/e0

5-

, Lab record! need to be !ubmitted on or before date of !ubmi!!ion0 : $tudent! are not !u<<o!ed to u!e flo<<" di!>!

Lab .ro$ram+

57

OOPS THROUGH JAVA

a! "#$e%ule"

Submissions should includea#Aim of the <ro/ram b# +e!cri<tion c#2rocedure d# Al/orithm e#2ro/ram( <rintoutA which include! NameA rollnoA date on the to<# f# &;<ected out<ut /# *b!er9ed out<ut0

3ee4 5 5 6rite a Ja9a <ro/ram to find !im<le Intere!t0 2 +efinin/ a cla!! called $tudent0 *b!er9e the member! of the cla!! i0e the data and

5,

7 3ee4 -2 , 2ro/ram on u!a/e of final >e"word : !in/ $tatic >e" in a 9ariable 7 Ja9a 2ro/ram u!in/ *b8ect reference 9ariable! 3ee4 -6 8 2ro/ram on inheritance0 50 6rite a 8a9a <ro/ram that <rom<t! the u!er for an inte/er and then <rint! out all <rime number! u< to that inte/er0 55 6rite a 8a9a <ro/ram that chec>! whether a /i9en !trin/ i! <alindrome or not0 &;( MA+AM i! a <alindrome0 52 6rite a 8a9a <ro/ram to find the bi//e!t I !malle!t number from the /i9en arra" of 50 inte/er!0 3ee4 -7 56rite a 8a9a <ro/ram to !ort /i9en arra" of 50 inte/er! in a!cendin/ I de!cendin/
5:

function!0 2ro/ram on Method o9erloadin/ 2ro/ram on 'on!tructor o9erloadin/

57 5, 5:

order0 6rite a 8a9a <ro/ram to delete an element from an arra" of 50 element!0 6rite a 8a9a <ro/ram to add two /i9en matrice!0 6rite a 8a9a <ro/ram that <rint! tran!<o!e of a /i9en matri;0

3ee4 -8 57 6rite a 8a9a <ro/ram to multi<l" two /i9en matrice!0 58 6rite a 8a9a <ro/ram for !ortin/ a /i9en li!t of name! in a!cendin/ order0 5= 6rite a 8a9a <ro/ram to di!<la" literal 9alue! of all the element! in a /i9en matri;0 3ee4 -9 20 6rite a 8a9a <ro/ram that read! a line of inte/er!A and then di!<la"! each inte/erA and the !um of all the inte/er! (u!e !trin/ to>eni@er cla!!#0 25 6rite a 8a9a <ro/ram to create an ordinar" file0 22 6rite a 8a9a <ro/ram to co<" content!
57

2-

from one file into another file0 6rite a 8a9a <ro/ram to di!<la" the content! in a /i9en file0

3ee4 -7 27 6rite a 8a9a <ro/ram read! one file name from the u!er then di!<la"! information about whether the file e;i!t!A whether the file i! readableA whether the file i! writableA the t"<e of file and the len/th of the file! in b"te!0 2, 6rite a 8a9a <ro/ram that read! a file and di!<la" file on the !creenA line number before each line0 2: 6rite a 8a9a <ro/ram that di!<la"! the number! of character!A line! and word! in a te;t file0 3ee4 -8 27 6rite a 8a9a <ro/ram to im<lement inheritance and o9erloadin/ method!0 28 6rite a 8a9a <ro/ram to create a <ac>a/e and im<ortin/ the <ac>a/e0 2= 6rite a 8a9a <ro/ram that im<lement! !tac> A+T0
58

-0 -5

6rite a 8a9a <ro/ram to con9ert infi; e;<re!!ion into <o!tfi; form0 6rite a 8a9a <ro/ram to im<lement e;ce<tion handlin/0

3ee4 -: -5 6rite an a<<let that di!<la"! a !im<le me!!a/e -2 'reatin/ a mo9in/ banner a<<let0 3ee4 -50 -6rite an a<<let that com<ute! the <a"ment of a loan ba!ed on the amount of the loanA the intere!t rate and the number of month!0 It ta>e! one <arameter from the brow!er( Monthl" rateJ if trueA the intere!t i! <er monthJ *ther wi!e the intere!t rate i! annual0 3ee4 -55 -7 6rite a 8a9a <ro/ram that wor>! a! a !im<le calculator0 !e a /rid la"out to arran/e button! for the di/it! and for the H - K L o<eration!0 Add a te;t field to di!<la" the re!ult0
5=

-,

6rite a 8a9a <ro/ram to for handlin/ mou!e e9ent!0

3ee4 52 -: 6rite a 8a9a <ro/ram for creatin/ a thread u!in/ Thread cla!!0 -7 6rite a 8a9a <ro/ram for creatin/ a thread b" im<lementin/ runnable interface0 -8 6rite a 8a9a <ro/ram for creatin/ a multi<le thread! 3ee4 -56 -= 6rite a 8a9a <ro/ram that correctl" im<lement! <roducer con!umer <roblem u!in/ the conce<t of inter thread communication0 70 6rite a 8a9a <ro/ram that let! u!er! create <ie chart!0 +e!i/n "our own u!er interface (with !win/ I A6T# 3ee4 -57 75 6rite a 8a9a <ro/ram that allow! the u!er to draw-line!A rectan/le! etc00 72 6rite a 8a9a <ro/ram that illu!trate! how
20

run time <ol"mor<hi!m i! achie9ed0 3ee4 -58 76rite a 8a9a <ro/ram that im<lement! a !im<le clientG!er9er a<<lication0 The client !end! data to a !er9er0 The !er9er recei9e! the dataA u!e! it to <roduce a re!ultA an then !end! the re!ult bac> to the client0 The client di!<la"! the re!ult n the con!ole0 Bor &;0 The data !ent from the client i! the radiu! of a circleA and the re!ult <roduced b" the !er9er i! the area of the circle0

25

OBJECT ORIENTED PROGRAMMING: "". 0once#t+/ The ob8ect oriented <aradi/m i! built on the foundation laid b" the !tructured <ro/rammin/ conce<t!0 The fundamental chan/e in **2 i! that a <ro/ram i! de!i/ned around the data bein/ o<erated u<on rather u<on the o<eration! them!el9e!0 +ata and it! function! are enca<!ulated into a !in/le entit"0**2 facilitate! creatin/ reu!able code that can e9entuall" !a9e a lot of wor>0 A feature called <ol"mor<hi!m <ermit! to create multi<le definition! for o<erator! and function!0 Another feature called inheritance <ermit! to deri9e new cla!!e! from old one!0 **2 introduce! man" new idea! and in9ol9e! a different a<<roach to <ro/rammin/ than the <rocedural <ro/rammin/0 Benefit+ of ob;ect oriented #ro$rammin$/ +ata !ecurit" i! enforced0 Inheritance !a9e! time0 !er defined data t"<e! can be ea!il" con!tructed0

22

Inheritance em<ha!i@e! in9ention! of new data t"<e!0 Lar/e com<le;it" in the !oftware de9elo<ment cn be ea!il" mana/ed0 Ba+ic 0<< =no(led$e/ 'HH be/an it! life in %ell Lab!A where %8arne $trou!tru< de9elo<ed the lan/ua/e in the earl" 5=80!0 'HH i! a <owerful and fle;ible <ro/rammin/ lan/ua/e0 Thu!A with minor e;ce<tion!A 'HH i! a !u<er!et of the ' 2ro/rammin/ lan/ua/e0 The <rinci<al enhancement bein/ the ob8ect ? oriented conce<t of a cla!!0 A 'la!! i! a u!er defined t"<e that enca<!ulate! man" im<ortant mechani!m!0 'la!!e! enable <ro/rammer! to brea> an a<<lication u< into !mallA mana/eable <iece!A or ob8ect!0 Ba+ic conce#t+ of "b;ectoriented #ro$rammin$/ "b;ect/ *b8ect! are the ba!ic run time entitie! in an ob8ect-oriented !"!tem0
2-

th" ma" re<re!ent a <er!onA a <laceAa ban> accountAa table of data or an" item that the <ro/ram ha! to handle0 cla++/ the entire !et of data and code of an ob8ect can be made of au!erdefined data t"<e with the hel< of a cl!!0 in factA*b8ect! are 9ariable! of the t"<e cla!!0 once a cla!! ha! been defined Awe can create an" number of ob8ect! belon/in/ to that cla!!0 A cla!! i! thu! a collection of ob8ect! of !imilar t"<e0 for e;am<le(man/oAa<<leAand oran/e are member! of the cla!! fruit0 e;( fruit man/oJ will create an ob8ect man/o belon/in/ to the cla!! fruit0 !ata Ab+traction and nca#+ulation/

27

the wra<<in/ u< of data and function! in to a !in/le unit i! >nown a! enca<!ulation0 +ata enca<!ulation i! the mo!t !tri>in/ feature of a cla!!0 the data i! not acce!!abile to the out!ide worldAand onl" tho!e function! which are wra<<ed in the cla!! can acce!!0 thi! in!ulation of the data from direct acce!! b" the <ro/ram i! calld data hidin/0 Ab+traction / ab!traction refere! to the act of re<re!entin/ e!!eential fearture! without includin/ the bac>/round detail! or e;<lanation!0 !ince the cla!!e! u!e the conce<t of data ab!traction Ath" are >nown a! ab!traction data t"<e(A+T#0 Inheritance / interitance i! the <roce!! b" which ob8ect! of one cla!! acCuire the <ro<eritie! of ob8ect! of another cla!!0 Inheritance !u<<ort! the conce<t of hierarchical cla!!ification0

2,

for e;am<le(
%ird
Attribute!( Beather! La" e//!

Bl"in/ bird
Attribute!( --------------------

Non fl"in/ bird


Attribute!( ---------------------

.obin
Attribute!( NNNNNNNNN

$wallow
Attribute!( NNNNNNNNN

2en/uin
Attribute!( NNNNNNNNN

4iwi
Attribute!( NNNNNNNNN

the bird Mrobin M i! a <art of the cla!! Mfl"in/ birdM which i! a/ian a <art of the cla!! MbirdM0 the conce<t of inheritance <ro9ide the idea of reu!abilit"0 ."LYM"1.&I-M/ 2ol"mor<hi!m i! another im<ortant oo< conce<t0 2ol"mor<hi!m mean! the abilit" to ta>e more than one form0 an o<eration ma" e;hibit

2:

different in!tance!0 The beha9ior de<end! u<on the t"<e! of data u!ed in the o<eration0 The <roce!! of ma>in/ an o<erator to e;hibit different beha9ior! in different in!tance i! >nown a! o<erator o9erloadin/0 2ol"mor<hi!m <la"! an im<ortant role in allowin/ ob8ect! ha9in/ different internal !tructure! to !hare the !ame e;ternal interface0 2ol"mor<hi!m i! e;ten!i9el" u!ed if im<lementin/ inheritance0
$ha<e +raw(#

'ircle *b8ect +raw(#

%o; *b8ect +raw(#

Trian/le *b8ect +raw(#

The "b;ect-"riented A##roach The fundamental idea behind ob8ect-oriented lan/ua/e! i! to combine into a !in/le <ro/ram entit" both data and the function! that o<erate on that data0 $uch an entit" i! called an ob8ect0
27

An ob8ectM! function!A called member function! in 'HH (becau!e the" belon/ to a <articular cla!! of ob8ect!#A t"<icall" <ro9ide the onl" wa" to acce!! it! data0 If "ou want to read a data item in an ob8ectA "ou call a member function in the ob8ect0 It will read the item and return the 9alue to "ou0 You canMt acce!! the data directl"0 The data i! hiddenA !o it i! !afe from accidental alteration0 +ata and it! function! are !aid to be enca<!ulated into a !in/le entit"0 &nca<!ulation and data hidin/ are >e" term! in the de!cri<tion of ob8ect-oriented lan/ua/e!0

28

)a>a &i+tor%/ Ja9a i! a /eneral-<ur<o!eJ ob8ect oriented <ro/rammin/ lan/ua/e de9elo<ed b" $un Micro!"!tem! of $A in 5==50 *ri/inall" called Ooa>P b" Jame! 1o!lin/A one of the in9entor! if the lan/ua/e0 Thi! /oal had a !tron/ im<act on the de9elo<ment team to ma>e the lan/ua/e !im<leA <ortableA hi/hl" reliable and <owerful lan/ua/e0 Ja9a al!o add! !ome new feature!0 6hile 'HH i! a !u<er!et of '0 Ja9a i! neither a !u<er!et nor a !ub!et of ' or 'HH0

C&& C Ja'a

!ata T%#e+

2=

------------------------------------------------------------------------------*b8ecti9e! 3a9in/ read thi! !ection "ou !hould be able to( declare (name# a local 9ariable a! bein/ one of 'M! fi9e data t"<e! initiali!e local 9ariable! <erform !im<le arithmetic u!in/ local 9ariable! ------------------------------------------------------------------------------There are fi9e ba!ic data t"<e! a!!ociated with 9ariable!( int - inte/er( a whole number0 float - floatin/ <oint 9alue( ie a number with a fractional <art0 double - a double-<reci!ion floatin/ <oint 9alue0 char - a !in/le character0 9oid - 9aluele!! !<ecial <ur<o!e t"<e which we will e;amine clo!el" in later !ection!0 *ne of the confu!in/ thin/! about the ' lan/ua/e i! that the ran/e of 9alue! and the amount of !tora/e that each of the!e t"<e! ta>e! i! not defined0 Thi! i! becau!e in each ca!e the
-0

MnaturalM choice i! made for each t"<e of machine0 You can call 9ariable! what "ou li>eA althou/h it hel<! if "ou /i9e them !en!ible name! that /i9e "ou a hint of what the"Mre bein/ u!ed for - name! li>e !umA totalA a9era/e and !o on0 If "ou are tran!latin/ a formula then u!e 9ariable name! that reflect the element! u!ed in the formula0 Bor e;am<leA 2<r (that !hould read a! Q2 <i rQ but that de<end! u<on how "our brow!er ha! been !et-u<# would /i9e local 9ariable! name! of <i and r0 .ememberA ' <ro/rammer! tend to <refer !hort name!R Note( all 'M! 9ariable! mu!t be/in with a letter or a QNQ (under!core# character0

-5

+ATA TY2&$ IN JA)A

2rimiti9e (intrin!ic#

Non-2rimiti9e (+eri9ed#

Numeric

Non-Numeric

'la!!

Arra"!

Inte/er

Bloatin/-<oint

'haracter

%oolean

Interface Inte/er

Bloatin/ <oint

%"te

$hort

Int

Lon/ Bloat +ouble

------------------------------------------------------------------------------Inte/er Number )ariable! The fir!t t"<e of 9ariable we need to >now about i! of cla!! t"<e int - !hort for inte/er0 An int 9ariable can !tore a 9alue in the ran/e --27:8 to H-27:70 You can thin> of it a! a lar/i!h <o!iti9e or ne/ati9e whole number( no fractional <art i!

-2

allowed0 To declare an int "ou u!e the in!truction( int 9ariable nameJ

Bor e;am<le( int aJ declare! that "ou want to create an int 9ariable called a0 To a!!i/n a 9alue to our inte/er 9ariable we would u!e the followin/ ' !tatement( aS50J The ' <ro/rammin/ lan/ua/e u!e! the QSQ character for a!!i/nment0 A !tatement of the form aS50J !hould be inter<reted a! ta>e the numerical 9alue 50 and !tore it in a memor" location a!!ociated with the inte/er 9ariable a0 The QSQ character !hould not be !een a! an

--

eCualit" otherwi!e writin/ !tatement! of the form( aSaH50J will /et mathematician! blowin/ fu!e!R Thi! !tatement !hould be inter<reted a! ta>e the current 9alue !tored in a memor" location a!!ociated with the inte/er 9ariable aJ add the numerical 9alue 50 to it and then re<lace thi! 9alue in the memor" location a!!ociated with a0 ------------------------------------------------------------------------------+ecimal Number )ariable! A! de!cribed abo9eA an inte/er 9ariable ha! no fractional <art0 Inte/er 9ariable! tend to be u!ed for countin/A wherea! real number! are u!ed in arithmetic0 ' u!e! one of two >e"word! to declare a 9ariable that i! to be a!!ociated with a decimal number( float and double0 The" are each offer a different le9el of <reci!ion a! outlined below0 float A floatA or floatin/ <ointA number ha! about !e9en di/it! of <reci!ion and a ran/e of about
-7

50&--: to 50&H-:0 A float ta>e! four b"te! to !tore0 double A doubleA or double <reci!ionA number ha! about 5- di/it! of <reci!ion and a ran/e of about 50&-0- to 50&H-0-0 A double ta>e! ei/ht b"te! to !tore0 Bor e;am<le( float totalJ double !umJ To a!!i/n a numerical 9alue to our floatin/ <oint and double <reci!ion 9ariable! we would u!e the followin/ ' !tatement( totalS000J !umS520,0J ------------------------------------------------------------------------------'haracter )ariable! ' onl" ha! a conce<t of number! and character!0 It 9er" often come! a! a !ur<ri!e to !ome <ro/rammer! who learnt a be/innerM! lan/ua/e
-,

!uch a! %A$I' that ' ha! no under!tandin/ of !trin/! but a !trin/ i! onl" an arra" of character! and ' doe! ha9e a conce<t of arra"! which we !hall be meetin/ later in thi! cour!e0 To declare a 9ariable of t"<e character we u!e the >e"word char0 - A !in/le character !tored in one b"te0 ?or e@am#le/ char cJ To a!!i/nA or !toreA a character 9alue in a char data t"<e i! ea!" - a character 9ariable i! 8u!t a !"mbol enclo!ed b" !in/le Cuote!0 Bor e;am<leA if c i! a char 9ariable "ou can !tore the letter A in it u!in/ the followin/ ' !tatement( cSMAM Notice that "ou can onl" !tore a !in/le character in a char 9ariable0 Later we will be di!cu!!in/ u!in/ character !trin/!A which ha! a 9er" real <otential for confu!ion becau!e a !trin/ con!tant i! written between double Cuote!0 %ut for the moment remember that a char 9ariable i! MAM and not QAQ0 ------------------------------------------------------------------------------A!!i/nment $tatement
-:

*nce "ouM9e declared a 9ariable "ou can u!e itA but not until it ha! been declared - attem<t! to u!e a 9ariable that ha! not been defined will cau!e a com<iler error0 !in/ a 9ariable mean! !torin/ !omethin/ in it0 You can !tore a 9alue in a 9ariable u!in/( name S 9alueJ Bor e;am<le( aS50J !tore! the 9alue 50 in the int 9ariable a0 6hat could be !im<lerT Not muchA but it i!nMt actuall" 9er" u!efulR 6ho want! to !tore a >nown 9alue li>e 50 in a 9ariable !o "ou can u!e it laterT It i! 50A alwa"! wa! 50 and alwa"! will be 500 6hat ma>e! 9ariable! u!eful i! that "ou can u!e them to !tore the re!ult of !ome arithmetic0 'on!ider four 9er" !im<le mathematical o<eration!( addA !ubtractA multi<l" and di9ide0 Let u! !ee how ' would u!e the!e o<eration! on two float 9ariable! a and b0 add aHb !ubtract a-b multi<l"
-7

aKb di9ide aGb Note that we ha9e u!ed the followin/ character! from 'M! character !et( H for add - for !ubtract K for multi<l" G for di9ide %& 'A.&B L 6IT3 A.IT3M&TI'RRR 6hat i! the an!wer to thi! !im<le calculationT aS50GThe an!wer de<end! u<on how a wa! declared0 If it wa! declared a! t"<e int the an!wer will be -J if a i! of t"<e float then the an!wer will be -0---0 It i! left a! an e;erci!e to the reader to find out the an!wer for a of t"<e char0 Two <oint! to note from the abo9e calculation( ' i/nore! fraction! when doin/ inte/er di9i!ionR when doin/ float calculation! inte/er! will be con9erted into float0 6e will !ee later how ' handle! t"<e con9er!ion!0 ------------------------------------------------------------------------------Arithmetic *rderin/
-8

6hil!t we are dealin/ with arithmetic we want to remind "ou about !omethin/ that e9er"one learn! at 8unior !chool but then we for/et it0 'on!ider the followin/ calculation( aS5000 H 200 K ,00 - :00 G 200 6hat i! the an!werT If "ou thin> it! 27 /o to the bottom of the cla!!R 2erha<! "ou /ot that an!wer b" followin/ each in!truction a! if it wa! bein/ t"<ed into a calculator0 A com<uter doe!nMt wor> li>e that and it ha! it! own !et of rule! when <erformin/ an arithmetic calculation0 All mathematical o<eration! form a hierarch" which i! !hown here0 In the abo9e calculation the multi<lication and di9i!ion <art! will be e9aluated fir!t and then the addition and !ubtraction <art!0 Thi! /i9e! an an!wer of 570 Note( To a9oid confu!ion u!e brac>et!0 The followin/ are two different calculation!( aS5000 H (200 K ,00# - (:00 G 200# aS(5000 H 200# K (,00 - :00# G 200 You can freel" mi; intA float and double 9ariable! in e;<re!!ion!0 In nearl" all ca!e! the lower <reci!ion 9alue! are con9erted to the hi/he!t <reci!ion 9alue! u!ed in the e;<re!!ion0
-=

Bor e;am<leA the e;<re!!ion fKiA where f i! a float and i i! an intA i! e9aluated b" con9ertin/ the int to a float and then multi<l"in/0 The final re!ult i!A of cour!eA a float but thi! ma" be a!!i/ned to another data t"<e and the con9er!ion will be made automaticall"0 If "ou a!!i/n to a lower <reci!ion t"<e then the 9alue i! truncated and not rounded0 In other word!A in nearl" all ca!e! "ou can i/nore the <roblem! of con9ertin/ between t"<e!0

70

.roce++ of buildin$ and runnin$ ;a>a a##lication #ro$ram+/


Te;t &ditor

Ja9a $ource 'ode

Ja9adoc

3TML file!

Ja9ac

Ja9a 'la!! Bile

Ja9ah

3eader Bile!

Ja9a (onl" file name#

Jdb (databa!e#

Ja9a <ro/ra m *ut<ut

The wa" the!e tool! are a<<lied to build and run a<<lication <ro/ram! i! create a <ro/ram0 6e need create a !ource code file u!in/ a te;t editor0 The !ource code i! then com<iled u!in/ the 8a9a com<iler 8a9ac and e;ecuted u!in/ the 8a9a inter<reter 8a9a0 The 8a9a debu//er 8db i! u!ed to

75

find error!0 A com<lied 8a9a <ro/ram can be con9erted into a !ource code0 A11AY-A -T1IN2- AN! -T1IN2B*?? 1Arra"! are u!ed to !troe lar/e number data of !ame data t"<e!0 which are /rou<ed to/ether and called b" a common name0The!e are called element! of the arra"0 Arra"! can !tore both <rimiti9e data t"<e! and ob8ect!0but the" mu!t be of !ame t"<e0The!e element! are !tored in conti/uou! memor" location!0 Arra"! are ordered in the !en!e that each element i! inde;ed0 !tartin/ from 0 and can be retrei9ed ba!ed on the inde;ed 9alue0Arra"! in 8a9a unli>e in ' are ob8ect!A and are of fi;ed !i@e0 -T1IN2A $trin/ i! a !erie! of character! enclo!ed in double Cuote! and i! treated a! a !in/le unit0 nli>e cGcHH !trin/ in 8a9a i! an ob8ect from cla!! !trin/0A !trin/ in 8a9a doe! not terminate with a null character(Uo#0A !trin/ ma" be a!!i/ned in declaration to a !trin/ reference0The declaration $trin/ a/eSQoldQJ

72

initiali@e! a $trin/ reference a/e to refer to the anon"mou! !trin/ ob8ect QoldQ0 -trin$ 0oncatenation $trin/ can be concatenated with H o<erator0 $trin/ concatenation u!in/ !trin/ reference! <ublic cla!! $trin/'oncatenation V <ublic !tatic 9oid main($trin/ ar/!WX#V $trin/ fir!tNnameSQNa/e!hQJ $trin/ la!tNnameSQ.aoQJ $trin/ !irNname S Q$ureQJ $trin/ concat5S!irNnameHfir!tNnameHla!tNnameJ $trin/ concat2S !irNnameHQMHfir!tNnameHQQHla!tNnameJ $"!tem0out0<rintln(concat5#J $"!tem0out0<rintln(concat5#J $"!tem0out0<rintln(concat2#J Y Y "*T.*T/ $ureNe/e!h.ao $ure Ne/e!h .ao

7-

The len/th(!i@e# of a !trin/ can be found with the !trin/ method len/th(#0Two !trin/ can be com<ared with eCual!(# methodA if both !trin/! are !ameA the method return! a boolean 9alue of true and if not fal!e0 In eCual!(# method the ca!e ( u<<er or lower# of the letter! will be ta>en into con!ideration and if ca!e i! not a matterA th method eCual!I/nore'a!e(# ma" be u!ed0 The o<erator0 SS i! u!ed to com<are two ob8ect! reference! to !ee if the" refer the !ame in!tance0 u!in/ eCual!(#A eCual!I/nore'a!e(# and to !ee the differece between the u!a/e of eCual! and(# and SS <ublic cla!! $trin/'om<ari!ionV <ublic !tatic 9oid main($trin/ ar/!WX# V $trin/ !5 S Q8a9aQ $trin/ !2 S Q8a9aQ $trin/ !- S Q8a9aQ $trin/ !7 S !5J $"!tem0out0<rintln(QLen/th of the !trin/ !5S QH!50len/ht(##J $"!tem0out0<rintln(Q!5(8a9a#eCual! !2(8a9a#(QH !50eCual!(!2##J

77

$"!tem0out0<rintln(Q!5(8a9a# eCual!I/nore'a!e$2(8a9a#( H!50eCual!i/nore'a!e(!2##J $"!tem0out0<rintln(Q!5(8a9a# SS !2(8a9a#(Q H !5SS!2#J $"!tem0out0<rintln(Q!2(8a9a# SS !-(8a9a#(Q H!2SS !-#J $"!tem0out0<rintln(Q!5(8a9a#SS!7(8a9a#(Q H !5SS!7#J Y Y * T2 T( Len/th of the $trin/ !5 S7 !5(8a9a# eCual! !2(8a9a#( fal!e !5(8a9a#eCual!i/noreca!e!2(8a9a#(true !5(8a9a#SS!2(8a9a#(fal!e !2(8a9a#SS!-(8a9a#( fal!e !5(8a9a#SS !7(8a9a#( fal!e The method com<areTo(# can al!o be u!ed to com<are two !trin/ but com<ari!ion i! done le;o/ra<hicall"(that i! letter b" letter in a dictionar" order(#0 Method com<areTo(# return! 0 if both !trin/! are eCualA a ne/ati9e number if the !trin/ that in9o>e! com<areTo i! le!! than
7,

the !trin/ that i! <a!!ed an an ar/ument and a <o!iti9e number if the !trin/ that in9o>e! com<areTo i! /reater than the !trin/ that i! <a!!ed a! an ar/ument0A <art of a !trin/ can be co<ied with the method !ub!trin/(# method0 -T1IN2B*?? 1- 'la!!( *nce a $trin/ ob8ect i! createdA itF! content! can ne9er be chan/ed0 That i! to !a" $trin/! are immutable and if tried to chan/e a nbew reference i! created0Thi! can be o9ercome b" u!in/ the ob8ect! of cla!! $trin/%uffer0 6ith $trin/%uffer !trin/! can be mani<ulated d"namicall"0 $trin/ ob8ect! are con!tant !trin/! and $trin/%uffer ob8ect! are modifiable !trin/!0 2ro/ram on findin/ the len/th and ca<acit" of a $trin/%uffer ob8ect0 2ublic cla!! Len/th'a<acit" V 2ublic !tatic 9oid main($trin/ ar/!WX#V $trin/%uffer !b5 S new $trin/%uffer(#J $trin/%uffer !b2 S new $trin/%uffer(O*b8ect*neP#J $"!tem0out0<rintln(OLen/th of !b5 S OH!b0lenth(##J

7:

$"!tem0out0<rintln(OLen/th of !b2 S O !b20len/ht(##J $"!tem0out0<rintln(O'a<acit" of !b5 S O!b50ca<acit"(##J $"!tem0out0<rintln(O'a<acit" of !b2 S O!b20ca<acit"(##J Y Y "*T.*T Len/th of !b5 S 0 Len/th of !b2 S = 'a<acit" of !b5 S 5: 'a<acit" of !b2 S 2, M*LTIT&1 A!IN2 Multithreadin/ allow! two <art! of the !ame <ro/ram to run concurrentl"0 Thi! article di!cu!!e! how to <ull off thi! <erformanceim<ro9in/ feat in Ja9a0 It i! e;cer<ted from cha<ter 50 of the boo> Ja9a +em"!tifiedA written b" Jim 4eo/h (Mc1raw-3illG*!borneA 2007J I$%N( 00722,7,78 Multita!>in/ i! <erformin/ two or more ta!>! at the !ame time0 Nearl" all o<eratin/ !"!tem! are ca<able of multita!>in/ b" u!in/ one of two
77

multita!>in/ techniCue!( <roce!!-ba!ed multita!>in/ and thread-ba!ed multita!>in/0 2roce!!-ba!ed multita!>in/ i! runnin/ two <ro/ram! concurrentl"0 2ro/rammer! refer to a <ro/ram a! a <roce!!0 ThereforeA "ou could !a" that <roce!!-ba!ed multita!>in/ i! <ro/ramba!ed multita!>in/0 Thread-ba!ed multita!>in/ i! ha9in/ a <ro/ram <erform two ta!>! at the !ame time0 Bor e;am<leA a word <roce!!in/ <ro/ram can chec> the !<ellin/ of word! in a document while "ou write the document0 Thi! i! thread-ba!ed multita!>in/0 A /ood wa" to remember the difference between <roce!!-ba!ed multita!>in/ and thread-ba!ed multita!>in/ i! to thin> of <roce!!-ba!ed a! wor>in/ with multi<le <ro/ram! and threadba!ed a! wor>in/ with <art! of one <ro/ram0

78

Main thread NNNNNNNNNNNNNN NNNNNNNNNNNNNN NNNNNNNNNNN

Thread A NNNNNNNNN NNNNNNNNN NNNNNNNNN NNNNNNNNN

Thread % NNNNNNNNN NNNNNNNNN NNNNNNNNN NNNNNNNNN

Thread ' NNNNNNNNN NNNNNNNNN NNNNNNNNN NNNNNNNNN

The ob8ecti9e of multita!>in/ i! to utili@e the idle time of the '2 0 Thin> of the '2 a! the en/ine of "our car0 Your en/ine >ee<! runnin/ re/ardle!! of whether the car i! mo9in/0 Your ob8ecti9e i! to >ee< "our car mo9in/ a! much a! <o!!ible !o "ou can /et the mo!t mile! from a /allon of /a!0 An idlin/ en/ine wa!te! /a!0 The !ame conce<t a<<lie! to the '2 in "our com<uter0 You want "our '2 c"cle! to be <roce!!in/ in!truction! and data rather than waitin/ for !omethin/ to <roce!!0 A '2 c"cle i! !omewhat !imilar to "our en/ine runnin/0 It ma" be hard to belie9eA but the '2 idle! more than it <roce!!e! in man" de!>to< com<uter!0 LetF! !a" that "ou are u!in/ a word
7=

<roce!!or to write a document0 Bor the mo!t <artA the '2 i! idle until "ou enter a character from the >e"board or mo9e the mou!e0 Multita!>in/ i! de!i/ned to u!e the fraction of a !econd between !tro>e! to <roce!! in!truction! from either another <ro/ram or from a different <art of the !ame <ro/ram0 Ma>in/ efficient u!e of the '2 ma" not be too critical for a<<lication! runnin/ on a de!>to< com<uter becau!e mo!t of u! rarel" need to run concurrent <ro/ram! or run <art! of the !ame <ro/ram at the !ame time0 3owe9erA <ro/ram! that run in a networ>ed en9ironmentA !uch a! tho!e that <roce!! tran!action! from man" com<uter!A need to ma>e a '2 F! idle time <roducti9e0 LI? 0Y0L "? A T&1 A!/ Ne( born +tate/ 6e created a thread ob8ectA the thread i! born and i! !aid to be in newborn !tate0 The thread i! not "et !cheduled for runnin/0 $chedule it for runnin/ u!in/ +tart', method0
,0

4ill it u!in/ +to#', method0 1unnable +tate/ The runnable !tate mean! that the thread i! read" for e;ecution and i! waitin/ for the a9ailabilit" of the <roce!!or0 That i!A the thread ha! 8oined the Cueue of thread! that are waitin/ for e;ecution0 If all thread! ha9e eCual <riorit"A then the" are /i9en time !lot! for e;ecution in round robinA fir!t-comeA fir!t!er9e manner0 If we want a thread to relinCui!h control to another thread of eCual <riorit" before it! turn come!A we can do !o b" u!in/ the %ield', method0

,5

Ne, *$rea%

Newborn
"*o+ "*ar*

A#*i'e T$rea%

"*o+

Running
)iel%

Runnable

Dead
(ille% T$rea%

Su"+en% "lee+ ,ai*

Re"u/e no*i0)

"*o+

-%le T$rea% .no* Runna!le)

Blocked

1unnin$ +tate/ .unnin/ mean! that the <roce!!or ha! /i9en it! time to the thread for it! e;ecution0 The thread run! until it relinCui!he! control on it! own or it i! <reem<ted b" a hi/her <riorit" thread0 $u!<end (# method( A !u!<ended thread can be re9i9ed b" u!in/ the re+ume ', method0 Thi! a<<roach i! u!eful when we want to !u!<end a thread for !ome time due to certain rea!onA but do not want to >ill it0 $lee<(# method(
,2

6e can <ut a thread to !lee< for a !<ecified time <eriod u!in/ the method +lee# 'time, where time i! in milli!econd!0 Thi! mean! that the thread i! out of the Cueue durin/ thi! time <eriod0the thread re-enter! the runnable !tate a! !oon a! thi! time <eriod i! ela<!ed0 Notif" (# method( It ha! been told to wait until !ome e9ent occur!0 Thi! i! done u!in/ the (ait ', method0 The thread can be !cheduled to run a/ain u!in/ the notif% ', method0 Bloc4ed +tate/ A thread i! !aid to be bloc>ed when it i! <re9ented from enterin/ into the runnable !tate and !ub!eCuentl" the runnin/ !tate0 Thi! ha<<en! when the thread i! !u!<endedA !lee<in/ A or waitin/ in order to !ati!f" certain reCuirement!0 A bloc>ed thread i! con!idered Onot runnableP but not dead and therefore full" Cualified to run a/ain0 +ead !tate( &9er" thread ha! a life c"cle0 A runnin/ thread end! it! life when i! ha! com<leted
,-

e;ecutin/ it! run', method0 6e can >ill it b" !endin/ the !to< me!!a/e to it at an" !tate thu! cau!in/ a <remature death to it0 A thread can be >illed a! !oon it i! bornA or while it i! runnin/A or e9en when it i! in Onot runnableP (bloc>ed# condition0 B0 .TI"N &AN!LIN2 @ce#tion+ / &;ce<tion! are <art of the inheritance hierarch" and are deri9ed from the Throwable cla!!0That i! an e;ce<tion i! an in!tance of the Throwable cla!!0 rror/ Thi! cla!! de!cribe! internal error!A!uch a! out of di!> !<ace etc0The u!er can onl" be informed about !uch error! and !o ob8ect! of the!e cannot be thrown0 &;ce<tion! Two cla!!e! !hown abo9e are deri9ed from thi! cla!! .un Time &;ce<tion &;ce<tion! that inherit from thi! cla!! include
,7

a bad ca!t0 out of bound arra" acce!! a null <ointer ace!!0 The!e <roblem! ari!e out of wron/ <ro/rammin/ lo/ic and mu!t be corrected b" the <ro/rammer him!elf0 $ome of the!e e;ce<tion! are Arithmetic &;ce<tion Null2ointer &;ce<tion 'la!!'a!t &;ce<tion Arra"Inde;*ut%ound! &;ce<tion0 "ther e@ce#tion+ (The!e include e;ce<tion! for multithreadin/A malformed .LAreadin/ <a!t end of file etc0 $ome of the !ubcla!!e!( I*&;ce<tion A6T&;ce<tion 'la!!NotBound&;ce<tion Ille/alAce!!&;ce<tion Thro(in$ @ce#tion+ A method tha return! a 9alue can al!o be made to throw an e;ce<tion0 6hat i! e;ce<tion handlin/T
,,

An e;ce<tion !i/nifie! an ille/alA in9alid or une;cectedA i!!ue durin/ <ro/ram e;ecution0 $ince e;ce<tion! are alwa"! a!!umed to be antici<ated0A "ou need to <ro9ide a<<ro<riate e;ce<tion handlin/ 6hat are the >e"word! that are freCuentl" u!ed in e;ce<tion handlin/T The im<ortant >e"word! of e;ce<tion handlin/ are tr" and catch and the" are not method!A but /enerall" termed a! tr" bloc> and catch bloc> and in the!e bloc>! we write the handlin/ code0 A! u!ual each bloc> contain! !tatement! delimeted b" brace! (VY#0The !tatement! that are !u!<ected to rai!e the e;ce<tion! ae written in tr" bloc> and the !tatement! to handle the !ituation when the tr" bloc> !tatement! rai!e! an e;ce<tion are written in catch bloc>(li>e catch(Arithmetic&;ce<tion ae# @am#le/ 2ublic cla!! Arra"inde;V 2ublic !tatic 9oid main($trin/ ar/!WX# V Int mar>!WXSV50A20A-0A70A,0Y Tr"V $"!tem0out0<rintln(mar>!W50X#J
,:

Y 'atch(Arra"Inde;*ut*f%ound!&;ce<tion ai# V $"!tem0out0<rintln(O3elloR &;ce<tion! i! cau/ht b" meP Hai#J Y Binall" V $"!tem0out0<rintln(OThi! e;ecute! irre!<ecti9e of rai!in/ of an e;ce<tionP#J Y $"!tem0out0<rintln(Omar>!W7XSPHmar>!W7X#J Y Y Multi#le catch Bloc4+ In !ome ca!e! a method ma" ha9e to catch different t"<e! of e;ce<tion!0 Ja9a !u<<ort! multi<le catch bloc>!A that i! a !in/le tr" bloc> can contain an" number of catch bloc>!0 A <recation to be ob!er9ed i! each bloc> mu!t !<ecif" a different t"<e of e;ce<tion0 2ublic cla!! Multi<le'atch V 2ublic !tatic 9oid main($trin/ ar/!WX# V Int aS8A bS0A cA dA mar>!WXSV50A20A-0A70A,0YJ Tr" 'S aGbJ
,7

$"!tem0out0<rintln(OaGb S OHc#J dSmar>!W50XJ $"!tem0out0<rintln(Omar>!W50XSHd#J Y 'atch(Arra"Inde;*ut%ound&;ce<tion aie#V $"!tem0out0<rintln(O&;ce<tion cau/ht b" me i! OHaie#J Y 'atch(Arithmetic&;ce<tion e# V $"etem0out0<rintln(O&;ce<ion cau/ht b" me i!P He#J Y 'atch(&;ce<tion e# V $"!tem0out0<ritnln(OBrom &;ce<tion (PHe#J Y Y Y A..L TAn a<<let i! a <ro/ram written in the Ja9a <ro/rammin/ lan/ua/e that can be included in an 3TML <a/eA much in the !ame wa" an ima/e i! included in a <a/e0 6hen "ou u!e a Ja9a technolo/"-enabled brow!er to 9iew a <a/e that contain! an a<<letA the a<<letM! code i!
,8

tran!ferred to "our !"!tem and e;ecuted b" the brow!erM! Ja9a )irtual Machine (J)M#0 Bor information and e;am<le! on how to include an a<<let in an 3TML <a/eA refer to thi! de!cri<tion of the ZA22L&T[ ta/0 Ja9a 2lu/-in !oftware enable! enter<ri!e cu!tomer! to direct a<<let! or bean! written in the Ja9a <ro/rammin/ lan/ua/e on their intranet web <a/e! to run u!in/ $unM! Ja9a .untime &n9ironment (J.&#A in!tead of the brow!erM! default0 Thi! enable! an enter<ri!e to de<lo" a<<let! that ta>e full ad9anta/e of the late!t ca<abilite! and feature! of the Ja9a <latform and be a!!ured that the" will run reliabl" and con!i!tentl"0 A..L T LI? 0Y0L / InitialiCation +tate/ A<<let enter! the initiali@ation !tate when it i! fir!t loaded0 Thi! i! achie9ed b" callin/ the init(# method of A<<let cla!!0 'reate ob8ect! needed b" the a<<let0 $et u< initial 9alue!0 Load ima/e! or font!0 $et u< color!0
,=

1unnin$ +tate/ A<<let enter! the runnin/ !tate when the !"!tem call! the !tart(# method of A<<let cla!!0 Thi! occur! automaticall" after the a<<let i! initiali@ed0 $tartin/ can al!o occur if the a<<let i! alread" in O!to<<edP (idle# !tate0 Idle or -to##ed -tate/ An a<<let become! idle when it i! !to<<ed from runnin/0 $to<<in/ occur! automaticall" when we lea9e the <a/e containin/ the currentl" runnin/ a<<let0 6e can callin/ the !to< (# method e;<licitl"0 !ead -tate/ An a<<let i! !aid to be dead when it i! remo9ed from memor"0 Thi! occur! automaticall" b" in9o>in/ the de!tro"(# method when we Cuit the brow!er0 !i+#la% -tate/ A<<let mo9e! to the di!<la" !tate whene9er it ha! to <erform !ome out<ut o<eration on the !creen0 Thi! ha<<en!
:0

immediatel" after the a<<let enter! into the runnin/ !tate0


2egin .loa% A++le*) Born ini*iali3a*ion

S*ar*.) S*o+.) Running

1i"+la)
Pain*.)

Idle
S*ar*.)

"*o++e%

1e"*ro).)

1e"*ro)e%

Dead

Exit of Browser

A++le* "*a*e *ran"i*ion %iagra/

5. @#eriment no .5 / 3rite a .ro$ram to find -im#le Intere+t. 5. AIM/

:5

6rite a 8a9a <ro/ram to find the !im<le intere!t0 2. Al$orithm/ 50$tart the <ro/ram0 20+eclare the 9ariable! and a!!ume 9alue! -0Intere!tTo<a"S<rinci<leKtimeKrateG500J 70<rint the 9alue!0 ,0&nd of cla!! and main method0 :0!to< the <ro/ram0 6. .1"21AM( im<ort 8a9a0io0KJ im<ort 8a9a0lan/0KJ cla!! $im<leintere!t V <ublic !tatic 9oid main($trin/ ar/!WX# V int <rinci<leS2,000J float rate S 520,fJ double intere!tTo2a"Atime S 207,J intere!tTo2a"S<rinci<leKtimeKrateG500J
:2

$"!tem0out0<rintln(Q2rinci<le amount i! .!0QH<rinci<leH Qintere!tS.!0QHintere!tTo2a"#J $"!tem0out0<rintln (QTotal amount to <a" to clear the loan S .!0QH (<rinci<leHintere!tTo2a"##J Y Y 7. "*T.*T/ 2rinci<le amount i! .!02,000 intere!t S .!08,=-07, Total amount to <a" to clear the loan S .!0--,=-07,

:-

2. @#eriment no .2 / .ro$ram To ?ind The Arthematic "#eration+ 5. AIM/ 6rite a 8a9a <ro/ram to find the /i9en number! of arithmetic o<eration!0 2. Al$orithm/ 50$tart the <ro/ram0 20+eclare the 9ariable! a and b0 -0.ead a !trin/ with in<ut!tream.eader($"!tem0in#0 70con9ert the !trin/ into Inte/er0<ar!eInt(!tdin0readLine(##J ,01i9en 9ariable! additionA !ubtractionA multi<licationA di9i!ion0 :0&nd of cla!! and main method0 70!to< the <ro/ram0 6. .1"21AM( im<ort 8a9a0io0KJ cla!! arthematic V <ublic !tatic 9oid main($trin/ !WX# throw! I*&;ce<tion
:7

V int aAbAaddA!ubAmulAdi9J %uffered.eader !tdinSnew %uffered.eader(new In<ut$tream.eader($"!tem0in##J $"!tem0out0<rintln(Q&nter Bir!t Number(Q#J aSInte/er0<ar!eInt(!tdin0readLine(##J $"!tem0out0<rintln(Q&nter $econd Number(Q#J bSInte/er0<ar!eInt(!tdin0readLine(##J addSaHbJ !ubSa-bJ mulSaKbJ di9SaGbJ $"!tem0out0<rintln(QAddition of a and b S QHadd#J $"!tem0out0<rintln(Q$ubtraction of a and b S QH!ub#J $"!tem0out0<rintln(QMulti<lication of a and b S QHmul#J $"!tem0out0<rintln(Q+i9i!ion of a and b S QHdi9#J Y Y 70 * T2 T( &nter fir!t number( ,
:,

&nter !econd number( Addition of a and b S 8 $ubtraction of a and bS2 Multi<lication of a and bS5, +i9i!ion of a and bS500 6. @#eriment no .6 / .ro$ram To ?ind The -in$le !i$it Number 5. AIM/ 6rite a 8a9a <ro/ram to find the /i9en !in/le di/it number u!in/ !witch ca!e0 2. Al$orithm/ 50$tart the <ro/ram0 20.ead a !trin/ with in<ut!tream.eader($"!tem0in#0 -0con9ert the !trin/ into Inte/er0<ar!eInt(!tdin0readLine(##J 70%" u!in/ !witch ca!e ( multi wa" deci!ion !tatement# when a match i! foundA that ca!e i! e;ecuted0 ,0+efault it i! a brea> !tatement e;it the !witch !tatement0 :0$to< the <ro/ram0
::

6. .1"21AM( im<ort 8a9a0io0KJ cla!! di/it V <ublic !tatic 9oid main($trin/ !WX# throw! I*&;ce<tion V int nJ %uffered.eader !tdinSnew %uffered.eader(new In<ut$tream.eader($"!tem0in##J $"!tem0out0<rintln(Q&nter An" <o!iti9e !in/le di/it number (Q#J nSInte/er0<ar!eInt(!tdin0readLine(##J !witch(n# V ca!e 0( $"!tem0out0<rintln(Q\eroQ#J brea>J ca!e 5( $"!tem0out0<rintln(Q*neQ#J brea>J ca!e 2( $"!tem0out0<rintln(QTwoQ#J brea>J
:7

ca!e -( $"!tem0out0<rintln(QThreeQ#J brea>J ca!e 7( $"!tem0out0<rintln(QBourQ#J brea>J ca!e ,( $"!tem0out0<rintln(QBi9eQ#J brea>J ca!e :( $"!tem0out0<rintln(Q$i;Q#J brea>J ca!e 7( $"!tem0out0<rintln(Q$e9enQ#J brea>J ca!e 8( $"!tem0out0<rintln(Q&i/htQ#J brea>J ca!e =( $"!tem0out0<rintln(QNineQ#J brea>J default( $"!tem0out0<rintln(QIn9alid NumberQ#J brea>J Y
:8

Y Y 6. "*T.*T/ &nter an" <o!iti9e !in/le di/it number( : $i; &nter an" <o!iti9e !in/le di/it number( , Bi9e

:=

7. @#eriment no .7 / .ro$ram To ?ind The ?actorial "f A Number 5. AIM/ 6rite a 8a9a <ro/ram to find the /i9en factorial number!0 2. Al$orithm/ 50$tart the <ro/ram0 Im<ort the <ac>a/e!0 20.ead a !trin/ with in<ut!tream.eader($"!tem0in#0 -0con9ert the !trin/ into Inte/er0<ar!eInt(!tdin0readLine(##J 70%" u!in/ for loo< rotatin/ the inte/er 9alue0 ,0.e<eat enter the 9alue until end of loo<0 :0&nd of cla!! and main method0 70!to< the <ro/ram0 6. .1"21AM( im<ort 8a9a0io0KJ GGim<ortin/ io <ac>a/e im<ort 8a9a0lan/0KJ GGim<ortin/ lan/ <ac>a/e cla!! Bactorial V
70

<ublic !tatic 9oid main($trin/ ar/!WX# throw! I*&;ce<tion V int iAnAfS5J $"!tem0out0<rintln(Q&nter the numbert "ou want to calculate the factorialQ#J %uffered.eader !tdinSnew %uffered.eader(new In<ut$tream.eader($"!tem0in##J nSInte/er0<ar!eInt(!tdin0readLine(##J for(iS5JiZSnJiHH# V fSfKiJ Y $"!tem0out0<rintln(QThe factorial of Q H n H Q i! Q H f#J Y GG&nd of main Y GG&nd of cla!! Bactorial 7. "*T.*T &nter the number for which "ou want the factorial 7
75

The factorial of 7 i! 27 &nter the number for which "ou want the factorial The factorial of - i! : &nter the number for which "ou want the factorial : The factorial of : i! 520

72

8. @#eriment no. 8/ .ro$ram to chec4 (hether the fir+t number i+ a multi#le of +econd number. 5. AIM/ To chec> whether the fir!t number i! a multi<le of !econd number0 2. Al$orithm/ 50$tart the <ro/ramA im<ort the <ac>a/e!0 20'reate a cla!! and 9ariable! with data t"<e!0 -0.ead a !trin/ with in<ut!tream.eader($"!tem0in#0 70con9ert the !trin/ into Inte/er0<ar!eInt(!tdin0readLine(##J ,0%" u!in/ ifelse loo< rotatin/ the !trin/0 :02rint the concatenation of arra"!0 70$to< the <ro/ram0 6. .1"21AM/ im<ort 8a9a0io0KJ <ac>a/e cla!! Multi<le V GGIm<ortin/ io

7-

<ublic !tatic 9oid main($trin/ ar/!WX# throw! I*&;ce<tion V int mAnJ %uffered.eader !tdinSnew %uffered.eader(new In<ut$tream.eader($"!tem0in##J $"!tem0out0<rintln(Q&nter the fir!t numberQ #J mSInte/er0<ar!eInt(!tdin0readLine(##J $"!tem0out0<rintln(Q&nter the !econd number Q#J nSInte/er0<ar!eInt(!tdin0readLine(##J if ( mLnSS0# V $"!tem0out0<rintln(QThe fir!t number i! the multi<le of !econd numberQ #J Y el!e V $"!tem0out0<rintln(QThe fir!t number i! not the multi<le of !econd numberQ #J Y Y GG&nd of main Y GG&nd of cla!! Multi<le
77

7. "*T.*T &nter the fir!t number 50 &nter the !econd number , The fir!t number i! the multi<le of !econd number &nter the fir!t number 2 &nter the !econd number The fir!t number i! not the multi<le of !econd number 9. @#eriment no. 9/ .ro$ram to chec4 the $i>en arra% i+ +ortin$ order 5. AIM/ 6rite a 8a9a <ro/ram to chec> /i9en number! in a !ortin/ order0 2. Al$orithm/ 50$tart the <ro/ram0 20'reate a cla!! and 9ariable! with data t"<e!0 -0.ead a !trin/ with +atain<ut!tream.eader($"!tem0in#0 70con9ert the !trin/ into Inte/er0<ar!eInt(!tdin0readLine(##J
7,

,0%" u!in/ for loo< rotatin/ the arra"0 :02rint the concatenation of arra"!0 70$to< the <ro/ram0 6. .1"21AM/ im<ort 8a9a0io0KJ cla!! !ortin/ V <ublic !tatic 9oid main($trin/ !WX# throw! I*&;ce<tion V int aWXSnew intW50XJ int iA8J +ataIn<ut$tream !tdinSnew +ataIn<ut$tream($"!tem0in#J $"!tem0out0<rintln(Q&nter 50 &lement! into Arra"Q#J for(iS0JiZ50JiHH# aWiXSInte/er0<ar!eInt(!tdin0readLine(##J for(iS0JiZ=JiHH# for(8S0J8Z=-iJ8HH# V if (aW8H5XZaW8X# V
7:

int tem<SaW8H5XJ aW8H5XSaW8XJ aW8XStem<J Y Y $"!tem0out0<rintln(Q.eCuired *rder i! Q#J for(iS0JiZ50JiHH# $"!tem0out0<rintln(aWiX#J Y Y 7. "*T.*T/ &nter 50 element! into Arra"(50 = 8 7 : , 7 - 2 5 .eCuired order i!(5 2 - 7 , : 7 8 = 50 7. @#eriment no. 7/ .ro$ram To $enerate the A1M-T1"N2 number 5. AIM/ 6rite a 8a9a <ro/ram to /enerate the Arm!tron/ number0 2. Al$orithm/ 50$tart the <ro/ram0
77

20.ead a !trin/ with +atain<ut!tream.eader($"!tem0in#0 -0con9ert the !trin/ into Inte/er0<ar!eInt(!tdin0readLine(##J 70!umS!umHrKrKr formula0 ,0 !in/ if el!e !tatement0 :0$to< the <ro/ram0 6. .1"21AM/ im<ort 8a9a0io0KJ cla!! arm!tron/ V <ublic !tatic 9oid main($trin/ !WX# throw! I*&;ce<tion V int nArAtem<A!umJ +ataIn<ut$tream !tdinSnew +ataIn<ut$tream($"!tem0in#J $"!tem0out0<rintln(Q&nter an" 2o!iti9e Inte/er Number (Q#J nSInte/er0<ar!eInt(!tdin0readLine(##J tem<SnJ !umS0J while(tem<[0# V
78

rStem< L 50J !umS!umHrKrKrJ tem<Stem<G50J Y if (nSS!um# $"!tem0out0<rintln(Q1i9en Number i! Arm!tron/ NumberQ#J el!e $"!tem0out0<rintln(Q1i9en Number i! not Arm!tron/ NumberQ#J Y Y 7. "*T.*T/ &nter an" <o!iti9e Inte/er number( 5,1i9en number i! Arm!tron/ number0 &nter an" <o!iti9e Inte/er number( 57: 1i9en number i! not Arm!tron/ number0 8. @#eriment no. 8/ .ro$ram To $enerate the Duadratic eEuation 5. AIM/ 6rite a 8a9a <ro/ram to /enerate the Cuadratic eCuation0 2. Al$orithm/
7=

50$tart the <ro/ram0 Im<ort the <ac>a/e!0 20'reate a cla!! and 9ariable! with data t"<e!0 -0+eclaration of the main cla!!0 70.ead a !trin/ with in<ut!tream.eader($"!tem0in#0 ,0con9ert the !trin/ into Inte/er0<ar!eInt(!tdin0readLine(##J :0%" u!in/ if(dSS0# O.oot! are &CualentP loo< rotatin/ the inte/er 9alue0 70if(d[0# O.oot! are .ealP otherwi!e (Q.oot! are Ima/inar"Q#J 80.e<eat! enter the 9alue until end of loo<0 =0&nd of cla!! and main method0 500 $to< the <ro/ram0 6. .1"21AM/ im<ort 8a9a0io0KJ im<ort 8a9a0math0KJ cla!! Cuadratic V <ublic !tatic 9oid main($trin/ !WX# throw! I*&;ce<tion V int aAbAcAdJ
80

double r5Ar2J %uffered.eader !tdinSnew %uffered.eader(new In<ut$tream.eader($"!tem0in##J $"!tem0out0<rintln(Q&nter 9alue of a(Q#J aSInte/er0<ar!eInt(!tdin0readLine(##J $"!tem0out0<rintln(Q&nter 9alue of b(Q#J bSInte/er0<ar!eInt(!tdin0readLine(##J $"!tem0out0<rintln(Q&nter 9alue of c(Q#J cSInte/er0<ar!eInt(!tdin0readLine(##J dSbKb-7KaKcJ if(dSS0# V $"!tem0out0<rintln(Q.oot! are &CualentQ#J r5S-bG(2Ka#J r2S-bG(2Ka#J $"!tem0out0<rintln(Q.oot5 S QHr5#J $"!tem0out0<rintln(Q.oot2 S QHr2#J Y el!e if (d[0# V $"!tem0out0<rintln(Q.oot! are .ealQ#J r5S(-bHMath0!Crt(d##G(2Ka#J r2S(-b-Math0!Crt(d##G(2Ka#J
85

$"!tem0out0<rintln(Q.oot5 S QHr5#J $"!tem0out0<rintln(Q.oot2 S QHr2#J Y el!e $"!tem0out0<rintln(Q.oot! are Ima/inar"Q#J Y 7. "*T.*T/ &nter 9alue of a(5 &nter 9alue of b(2 &nter 9alue of c(5 .oot! are &Cualent &nter 9alue of a(2 &nter 9alue of b(&nter 9alue of c(2 .oot! are Ima/inar"

82

:. @#eriment no. :/ .ro$ram to ?ind The .rimar% Number+ 5. AIM/ 6rite a 8a9a <ro/ram to find the /i9en <rimar" number!0 2. Al$orithm/ 50$tart the <ro/ram0 Im<ort the <ac>a/e!0 20.ead a !trin/ with in<ut!tream.eader($"!tem0in#0 -0con9ert the !trin/ into Inte/er0<ar!eInt(!tdin0readLine(##J 70%" u!in/ Ne!ted for(# loo< rotatin/ the inte/er 9alue0 ,0.e<eat enter! the 9alue until end of loo<0 :0&nd of cla!! and main method0 70!to< the <ro/ram0 6. .1"21AM( im<ort 8a9a0io0KJ cla!! <rime V <ublic !tatic 9oid main($trin/ !WX# throw! I*&;ce<tion V
8-

int iA8AcAnJ %uffered.eader !tdinSnew %uffered.eader(new In<ut$tream.eader($"!tem0in##J $"!tem0out0<rintln(Q&nter 2o!iti9e 9alue (Q#J nSInte/er0<ar!eInt(!tdin0readLine(##J for (iS5JiZSnJiHH# V for(8S5AcS0J8ZSiJ8HH# V if (iL8SS0# cScH5J Y if(cSS2# $"!tem0out0<rintln(i#J Y Y Y 7. "*T.*T/ &nter <o!iti9e 9alue( 50 5 2 ,
87

7 50. @#eriment no. 50/ .ro$ram to $enerate the ?ibonacci +erie+ 5. AIM/ 6rite a 8a9a <ro/ram to /enerate the Bibonacci !erie!A /i9en number of n 9alue!0 2. Al$orithm/ 50$tart the <ro/ram0 Im<ort the <ac>a/e!0 20'reate a cla!! and 9ariable! with data t"<e!0 -0+eclaration of the main cla!!0 70.ead a !trin/ with in<ut!tream.eader($"!tem0in#0 ,0con9ert the !trin/ into Inte/er0<ar!eInt(!tdin0readLine(##J :0%" u!in/ for loo< rotatin/ the inte/er 9alue0 70.e<eat! enter the 9alue until end of loo<0 80&nd of cla!! and main method0 =0$to< the <ro/ram0 6. .1"21AM/ im<ort 8a9a0io0KJ GGim<ortin/ io <ac>a/e

8,

im<ort 8a9a0lan/0KJ GGim<ortin/ Lan/ <ac>a/e cla!! A V int aAbAcJ A( int f5Aint f2 # V aSf5J bSf2J Y GG&nd of con!tructor A 9oid Beb(# V cSaHbJ $"!tem0out0<rint(QUtQ H c#J aSbJ bScJ Y GG&nd of method Beb Y GG&nd of cla!! A cla!! Bebinocci V <ublic !tatic 9oid main($trin/ ar/!WX# throw! I*&;ce<tion V int nAf-A iJ
8:

A aSnew A(0A5#J $"!tem0out0<rintln(Q&nter how man" number! "ou want in febinoci !erie!Q#J %uffered.eader !tdinSnew %uffered.eader(new In<ut$tream.eader($"!tem0in##J nSInte/er0<ar!eInt(!tdin0readLine(##J $"!tem0out0<rintln(QThe febinocci !erie! i! a! follow!Q#J $"!tem0out0<rint(QUtQ H 0#J $"!tem0out0<rint(QUtQ H 5#J for(iS0JiZ(n-2#JiHH# V a0Beb(#J Y GG&nd of for loo< Y GG&nd of main Y GG&nd of cla!! Bebinocci 7. "*T.*T &nter how man" number! "ou want in febinoci !erie! The febinocci !erie! i! a! follow! 0 5 5 &nter how man" number! "ou want in febinoci !erie! :

87

The febinocci !erie! i! a! follow! 0 5 5 2 - , &nter how man" number! "ou want in febinoci !erie! 50 The febinocci !erie! i! a! follow! 0 5 5 2 , 8 5- 25 -7

88

55. @#eriment no. 55/ .ro$ram to $enerate the +ortin$ order of arra% 5. AIM/ 6rite a 8a9a <ro/ram to /enerate the !ortin/ order of a /i9en number of n 9alue!0 2. Al$orithm/ 50$tart the <ro/ram0 Im<ort the <ac>a/e!0 20'reate a cla!! and 9ariable! with data t"<e!0 -0+eclaration of the main cla!!0 70.ead a !trin/ with in<ut!tream.eader($"!tem0in#0 ,0con9ert the !trin/ into Inte/er0<ar!eInt(!tdin0readLine(##J :0%" u!in/ for loo< rotatin/ the inte/er 9alue0 70$wa<<in/ the 9alue! into a tem<SaWiXJ 80.e<eat! enter the 9alue until end of loo<0 =0&nd of cla!! and main method0 500 $to< the <ro/ram0 6. .1"21AM/ im<ort 8a9a0io0KJ cla!! !ortin/
8=

V <ublic !tatic 9oid main($trin/ !WX# throw! I*&;ce<tion V int aWXSnew intW20XJ int iA8Atem<J %uffered.eader !tdinSnew %uffered.eader(new In<ut$tream.eader($"!tem0in##J $"!tem0out0<rintln(Q&nter 50 inte/er! into arra"Q#J for(iS0JiZ50JiHH# aWiXSInte/er0<ar!eInt(!tdin0readLine(##J for(iS0JiZ50-5JiHH# V for(8S0J8Z50-i-5J8HH# V if (aW8X[aW8H5X# V tem<SaW8XJ aW8XSaW8H5XJ aW8H5XStem<J Y Y Y
=0

$"!tem0out0<rintln(Q.eCuired *rder i! Q#J for(iS0JiZ50JiHH# $"!tem0out0<rintln(aWiX#J Y Y 7. "*T.*T( &nter 50 inte/er! in to arra"( =87:,7-250 .eCuired order i!( 0 5 2 - 7 , : 7 8 =

=5

52. @#eriment no. 52/ .ro$ram to find the 2 lar$e+t number+ of a $i>en arra% 5. AIM/

=2

To find the lar$e+t number of $i>en number+A b% u+in$ arra%+. 2. Al$orithm/ 50$tart the <ro/ramA im<ort the <ac>a/e!0 20'reate a cla!! and 9ariable! with data t"<e!0 -0.ead a !trin/ with in<ut!tream.eader($"!tem0in#0 70con9ert the !trin/ into Inte/er0<ar!eInt(!tdin0readLine(##J ,0%" u!in/ for loo< rotatin/ the !in/le dimen!ional arra"! 9alue0 :0.e<eat! enter the 9alue until end of loo<0 702rint the concatenation of !trin/0 80$to< the <ro/ram0 6. .1"21AM/ im<ort 8a9a0io0KJ io <ac>a/e im<ort 8a9a0lan/0KJ lan/ <ac>a/e cla!! Lar/e!t GGIm<ortin/ GGIm<ortin/

=-

V <ublic !tatic 9oid main($trin/ ar/!WX# throw! I*&;ce<tion V int aWXS new intW50XJ int iA8A>J $"!tem0out0<rintln(Q&nter the number! of the arra"Q#J %uffered.eader !tdinSnew %uffered.eader(new In<ut$tream.eader($"!tem0in##J for(iS0JiZ50JiHH# V aWiXSInte/er0<ar!eInt(!tdin0readLine(##J Y for(iS0JiZ50JiHH# V for(8S(iH5#J8Z50J8HH# V if(aWiX[aW8X# V >SaWiXJ aWiXSaW8XJ aW8XS>J
=7

Y Y loo< Y

GG&nd of if GG&nd of inner for GG&nd of outer for

loo< $"!tem0out0<rintln(QThe fir!t lar/e!t number in the arra" i! Q H aW=X#J $"!tem0out0<rintln(QThe !econd lar/e!t number in the arra" i! Q H aW8X#J Y GG&nd of main Y GG&nd of cla!! Lar/e!t 7. "*T.*T &nter the number! of the arra" 5 2 : - 78 7 8 7, -2 2The fir!t lar/e!t number in the arra" i! 78 The !econd lar/e!t number in the arra" i! 7, &nter the number! of the arra" 52 2- -7 7, ,: :7 :8 78 8= 500 The fir!t lar/e!t number in the arra" i! 500 The !econd lar/e!t number in the arra" i! 8= &nter the number! of the arra" 55 52 5- 57 5, 5: 57 58 5= 20 The fir!t lar/e!t number in the arra" i! 20 The !econd lar/e!t number in the arra" i! 5=
=,

56. @#eriment no. 56/ .ro$ram to find #roductA +um and difference of 2 matice+ 5. AIM/ 6rite a 8a9a <ro/ram to find the !um of the matrice!A <roduct of the matrice! and difference! of matrice!A b" u!in/ two dimen!ional arra"!0 2. Al$orithm/ 50$tart the <ro/ramA im<ort the <ac>a/e!0 20'reate a cla!! and 9ariable! with two dimen!ional arra"!0 -0.ead a !trin/ with in<ut!tream.eader($"!tem0in#0 70con9ert the !trin/ into Inte/er0<ar!eInt(!tdin0readLine(##J ,0%" u!in/ for loo< rotatin/ the two dimen!ional arra"! 9alue0 :0'olumn i! iA row! i! 8 declare in two dimen!ional matrice!0 70.e<eat! enter the 9alue until end of loo<0 802rint the concatenation of arra"!0 =0$to< the <ro/ram0 6. .1"21AM/
=:

im<ort 8a9a0io0KJ GGIm<ortin/ io <ac>a/e cla!! Matri; V <ublic !tatic 9oid main($trin/ ar/!WX# throw! I*&;ce<tion V int aWXWXSnew intW-XW-XJ int bWXWXSnew intW-XW-XJ int !WXWXSnew intW-XW-XJ int dWXWXSnew intW-XW-XJ int <WXWXSnew intW-XW-XJ int iA8J %uffered.eader !tdinSnew %uffered.eader(new In<ut$tream.eader($"!tem0in##J $"!tem0out0<rintln(Q&nter the element! of 5!t matri;Q#J for( iS0JiZ-JiHH# GG.eadin/ the element! of fir!t matri; V for(8S0J8Z-J8HH# V aWiXW8XSInte/er0<ar!eInt(!tdin0readLine(##J Y Y
=7

$"!tem0out0<rintln(Q&nter the element! of the 2nd matri;Q#J for(iS0JiZ-JiHH# GG.eadin/ the element! of !econd matri; V for(8S0J8Z-J8HH# V bWiXW8XSInte/er0<ar!eInt(!tdin0readLine(##J Y Y for(iS0JiZ-JiHH# GG'alculatin/ additon and !ub!traction V for(8S0J8Z-J8HH# V !WiXW8XSaWiXW8XHbWiXW8XJ dWiXW8XSaWiXW8X-bWiXW8XJ Y Y $"!tem0out0<rintln(QThe !um of the matrice! i!Q#J for(iS0JiZ-JiHH# GG'alculatin/ <roduct V for(8S0J8Z-J8HH#
=8

V $"!tem0out0<rint(Q QH!WiXW8X#J Y $"!tem0out0<rintln(#J Y $"!tem0out0<rintln(QThe <roductof the matrice! i!Q#J for(iS0JiZ-JiHH# V for(8S0J8Z-J8HH# V <WiXW8XS0J for(int >S0J>Z-J>HH# V <WiXW8XS<WiXW8XHaWiXW>XKbW>XW8XJ Y $"!tem0out0<rint(Q QH<WiXW8X#J Y $"!tem0out0<rintln(#J Y $"!tem0out0<rintln(QThe difference of the matrice! i!Q#J for(iS0JiZ-JiHH# V for(8S0J8Z-J8HH#
==

V $"!tem0out0<rint(Q QHdWiXW8X#J Y $"!tem0out0<rintln(#J Y Y Y 7. "*T.*T &nter the element! of 5!t matri; 5 2 - 7 5 2 - 7 5 &nter the element! of 2nd matri; 2 - 7 , : 5 2 7 The !um of the matrice! i! - , 7 = 7 , 7 , The <roductof the matrice! i! 58 27 58 57 27 2, 28 -: 20 The difference of the matrice! i! -5 -5 -5 -5 -, 5 5 5 --

500

57. @#eriment no. 57/ .ro$ram To im#lement ab+tract 4e%(ord 5. AIM/ 6rite a 8a9a <ro/ram to im<lement the ab!tract >e"word and method o9erridin/0 2. Al$orithm/ 50$tart the <ro/ram0 20'reate a cla!!e! and 9ariable! with data t"<e!0 -0A!!i/n! the 9alue! of rectan/le and trian/le0 70'reate a ob8ect of rele9ant cla!!A to call the <rocedure0 ,01i9en a formula of area0 :02rint the concatenation of !trin/0 70$to< the <ro/ram0 6. .1"21AM/ ab!tract cla!! Bi/ure V double dim5Adim2J Bi/ure(double aAdouble b# V
505

dim5SaJ dim2SbJ Y ab!tract double area(#J Y cla!! .ectan/le e;tend! Bi/ure V .ectan/le(double aAdouble b# V !u<er(aAb#J Y double area(# V return(dim5Kdim2#J Y Y cla!! Trian/le e;tend! Bi/ure V Trian/le(double aAdouble b# V !u<er(aAb#J Y double area(# V return((dim5Kdim2#G2#J
502

Y Y cla!! Ab!tract V <ublic !tatic 9oid main($trin/ ar/!WX# V .ectan/le rSnew .ectan/le(,A,#J double arSr0area(#J Trian/le tSnew Trian/le(,A,#J double atSr0area(#J $"!tem0out0<rintln(Qarea of rectan/le Q H ar#J $"!tem0out0<rintln(Qarea of trian/le Q H at #J Y Y 7. "*T.*T/ area of rectan/le 2, area of trian/le 520,FG

50-

58. @#eriment no. 58/ .ro$ram to im#lement con+tructor ">erLoadin$ 5. AIM/ 6rite a 8a9a <ro/ram to im<lement the con!tructor and o9erloadin/ method <ro/ram0 2. Al$orithm/ 50$tart the <ro/ram0
507

20'reate a cla!! and 9ariable! with data t"<e!0 -0+eclare the method! in !ame name with different <arameter!0 70'reate a ob8ect to call the <rocedure0 ,02rint the concatenation of 9alue!0 :0$to< the <ro/ram0 6. .1"21AM/ cla!! A V int aS2AbS-AcS7J A(int c5# V aSc5J Y A(int c5Aint c2# V aSc5J bSc2J Y A(int c5Aint c2Aint c-# V aSc5J bSc2J cSc-JJ
50,

Y 9oid !how(# V $"!tem0out0<rintln(QThe 9alue of a i! Q H a#J $"!tem0out0<rintln(QThe 9alue of b i! Q H b#J $"!tem0out0<rintln(QThe 9alue of c i! Q H c#J Y Y cla!! 'on!o9er V <ublic !tatic 9oid main($trin/ ar/!WX# V A aSnew A(:#J A a2Snew A(7A8#J A a5Snew A(=A50A55#J a0!how(#J a50!how(#J a20!how(#J Y Y

50:

7. "*T.*T/ The 9alue of a i! : The 9alue of a i! 7 The 9alue of b i! 8 The 9alue of a i! = The 9alue of b i! 50 The 9alue of c i! 55

507

59. @#eriment no. 59/ .ro$ram To Im#lement !%namic'+u#er cla++, method di+#atch 5. AIM/ 6rite a 8a9a <ro/ram to im<lement the !u<er cla!! and !ub cla!! method0 2. Al$orithm/ 50$tart the <ro/ram0 20'reate a cla!!e! and 9ariable! with data t"<e!0 -0'reate a method! 9oid !how (#0 70'reate an ob8ect of rele9ant cla!!A to call the <rocedure0 ,02rint the concatenation of !trin/0 :0$to< the <ro/ram0 6. .1"21AM/ im<ort 8a9a0io0KJ
508

im<ort 8a9a0lan/0KJ cla!! A V 9oid !how(# V $"!tem0out0<rintln(QYou are in !u<ercla!!Q#J Y Y cla!! % e;tend! A V 9oid !how(# V $"!tem0out0<rintln(QYou are in !ubcla!!Q#J Y Y cla!! +md V <ublic !tatic 9oid main($trin/ ar/!WX# V A aJ GG'reatin/ reference 9ariable % bSnew %(#J GGcreatin/ ob8ect for cla!! % b0!how(#J aSbJ GGA!!i/nin/ ob8ect b to aJ
50=

a0!how(#J Y Y 7. "*T.*T/ You are in !u<ercla!! You are in !ubcla!! 57. @#eriment no. 57/ !eclarin$ and im#lementin$ an interface 5. AIM/ 6rite a 8a9a <ro/ram to im<lement the Interface command on multi<le cla!!0 2. Al$orithm/ 50$tart the <ro/ram0 20'reate a cla!!e! and 9ariable! with data t"<e!0 -0'reate a command of interface( #0 70'reate an ob8ect of rele9ant cla!!A to call the <rocedure0 ,0'alculate the formula in a <ro/ram0 :02rint the concatenation of !trin/0
550

70$to< the <ro/ram0

6. .ro$ram/ interface 2et V 9oid !<ea>(#J 9oid le/!(int ;#J $trin/ mo9e($trin/ how#J double wei/ht(double ;#J Y <ublic cla!! interface+emo im<lement! 2etV <ublic 9oid !<ea>(#V $"!tem0out0<rintln(Q+o/ bra>!Q#J Y <ublic 9oid le/!(int ;#V $"!tem0out0<rintln(Q+o/ ha! /otQ H;HQle/!Q#J Y <ublic $trin/ mo9e($trin/ howNtoN mo9e#V $"!tem0out0<rintln(Q+o/ mo9e! onQHhowNtoNmo9eHQon land#J returnQQJ
555

Y <ublic double wei/ht(double ;#V return ;J Y <ublic !tatic 9oid main(!trin/ ar/!WX#V double wtJ Interface+emo idS new Interface+emo(#J id0!<ea>(#J id0le/!(7#J id0mo9e(Qfour le/!Q#J wtSid0wei/ht(270,8#J $"!tem0out0<rintln(Q+o/ wei/ht!Q Hwt H Q>/!Q#J Y Y 7. "*T.*T( +o/ bar>! +o/ ha! /ot 7 le/! do/ mo9e! on four le/! on land +o/ wei/ht! 270,8 >/!

552

55-

58. @#eriment no. 58/ .ro$ram To Im#lement HInheritanceH 5. AIM/ 6rite a 8a9a <ro/ram to im<lement the Inheritance <ro/ram0 2. Al$orithm/ 50$tart the <ro/ramA im<ort the <ac>a/e!0 20'reate a cla!! and 9ariable! with data t"<e!0 -0+eclare the method! in different name! in Arithmetic o<eration!0 70Ar/ument! /i9e a throw! I*&;ce<tion!0 ,0.ead a !trin/ with in<ut!tream.eader($"!tem0in#0 :0con9ert the !trin/ into Inte/er0<ar!eInt(!tdin0readLine(##J 70'reate a ob8ect to call the <rocedure0 802rint the concatenation of !trin/0 =0$to< the <ro/ram0 6. .1"21AM/

557

im<ort 8a9a0io0KJ im<ort 8a9a0lan/0KJ cla!! Add V int cJ 9oid add(int aAint b# V cSaHbJ $"!tem0out0<rintln(Q.e!ult of addin/ i! QH c#J Y Y cla!! $ub e;tend! Add V 9oid !ub(int aAint b# V cSa-bJ $"!tem0out0<rintln(Q.e!ult of !ubtractin/ i! QH c#J Y Y cla!! Mul e;tend! $ub V 9oid mul(int aAint b# V
55,

cSaKbJ $"!tem0out0<rintln(Q.e!ult of multi<l"in/ i! QH c#J Y Y cla!! Inherit V <ublic !tatic 9oid main($trin/ ar/!WX# throw! I*&;ce<tion V %uffered.eader !tdinSnew %uffered.eader(new In<ut$tream.eader($"!tem0in##J $"!tem0out0<rintln(Q&nter 2 number! to <erform addA!ub and mulQ#J int iSInte/er0<ar!eInt(!tdin0readLine(##J int 8SInte/er0<ar!eInt(!tdin0readLine(##J Mul mSnew Mul(#J m0mul(iA8#J m0add(iA8#J m0!ub(iA8#J Y Y

55:

7. "*T.*T &nter 2 number! to <erform addA!ub and mul 50 20 .e!ult of multi<l"in/ i! -00 .e!ult of addin/ i! -0 .e!ult of !ubtractin/ i! -50 &nter 2 number! to <erform addA!ub and mul , 50 .e!ult of multi<l"in/ i! ,0 .e!ult of addin/ i! 5, .e!ult of !ubtractin/ i! -, &nter 2 number! to <erform addA!ub and mul 2 5 .e!ult of multi<l"in/ i! 2 .e!ult of addin/ i! .e!ult of !ubtractin/ i! 5

557

5:. @#eriment no. 5:/ .ro$ram to +ho( the *+e of Thi+ =e%(ord and 0on+tructor ">erloadin$ 5. AIM/ 6rite a 8a9a <ro/ram to !how the u!e of Thi! 4e"word And 'on!tructor *9erloadin/0 2. Al$orithm/ 50$tart the <ro/ramA im<ort the <ac>a/e!0 20'reate a cla!! and 9ariable! with data t"<e!0 -0+eclare the method! in !ame name with different <arameter!0 70Ar/ument! /i9e a throw! Io&;ce<tion!0 ,0.ead a !trin/ with in<ut!tream.eader($"!tem0in#0

558

:0con9ert the !trin/ into Inte/er0<ar!eInt(!tdin0readLine(##J 70'reate a ob8ect to call the <rocedure0 802rint the concatenation of !trin/0 =0$to< the <ro/ram0 6. .1"21AM/ im<ort 8a9a0io0KJ <ac>a/e im<ort 8a9a0lan/0KJ <ac>a/e cla!! A V int $Cuare( int ; # V int !S;K;J return(!#J Y GG&nd of con!tructor $Cuare with int a! return t"<e float $Cuare( float ; # V float !S;K;J return(!#J
55=

GGim<ortin/ io GGim<ortin/ Lan/

Y GG&nd of con!tructor $Cuare with float a! return t"<e double $Cuare( double ; # V double !S;K;J return(!#J Y GG&nd of con!tructor $Cuare with double a! return t"<e Y GG&nd of cla!! A cla!! Metho9er V <ublic !tatic 9oid main($trin/ ar/!WX# throw! I*&;ce<tion V $"!tem0out0<rintln(Q&nter the inte/er number "ou want to calculate !CuareQ#J %uffered.eader !tdinSnew %uffered.eader(new In<ut$tream.eader($"!tem0in##J int nSInte/er0<ar!eInt(!tdin0readLine(##J $"!tem0out0<rintln(Q&nter the floatin/ <oint number "ou want to calculate !CuareQ#J

520

float mSBloat0<ar!eBloat(!tdin0readLine(##J $"!tem0out0<rintln(Q&nter the double data t"<e number uou want to calculate !CuareQ#J double <S+ouble0<ar!e+ouble(!tdin0readLine(##J A aSnew A(#J int !C5Sa0$Cuare(n#J $"!tem0out0<rintln(QThe !Cuare of inte/er number i! Q H !C5#J A a5Snew A(#J float !C2Sa50$Cuare(m#J $"!tem0out0<rintln(QThe !Cuare of float number i! Q H !C2#J A a2Snew A(#J double !C-Sa20$Cuare(<#J $"!tem0out0<rintln(QThe !Cuare of double number i! Q H !C2#J Y Y 7. "*T.*T/

525

&nter the inte/er number "ou want to calculate !Cuare , &nter the floatin/ <oint number "ou want to calculate !Cuare 20, &nter the double data t"<e number uou want to calculate !Cuare -077 The !Cuare of inte/er number i! 2, The !Cuare of float number i! :02, The !Cuare of double number i! 550,:

20. @#eriment no. 20/ .ro$ram to in>o4e con+tructor+ u+in$ H-u#erH =e%(ord 5. AIM/ 6rite a 8a9a <ro/ram to In9o>e con!tructor!
522

u!in/ Q$u<erQ 4e"word0 2. Al$orithm/ 50$tart the <ro/ram0 20'reate a cla!! and 9ariable! with data t"<e!0 -0+eclare the method of 9oid !how (#J 702rint the concatenation of 9alue!0 ,0$to< the <ro/ram0 6. .1"21AM/ cla!! A V int aAbAcJ A(int d5Aint d2# GG'on!tructor of cla!! A V aSd5J bSd2J Y 9oid !how(# V $"!tem0out0<rintln(QThe 9alue of a i! Q H a#J $"!tem0out0<rintln(QThe 9alue of b i! Q H
52-

b#J Y Y GG&nd of cla!! A cla!! % e;tend! A V %(int d5Aint d2Aint d-# GG'on!tructor of cla!! % V !u<er(d5Ad2#J GG!u<er call! con!tructor A cSd-J Y 9oid !how(# V $"!tem0out0<rintln(QThe 9alue of a i! Q H a#J $"!tem0out0<rintln(QThe 9alue of b i! Q H b#J $"!tem0out0<rintln(QThe 9alue of c i! Q H c#J Y Y GG&nd of cla!! % cla!! $u<er5 V <ublic !tatic 9oid main($trin/ ar/!WX#
527

V A aSnew A(50A20#J GG'reatin/ ob8ect for cla!! A a0!how(#J % bSnew %(50A20A-0#J GG'reatin/ ob8ect for cla!! % b0!how(#J Y GG&nd of main Y GG&nd of cla!! !u<er5 7. "*T.*T/ The 9alue of a i! 50 The 9alue of b i! 20 The 9alue of a i! 50 The 9alue of b i! 20 The 9alue of c i! -0FG

52,

25. @#eriment no. 25/ .ro$ram to +ho( The -e#arate 3indo( multi#lication 5. AIM/ 6rite a 8a9a <ro/ram to !how the !e<arate window of multi<lication of three number!0
52:

2. Al$orithm/ 50$tart the <ro/ramA im<ort the <ac>a/e!0 20'reate a cla!! and 9ariable! with data t"<e!0 -0+eclare the !trin/(# 9alue!0 70J*<tion2ane0!howIn<ut+ialo/ command i! a window0 ,0J*<tion2ane0!howMe!!a/e+ialo/ dialo/ bo;0 :02rint the window! of out <ut0 70$to< the <ro/ram0 6. .1"21AM/ im<ort 8a9a0io0KJ im<ort 8a9a;0!win/0J*<tion2aneJ <ublic cla!! 2roduct V <ublic !tatic 9oid main( $trin/ ar/!WX # V int ;J GG fir!t number int "J GG !econd number int @J GG third number int re!ultJ GG <roduct of number! $trin/ ;)alJ GG fir!t !trin/ in<ut b" u!er
527

$trin/ ")alJ GG !econd !trin/ in<ut b" u!er $trin/ @)alJ GG third !trin/ in<ut b" u!er ;)al S J*<tion2ane0!howIn<ut+ialo/( Q&nter fir!t inte/er(Q #J ")al S J*<tion2ane0!howIn<ut+ialo/( Q&nter !econd inte/er(Q #J @)al S J*<tion2ane0!howIn<ut+ialo/( Q&nter third inte/er(Q #J ; S Inte/er0<ar!eInt( ;)al #J " S Inte/er0<ar!eInt( ")al #J @ S Inte/er0<ar!eInt( @)al #J re!ult S ; K " K @J J*<tion2ane0!howMe!!a/e+ialo/( nullA QThe <roduct i! Q H re!ult #J $"!tem0e;it( 0 #J Y GG end method main Y GG end cla!! 2roduct 7. "*T.*T/

528

52=

22. @#eriment no. 22/ .ro$ram To +ho( The M*LTIT&1 A!IN2 5. AIM/ 6rite a 8a9a <ro/ram to !how the multithreadin/ u!in/ tr" and catch(# method!0 2. Al$orithm/ 50$tart the <ro/ram0 20'reate a cla!! and 9ariable! !er Thread (#0 -0 !in/ tr" catch (# method!0 70Bor loo< e;ecution!0 ,02rint the tr" !tatement! and catch (# !tatement!0 :02rint the concatenation of !trin/0 70$to< the <ro/ram0 6. .1"21AM/ cla!! !erThread e;tend! Thread
5-0

V !erThread(# V !u<er(Q !erThreadQ#J $"!tem0out0<rintln(Qit i! a !erThreadQ#J !tart(#J Y <ublic 9oid main(# V tr" V for(int iS50Ji[0Ji--# V $"!tem0out0<rintln(Q !er9alueQHi#J Thread0!lee<(,00#J Y Y catch(Interru<ted&;ce<tion ie# V $"!tem0out0<rintln(Q !er thread &;ce<tionQ#J Y $"!tem0out0<rintln(Q !er thread com<letedQ#J Y Y <ublic cla!! Thread+emo
5-5

V <ublic !tatic 9oid main($trin/ ar/!WX# V int iJ !erThread utSnew !erThread(#J tr" V for(iS5JiSS2JiHH# V $"!tem0out0<rintln(Qmain Thread 9alue i!Q Hi#J Thread0!lee<(500#J Y Y catch(Interru<ted&;ce<tion ie# V $"!tem0out0<rintln(Qmain thread interreu<tedQ#J Y $"!tem0out0<rintln(Qmain thread com<letedQ#J Y Y 7. "*T.*T/ It i! !erThread Main thread com<leted

5-2

26. @#eriment no. 26/ .ro$ram to im#lement .ac4a$e+ 5. AIM/

5--

6rite a 8a9a <ro/ram to im<lement Arithmetic o<eration! the <ac>a/e!0 2. Al$orithm/ 50$tart the <ro/ram0 20Im<ort the Mathc <ac>a/e!0 -0'reate a cla!!e! and 9ariable! with data t"<e!0 70'reate an ob8ect of rele9ant cla!!A to call the <rocedure0 ,0'reate an ob8ect to call the <ac>a/e!0 :0.eturn arithmetic o<eration!0 702rint the concatenation of !trin/0 80$to< the <ro/ram0 6. .1"21AM/ <ac>a/e MathcJ <ublic cla!! Arithmetic V <ublic int aAbJ <ublic Arithmetic(int <Aint C# V aS<J bSCJ
5-7

Y <ublic int add(# V return(aHb#J Y <ublic int !ub(# V return(a-b#J Y <ublic int mul(# V return(aKb#J Y <ublic int di9(# V return(aGb#J Y Y

im<ort 8a9a0io0KJ im<ort M"<ac>0KJ cla!! !e<ac> V


5-,

<ublic !tatic 9oid main($trin/ ar/!WX# throw! I*&;ce<tion V %uffered.eader !tdinSnew %uffered.eader(new In<ut$tream.eader($"!tem0in##J $"!tem0out0<rintln(Q&nter 2 number! to <erform addA!ub and mulQ#J int iSInte/er0<ar!eInt(!tdin0readLine(##J int 8SInte/er0<ar!eInt(!tdin0readLine(##J Mul mSnew Mul(#J Add!ub aSnew Add!ub(#J a0add(iA8#J m0mul(iA8#J m0add(iA8#J m0!ub(iA8#J Y Y 7. "*T.*T/ &nter 2 number! to <erform addA!ub and mul 50 20 .e!ult of multi<l"in/ i! -00 .e!ult of addin/ i! -0
5-:

.e!ult of !ubtractin/ i! -50 &nter 2 number! to <erform addA!ub and mul , 50 .e!ult of multi<l"in/ i! ,0 .e!ult of addin/ i! 5, .e!ult of !ubtractin/ i! -, &nter 2 number! to <erform addA!ub and mul 2 5 .e!ult of multi<l"in/ i! 2 .e!ult of addin/ i! .e!ult of !ubtractin/ i! 5

27. @#eriment no. 27 / .ro$ram (hich im#ort+ the #ac4a$e Mathc 5. AIM/

5-7

6rite a 8a9a <ro/ram to im<lement Arithmetic o<eration! the <ac>a/e!0 2. Al$orithm/ 50$tart the <ro/ram0 20Im<ort the Mathc <ac>a/e!0 -0'reate a cla!!e! and 9ariable! with data t"<e!0 70'reate an ob8ect of rele9ant cla!!A to call the <rocedure0 ,0'reate an ob8ect to call the <ac>a/e!0 :0.eturn arithmetic o<eration!0 702rint the concatenation of !trin/0 80$to< the <ro/ram0 6. .1"21AM/ im<ort Mathc0KJ <ublic cla!! 2ac>+emo V <ublic !tatic 9oid main($trin/ ar/!WX# V Arithmetic aSnew Arithmetic(50A-0#J $"!tem0out0<rintln(a0add(##J $"!tem0out0<rintln(a0!ub(##J $"!tem0out0<rintln(a0mul(##J
5-8

$"!tem0out0<rintln(a0di9(##J Y Y 7. "*T.*T/ 70 -20 -00 0

28. @#eriment no. 28 / .ro$ram to #rint )AVA I- -IM.L in different +t%le+ and font+ 5. AIM/ 6rite a 8a9a <ro/ram to im<lement the A22L&T 2A'4A1&$ <ro/ram!0 2.Al$orithm/
5-=

50$tart the <ro/ram0 20Im<ort the <ac>a/e! of a<<letAawt0 -0'reate a cla!!e! <ublic 9oid <aint(1ra<hic! /#0 70A!!ume the 9alue! of !trin/A color and font0 ,0/0draw$trin/(# a<<lication of 1 I0 :02rintin/ in the !e<arated A<<let 9iewer window0 70$to< the <ro/ram0 6..1"21AM/ im<ort 8a9a0awt0KJ im<ort 8a9a0a<<let0KJ im<ort 8a9a;0!win/0KJ GKZa<<let codeSQJa9a+emoQ widthS-,0 hei/htS200[ ZGa<<let[KG <ublic cla!! Ja9a+emo e;tend! JA<<let V <ublic 9oid <aint(1ra<hic! /# V

570

Bont f5Snew Bont(QTime!New.omanQABont0%*L+] Bont0ITALI'A20#J /0!etBont(f5#J $trin/ !trSQJA)A I$ $IM2L&QJ /0draw$trin/(!trA50A20#J Bont f2Snew Bont(Q3el9eticaQABont0%*L+A-0#J /0!etBont(f2#J /0!et'olor('olor0.&+#J /0draw$trin/(!trA50A:0#J Bont f-Snew Bont(QTime!New.omanQABont02LAINA70#J /0!etBont(f-#J /0!et'olor('olor01.&&N#J /0draw$trin/(!trA50A500#J Y Y

7. "*T.*T

575

KG

572

29. @#eriment no. 29/ .ro$ram to dra( Line+A 1ectan$le+A 1ounded 1ectan$le+A filled .ol%$on+ and ">al+ 5. AIM/ 6rite a 8a9a <ro/ram to im<lement the A22L&T 2A'4A1&$A draw Line!A .ectan/le!A .ounded .ectan/le!A filled 2ol"/on! <ro/ram!0 2. Al$orithm/ 50$tart the <ro/ram0 20Im<ort the <ac>a/e! of a<<letAawtAawt0e9ent0 -0'reate a cla!!e!( <ublic 9oid <aint(1ra<hic! /#0 70A!!ume the 9alue! of !trin/A color and font0 ,0/0draw$trin/(# a<<lication of 1ra<hical !er Interface0 :02rintin/ in the !e<arated A<<let 9iewer window0 70$to< the <ro/ram0

57-

6. .1"21AM/ im<ort 8a9a0awt0KJ im<ort 8a9a0a<<let0KJ im<ort 8a9a0awt0e9ent0KJ GKZa<<let codeSQ$ha<e+emoQ widthS-00 hei/htS-00[ ZGa<<letKG <ublic cla!! $ha<e+emo e;tend! A<<let V <ublic 9oid <aint(1ra<hic! /# V !etLa"out(new BlowLa"out(##J /0drawLine(50A20A,0A20#J /0draw.ect(20A-0A-0A-0#J /0!et'olor('olor0.&+#J /0fill.ect(20A-0A-0A-0#J /0draw.ound.ect(20A70A,0A70A5,A5,#J /0!et'olor('olor01.&&N#J /0fill.ound.ect(20A70A,0A70A5,A5,#J /0draw*9al(20A5,0A,0A,0#J /0!et'olor('olor0%L &#J /0fill*9al(20A5,0A,0A,0#J Y Y

577

7. "*T.*T/

57,

27. @#eriment no. 27/ .ro$ram to im#lement Action >ent that #erform+ Arithmetic "#eration+ 5. AIM/ 6rite a 8a9a <ro/ram to im<lement the A22L&T 2A'4A1&$A draw e9ent handler! <ro/ram!0 2. Al$orithm/ 50$tart the <ro/ram0
57:

20Im<ort the <ac>a/e! of a<<letA awtA awt0e9ent0 -0'reate a cla!!e!A method!0 70A!!ume the 9alue! of !trin/ Inte/er0<ar!eInt0 ,0while u!in/ if loo<! rotatin/ 9alue!0 :0The e9ent ar/ument! e;ecution0 702rintin/ in the !e<arated A<<let 9iewer window0 80$to< the <ro/ram0 6. .1"21AM/ im<ort 8a9a0awt0KJ im<ort 8a9a0awt0e9ent0KJ im<ort 8a9a;0!win/0KJ <ublic cla!! &9ent+emo e;tend! JBrame im<lement! ActionLi!tener V <ublic JLabel l5Al2Al-J <ublic JTe;tBield t5At2J <ublic J%utton b5Ab2Ab-J &9ent+emo( # V 'ontainer c S /et'ontent2ane( #J c0 !etLa"out(new BlowLa"out(##J
577

l5Snew JLabel(QN M 5Q#J l2 S new JLabel(QN M 2Q#J l- S new JLabel( #J t5 S new JTe;tBield(:#J t2 S new JTe;tBield(:#J b5 S new J%utton(QA++Q#J b2 S new J%utton(Q$ %Q#J b- S new J%utton(QM LQ#J c0add(l5#J c0add(t5#J c0add(l2#J c0add(t2#J c0add(b5#J c0add(b2#J c0add(b-#J c0add(l-#J b50addActionLi!tener(thi!#J b20addActionLi!tener(thi!#J b-0addActionLi!tener(thi!#J !et$i@e(200A200#J !et)i!ible(true#J
578

Y <ublic 9oid action2erformed(Action&9ent ae# V int n5An2An-S0J n5 S Inte/er0<ar!eInt(t50/etTe;t( #0trim(##J n2 S Inte/er0<ar!eInt(t20/etTe;t( #0trim(##J $trin/ !tr S QThe re!ult i! QJ if(ae0/et$ource( # SS b5# n-Sn5 H n2J if(ae0/et$ource( # SS b2# n-S n5-n2J if(ae0/et$ource( # SS b-# n-Sn5Kn2J l-0!etTe;t(!trHQ Q Hn-#J Y <ublic !tatic 9oid main($trin/ ar/!W X # V &9ent+emo e S new &9ent+emo( #J Y Y 7. "*T.*T

57=

28. @#eriment no. 28/ .ro$ram to im#lement Mou+e Li+tener 'Mou+e >ent+, 5. AIM/ 6rite a 8a9a <ro/ram to im<lement the A22L&T 2A'4A1&$A draw Mou!e e9ent handler <ro/ram!0 2. Al$orithm/ 5. $tart the <ro/ram0 2. Im<ort the <ac>a/e! of a<<letA awtA awt0e9ent0 6. 'reate a cla!!e!A method!0 7. Mou!e moment!A mou!e 'lic>edA mou!e 2re!!edA mou!e .elea!edA mou!e &nteredA mou!e &;itedA mou!e +ra//ed e9ent! ar/!0

5,0

8. /0draw$trin/(# a<<lication of 1ra<hical !er Interface0 9. 6hile rotatin/ mou!e e9ent ar/!0 7. The mou!e e9ent ar/ument! e;ecution0 8. 2rintin/ in the !e<arated A<<let 9iewer window0 :. $to< the <ro/ram0 6. .1"21AM/ im<ort 8a9a0awt0KJ im<ort 8a9a0a<<let0KJ im<ort 8a9a0awt0e9ent0KJ GKZa<<let codeSQMou!e+emoQ widthS-00 hei/htS-00[ ZGa<<let[KG <ublic cla!! Mou!e+emo e;tend! A<<let im<lement! Mou!eLi!tenerAMou!eMotionLi!tener V int m;S0J int m"S0J $trin/ m!/SQQJ <ublic 9oid init(# V addMou!eLi!tener(thi!#J
5,5

addMou!eMotionLi!tener(thi!#J Y <ublic 9oid mou!e'lic>ed(Mou!e&9ent me# V m;S20J m"S70J m!/SQMou!e 'lic>edQJ re<aint(#J Y <ublic 9oid mou!e2re!!ed(Mou!e&9ent me# V m;S-0J m"S:0J m!/SQMou!e 2re!!edQJ re<aint(#J Y <ublic 9oid mou!e.elea!ed(Mou!e&9ent me# V m;S-0J m"S:0J m!/SQMou!e .elea!edQJ re<aint(#J Y <ublic 9oid mou!e&ntered(Mou!e&9ent me# V
5,2

m;S70J m"S80J m!/SQMou!e &nteredQJ re<aint(#J Y <ublic 9oid mou!e&;ited(Mou!e&9ent me# V m;S70J m"S80J m!/SQMou!e &;itedQJ re<aint(#J Y <ublic 9oid mou!e+ra//ed(Mou!e&9ent me# V m;Sme0/etE(#J m"Sme0/etY(#J !how$tatu!(Q'urrentl" mou!e dra//edQHm;HQ QHm"#J re<aint(#J Y <ublic 9oid mou!eMo9ed(Mou!e&9ent me# V m;Sme0/etE(#J m"Sme0/etY(#J

5,-

!how$tatu!(Q'urrentl" mou!e i! atQHm;HQ QHm"#J re<aint(#J Y <ublic 9oid <aint(1ra<hic! /# V /0draw$trin/(Q3andlin/ Mou!e &9ent!QA-0A20#J /0draw$trin/(m!/A:0A70#J Y Y 7. "*T.*T

5,7

2:. @#eriment no. 2:/ .ro$ram to im#lement =e%Li+tener '=e% >ent+, 5. AIM/

5,,

6rite a 8a9a <ro/ram to im<lement the A22L&T 2A'4A1&$A draw 4e" e9ent Li!tener! <ro/ram!0 2. Al$orithm/ 5. $tart the <ro/ram0 2. Im<ort the <ac>a/e! of a<<letA awtA awt0e9ent0 6. 'reate a cla!!e!A method!0 7. 4e" moment!A >e" 'lic>edA >e" 2re!!edA >e" .elea!edA >e" &nteredA >e" &;itedA >e" +ra//ed e9ent! ar/!0 8. /0draw$trin/(# a<<lication of 1ra<hical !er Interface0 9. while rotatin/ >e" e9ent ar/!0 7. 2rintin/ in the !e<arated A<<let 9iewer window0 8. $to< the <ro/ram0 6. .1"21AM/ im<ort 8a9a0awt0KJ im<ort 8a9a0a<<let0KJ im<ort 8a9a0awt0e9ent0KJ

5,:

GKZa<<let codeSQ4e"+emoQ widthS-00 hei/htS-00[ ZGa<<let[KG <ublic cla!! 4e"+emo e;tend! A<<let im<lement! 4e"Li!tener V $trin/ m!/SQQJ int ;S50J int "S20J <ublic 9oid init(# V add4e"Li!tener(thi!#J reCue!tBocu!(#J Y <ublic 9oid >e"2re!!ed(4e"&9ent >e# V !how$tatu!(Q>e" <re!!edQ#J int >e"S>e0/et4e"'ode(#J !witch(>e"# V ca!e 4e"&9ent0)4N 2(m!/Sm!/HQZu<[QJ brea>J ca!e 4e"&9ent0)4N$3IBT(m!/Sm!/HQZ!hift[QJ
5,7

brea>J ca!e 4e"&9ent0)4NB5(m!/HSQZB5[QJ brea>J Y re<aint(#J Y <ublic 9oid >e".elea!ed(4e"&9ent >e# V !how$tatu!(Q4e" .elea!edQ#J Y <ublic 9oid >e"T"<ed(4e"&9ent >e# V char chS>e0/et4e"'har(#J m!/Sm!/HchJ !how$tatu!(Q4e" T"<edQ#J Y <ublic 9oid <aint(1ra<hic! /# V /0draw$trin/(m!/A70A70#J Y Y 7. "*T.*T

5,8

60. @#eriment no. 60/ .ro$ram to im#lement BorderLa%out 5. AIM/ 6rite a 8a9a <ro/ram to im<lement the A22L&T 2A'4A1&$A 'reate %order la"out of window0 2. Al$orithm/ 50$tart the <ro/ram0 20Im<ort the <ac>a/e! of a<<letA awtA awt0e9ent0 -0'reate cla!!e! and method!0 70+eclare the %order La"out direction!0
5,=

,0A<<lication of 1ra<hical !er Interface0 :02rintin/ in the !e<arated A<<let 9iewer window0 70$to< the <ro/ram0 6. .1"21AM/ im<ort 8a9a0awt0KJ im<ort 8a9a0awt0e9ent0KJ im<ort 8a9a0a<<let0KJ GKZa<<let code S Q%order+emoQ widthS-00 hei/htS-00[ ZGa<<let[ KG <ublic cla!! %order+emo e;tend! A<<let V <ublic 9oid init( # V !etLa"out(new %orderLa"out( ##J add(new %utton(Q'$& !tudent!Q#A%orderLa"out0N*.T3#J add(new Label(Q!tud"in/ wellQ#A%orderLa"out0$* T3#J add(new %utton(Qri/htQ#A%orderLa"out0&A$T#J add(new %utton(QleftQ#A%orderLa"out06&$T#J
5:0

$trin/ m!/ S QThi! i! the +emo for %orderLa"outQHQdone b" '!e-5 !tudent!QJ add(new Te;tArea(m!/#A%orderLa"out0'&NT&.#J Y Y

7. "*T.*T

5:5

5:2

65. @#eriment no. 65/ .ro$ram to im#lement 1adio Li+tener 5. AIM/ 6rite a 8a9a <ro/ram to im<lement the A22L&T 2A'4A1&$A 'reate .adio button! in a window0 2. Al$orithm/ 50$tart the <ro/ram0 20Im<ort the <ac>a/e! of a<<letA awtA awt0e9ent0 -0'reate cla!!e!A method!0 70+eclare the .adio button! and /i9e 9alue!0 ,0A<<lication of 1ra<hical !er Interface0 :02rintin/ in the !e<arated A<<let 9iewer window0 70$to< the <ro/ram0 6. .1"21AM/ im<ort 8a9a0awt0KJ im<ort 8a9a0awt0e9ent0KJ im<ort 8a9a;0!win/0KJ <ublic cla!! .ad+emo e;tend! JBrame im<lement! ActionLi!tener
5:-

V <ublic JLabel l5J <ublic J.adio%utton r5Ar2Ar-J .ad+emo(# V 'ontainer c S /et'ontent2ane( #J c0!etLa"out(new BlowLa"out( ##J l5Snew JLabel(#J r5 S new J.adio%utton(Q'$&Q#J r2 S new J.adio%utton(Q&'&Q#J r- S new J.adio%utton(Q&&&Q#J %utton1rou< b/ S new %utton1rou<( #J b/0add(r5#J b/0add(r2#J b/0add(r-#J c0add(r5#J c0add(r2#J c0add(r-#J c0add(l5#J r50addActionLi!tener(thi!#J r20addActionLi!tener(thi!#J r-0addActionLi!tener(thi!#J !et$i@e(200A200#J !et)i!ible(true#J Y
5:7

<ublic 9oid action2erformed(Action&9ent ae# V if(ae0/etAction'ommand(#SSQ'$&Q# V l50!etTe;t(QYou !elected '$&Q#J Y if(ae0/etAction'ommand(#SSQ&'&Q# V l50!etTe;t(QYou !elected &'&Q#J Y if(ae0/etAction'ommand(#SSQ&&&Q# V l50!etTe;t(QYou !elected &&&Q#J Y Y <ublic !tatic 9oid main($trin/ ar/!WX# V .ad+emo rdSnew .ad+emo(#J Y Y 7. "*T.*T

5:,

1eference+(
5.

2.

6.

T$e #o/+le*e re0eren#e Ja'a4 J2SE 5*$ e%i*ion4 Her!er* S#$il%4 T5H +u!li"$ing Co/+an) li/i*e%4 Ne, 1el$i6 2ig Ja'a 2n% e%i*ion4 Ca) Hor"*/ann4 Jo$n 7ile) an% Son"6 Ja'a $o, *o +rogra//e4 "i8*$ e%i*ion4 H6561ie*el an% P6J6 1ei*el6
5::

7.

Core 9a'a 24Vol24 A%'an#e% :un%a/en*al" Ca)4 S6 Hor"$/ann an% Gar) Co/ell Se'en*$ e%i*ion Per"on e%i*ion6

5:7

Das könnte Ihnen auch gefallen