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'RXEOH XQDWWULEXWRGLWLSR GRXEOH H
FRVuYLD
ƒ
/HFODVVL ZUDSSHU VRQRVWDWHFRVWUXLWHSHUHVVHUH
LPPXWDELOL DVVXPRQRXQYDORUHDOPRPHQWRGHOOD
FUHD]LRQHHQRQORFDPELDQRPDLSL
ƒ
4XLQGLSHURJQLFODVVHHVLVWHXQFRVWUXWWRUHFKH
SUHQGHFRPHSDUDPHWURXQDYDULDELOHGHOWLSR
LQFDSVXODWRHORPHPRUL]]DQHOORVWDWR
ƒ
(VLVWHSRLXQPHWRGRFKHFRQVHQWHGLOHJJHUHLQPRGR
SURWHWWRLOYDORUHGHOORVWDWR
)RQGDPHQWLGL,QIRUPDWLFD/%
,QWHJHU ² ,SRWHVLGLLPSOHPHQWD]LRQH
ƒ
3RVVLDPRLPPDJLQDUHqXQ·LSRWHVLVHPSOLILFDWDFKH
ODFODVVH ,QWHJHU VLDIDWWDFRVu
FODVV
Integer
{
SULYDWH
SXEOLF
LQW
value;
Integer(int val)
{
value = val;
}
SXEOLF
LQW
intValue()
{
UHWXUQ
value;
}
}
)RQGDPHQWLGL,QIRUPDWLFD/%
,QWHJHU (VHPSLR
ƒ
9HGLDPRXQVHPSOLFHHVHPSLRGLXVRGHOODFODVVH
,QWHJHU
SXEOLFFODVV
EsempioWrapper
{
SXEOLF
VWDWLFYRLG
main(String args[])
{
int x = 35;
,QWHJHUL[
[
QHZ
,QWHJHU[
L[LQW9DOXH
System.out.println("x =" + x);
}
}
)RQGDPHQWLGL,QIRUPDWLFD/%
,QWHJHU ² $OWULPHWRGL
ƒ
,QUHDOWjODFODVVH ,QWHJHUFRPHWXWWHOHFODVVL
ZUDSSHUqSLFRPSOHVVDHPHWWHDGLVSRVL]LRQHPROWL
DOWULPHWRGL
ƒ
$EELDPRLQQDQ]LWXWWRXQVHFRQGRFRVWUXWWRUHFKH
SUHQGHFRPHSDUDPHWURXQDVWULQJDHODFRQYHUWHLQ
XQLQWHURSHUPHPRUL]]DUQHLOYDORUH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EOLF
VWDWLF
LQW
max(LQW a, LQW b)
VWDWLF ORQJ max(ORQJ a, ORQJ b)
VWDWLFIORDW max(IORDW a, IORDW b)
VWDWLF GRXEOH max(GRXEOH a, GRXEOH b)
)RQGDPHQWLGL,QIRUPDWLFD/%
(VHPSLRGLXVRGL 0DWK
ƒ
9HGLDPRXQHVHPSLRGLXWLOL]]RGL 0DWK
ƒ
0DWK qFRQWHQXWDQHOSDFNDJH MDYDODQJ HTXLQGL QRQq
QHFHVVDULRXVDUHLPSRUW
SXEOLFFODVV
EsempioMath
{
SXEOLF
VWDWLFYRLG
main(String args[])
{
GRXEOH
x,y;
x = 5;
\
0DWKORJ[
System.out.println(“Il logaritmo di “+x+” è “+y);
[
0DWK3,
// Pi greco/2
\
0DWKVLQ[
System.out.println(“Il seno di “+x+” è “+y);
}
}
)RQGDPHQWLGL,QIRUPDWLFD/%
0DWKFKHFRV·q3,
ƒ
1HOO·HVHPSLRDSSHQDYLVWRF·qXQ·LVWUX]LRQH
VWUDQD
[
ƒ
0DWK3,
0DWK3,HYLGHQWHPHQWHQRQqXQPHWRGRQRQ
KDOHSDUHQWHVLqXQDWWULEXWRFKHKDFRPH
YDORUH
ƒ
π «
3HUzF·qTXDOFRVDFKHQRQWRUQD
ƒ *OLDWWULEXWLFRVWLWXLVFRQRORVWDWRGL
XQ·LVWDQ]D
ƒ 3HUzDEELDPRGHWWRFKHODFODVVH 0DWK QRQ
JHQHUDLVWDQ]H
ƒ
&KHFRV·q3,"
)RQGDPHQWLGL,QIRUPDWLFD/%
$WWULEXWLGLFODVVH ƒ
3,qLQIDWWLTXDOFRVDGLGLYHUVRGDXQQRUPDOHDWWULEXWR
ƒ
/DVXDGHILQL]LRQHq
SXEOLF
ƒ
VWDWLF
ILQDO
GRXEOH
3,
(·PDUFDWRFRPH VWDWLF HFRPHWDOHKDXQVLJQLILFDWR
VLPLOHDLPHWRGL VWDWLF
ƒ
1RQDSSDUWLHQHDGXQDLVWDQ]DPDDOODFODVVH
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SXEOLF
FODVV
Lotto
{
SULYDWH
SXEOLF
Lotto() {}
VWDWLFLQW
getNum()
{
LQW
n;
n = (int)Math.round(Math.random() * 89)+1;
UHWXUQ n;
}
}
)RQGDPHQWLGL,QIRUPDWLFD/%
(VHPSLRODFODVVH/RWWR 9DULDQWH
ƒ
3URYLDPRDGLQWURGXUUHXQDYDULDQWHDJJLXQJHQGRXQD
VSHFLILFDDTXHOOHJLjIRUPXODWH
ƒ
6LYXROHFKHQRQSRVVDHVVHUHHVWUDWWRORVWHVVR
QXPHURSHUGXHYROWHGLVHJXLWR
ƒ
'REELDPRPHPRUL]]DUHO·XOWLPRQXPHURHVWUDWWRH
WHQHUQHFRQWRDOO·HVWUD]LRQHVXFFHVVLYD
ƒ
&RPHIDFFLDPR"/DFODVVH/RWWRQRQJHQHUDLVWDQ]HH
TXLQGLQRQVLSXzXWLOL]]DUHXQDQRUPDOHYDULDELOH
ƒ
/·XQLFDVROX]LRQHqTXHOODGLXVDUHXQ DWWULEXWRGL
FODVVHFLRqXQDWWULEXWRPDUFDWRFRPH VWDWLF
/RWWR
-ultimo : int
+getNum() : int
)RQGDPHQWLGL,QIRUPDWLFD/%
(VHPSLRODFODVVH/RWWR ² 'HILQL]LRQHFRQYDULDQWH
ƒ
'HILQLDPRTXLQGLODFODVVHLQPRGRGDWHQHUFRQWR
GHOODQXRYDVSHFLILFD
SXEOLF
FODVV
Lotto
{
SULYDWH
VWDWLFLQW
SULYDWH
Lotto() {}
SXEOLF
VWDWLFLQW
ultimo;
getNum()
{
LQW
n;
GR
n = Math.round((float)Math.random() * 89)+1;
ZKLOH (n==ultimo);
ultimo = n;
UHWXUQ n;
}
}
)RQGDPHQWLGL,QIRUPDWLFD/%
&RQFOXVLRQL
ƒ
$EELDPRYLVWRQHOO·HVHPSLRDSSHQDVYROWRFKHLQ
TXDOFKHFDVRJOLDWWULEXWLGLFODVVHTXHOOLPDUFDWL
FRPH VWDWLF SRVVRQRHVVHUHXWLOL
ƒ
6LWUDWWDSHUzVHPSUHGLVLWXD]LRQLSLXWWRVWRSDUWLFRODUL
0$WWHQ]LRQH QRQIDWHYLLQJDQQDUHGDTXHVWLHVHPSL
ƒ
1HLFDVLQRUPDOLJOLDWWULEXWLDSSDUWHQJRQRDOOH
LVWDQ]HHQHFRVWLWXLVFRQRORVWDWR
ƒ
2JQLLVWDQ]DKDLOVXRVWDWRLVXRLDWWULEXWL
VHSDUDWRGDTXHOORGHOOHDOWUHLVWDQ]H
ƒ
/RVWDWRHTXLQGLJOLDWWULEXWLFKHORFRPSRQJRQR
FRPLQFLDDHVLVWHUHTXDQGRYLHQHFUHDWDO·LVWDQ]DH
VPHWWHGLHVLVWHUHTXDQGRLO JDUEDJHFROOHFWRU OD
GLVWUXJJH
)RQGDPHQWLGL,QIRUPDWLFD/%
/RWWRJLXVWR
SXEOLF
FODVV
Lotto
{
SULYDWH
VWDWLFLQW>@
estratti
^`
SULYDWH
VWDWLFLQW
SULYDWH
Lotto() {}
numEstratti = 0
SULYDWH
VWDWLFERROHDQ
exists(LQW m)
{
ERROHDQ
found = false;
(int i=0; i<numEstratti; i++)
found = found || (estratti[i]==m);
UHWXUQ found;
IRU
}
SXEOLF
VWDWLFYRLG
SXEOLF
VWDWLFLQW
reset() { numEstratti=0 }
getNum()
{
LQW
n;
GR
n = Math.round(Math.random() * 89)+1;
(exists(n));
estratti[numEstratti]=n;
numEstratti++;
UHWXUQ n;
ZKLOH
}
}
)RQGDPHQWLGL,QIRUPDWLFD/%