Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java PUT Solution August 2024

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

SecionA

1. De fine Stahe impot oth Suitabls exanla.


Ana Sabhc Jang,such as
access stahe memku
Java
(vognakles and Umethods) class
withet class narme This
Can cods

avody ehhve class name seferne.

Consides a lass MathUhl s ! that


Contons seueral stahc methads
clau Matuis

nt add ( int a, it b)
Rblic stalie
etn atbj

Stahc int subhac (inta, intb) }


Pblic
rehma- b

sahic impo ou Can impot thhe


wth
add and Subhat
methos

Use
hem
diecty
othont
and
into cacs 'MathUhls
imbaa stoe athUhls -add;
imboft stah MatUhls gubat,
Publie class Main
publc stak vod nan

(s3);
S.o-PHn(Addihon lt +

1
biscuss cotch and tna Socks.
Ans) ne elose thu coda
that night throus
sechen

ephen might

Cod that may


exahhan
int result = 1o/0 Ihis lina
thoan

cabh ( Arithiehie Brapion e)


Excephon harey
So-PHn(Enor; ison zerd ),'
Carh Block
bodk and
The cath' tlock a

a
do
ees Lohat to
hare
ercabhon s than. Yn Cam

klocds to hamde erabhors


petmn ttorent achions kse
thh encelphi an thn ;

it Slesu

,Cateh (Arthmehe Erchon e)


"o ut. Pnintnrar; ivision by zore

.
csteh (Exaephen e) 1
(" An eror occd
System-ow: pintln ("An

Sesond
'catch! lok sen genenc
other Cabhon that V
hondlen
ok
Block
Ihe
ophonal and
and tollao tho
cortains code that s

ereaad
ushethe exaho
cammonly sed
cdeanp achom, Sch ay closir les, releay ryoucy a
Lombd

cotdn o
excetions en

final adhoy
out prntln
block)'
Keu Considerahon
Orden ot eohen : code eeci a
Cateh ! and V han to

Use - Calh blocky to


hamdle and
Sken anios

ehere Cxapn mignt ds honal

State lambda Sren


Ans) lamboa exfruien, introducd in Tava&
and in modem
many
IS a concise ano ane
that cbesit
nam and Can arond a a vaslabls
Useh
Lambda
( cQs oitn a SAM)
tmthona and
enaelir more

Ihe Sytax ot a lambda rejan in Tova


of:

oamameten ist
( obhional t
neaded)
An
that achan
A body
lontbcda eruim
aslist"Alica !
nameS =

"chalie "Bat.

names then
lamba Exoralon

rontd Softd nanes.


names out i: Pintn)
hambda reuien ede
Condse ’
mor readabl and
boilenlat ode
maintaùnakle
tnciona rognammi Tnbrove d API,
Ans
bisaua ieay colle«hin amear
he. Jawa cellechon Frameuo ore
unge avchitechme
rehruerti roide
nd man
ot
H consist
'classes and that allo
bandle collechon dject effient
danlsen
Here's hieranchlcal orenie of t
collechion ameuork; Aava,

Ltetacas
I. colleËon
|.
P

The aot of thu cdlech on


provi des asie on Bich
and ad dij
Tenovin
2. List
? &knds cllechon
Reresents cndend collehen (sgren)
shere elements ane stored n
ofden
3 Set
Extends colechon
"Linkadig!,Veor',Sa
Rebruent collechon that does not all
d_lieate el oort,
Dnklamentabans: HachSet Linkadtaytlreesete.
Treat, Hashet, LnkedList, Arayist, 3.
Abshetuus& tbshratSet, Abshaclist, 2.
Ser
metha
colleion! tnu of mast
collehon Abshact |
Classes.
Hotma mletentasons valic
and
blo matiy ReheLeniy Tntgay: Ma5
b/uo
"LinkalgI
op
'nof
shchion and
estrachon inserh additional
on foceuiy Trovides
-to þnor
haldiny fdesigned
or collechon Rebrtsont
collech
on Estends
Disvs Koy
AsIhe coripreherse
Jamek Jova
arte fechucs and
fationalte thot doelamond, tot
Towa pliaia,
: Core Contine
ncludy Ioc (Tnensen

Contn
The Beantatorg and ytphaenlontat
Beany'. Qnd
mechanlsmy

oterntd
A0P ( Aspet
Aceu / Integaan .
s)ota
7
oRM integahen
TiansachonMaoenen
() Neb:> web MVc
WebFlx
etiy
Searth
Fiomed Achilh
Sechon - B

Inplement nurhime and comp1len ?Hirne


Sutabla eramfle
named methods
fo
AnsFa terent bataio
that bawe
mernkehon) ofect they
th

