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GHOOD 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]]DUQHLOYDORUHQHOORVWDWR SXEOLF Integer(String s); $EELDPRSRLDOFXQLPHWRGLFKHFRQYHUWRQRLOYDORUH LQWHUQRLQXQDOWURWLSR 3HUHVHPSLR SXEOLF SXEOLF String toString(); GRXEOH doubleValue(); )RQGDPHQWLGL,QIRUPDWLFD/% &ODVVL ZUDSSHU FRPHFODVVLGLVHUYL]LR /HIXQ]LRQD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RODUHXQDIXQ]LRQHPDWHPDWLFD GRYUHLFUHDUHXQ·LVWDQ]DGL 'RXEOH 6LqTXLQGLVFHOWDXQDVWUDGDSLVHPSOLFHHVLVWHXQDFODVVH GHQRPLQDWD 0DWKFKHGHILQLVFH VRORPHWRGLVWDWLFL HRJQLPHWRGR FRUULVSRQGHDGXQDIXQ]LRQHPDWHPDWLFD )RQGDPHQWLGL,QIRUPDWLFD/% ,PHWRGLGL 0DWK 0DWK FRQWLHQHXQJUDQQXPHURGLPHWRGL 7XWWLLPHWRGLVRQRGLFKLDUDWLFRPH SXEOLF VWDWLF 9HGLDPROLSHUFDWHJRULH 3RWHQ]HUDGLFL SRZH VTUW )XQ]LRQLWULJRQRPHWULFKHVLQFRV WDQ« /RJDULWPRQDWXUDOHHGHVSRQHQ]LDOHORJ H[S )XQ]LRQLGLFRQYHUVLRQHGDUHDOLDLQWHULURXQG 9DULHHGHYHQWXDOL DEV PD[« 'LPROWLPHWRGLHVLVWRQRYHUVLRQL RYHUORDGHG SHU JHVWLUHLYDULWLSL3HUHVHPSLR PD[KDGHILQL]LRQL SXEOLF SXEOLF SXEOLF SXEOLF 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, 1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HQHUDUH QXPHULFDVXDOL )RUWXQDWDPHQWHODFODVVH 0DWK PHWWHDGLVSRVL]LRQH XQPHWRGRVWDWLFRFRPHWXWWLJOLDOWULFKHJHQHUD QXPHULUHDOLFDVXDOLFRPSUHVLIUDH SXEOLF 6DUjVXIILFLHQWHVFDODUHLOYDORUHUHVWLWXLWRGD UDQGRP VWDWLFGRXEOHUDQGRP VXOO·LQWHUYDOORHWUDVIRUPDUORLQXQLQWHURXVDQGR XQDOWURPHWRGRGL 0DWKURXQG )RQGDPHQWLGL,QIRUPDWLFD/% (VHPSLRODFODVVH/RWWR ² 'HILQL]LRQH 'HILQLDPRTXLQGLODFODVVH 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/%
© Copyright 2024 ExpyDoc