{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chapter 03 - Introduction to Parameters and Objects

Chapter 03 - Introduction to Parameters and Objects -...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: &KDSWHU ,QWURGXFWLRQ WR 3DUDPHWHUV DQG 2EMHFWV &RS\ULJKW E\ 6WXDUW 5HJHV DQG 0DUW\ 6WHSS 8VLQJ 2EMHFWV 6WULQJ 2EMHFWV 3RLQW 2EMHFWV 5HIHUHQFH 6HPDQWLFV 0XOWLSOH 2EMHFWV 2EMHFWV DV 3DUDPHWHUV WR 0HWKRGV ,QWHUDFWLYH 3URJUDPV 6FDQQHU 2EMHFWV $ 6DPSOH ,QWHUDFWLYH 3URJUDP &DVH 6WXG\ 3URMHFWLOH 7UDMHFWRU\ $Q 8QVWUXFWXUHG 6ROXWLRQ $ 6WUXFWXUHG 6ROXWLRQ 3DUDPHWHUV 7KH 0HFKDQLFV RI 3DUDPHWHUV /LPLWDWLRQV RI 3DUDPHWHUV 0XOWLSOH 3DUDPHWHUV 3DUDPHWHUV 9HUVXV &RQVWDQWV 2YHUORDGLQJ RI 0HWKRGV 0HWKRGV WKDW 5HWXUQ 9DOXHV 7KH 0DWK &ODVV 'HILQLQJ 0HWKRGV WKDW 5HWXUQ 9DOXHV ,QWURGXFWLRQ &KDSWHU LQWURGXFHG WHFKQLTXHV IRU PDQDJLQJ FRPSOH[LW\ LQFOXGLQJ WKH XVH RI FODVV FRQVWDQWV ZKLFK PDNH SURJUDPV PRUH IOH[LEOH 7KLV FKDSWHU H[SORUHV D PRUH SRZHUIXO WHFKQLTXH IRU REWDLQLQJ VXFK IOH[LELOLW\ <RX ZLOO OHDUQ KRZ WR XVH SDUDPHWHUV WR FUHDWH PHWKRGV WKDW VROYH QRW MXVW VLQJOH WDVNV EXW ZKROH IDPLOLHV RI WDVNV &UHDWLQJ VXFK PHWKRGV UHTXLUHV DQ LQVLJKW LQWR SUREOHPV FDOOHG JHQHUDOL]DWLRQ ,W UHTXLUHV ORRNLQJ EH\RQG D VSHFLILF WDVN WR ILQG D PRUH JHQHUDO WDVN IRU ZKLFK WKLV WDVN LV MXVW RQH H[DPSOH 7KH DELOLW\ WR JHQHUDOL]H LV RQH RI WKH PRVW LPSRUWDQW TXDOLWLHV RI D VRIWZDUH HQJLQHHU DQG WKH JHQHUDOL]DWLRQ WHFKQLTXH \RX ZLOO VWXG\ LQ WKLV FKDSWHU LV RQH RI WKH PRVW SRZHUIXO WHFKQLTXHV XVHG E\ SURJUDPPHUV $IWHU H[SORULQJ SDUDPHWHUV WKH FKDSWHU GLVFXVVHV RWKHU LVVXHV DVVRFLDWHG ZLWK PHWKRGV SDUWLFXODUO\ WKH DELOLW\ RI D PHWKRG WR UHWXUQ D YDOXH 7KH FKDSWHU HQGV ZLWK DQ LQWURGXFWLRQ WR XVLQJ REMHFWV 125 3DUDPHWHUV +XPDQV DUH YHU\ JRRG DW OHDUQLQJ QHZ WDVNV ,Q GRLQJ VR ZH RIWHQ JURXS D IDPLO\ RI UHODWHG WDVNV LQWR RQH JHQHUDOL]HG VROXWLRQ )RU H[DPSOH VRPHRQH PLJKW DVN \RX WR WDNH VWHSV IRUZDUG RU VRPHRQH PLJKW DVN \RX WR WDNH VWHSV IRUZDUG 7KHVH DUH GLIIHUHQW WDVNV EXW WKH\ ERWK LQYROYH WDNLQJ D FHUWDLQ QXPEHU RI VWHSV IRUZDUG :H WKLQN RI WKLV DFWLRQ DV D VLQJOH WDVN RI WDNLQJ VWHSV IRUZDUG EXW ZH XQGHUVWDQG WKDW WKH QXPEHU RI VWHSV ZLOO YDU\ IURP RQH WDVN WR DQRWKHU ,Q SURJUDPPLQJ ZH UHIHU WR WKH QXPEHU RI VWHSV DV D SDUDPHWHU WKDW DOORZV \RX WR JHQHUDOL]H WKH WDVN 3DUDPHWHU SDUDPHWHUL]H $Q\ RI D VHW RI FKDUDFWHULVWLFV WKDW GLVWLQJXLVK GLIIHUHQW PHPEHUV RI D IDPLO\ RI WDVNV 7R SDUDPHWHUL]H D WDVN LV WR LGHQWLI\ D VHW RI LWV SDUDPHWHUV )RU D SURJUDPPLQJ H[DPSOH OHW V UHWXUQ WR WKH 'UDZ)LJXUH SURJUDP RI &KDSWHU ,W SHUIRUPV LWV WDVN DGHTXDWHO\ EXW WKHUH DUH VHYHUDO DVSHFWV WKDW FDQ EH LPSURYHG )RU H[DPSOH WKHUH DUH VL[ GLIIHUHQW SODFHV ZKHUH D IRU ORRS ZULWHV RXW VSDFHV 7KLV DSSURDFK LV UHGXQGDQW DQG FDQ EH FRQVROLGDWHG LQWR D VLQJOH PHWKRG WKDW SHUIRUPV DOO VSDFH ZULWLQJ WDVNV (DFK VSDFH ZULWLQJ WDVN UHTXLUHV D GLIIHUHQW QXPEHU RI VSDFHV VR ZH QHHG VRPH ZD\ WR WHOO WKH PHWKRG KRZ PDQ\ VSDFHV WR ZULWH 7KH PHWKRGV ZH KDYH EHHQ ZULWLQJ KDYH D VLPSOH FDOOLQJ PHFKDQLVP ZKHUH ZH VD\ writeSpaces(); <RX FRXOG LPDJLQH VHWWLQJ D YDULDEOH WR D SDUWLFXODU YDOXH EHIRUH WKH PHWKRG LV FDOOHG DV LQ int number = 10; writeSpaces(); 7KHQ WKH PHWKRG FRXOG ORRN DW WKH YDOXH RI WKH YDULDEOH QXPEHU WR VHH KRZ PDQ\ VSDFHV WR ZULWH 8QIRUWXQDWHO\ WKLV DSSURDFK ZRQ W ZRUN 5HFDOO IURP &KDSWHU WKDW VFRSH UXOHV GHWHUPLQH ZKHUH YDULDEOHV FDQ EH DFFHVVHG )ROORZLQJ WKRVH UXOHV WKH YDULDEOH QXPEHU ZRXOG EH D ORFDO YDULDEOH LQ PDLQ WKDW FRXOG QRW EH VHHQ LQVLGH PHWKRG ZULWH6SDFHV :H ZDQW WR EH DEOH WR VRPHKRZ LQFOXGH WKLV YDOXH LQ WKH FDOO VR WKDW LI ZH ZDQW WR JHW VD\ writeSpaces(10); VSDFHV ZH DQG LI ZH ZDQW WR JHW writeSpaces(20); VSDFHV ZH VD\ 3DUDPHWHUV LQ -DYD DOORZ XV WR GR H[DFWO\ WKLV 7KH\ DOORZ XV WR VSHFLI\ RQH RU PRUH SDUDPHWHUV WR WKH PHWKRG 7KH LGHD LV WKDW LQVWHDG RI ZULWLQJ D PHWKRG WKDW SHUIRUPV MXVW RQH YHUVLRQ RI D WDVN ZH ZULWH D PRUH IOH[LEOH YHUVLRQ WKDW VROYHV D IDPLO\ RI UHODWHG WDVNV WKDW DOO GLIIHU E\ RQH RU PRUH SDUDPHWHUV ,Q WKH FDVH RI WKH ZULWH6SDFHV PHWKRG WKH SDUDPHWHU LV WKH QXPEHU RI VSDFHV WR ZULWH 7KLV QXPEHU FKDUDFWHUL]HV WKH GLIIHUHQW VSDFH ZULWLQJ WDVNV 126 7KH IROORZLQJ LV WKH GHILQLWLRQ RI ZULWH6SDFHV ZLWK D SDUDPHWHU IRU WKH QXPEHU RI VSDFHV WR ZULWH public static void writeSpaces(int number) { for (int i = 1; i <= number; i++) { System.out.print(" "); } } 7KH SDUDPHWHU DSSHDUV LQ WKH PHWKRG KHDGHU DIWHU WKH QDPH DQG LQVLGH WKH SDUHQWKHVHV ZH KDYH EHHQ OHDYLQJ HPSW\ 7KLV PHWKRG XVHV D SDUDPHWHU FDOOHG QXPEHU RI W\SH LQW $V LQGLFDWHG DERYH \RX FDQ QR ORQJHU FDOO WKH SDUDPHWHUL]HG PHWKRG E\ XVLQJ MXVW LWV QDPH writeSpaces(); <RX PXVW QRZ VD\ VRPHWKLQJ OLNH writeSpaces(10); :KHQ D FDOO OLNH WKLV LV PDGH WKH YDOXH LV XVHG WR LQLWLDOL]H WKH SDUDPHWHU FDOOHG QXPEHU <RX FDQ WKLQN RI WKLV DV LQIRUPDWLRQ IORZLQJ LQWR WKH PHWKRG IURP WKH FDOO writeSpaces(10); | V +--->--->--->--->--->---+ | V +----------------------------------------------+ | public static void writeSpaces(int number) { | | ... | | } | +----------------------------------------------+ 7KH SDUDPHWHU QXPEHU LV D ORFDO YDULDEOH EXW LW JHWV LWV LQLWLDO YDOXH IURP WKH FDOO *LYHQ WKDW ZH DUH FDOOLQJ WKLV PHWKRG ZLWK WKH YDOXH LW V DV LI ZH KDG LQFOXGHG WKH IROORZLQJ GHFODUDWLRQ DW WKH EHJLQQLQJ RI PHWKRG ZULWH6SDFHV int number = 10; 2I FRXUVH WKLV PHFKDQLVP LV PRUH IOH[LEOH WKDQ D VSHFLILF YDULDEOH GHFODUDWLRQ EHFDXVH ZH FDQ LQVWHDG VD\ writeSpaces(20); DQG LW ZLOO EH DV LI ZH KDG VDLG int number = 20; DW WKH EHJLQQLQJ RI WKH PHWKRG :H FDQ HYHQ XVH DQ LQWHJHU H[SUHVVLRQ IRU WKH FDOO writeSpaces(3 * 4 - 5); ,Q WKLV FDVH -DYD HYDOXDWHV WKH H[SUHVVLRQ WR JHW WKH YDOXH QXPEHU WR 127 DQG WKHQ FDOOV ZULWH6SDFHV LQLWLDOL]LQJ &RPSXWHU VFLHQWLVWV XVH WKH ZRUG SDUDPHWHU OLEHUDOO\ WR PHDQ ERWK ZKDW DSSHDUV LQ WKH PHWKRG KHDGHU WKH IRUPDO SDUDPHWHU DQG ZKDW DSSHDUV LQ WKH PHWKRG FDOO WKH DFWXDO SDUDPHWHU )RUPDO 3DUDPHWHU $ YDULDEOH WKDW DSSHDUV LQVLGH SDUHQWKHVHV LQ WKH KHDGHU RI D PHWKRG WKDW LV XVHG WR JHQHUDOL]H WKH PHWKRG V EHKDYLRU $FWXDO 3DUDPHWHU $ VSHFLILF YDOXH RU H[SUHVVLRQ WKDW DSSHDUV LQVLGH SDUHQWKHVHV LQ D PHWKRG FDOO 7KH WHUP IRUPDO SDUDPHWHU LV QRW YHU\ GHVFULSWLYH RI LWV SXUSRVH $ EHWWHU QDPH ZRXOG EH JHQHUDOL]HG SDUDPHWHU ,Q WKH PHWKRG DERYH QXPEHU LV WKH JHQHUDOL]HG SDUDPHWHU WKDW DSSHDUV LQ WKH PHWKRG GHFODUDWLRQ ,W LV D SODFHKROGHU IRU VRPH XQVSHFLILHG YDOXH 7KH YDOXHV DSSHDULQJ LQ WKH PHWKRG FDOOV DUH WKH DFWXDO SDUDPHWHUV EHFDXVH HDFK FDOO LQGLFDWHV D VSHFLILF WDVN WR SHUIRUP ,Q RWKHU ZRUGV HDFK FDOO SURYLGHV DQ DFWXDO YDOXH WR ILOO WKH SODFHKROGHU 7KH ZRUG DUJXPHQW LV RIWHQ XVHG DV D V\QRQ\P IRU SDUDPHWHU DV LQ 7KHVH DUH WKH DUJXPHQWV , P SDVVLQJ WR WKLV PHWKRG 6RPH SHRSOH SUHIHU WR UHVHUYH WKH ZRUG DUJXPHQW IRU DFWXDO SDUDPHWHUV DQG WKH ZRUG SDUDPHWHU IRU IRUPDO SDUDPHWHUV /HW V ORRN DW DQ H[DPSOH RI KRZ \RX PLJKW XVH WKLV ZULWH6SDFHV PHWKRG 5HPHPEHU WKDW WKH 'UDZ)LJXUH SURJUDP KDG WKH IROORZLQJ PHWKRG FDOOHG GUDZ7RS // This produces the top half of the hourglass figure public static void drawTop() { for (int line = 1; line <= SUB_HEIGHT; line++) { System.out.print("|"); for (int i = 1; i <= (line - 1); i++) { System.out.print(" "); } System.out.print("\\"); for (int i = 1; i <= (2 * SUB_HEIGHT - 2 * line); i++) { System.out.print(" "); } System.out.print("/"); for (int i = 1; i <= (line - 1); i++) { System.out.print(" "); } System.out.println("|"); } } 8VLQJ WKH ZULWH6SDFHV PHWKRG ZH FDQ UHZULWH WKLV DV IROORZV 128 public static void drawTop() { for (int line = 1; line <= SUB_HEIGHT; line++) { System.out.print("|"); writeSpaces(line - 1); System.out.print("\\"); writeSpaces(2 * SUB_HEIGHT - 2 * line); System.out.print("/"); writeSpaces(line - 1); System.out.println("|"); } } 1RWLFH WKDW ZH FDOO ZULWH6SDFHV WKUHH GLIIHUHQW WLPHV VSHFLI\LQJ KRZ PDQ\ VSDFHV ZH ZDQW LQ HDFK FDVH 6LPLODUO\ ZH FRXOG DOVR PRGLI\ WKH GUDZ%RWWRP PHWKRG IURP WKH 'UDZ)LJXUH SURJUDP WR VLPSOLI\ LW 7KH 0HFKDQLFV RI 3DUDPHWHUV :KHQ -DYD H[HFXWHV D FDOO RQ D PHWKRG LW LQLWLDOL]HV WKH SDUDPHWHUV RI WKH PHWKRG )RU HDFK SDUDPHWHU LW ILUVW HYDOXDWHV WKH H[SUHVVLRQ SDVVHG DV WKH DFWXDO SDUDPHWHU DQG XVHV WKH UHVXOW WR LQLWLDOL]H D ORFDO YDULDEOH ZKRVH QDPH LV JLYHQ E\ WKH IRUPDO SDUDPHWHU /HW V XVH DQ H[DPSOH WR XQGHUVWDQG WKLV SURFHVV EHWWHU 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 public class ParameterExample { public static void main(String args) { int spaces1 = 3; int spaces2 = 5; System.out.print("*"); writeSpaces(spaces1); System.out.println("*"); System.out.print("!"); writeSpaces(spaces2); System.out.println("!"); System.out.print("'"); writeSpaces(8); System.out.println("'"); System.out.print("<"); writeSpaces(spaces1 * spaces2 - 5); System.out.println(">"); } // Writes "number" spaces on the current output line to System.out public static void writeSpaces(int number) { for(int i = 1; i <= number; i++) { System.out.print(" "); } } } ,Q WKH ILUVW WZR OLQHV RI PHWKRG PDLQ WKH FRPSXWHU ILQGV LQVWUXFWLRQV WR DOORFDWH DQG LQLWLDOL]H WZR YDULDEOHV 129 +---+ spaces1 | 3 | +---+ +---+ spaces2 | 5 | +---+ 7KH QH[W WKUHH OLQHV RI FRGH System.out.print("*"); writeSpaces(spaces1); System.out.println("*"); SURGXFH DQ RXWSXW OLQH ZLWK VSDFHV ERXQGHG E\ DVWHULVNV RQ HLWKHU VLGH <RX FDQ VHH ZKHUH WKH DVWHULVNV FRPH IURP EXW ORRN DW WKH PHWKRG FDOO WKDW SURGXFHV WKH VSDFHV :KHQ -DYD H[HFXWHV WKH FDOO RQ PHWKRG ZULWH6SDFHV LW PXVW VHW XS LWV SDUDPHWHU 7R VHW XS WKH SDUDPHWHU -DYD ILUVW HYDOXDWHV WKH H[SUHVVLRQ EHLQJ SDVVHG DV WKH DFWXDO SDUDPHWHU 7KH H[SUHVVLRQ LV VLPSO\ WKH YDULDEOH VSDFHV ZKLFK KDV WKH YDOXH 7KHUHIRUH WKH H[SUHVVLRQ HYDOXDWHV WR -DYD XVHV WKLV UHVXOW WR LQLWLDOL]H D ORFDO YDULDEOH FDOOHG QXPEHU 7KH IROORZLQJ GLDJUDP LQGLFDWHV KRZ PHPRU\ ZRXOG ORRN DV ZH HQWHU PHWKRG ZULWH6SDFHV WKH ILUVW WLPH %HFDXVH WKHUH DUH WZR PHWKRGV LQYROYHG PDLQ DQG ZULWH6SDFHV WKH GLDJUDP LQGLFDWHV ZKLFK YDULDEOHV DUH ORFDO WR PDLQ VSDFHV DQG VSDFHV DQG ZKLFK DUH ORFDO WR ZULWH6SDFHV WKH SDUDPHWHU QXPEHU method main +---------------------------------+ | +---+ +---+ | | spaces1 | 3 | spaces2 | 5 | | | +---+ +---+ | +---------------------------------+ method writeSpaces +--------------------+ | +---+ | | number | 3 | | | +---+ | +--------------------+ 7KH QHW HIIHFW RI WKLV SURFHVV LV WKDW PHWKRG ZULWH6SDFHV KDV D ORFDO FRS\ RI WKH YDOXH VWRUHG LQ WKH YDULDEOH VSDFHV IURP WKH PDLQ PHWKRG 7KH SULQWOQ WKDW FRPHV DIWHU WKH FDOO RQ ZULWH6SDFHV SXWV DQ DVWHULVN DW WKH HQG RI WKH OLQH DQG WKHQ FRPSOHWHV WKH OLQH RI RXWSXW /HW V QRZ WUDFH WKH QH[W WKUHH OLQHV RI FRGH System.out.print("!"); writeSpaces(spaces2); System.out.println("!"); :H ILUVW SULQW DQ H[FODPDWLRQ PDUN RQ WKH VHFRQG OLQH RI RXWSXW 7KHQ ZH FDOO ZULWH6SDFHV DJDLQ WKLV WLPH ZLWK WKH YDULDEOH VSDFHV DV LWV DFWXDO SDUDPHWHU 7KH FRPSXWHU HYDOXDWHV WKLV H[SUHVVLRQ REWDLQLQJ WKH UHVXOW 7KLV YDOXH LV XVHG WR LQLWLDOL]H QXPEHU 7KXV WKLV WLPH LW FUHDWHV D FRS\ RI WKH YDOXH VWRUHG LQ WKH YDULDEOH VSDFHV IURP PHWKRG PDLQ method main +---------------------------------+ | +---+ +---+ | | spaces1 | 3 | spaces2 | 5 | | | +---+ +---+ | +---------------------------------+ method writeSpaces +--------------------+ | +---+ | | number | 5 | | | +---+ | +--------------------+ 130 %HFDXVH QXPEHU KDV D GLIIHUHQW YDOXH WKLV WLPH LQVWHDG RI WKH PHWKRG SURGXFHV D GLIIHUHQW QXPEHU RI VSDFHV $IWHU WKH PHWKRG H[HFXWHV WKHG SULQWOQ ILQLVKHV WKH OLQH RI RXWSXW ZLWK D VHFRQG H[FODPDWLRQ PDUN 1H[W ZH FRPH DFURVV WKHVH WKUHH OLQHV RI FRGH System.out.print("'"); writeSpaces(8); System.out.println("'"); 7KLV FRGH ZULWHV D VLQJOH TXRWDWLRQ PDUN DW WKH EHJLQQLQJ RI WKH WKLUG OLQH RI RXWSXW DQG WKHQ FDOOV PHWKRG ZULWH6SDFHV DJDLQ 7KLV WLPH LW XVHV WKH LQWHJHU OLWHUDO DV WKH H[SUHVVLRQ ZKLFK PHDQV LW LQLWLDOL]HV WKH SDUDPHWHU QXPEHU DV D FRS\ RI WKH QXPEHU method main +---------------------------------+ | +---+ +---+ | | spaces1 | 3 | spaces2 | 5 | | | +---+ +---+ | +---------------------------------+ method writeSpaces +--------------------+ | +---+ | | number | 8 | | | +---+ | +--------------------+ $JDLQ WKH PHWKRG ZLOO EHKDYH GLIIHUHQWO\ EHFDXVH RI WKH GLIIHUHQW YDOXH RI QXPEHU ,W SULQWV VSDFHV RQ OLQH DQG ILQLVKHV H[HFXWLQJ 7KHQ WKH SULQWOQ FRPSOHWHV WKH OLQH RI RXWSXW E\ SULQWLQJ DQRWKHU VLQJOH TXRWDWLRQ PDUN DW WKH HQG RI WKH OLQH )LQDOO\ ZH HQFRXQWHU WKHVH OLQHV RI FRGH System.out.print("<"); writeSpaces(spaces1 * spaces2 - 5); System.out.println(">"); 7KH FRGH SULQWV D OHVV WKDQ FKDUDFWHU DW EHJLQQLQJ RI WKH IRXUWK OLQH RI RXWSXW DQG WKHQ PDNHV D FDOO RQ WKH ZULWH6SDFHV PHWKRG 7KLV WLPH WKH DFWXDO SDUDPHWHU LV DQ H[SUHVVLRQ QRW MXVW D YDULDEOH RU OLWHUDO YDOXH 7KDW PHDQV WKDW EHIRUH WKH FDOO LV PDGH WKH FRPSXWHU HYDOXDWHV WKH H[SUHVVLRQ WR GHWHUPLQH LWV YDOXH spaces1 * spaces2 - 5 \----/ \-----/ 3 * 5 - 5 \----------/ 15 - 5 \------------/ 10 7KH FRPSXWHU XVHV WKLV UHVXOW WR LQLWLDOL]H QXPEHU method main +---------------------------------+ | +---+ +---+ | | spaces1 | 3 | spaces2 | 5 | | | +---+ +---+ | +---------------------------------+ method writeSpaces +--------------------+ | +----+ | | number | 10 | | | +----+ | +--------------------+ 131 7KXV QRZ QXPEHU LV D FRS\ RI WKH YDOXH GHVFULEHG E\ WKLV FRPSOH[ H[SUHVVLRQ 7KHUHIRUH WKH WRWDO RXWSXW RI WKLV SURJUDP LV * ! ' < * ! ' > &RPPRQ 3URJUDPPLQJ (UURU &RQIXVLQJ $FWXDO DQG )RUPDO 3DUDPHWHUV 0DQ\ VWXGHQWV JHW XVHG WR VHHLQJ GHFODUDWLRQV RI IRUPDO SDUDPHWHUV DQG PLVWDNHQO\ EHOLHYH WKDW WKLV LV LGHQWLFDO WR WKH V\QWD[ IRU SDVVLQJ DFWXDO SDUDPHWHUV ,W V D FRPPRQ PLVWDNH WR ZULWH WKH W\SH RI D YDULDEOH DV LW V EHLQJ SDVVHG WR D SDUDPHWHU writeSpaces(int spaces1); // this doesn't work 7KLV FRQIXVLRQ LV RIWHQ GXH WR WKH IDFW WKDW SDUDPHWHUV W\SHV DUH ZULWWHQ LQ WKH GHFODUDWLRQ RI WKH PHWKRG OLNH WKLV public static void writeSpaces(int number) 7\SHV PXVW EH ZULWWHQ ZKHQ YDULDEOHV RU SDUDPHWHUV DUH GHFODUHG %XW ZKHQ YDULDEOHV DUH XVHG VXFK DV ZKHQ FDOOLQJ D PHWKRG DQG SDVVLQJ WKHP DV DFWXDO SDUDPHWHUV WKHLU W\SHV DUH QRW ZULWWHQ $FWXDO SDUDPHWHUV DUH QRW GHFODUDWLRQV VR WKHUHIRUH W\SHV VKRXOG QRW EH ZULWWHQ EHIRUH WKHP writeSpaces(spaces1); // much better! /LPLWDWLRQV RI 3DUDPHWHUV :H YH VHHQ WKDW D SDUDPHWHU FDQ EH XVHG WR SURYLGH LQSXW WR D PHWKRG <RX FDQ XVH D SDUDPHWHU WR VHQG D YDOXH LQWR D PHWKRG 8QIRUWXQDWHO\ \RX FDQ W XVH D SDUDPHWHU WR JHW D YDOXH RXW RI D PHWKRG /HW V VHH ZK\ :KHQ D SDUDPHWHU LV VHW XS D ORFDO YDULDEOH LV FUHDWHG DQG LV LQLWLDOL]HG WR WKH YDOXH EHLQJ SDVVHG DV WKH DFWXDO SDUDPHWHU 7KH QHW HIIHFW LV WKDW WKH ORFDO YDULDEOH LV D FRS\ RI WKH YDOXH FRPLQJ IURP WKH RXWVLGH 6LQFH LW LV D ORFDO YDULDEOH LW FDQ W LQIOXHQFH DQ\ YDULDEOHV RXWVLGH WKH PHWKRG &RQVLGHU WKH IROORZLQJ VDPSOH SURJUDP 132 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class ParameterExample2 { public static void main(String args) { int x = 17; doubleNumber(x); System.out.println("x = " + x); System.out.println(); int number = 42; doubleNumber(number); System.out.println("number = " + number); } public static void doubleNumber(int number) { System.out.println("Initial value of number = " + number); number *= 2; System.out.println("Final value of number = " + number); } } 7KLV SURJUDP EHJLQV E\ GHFODULQJ DQG LQLWLDOL]LQJ DQ LQWHJHU YDULDEOH FDOOHG [ ZLWK YDOXH method main +-------------+ | +----+ | | x | 17 | | | +----+ | +-------------+ ,W WKHQ FDOOV WKH PHWKRG GRXEOH1XPEHU SDVVLQJ [ DV D SDUDPHWHU 7KH YDOXH RI [ LV XVHG WR LQLWLDOL]H WKH SDUDPHWHU QXPEHU DV D ORFDO YDULDEOH RI WKH PHWKRG FDOOHG GRXEOH1XPEHU method main +-------------+ | +----+ | | x | 17 | | | +----+ | +-------------+ method doubleNumber +---------------------+ | +----+ | | number | 17 | | | +----+ | +---------------------+ :H WKHQ H[HFXWH WKH VWDWHPHQWV LQVLGH RI GRXEOH1XPEHU ,W EHJLQV E\ SULQWLQJ WKH LQLWLDO YDOXH RI QXPEHU 7KHQ LW GRXEOHV QXPEHU method main +-------------+ | +----+ | | x | 17 | | | +----+ | +-------------+ method doubleNumber +---------------------+ | +----+ | | number | 34 | | | +----+ | +---------------------+ 1RWLFH WKDW WKLV KDV QR HIIHFW RQ WKH YDULDEOH [ 7KH SDUDPHWHU FDOOHG QXPEHU LV D FRS\ RI [ VR HYHQ WKRXJK WKH\ VWDUWHG RXW WKH VDPH WKH FKDQJH WR QXPEHU KDV QR HIIHFW RQ [ 7KHQ ZH UHSRUW WKH QHZ YDOXH RI QXPEHU $W WKLV SRLQW PHWKRG GRXEOH1XPEHU ILQLVKHV H[HFXWLQJ DQG ZH UHWXUQ WR PDLQ 133 method main +-------------+ | +----+ | | x | 17 | | | +----+ | +-------------+ :H UHSRUW WKH YDOXH RI [ ZKLFK LV YDOXH method main +----------------------------+ | +----+ +----+ | | x | 17 | number | 42 | | | +----+ +----+ | +----------------------------+ 7KHQ ZH GHFODUH DQG LQLWLDOL]H D YDULDEOH FDOOHG QXPEHU ZLWK $QG ZH RQFH DJDLQ FDOO GRXEOH1XPEHU WKLV WLPH SDVVLQJ LW WKH YDOXH RI QXPEHU 7KLV LV DQ RGG VLWXDWLRQ EHFDXVH WKH SDUDPHWHU KDV WKH VDPH QDPH DV WKH YDULDEOH LQ PDLQ %XW -DYD GRHVQ W FDUH WKDW WKH\ KDYH WKH VDPH QDPH ,W DOZD\V FUHDWHV D QHZ ORFDO YDULDEOH IRU PHWKRG GRXEOH1XPEHU method main +----------------------------+ | +----+ +----+ | | x | 17 | number | 42 | | | +----+ +----+ | +----------------------------+ method doubleNumber +---------------------+ | +----+ | | number | 42 | | | +----+ | +---------------------+ 6R DW WKLV SRLQW LQ WLPH WKHUH DUH WZR GLIIHUHQW YDULDEOHV FDOOHG QXPEHU RQH LQ HDFK PHWKRG :H WKHQ H[HFXWH WKH VWDWHPHQWV RI GRXEOH1XPEHU :H ILUVW UHSRUW WKH YDOXH RI QXPEHU 7KHQ ZH GRXEOH LW method main +----------------------------+ | +----+ +----+ | | x | 17 | number | 42 | | | +----+ +----+ | +----------------------------+ method doubleNumber +---------------------+ | +----+ | | number | 84 | | | +----+ | +---------------------+ $JDLQ QRWLFH WKDW GRXEOLQJ QXPEHU LQVLGH RI GRXEOH1XPEHU KDV QR HIIHFW RQ WKH RULJLQDO YDULDEOH QXPEHU WKDW LV LQ PHWKRG PDLQ 7KHVH DUH VHSDUDWH YDULDEOHV 7KH PHWKRG WKHQ UHSRUWV WKH QHZ YDOXH RI QXPEHU DQG UHWXUQV WR PDLQ method main +----------------------------+ | +----+ +----+ | | x | 17 | number | 42 | | | +----+ +----+ | +----------------------------+ 7KH SURJUDP WKHQ UHSRUWV WKH YDOXH RI QXPEHU DQG WHUPLQDWHV 6R WKH RYHUDOO RXWSXW IRU WKH SURJUDP LV DV IROORZV 134 Initial value of number = 17 Final value of number = 34 x = 17 Initial value of number = 42 Final value of number = 84 number = 42 7KXV WKH ORFDO PDQLSXODWLRQV RI WKH SDUDPHWHU GR QRW FKDQJH WKHVH YDULDEOHV RXWVLGH WKH PHWKRG 7KH IDFW WKDW YDULDEOHV DUH FRSLHG LV DQ LPSRUWDQW DVSHFW RI SDUDPHWHUV 2Q WKH SRVLWLYH VLGH ZH NQRZ WKDW YDULDEOHV DUH SURWHFWHG IURP FKDQJH EHFDXVH WKH SDUDPHWHU ZLOO EH D FRS\ RI WKH RULJLQDO 2Q WKH QHJDWLYH VLGH LW PHDQV WKDW DOWKRXJK SDUDPHWHUV ZLOO DOORZ XV WR VHQG YDOXHV LQWR D PHWKRG WKH\ ZLOO QRW DOORZ XV WR JHW YDOXHV EDFN RXW RI D PHWKRG 0XOWLSOH 3DUDPHWHUV 6R IDU WKH GLVFXVVLRQ RI SDUDPHWHU V\QWD[ KDV EHHQ LQIRUPDO ,W V DERXW WLPH WKDW ZH ZURWH GRZQ PRUH SUHFLVHO\ ZKDW V\QWD[ ZH DUH XVLQJ WR GHFODUH VWDWLF PHWKRGV ZLWK SDUDPHWHUV public static void <name>(<type> <name>, ..., <type> <name>) { <statement or variable declaration>; <statement or variable declaration>; ... <statement or variable declaration>; } 7KLV WHPSODWH LQGLFDWHV WKDW ZH FDQ GHFODUH DV PDQ\ SDUDPHWHUV DV ZH ZDQW LQVLGH WKH SDUHQWKHVHV WKDW DSSHDU DIWHU WKH QDPH RI D PHWKRG LQ LWV KHDGHU :H XVH FRPPDV WR VHSDUDWH GLIIHUHQW SDUDPHWHUV $V DQ H[DPSOH RI D PHWKRG ZLWK PXOWLSOH SDUDPHWHUV OHW V FRQVLGHU D YDULDWLRQ RI ZULWH6SDFHV ,W LV FRQYHQLHQW WKDW ZH FDQ WHOO LW D GLIIHUHQW QXPEHU RI VSDFHV WR ZULWH EXW LW DOZD\V ZULWHV VSDFHV :KDW LI ZH ZDQW DVWHULVNV RU SHULRGV RU TXHVWLRQ PDUNV" :H FDQ JHQHUDOL]H WKH WDVN HYHQ IXUWKHU E\ KDYLQJ WKH PHWKRG WDNH WZR SDUDPHWHUV ERWK D FKDUDFWHU DQG D QXPEHU RI WLPHV WR ZULWH WKDW FKDUDFWHU public static void writeChars(char ch, int number) { for (int i = 1; i <= number; i++) { System.out.print(ch); } } 7KH FKDUDFWHU WR EH SULQWHG LV D SDUDPHWHU RI W\SH FKDU ZKLFK ZH ZLOO GLVFXVV LQ ...
View Full Document

{[ snackBarMessage ]}