name
Tolymorphig geans one

Rutime wit the


eec is bound
whenee an Rnnn as guhie
Ahis
achieved by
alo
method
an
clacs est

Public vod mehod U


i so-pn (Method1")

entends Test
publjc clau cfG
Public void metho! )
so PHnMeha 2")

PS V m
(shiny ay)
legt test =

GaFG).
tess method,

irplot
Combile- ime polynobhigrn stererenat antha oco
boumd totits
Comþle time this Rnoun athe
comble -

hme
metho by
th metho
called stahe caly bindy
achieved metho
public chss GFG,
pblie bic nt add i&bntb)
)
metm atb,
Public stie dobla add ( doutle a dolb)
rehm

Shiq ngl3)
So PHn add
add (23) sildwg
so PHn (add (2-9 3-0,
Discugs oole Conghu
beste he Cede
and torth the

Constude

Jana
evelndy
Consttons sheio meheds
/
cbect cf a
thot 'nvokd vshen
Ihe man
class cratd
nihaloze
Koles initiadze
Constvcdors thu
Oobjet Trihaliaion ensire hat
neoly cretad stot hen t s
valid
oijcct ( W

Gccesse,

Automote Invocahon '- co nsrvetors nstonht


Ovokae shen

aomahiaAaesa'
No Reurn
Conshvetos do ot hare a
even yojd? Thes
The name
not
etum lau name
math he
mus
Java constutors
( oroleading hane mulhk
fhe Same
construcdors otthin
have
siterentoomee
(5o), Calado Shape ahapealaaeri ne
be
dffennstvcos obects
(Shoy moin void tahc ublk
sde) (int fhan Area Calalat Pinat
tha calonla Method y
adiug adus
madns) (dotle lole Area,Ealalat dokl prvat
drela aa of ea alelat toMethad y
mea),
"is
Sdk + sida uitn Squarne f Area prtn
( out Ssten
si) shapelalalaa
int ( Pblic
(sid) consructor 7
Ohe) .+ is:
oth drle Area priin
nl out -
caleudstAraGela
(adus) double
vada) dousle calalator( shape
constto Public
class
exn7le, CvEHloac
Conshuctu
ANscuss checked exahhm, Uncheckad eraen an d
in detail
debne eoehlien
Aas cheched xetions : checkad Eabhony -ha
cheskst d -tme Ihs means, that

onilen ènsut tha codo han these exoþho,


esthe tharn torth
that -the mehd
methcd throos
throos the
Ithros'

's&L Bbhon' FleNot found

Cauye checka d exaphon oeprencot entemal


that Cal aftee a erecuhon,
dotabe
Camnechih
ises, ek,
Uncheckad Eethon ( Rntime Bephonj :
tha
th exeOnhon
Dutoond
Nall fointe,buahhon!
A

Callye Gneally . ncheckucd exabhea ndiahe


eos +hat sha

blic class UncheckedExa-ia Examgle /


Pablic siahe vÝd main
Sh shr nol;
S.o Pl n
Cicen (Nuilfainteibajn e)
S-0 P
NallPonton braphen:+
3

t lam.hred Ereefhors. t
CTate d by

fpliao,. These
handl a gefie
erashon extend cite

puli class Irsufident BalanuExabhon ented, Eretion )


patlic sutiaetfalanuExeebtenl)l
Siben (Insoffiert blancs
pukie Tratienl Balana Exceptn |Si
eag)

Custom uncheckad

pubic chs Thvabdgaþhe catends Rfbirebaicn


anot b

public

3
thea and Thread life
ashich shos Tead bricntie

independort
othin Ihay allas Conunret
eKeouhcn
t task
efon snuaneo
Thread Athra d
that an
exeanti d

by Shedlen
head enablo mulhla task to mn
norreny She cPU tine

aod memory

Tbyea
th)s stt its
New Tha thead
the staut () method s
crcahon
1

Onca stant () is calG, the


Ronahla: the mnable tat
thread to
Can
Schduld to
Hery the thread
TVM. ts tosk
thread
Blckad witin IR -thread
mehytoy lock
Dad/Temnata d ha ho cembaled
-teratd ds

ecbh.
ptlie clas
Publie sthe
Ihead thred! Den
Thread(ne Wetkynlbre d),"Tir
7 set pricrtes
thread d2. PoRIT)
thead shtl);
ttread2. stat);
shhe clau Workuih,recd imkkents Rnnable
Public void nlf
(int i = o; i<5,it)?
Sotln Thread cmet hncad () getang
0s

Ihread. Sleoþ (1o0),


{ catch ( ToterfedExcephan e) $
e pöntstack hrul ),'
fncional
exantl!
As) Java a nchinal intefoce
-that Contcuy enoty a absht metn

hndiona intoyou
'erable lanba
and method
chaacdeishc of FL
Abshract Method: A
(0rSAM
tee ahshtt
Ihis methel m contret
methad mefhod agorne
lanbh epreulon Y

-that oill be Used to m7lement

) Dehalt Mehads pnchonal intefacs


methods . shih
detau
Can
ovevmidon
wed
Mnken Anngtabs o ohonal nnotwhn
Can
Sch
add mank
int calolor (int.a, int b);

S.oPh ( s is
methed,):

berehk ot tnciorelTnta
. Lanbda Sspruion Sutpet
Method ene
aana Condseen
Feadabilih
silotle olapl
ndCode and ty iA rsotu wih
Rilercots Jara &prmhs a n) featne

2) Pafenu to ardane metho


latesfete SayqbleS
Becs Snle and Delode

lgiase.
dodis
Tusday. nuwber
is 3 fr name day Week
(I-7):3 humber wWkdoy Entera outpuf:
nuber"; wekday "Dnvalid defaul!->
Saturday" taw7>
Thunday S> Case
wedneddey"; y- Cose
2->iMonday") (an
"Sonday 1> Case
;
Stn
dayunber) (week switchwekdayMame=
'; werdoy por-tl('entera outSystaq
Wekdayae claus pibtie
wtil*) java nport So
umber
912(0) Implem] n Aomist an4 adainy
eltme«fs cf "Soiy pe" to it, ato Iforahy 912

Angist inn totast any iratrr() Me hod


Sol ieport java uhliAsaylist
Tnprt java-utl. Zretor
pubtie cl os Aoaylistexarple
putic tate Doid man (Stng 4pl)

oal'sstin> asylist= ne ();


Arayite>U;
S elme5to tea
oreyis add ("Apple "):
amaylist add ('Banana';
arayist add /"Chery"
arayist. add ( Date
amaist-add i"eldrbey"):

(3t));
onray ist. forEash (St’ Sytug out.ptt

iteratoy <sbiys iTerator = amaylist. iterator ();


stis elmet =itrato netO:
et:"yquu); "Qusue piutta( Sytayout
Lnkefit operatfay QueuDergonstale )
pekelemeat=tack
O;puk iut
dtalk): pop:"+ aftr "Sack pintle ouf Sytom
elomu); mats"
piata("poppud
t euti System
pop() poppetelomat
gat int
fonelmts pop
(3o); push Bfak
(2); push 8ta
(1o); push Stak-
Sfack new sfaekntiger) Stal
<O; I
Dperatag Staek toate Demons
arg) (Sbig mau'n (loid Shahe pubdie
uwDemoZ Stat c| e
as pub'
Ul-; jara pot Sol":
giuet opvatys
of ceypnstpate
all wlich intevface
syoatoag pilu<" puauu aftr poll
:'4guu),
stnis peeke4QuaEltat =quue. pak ();
Stinoutpa~atb"Paked 2lemet +queu);
hpskouwuch
after pe:"

outputi stacle elmeuts : to,2o, 3e]


POppe elemet : 2o
Sjay aftepop : Lle, 20)
peetod eleat8 20
Stale atr e Cio]

Dweotr poll: Fst


Csecnd, Tid)
feekod e lameut : Seleng
Dusue ater pele
lelond,7to]
linkisis)i elmaf 4r(Sbing
ts'");
): posior+ pintea("In
Velid oef Sylas
positiero2nsert); +
i toTwwtt
'at Syebmtut.
emtroDwutt
); (pesifiADTnset ad Alst late
zinkedlist.
ieet) poi0i Jniaty=
044 i(ostfim-T6
(indr) posifn spefd ata lmef Znsef
te
telir); Linkadih+ IihA patlu(" Ssteut.
Cins4"); Lintedlistadd
(Ravi): (t2. Lnkedi
qotdade sthstinked
inklst>
(: linkedbs elemeto
to
nesLinkelst- Some
Linleist<sting Add )
lintedist Crete )
intedistExomple clas pusle
intedist; uhl. jova. fimpt Sal:
brd ter inmts olitorate to(ode Java wotae
Jmploat (o) 93

You might also like