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 PRUH GHWDLO LQ WKH QH[W FKDSWHU 5HFDOO WKDW FKDUDFWHU OLWHUDOV DUH HQFORVHG LQ VLQJOH TXRWDWLRQ PDUNV 7KH V\QWD[ WHPSODWH IRU FDOOLQJ D PHWKRG WKDW DFFHSWV SDUDPHWHUV LV WKH IROORZLQJ <method name>(<expression>, <expression>, ..., <expression>); %\ FDOOLQJ WKLV PHWKRG ZH FDQ ZULWH FRGH OLNH WKH IROORZLQJ 135 writeChars('=', 20); System.out.println(); for(int i = 1; i <= 10; i++) { writeChars('>', i); writeChars(' ', 20 - 2 * i); writeChars('<', i); System.out.println(); } writeChars('=', 20); System.out.println(); ZKLFK SURGXFHV WKH IROORZLQJ RXWSXW ==================== > < >> << >>> <<< >>>> <<<< >>>>> <<<<< >>>>>> <<<<<< >>>>>>> <<<<<<< >>>>>>>> <<<<<<<< >>>>>>>>> <<<<<<<<< >>>>>>>>>><<<<<<<<<< ==================== <RX FDQ LQFOXGH DV PDQ\ SDUDPHWHUV DV \RX ZDQW ZKHQ \RX GHILQH D PHWKRG (DFK PHWKRG FDOO PXVW SURYLGH H[DFWO\ WKDW QXPEHU RI SDUDPHWHUV 7KH\ DUH OLQHG XS VHTXHQWLDOO\ )RU H[DPSOH FRQVLGHU WKH ILUVW FDOO RQ ZULWH&KDUV LQ WKH FRGH IUDJPHQW DERYH YHUVXV WKH KHDGHU IRU ZULWH&KDUV -DYD OLQHV WKHVH WZR XS LQ VHTXHQWLDO RUGHU WKH ILUVW DFWXDO SDUDPHWHU JRLQJ LQWR WKH ILUVW IRUPDO SDUDPHWHU WKH VHFRQG DFWXDO SDUDPHWHU JRLQJ LQWR WKH VHFRQG IRUPDO SDUDPHWHU writeChars('=', 20); | | | V | +--->--->--->--->--->--->---+ V | +--->--->--->--->--->-+ | | | V V +------------------------------------------------------+ | public static void writeChars(char ch, int number) { | | ... | | } | +------------------------------------------------------+ :KHQ ZULWLQJ PHWKRGV WKDW DFFHSW PDQ\ SDUDPHWHUV WKH PHWKRG KHDGHU OLQH FDQ EHFRPH YHU\ ORQJ ,W LV FRPPRQ WR ZUDS ORQJ OLQHV RQHV WKDW H[FHHG URXJKO\ FKDUDFWHUV LQ OHQJWK E\ LQVHUWLQJ D OLQH EUHDN DIWHU DQ RSHUDWRU RU SDUDPHWHU DQG LQGHQWLQJ WKH IROORZLQJ OLQH E\ WZLFH WKH QRUPDO LQGHQWDWLRQ ZLGWK )RU H[DPSOH // This method's header line is too long, so we'll wrap it. public static void printTriangle(int xCoord1, int yCoord1, int xCoord2, int yCoord2, int xCoord3, int yCoord3) { ... } 136 3DUDPHWHUV 9HUVXV &RQVWDQWV +RZ GRHV WKLV QHZ WHFKQLTXH UHODWH WR ZKDW \RX DOUHDG\ NQRZ" :H VDZ LQ &KDSWHU WKDW FODVV FRQVWDQWV DUH D XVHIXO WHFKQLTXH WR LQFUHDVH WKH IOH[LELOLW\ RI RXU SURJUDPV %\ XVLQJ VXFK FRQVWDQWV \RX FDQ PDNH LW HDV\ WR PRGLI\ D SURJUDP WR EHKDYH GLIIHUHQWO\ 7KH SDUDPHWHU SURYLGHV PXFK RI WKH VDPH IOH[LELOLW\ DQG PRUH &RQVLGHU WKH ZULWH6SDFHV PHWKRG 6XSSRVH \RX ZURWH LW XVLQJ D FODVV FRQVWDQW public static final int NUMBER_OF_SPACES = 10; 7KLV JLYHV \RX WKH IOH[LELOLW\ WR SURGXFH D GLIIHUHQW QXPEHU RI VSDFHV EXW KDV RQH PDMRU OLPLWDWLRQ 7KH FRQVWDQW FDQ FKDQJH RQO\ IURP H[HFXWLRQ WR H[HFXWLRQ ,W FDQQRW FKDQJH ZLWKLQ D VLQJOH H[HFXWLRQ ,Q RWKHU ZRUGV \RX FDQ H[HFXWH WKH SURJUDP RQFH ZLWK RQH YDOXH HGLW WKH SURJUDP UHFRPSLOH DQG WKHQ H[HFXWH LW DJDLQ ZLWK D GLIIHUHQW YDOXH %XW \RX FDQ W XVH GLIIHUHQW YDOXHV LQ D VLQJOH H[HFXWLRQ RI WKH SURJUDP XVLQJ D FODVV FRQVWDQW 3DUDPHWHUV DUH PRUH IOH[LEOH %HFDXVH \RX VSHFLI\ WKH YDOXH WR EH XVHG HDFK WLPH \RX FDOO WKH PHWKRG \RX FDQ XVH VHYHUDO GLIIHUHQW YDOXHV LQ D VLQJOH SURJUDP H[HFXWLRQ $V \RX KDYH VHHQ \RX FDQ FDOO WKH PHWKRG PDQ\ GLIIHUHQW WLPHV ZLWKLQ D VLQJOH SURJUDP H[HFXWLRQ DQG KDYH LW EHKDYH GLIIHUHQWO\ HYHU\ WLPH $W WKH VDPH WLPH KRZHYHU WKH SDUDPHWHU LV PRUH ZRUN IRU WKH SURJUDPPHU WKDQ WKH FODVV FRQVWDQW ,W PDNHV \RXU PHWKRG KHDGHUV DQG PHWKRG FDOOV PRUH WHGLRXV QRW WR PHQWLRQ PDNLQJ WKH H[HFXWLRQ DQG WKXV WKH GHEXJJLQJ PRUH FRPSOH[ 7KHUHIRUH \RX ZLOO SUREDEO\ ILQG RFFDVLRQ WR XVH HDFK WHFKQLTXH 7KH EDVLF UXOH LV WR XVH D FODVV FRQVWDQW ZKHQ \RX RQO\ ZDQW WR FKDQJH WKH YDOXH IURP H[HFXWLRQ WR H[HFXWLRQ ,I \RX ZDQW WR XVH GLIIHUHQW YDOXHV ZLWKLQ D VLQJOH H[HFXWLRQ XVH WKH SDUDPHWHU 2YHUORDGLQJ RI 0HWKRGV ,W LV RIWHQ WKH FDVH WKDW ZH ZDQW WR FUHDWH VOLJKW YDULDWLRQV RI WKH VDPH PHWKRG ZLWK GLIIHUHQW SDUDPHWHU SDVVLQJ )RU H[DPSOH ZH PLJKW KDYH D PHWKRG GUDZ%R[ WKDW DOORZV \RX WR VSHFLI\ D SDUWLFXODU KHLJKW DQG ZLGWK EXW \RX PLJKW ZDQW WR DOVR KDYH D YHUVLRQ WKDW GUDZV D ER[ RI GHIDXOW VL]H ,Q RWKHU ZRUGV VRPHWLPHV \RX ZDQW WR VSHFLI\ WKHVH YDOXHV drawBox(8, 10); DQG RWKHU WLPHV \RX ZDQW WR MXVW WHOO LW WR GUDZ D ER[ ZLWK WKH VWDQGDUG KHLJKW DQG ZLGWK drawBox(); 6RPH SURJUDPPLQJ ODQJXDJHV UHTXLUH \RX WR FRPH XS ZLWK GLIIHUHQW QDPHV IRU WKHVH 2QH PLJKW EH FDOOHG GUDZ%R[ DQG WKH RWKHU PLJKW EH FDOOHG GUDZ'HIDXOW%R[ &RPLQJ XS ZLWK QHZ QDPHV IRU HDFK YDULDWLRQ EHFRPHV WHGLRXV )RUWXQDWHO\ -DYD DOORZV XV WR KDYH PRUH WKDQ RQH PHWKRG ZLWK WKH VDPH QDPH DV ORQJ DV WKH\ KDYH GLIIHUHQW SDUDPHWHUV 7KLV SURFHVV LV FDOOHG RYHUORDGLQJ DQG WKH SULPDU\ UHTXLUHPHQW IRU RYHUORDGLQJ LV WKDW WKH GLIIHUHQW PHWKRGV WKDW \RX GHILQH KDYH GLIIHUHQW PHWKRG VLJQDWXUHV 137 0HWKRG 6LJQDWXUH 7KH QDPH RI D PHWKRG DORQJ ZLWK LWV QXPEHU DQG W\SH RI SDUDPHWHUV 0HWKRG 2YHUORDGLQJ 7KH DELOLW\ WR GHILQH WZR RU PRUH GLIIHUHQW PHWKRGV ZLWK WKH VDPH QDPH DV ORQJ DV WKH\ KDYH GLIIHUHQW PHWKRG VLJQDWXUHV )RU WKH GUDZ%R[ H[DPSOH WKH WZR YHUVLRQV ZRXOG FOHDUO\ KDYH GLIIHUHQW PHWKRG VLJQDWXUHV EHFDXVH RQH KDV WZR SDUDPHWHUV DQG WKH RWKHU KDV ]HUR SDUDPHWHUV ,W ZRXOG EH REYLRXV IURP DQ\ FDOO RQ WKH PHWKRG ZKLFK RQH WR XVH ,I \RX VHH WZR SDUDPHWHUV \RX H[HFXWH WKH YHUVLRQ ZLWK WZR SDUDPHWHUV ,I \RX VHH ]HUR SDUDPHWHUV \RX H[HFXWH WKH YHUVLRQ ZLWK ]HUR SDUDPHWHUV ,W JHWV PRUH FRPSOLFDWHG ZKHQ RYHUORDGLQJ LQYROYHV WKH VDPH QXPEHU RI SDUDPHWHUV EXW WKLV WXUQV RXW WR EH RQH RI WKH PRVW XVHIXO DSSOLFDWLRQV RI RYHUORDGLQJ )RU H[DPSOH WKH SULQWOQ PHWKRG LV DFWXDOO\ D VHULHV RI RYHUORDGHG PHWKRGV :H FDQ FDOO SULQWOQ SDVVLQJ LW D 6WULQJ RU SDVVLQJ LW DQ LQW RU SDVVLQJ LW D GRXEOH DQG VR RQ 7KLV IOH[LELOLW\ LV LPSOHPHQWHG DV D VHULHV RI GLIIHUHQW PHWKRGV DOO RI ZKLFK WDNH RQH SDUDPHWHU EXW RQH YHUVLRQ WDNHV D 6WULQJ DQRWKHU YHUVLRQ WDNHV DQ LQW DQRWKHU YHUVLRQ WDNHV D GRXEOH DQG VR RQ 2EYLRXVO\ \RX GR VOLJKWO\ GLIIHUHQW WKLQJV WR SULQW RQH RI WKHVH NLQGV RI GDWD YHUVXV DQRWKHU ZKLFK LV ZK\ LW V XVHIXO WR KDYH WKHVH GLIIHUHQW YHUVLRQV RI WKH PHWKRG 0HWKRGV WKDW 5HWXUQ 9DOXHV :H KDYH EHHQ ORRNLQJ DW DFWLRQ RULHQWHG PHWKRGV WKDW SHUIRUP VRPH VSHFLILF WDVN <RX FDQ WKLQN RI WKHP DV EHLQJ OLNH FRPPDQGV WKDW \RX FRXOG JLYH VRPHRQH DV LQ 'UDZ D ER[ RU 'UDZ D WULDQJOH 3DUDPHWHUV DOORZ WKHVH FRPPDQGV WR EH PRUH IOH[LELOH DV LQ 'UDZ D ER[ WKDW LV E\ :H ZLOO DOVR ZDQW WR EH DEOH WR ZULWH PHWKRGV WKDW FRPSXWH YDOXHV 7KHVH PHWKRGV DUH PRUH OLNH TXHVWLRQV DV LQ :KDW LV WKH VTXDUH URRW RI " RU :KDW GR \RX JHW ZKHQ \RX FDUU\ WR WKH WK SRZHU" )RU H[DPSOH ZH FDQ LPDJLQH KDYLQJ D PHWKRG FDOOHG VTUW WKDW ZRXOG FRPSXWH WKH VTXDUH URRW RI D QXPEHU ,W PLJKW VHHP WKDW WKH ZD\ WR ZULWH VXFK D VTUW PHWKRG ZRXOG EH WR KDYH LW DFFHSW D SDUDPHWHU RI W\SH GRXEOH DQG ZRXOG SULQWOQ LWV VTXDUH URRW WR WKH FRQVROH %XW RIWHQ ZH ZDQW WR XVH WKH VTXDUH URRW DV SDUW RI D ODUJHU H[SUHVVLRQ RU FRPSXWDWLRQ VXFK DV VROYLQJ D TXDGUDWLF HTXDWLRQ RU FRPSXWLQJ GLVWDQFH EHWZHHQ SRLQWV RQ DQ [ \ SODQH $ EHWWHU VROXWLRQ ZRXOG EH D VTXDUH URRW FRPPDQG ZKHUH WKH QXPEHU RI LQWHUHVW LV SDVVHG DV D SDUDPHWHU DQG LWV VTXDUH URRW FRPHV EDFN WR RXU SURJUDP DV D UHVXOW :H FRXOG XVH WKH UHVXOW DV SDUW RI DQ H[SUHVVLRQ VWRUH LW LQWR D YDULDEOH RU SULQW LW RQ WKH FRQVROH 6XFK D FRPPDQG LV D QHZ W\SH RI PHWKRG WKDW LV VDLG WR UHWXUQ D YDOXH 138 5HWXUQ 7R VHQG D YDOXH RXW DV WKH UHVXOW RI D PHWKRG ZKLFK FDQ EH XVHG LQ DQ H[SUHVVLRQ LQ \RXU SURJUDP 9RLG PHWKRGV GR QRW UHWXUQ DQ\ YDOXH ,I ZH KDG VXFK D PHWKRG ZH FRXOG DVN IRU WKH VTXDUH URRW RI // assuming that we had a method named sqrt double answer = sqrt(2.5); E\ ZULWLQJ FRGH OLNH WKLV 7KH VTUW PHWKRG KDV D SDUDPHWHU WKH QXPEHU WR ILQG WKH VTXDUH URRW RI EXW LW DOVR UHWXUQV D YDOXH WKH VTXDUH URRW 7KH DFWXDO SDUDPHWHU JRHV LQWR WKH PHWKRG DQG WKH VTXDUH URRW FRPHV RXW ,Q WKH FRGH DERYH ZH VWRUH WKH UHWXUQHG UHVXOW LQ D YDULDEOH FDOOHG DQVZHU <RX FDQ WHOO ZKHWKHU RU QRW D PHWKRG UHWXUQV D YDOXH E\ ORRNLQJ DW LWV KHDGHU :H KDYH EHHQ ZULWLQJ PHWKRGV WKDW DOO EHJLQ ZLWK public static void DV LQ public static void drawTriangle(int height) 7KH ZRUG void LV NQRZQ DV WKH UHWXUQ W\SH RI WKH PHWKRG public static void drawTriangle(int height) ~~~~ return type 7KH YRLG UHWXUQ W\SH LV D OLWWOH RGG EHFDXVH DV WKH ZRUG LPSOLHV LW PHDQV WKDW WKH PHWKRG UHWXUQV QRWKLQJ ,QVWHDG RI void ZH FDQ XVH DQ\ OHJDO W\SH 6R ZH FDQ ZULWH PHWKRGV WKDW UHWXUQ DQ LQW RU WKDW UHWXUQ D GRXEOH RU DQ\ RWKHU W\SH ,Q WKH FDVH RI WKH PHWKRG WR FRPSXWH VTXDUH URRWV ZH ZDQW LW WR UHWXUQ D GRXEOH VR ZH ZRXOG ZULWH LWV KHDGHU DV IROORZV public static double sqrt(double n) $V LQ WKH SUHYLRXV FDVH WKH ZRUG WKDW FRPHV DIWHU public static LV WKH UHWXUQ W\SH RI WKH PHWKRG public static double sqrt(double n) ~~~~~~ return type )RUWXQDWHO\ ZH GRQ W QHHG WR ZULWH D PHWKRG IRU FRPSXWLQJ WKH VTXDUH URRW RI D QXPEHU EHFDXVH -DYD KDV RQH WKDW LV EXLOW LQ 7KH PHWKRG LV LQFOXGHG LQ D FODVV NQRZQ DV 0DWK WKDW LQFOXGHV PDQ\ XVHIXO FRPSXWLQJ PHWKRGV 6R EHIRUH ZH GLVFXVV WKH GHWDLOV RI ZULWLQJ RXU RZQ PHWKRGV WKDW UHWXUQ YDOXHV OHW V H[SORUH WKH 0DWK FODVV DQG ZKDW LW KDV WR RIIHU 7KH 0DWK &ODVV ,Q &KDSWHU ZH PHQWLRQHG WKDW D JUHDW GHDO RI SUHGHILQHG FRGH KDV EHHQ ZULWWHQ IRU -DYD WKDW LV FROOHFWLYHO\ NQRZQ DV WKH -DYD FODVV OLEUDULHV 2QH RI WKH PRVW XVHIXO FODVVHV LV FDOOHG 0DWK ,W LQFOXGHV SUHGHILQHG PDWKHPDWLFDO FRQVWDQWV DQG D ODUJH QXPEHU RI FRPPRQ PDWKHPDWLFDO IXQFWLRQV 7KH 0DWK FODVV VKRXOG EH DYDLODEOH RQ DQ\ PDFKLQH WKDW KDV -DYD SURSHUO\ LQVWDOOHG 139 $V QRWHG DERYH WKH 0DWK FODVV KDV D PHWKRG FDOOHG VTUW WKDW FRPSXWHV WKH VTXDUH URRW RI D QXPEHU 7KH PHWKRG KDV WKH IROORZLQJ KHDGHU public static double sqrt(double n) 7KLV KHDGHU VD\V WKDW WKH PHWKRG LV FDOOHG VTUW WKDW LW WDNHV D SDUDPHWHU RI W\SH GRXEOH DQG WKDW LW UHWXUQV D YDOXH RI W\SH GRXEOH 8QIRUWXQDWHO\ ZH FDQ W MXVW FDOO WKLV PHWKRG GLUHFWO\ E\ UHIHUULQJ WR LW DV VTUW EHFDXVH LW LV LQ DQRWKHU FODVV :KHQHYHU \RX ZDQW WR UHIHU WR VRPHWKLQJ GHFODUHG LQ DQRWKHU FODVV \RX XVH WKH GRW QRWDWLRQ <class name>.<element> 6R ZH UHIHU WR WKLV PHWKRG DV 0DWK VTUW 6R ZH PLJKW ZULWH D SURJUDP OLNH WKH IROORZLQJ 1 2 3 4 5 6 public class WriteRoots { public static void main(String args) { for (int i = 1; i <= 20; i++) System.out.println("sqrt(" + i + ") = " + Math.sqrt(i)); } } ZKLFK SURGXFHV WKH IROORZLQJ RXWSXW sqrt(1) = 1.0 sqrt(2) = 1.4142135623730951 sqrt(3) = 1.7320508075688772 sqrt(4) = 2.0 sqrt(5) = 2.23606797749979 sqrt(6) = 2.449489742783178 sqrt(7) = 2.6457513110645907 sqrt(8) = 2.8284271247461903 sqrt(9) = 3.0 sqrt(10) = 3.1622776601683795 sqrt(11) = 3.3166247903554 sqrt(12) = 3.4641016151377544 sqrt(13) = 3.605551275463989 sqrt(14) = 3.7416573867739413 sqrt(15) = 3.872983346207417 sqrt(16) = 4.0 sqrt(17) = 4.123105625617661 sqrt(18) = 4.242640687119285 sqrt(19) = 4.358898943540674 sqrt(20) = 4.47213595499958 1RWLFH WKDW ZH DUH SDVVLQJ D YDOXH RI W\SH LQW WR 0DWK VTUW EXW WKH KHDGHU VD\V WKDW LW H[SHFWV D YDOXH RI W\SH GRXEOH 5HPHPEHU WKDW LI -DYD LV H[SHFWLQJ D GRXEOH DQG JHWV DQ LQW LW FRQYHUWV WKH LQW LQWR D FRUUHVSRQGLQJ GRXEOH 7KH 0DWK FODVV DOVR GHILQHV WZR FRQVWDQWV WKDW DUH IUHTXHQWO\ XVHG H DQG SL )ROORZLQJ WKH -DYD FRQYHQWLRQ ZH XVH DOO XSSHUFDVH OHWWHUV IRU WKHLU QDPHV DQG UHIHU WR WKHP DV 0DWK ( DQG 0DWK 3, 140 0DWK &RQVWDQWV &RQVWDQW E PI 'HVFULSWLRQ EDVH XVHG LQ QDWXUDO ORJDULWKPV UDWLR RI FLUFXPIHUHQFH RI D FLUFOH WR LWV GLDPHWHU 7KH IROORZLQJ LV D VKRUW OLVW RI VRPH RI WKH PRVW XVHIXO VWDWLF PHWKRGV IURP WKH 0DWK FODVV 8VHIXO 6WDWLF 0HWKRGV LQ WKH 0DWK &ODVV 0HWKRG abs ceil cos exp floor log log10 max min pow random sin 'HVFULSWLRQ DEVROXWH YDOXH FHLOLQJ URXQGV XSZDUG FRVLQH UDGLDQV H[SRQHQW EDVH H IORRU URXQGV GRZQZDUG ORJDULWKP EDVH H ORJDULWKP EDVH PD[LPXP RI WZR YDOXHV PLQLPXP RI WZR YDOXHV SRZHU JHQHUDO H[SRQHQWLDWLRQ UDQGRP YDOXH Math.abs(-308) ([DPSOH UHWXUQV Math.ceil(2.13) UHWXUQV Math.cos(Math.PI) UHWXUQV Math.exp(1) UHWXUQV Math.floor(2.93) UHWXUQV Math.log(Math.E) UHWXUQV Math.log10(1000) UHWXUQV Math.max(45, 207) UHWXUQV Math.min(3.8, 2.75) UHWXUQV Math.pow(3, 4) UHWXUQV Math.random() UHWXUQV D UDQGRP GRXEOH YDOXH N VXFK WKDW N VLQH sqrt VTXDUH URRW toDegrees FRQYHUWV UDGLDQ DQJOHV WR GHJUHHV toRadians FRQYHUWV GHJUHH DQJOHV WR UDGLDQV UHWXUQV Math.sqrt(2) UHWXUQV Math.sin(0) Math.toDegrees(Math.PI) Math.toRadians(270.0) UHWXUQV UHWXUQV <RX FDQ VHH D FRPSOHWH OLVW RI PHWKRGV GHILQHG LQ WKH 0DWK FODVV E\ FKHFNLQJ RXW WKH $3, GRFXPHQWDWLRQ IRU \RXU YHUVLRQ RI -DYD 7KH DFURQ\P $3, VWDQGV IRU $SSOLFDWLRQ 3URJUDP ,QWHUIDFH RU $SSOLFDWLRQ 3URJUDPPLQJ ,QWHUIDFH 7KH $3, GHVFULEHV KRZ WR PDNH XVH RI WKH VWDQGDUG OLEUDULHV WKDW DUH DYDLODEOH WR -DYD SURJUDPPHUV ,W FDQ EH D ELW RYHUZKHOPLQJ WR UHDG WKURXJK WKH $3, GRFXPHQWDWLRQ EHFDXVH WKH -DYD OLEUDULHV DUH YDVW 6R ZDQGHU DURXQG D ELW LI \RX DUH VR LQFOLQHG EXW GRQ W EH GLVPD\HG WKDW WKHUH DUH VR PDQ\ OLEUDULHV WR FKRRVH IURP LQ -DYD ,I \RX GR ORRN LQWR WKH 0DWK $3, \RX OO QRWLFH WKDW WKH 0DWK FODVV KDV VHYHUDO RYHUORDGHG PHWKRGV )RU H[DPSOH WKHUH LV D YHUVLRQ RI WKH DEVROXWH YDOXH PHWKRG 0DWK DEV IRU LQWV DQG DQRWKHU IRU GRXEOHV 7KH UXOHV WKDW JRYHUQ ZKLFK PHWKRG LV FDOOHG DUH FRPSOH[ VR ZH ZRQ W FRYHU WKHP KHUH 7KH EDVLF LGHD WKRXJK LV WKDW -DYD WULHV WR ILQG WKH PHWKRG WKDW LV WKH EHVW ILW )RU WKH PRVW SDUW \RX GRQ W KDYH WR WKLQN PXFK DERXW WKLV LVVXH <RX FDQ MXVW OHW -DYD FKRRVH IRU \RX DQG LW ZLOO JHQHUDOO\ PDNH WKH ULJKW FKRLFH 141 'HILQLQJ 0HWKRGV WKDW 5HWXUQ 9DOXHV :H FDQ ZULWH RXU RZQ PHWKRGV WKDW UHWXUQ YDOXHV E\ XVLQJ D VSHFLDO VWDWHPHQW NQRZQ DV D UHWXUQ VWDWHPHQW )RU H[DPSOH KHUH LV D PHWKRG WKDW WDNHV D GLVWDQFH VSHFLILHG DV D QXPEHU RI IHHW WKDW UHWXUQV WKH FRUUHVSRQGLQJ QXPEHU RI PLOHV public static double miles(double feet) { return feet / 5280.0; } 7KH PLOHV PHWKRG FRXOG EH XVHG E\ WKH PDLQ PHWKRG LQ FRGH VXFK DV WKH IROORZLQJ System.out.println("15000 feet is " + miles(15000) + " miles."); 1RWLFH RQFH DJDLQ WKDW LQ WKH KHDGHU IRU WKH PHWKRG WKH IDPLOLDU ZRUG void KDV EHHQ UHSODFHG ZLWK WKH ZRUG double 5HPHPEHU WKDW ZKHQ \RX GHFODUH D PHWKRG WKDW UHWXUQV D YDOXH \RX KDYH WR WHOO -DYD ZKDW NLQG RI YDOXH LW ZLOO UHWXUQ ,Q IDFW WKH NH\ZRUG void VLPSO\ PHDQV QR UHWXUQ YDOXH :H FDQ XSGDWH RXU V\QWD[ WHPSODWH IRU VWDWLF PHWKRGV RQFH PRUH WR LQFOXGH WKH IDFW WKDW WKH KHDGHU LQFOXGHV D UHWXUQ W\SH YRLG IRU QRQH public static <type> <name>(<type> <name>, ..., <type> <name>) { <statement or variable declaration>; <statement or variable declaration>; ... <statement or variable declaration>; } 7KH V\QWD[ RI WKH UHWXUQ VWDWHPHQW LV return <expression>; :KHQ -DYD HQFRXQWHUV D UHWXUQ VWDWHPHQW LW HYDOXDWHV WKH JLYHQ H[SUHVVLRQ DQG LPPHGLDWHO\ WHUPLQDWHV WKH PHWKRG UHWXUQLQJ WKH YDOXH LW REWDLQHG IURP WKH H[SUHVVLRQ %HFDXVH RI WKLV LW V QRW OHJDO WR KDYH DQ\ RWKHU VWDWHPHQWV DIWHU D UHWXUQ VWDWHPHQW WKH UHWXUQ PXVW EH WKH ODVW VWDWHPHQW LQ \RXU PHWKRG ,W LV DOVR DQ HUURU IRU D -DYD PHWKRG ZLWK D QRQ YRLG UHWXUQ W\SH WR WHUPLQDWH ZLWKRXW D UHWXUQ 7KHUH DUH H[FHSWLRQV WR WKH SUHYLRXV UXOHV WKDW ZH OO VHH ODWHU )RU H[DPSOH LW LV SRVVLEOH WKDW D PHWKRG PD\ KDYH PRUH WKDQ RQH UHWXUQ VWDWHPHQW DOWKRXJK WKLV ZRQ W FRPH XS XQWLO WKH QH[W FKDSWHU ZKHQ ZH FDQ GR VRPHWKLQJ FDOOHG FRQGLWLRQDO H[HFXWLRQ XVLQJ LI DQG LI HOVH VWDWHPHQWV &RPPRQ 3URJUDPPLQJ (UURU ,JQRULQJ 5HWXUQ 9DOXH :KHQ \RX FDOO D PHWKRG WKDW UHWXUQV D YDOXH WKH H[SHFWDWLRQ LV WKDW \RX OO GR VRPHWKLQJ ZLWK WKH YDOXH EHLQJ UHWXUQHG <RX FDQ SULQW LW VWRUH LW LQWR D YDULDEOH RU XVH LW DV SDUW RI D ODUJHU H[SUHVVLRQ ,W LV OHJDO EXW XQZLVH WR VLPSO\ FDOO WKH PHWKRG DQG LJQRUH WKH YDOXH EHLQJ UHWXUQHG IURP LW miles(15000); // doesn't do anything 142 %XW WKH SUHFHGLQJ FDOO GRHVQ W SULQW WKH QXPEHU RI PLOHV RU KDYH DQ\ QRWLFHDEOH HIIHFW ,I \RX ZDQW WKH YDOXH SULQWHG \RX PXVW GR VR \RXUVHOI VLPSO\ FDOOLQJ WKH PLOHV PHWKRG QHYHU FDXVHV D SULQWOQ VWDWHPHQW WR H[HFXWH double vacation = miles(15000); // better System.out.println("I drove " + vacation + " miles."); $ VKRUWHU IRUP RI WKH IL[HG FRGH ZRXOG EH WKH IROORZLQJ System.out.println("I drove " + miles(15000) + " miles."); /HW V ORRN DW DQRWKHU H[DPSOH PHWKRG WKDW UHWXUQV D YDOXH 7KH 3\WKDJRUHDQ 7KHRUHP RI ULJKW WULDQJOHV DV VWDWHG E\ WKH VFDUHFURZ LQ 7KH :L]DUG RI 2] VD\V WKDW WKH OHQJWK RI WKH K\SRWHQXVH RI D ULJKW WULDQJOH LV HTXDO WR WKH VTXDUH URRW RI WKH VXPV RI WKH VTXDUHV RI WKH WZR UHPDLQLQJ VLGHV ,I \RX NQRZ WKH OHQJWKV RI WZR VLGHV D DQG E RI D ULJKW WULDQJOH DQG ZDQW WR ILQG WKH OHQJWK RI WKH WKLUG VLGH F \RX FRPSXWH LW DV IROORZV 6R LPDJLQH WKDW ZH ZDQW WR SULQW RXW WKH OHQJWKV RI WKH K\SRWHQXVHV RI WZR ULJKW WULDQJOHV RQH ZLWK VLGH OHQJWKV RI DQG DQG WKH RWKHU ZLWK VLGH OHQJWKV RI DQG :H FRXOG ZULWH FRGH VXFK DV WKH IROORZLQJ double c1 = Math.sqrt(Math.pow(5, 2) System.out.println("hypotenuse 1 = " double c2 = Math.sqrt(Math.pow(3, 2) System.out.println("hypotenuse 2 = " + + + + Math.pow(12, 2)); c1); Math.pow(4, 2)); c2); 7KH SUHFHGLQJ FRGH LV FRUUHFW EXW LW V D ELW KDUG WR UHDG DQG ZH G KDYH WR GXSOLFDWH WKH VDPH FRPSOH[ PDWK D WKLUG WLPH LI ZH KDG D WKLUG WULDQJOH RI LQWHUHVW $ EHWWHU VROXWLRQ ZRXOG EH WR FUHDWH D PHWKRG WKDW FRPSXWHV DQG UHWXUQV WKH K\SRWHQXVH OHQJWK ZKHQ JLYHQ WKH WZR RWKHU VLGH OHQJWKV DV SDUDPHWHUV 6XFK D PHWKRG ZRXOG ORRN OLNH WKLV public static double hypotenuse(double a, double b) { double c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); return c; } 2XU QHZ K\SRWHQXVH PHWKRG FDQ EH XVHG WR FUDIW D PRUH FRQFLVH DQG UHDGDEOH PDLQ PHWKRG 7KH RYHUDOO SURJUDP ORRNV OLNH WKH IROORZLQJ 1 2 3 4 5 6 7 8 9 10 11 12 public class Triangles { public static void main(String args) { System.out.println("hypotenuse 1 = " + hypotenuse(5, 12)); System.out.println("hypotenuse 2 = " + hypotenuse(3, 4)); } public static double hypotenuse(double a, double b) { double c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); return c; System.out.println(c); } } 143 $ IHZ YDULDWLRQV RI RXU SURJUDP DUH SRVVLEOH )RU RQH LW LVQ W QHFHVVDU\ WR VWRUH RXU UHWXUQ YDOXH LQWR WKH YDULDEOH F :H FDQ VLPSO\ FRPSXWH DQG UHWXUQ WKH YDOXH LQ RQH OLQH LI ZH SUHIHU 7KH ERG\ RI WKH K\SRWHQXVH PHWKRG ZRXOG EHFRPH WKH IROORZLQJ return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); $OVR VRPH SURJUDPPHUV DYRLG XVLQJ 0DWK SRZ IRU ORZ SRZHUV VXFK DV DQG MXVW PDQXDOO\ GR WKH PXOWLSOLFDWLRQ ,I ZH G GRQH WKDW KHUH WKH ERG\ RI RXU K\SRWHQXVH PHWKRG ZRXOG KDYH ORRNHG OLNH WKLV return Math.sqrt(a * a + b * b); &RPPRQ 3URJUDPPLQJ (UURU 6WDWHPHQW $IWHU 5HWXUQ ,W LVQ W OHJDO WR KDYH RWKHU VWDWHPHQWV LPPHGLDWHO\ IROORZLQJ D UHWXUQ VWDWHPHQW EHFDXVH WKH VWDWHPHQWV FDQ QHYHU EH UHDFKHG RU H[HFXWHG 2QH FRPPRQ FDVH ZKHUH QHZ SURJUDPPHUV DFFLGHQWDOO\ GR WKLV LV ZKHQ WU\LQJ WR SULQW WKH YDOXH RI D YDULDEOH DIWHU UHWXUQLQJ ,PDJLQH WKDW ZH YH ZULWWHQ WKH K\SRWHQXVH PHWKRG EXW KDYH DFFLGHQWDOO\ ZULWWHQ WKH SDUDPHWHUV WR 0DWK SRZ LQ WKH ZURQJ RUGHU VR RXU PHWKRG LV QRW SURGXFLQJ WKH ULJKW DQVZHU :H WU\ WR GHEXJ WKLV E\ SULQWLQJ WKH YDOXH RI F WKDW LV EHLQJ UHWXUQHG +HUH V RXU IDXOW\ FRGH // Trying to find the bug in this buggy version of hypotenuse. public static double hypotenuse(double a, double b) { double c = Math.sqrt(Math.pow(2, a) + Math.pow(2, b)); return c; System.out.println(c); // this doesn't work } 7KH FRPSLOHU FRPSODLQV DERXW WKH SULQWOQ VWDWHPHQW EHLQJ XQUHDFKDEOH VLQFH LW IROORZV D UHWXUQ VWDWHPHQW 7KH FRPSLOHU HUURU RXWSXW ORRNV VRPHWKLQJ OLNH WKLV Triangles.java:10: unreachable statement System.out.println(c); ^ Triangles.java:11: missing return statement } ^ 2 errors 7KH IL[ LV WR PRYH VXFK D SULQWOQ VWDWHPHQW HDUOLHU LQ WKH PHWKRG EHIRUH WKH UHWXUQ VWDWHPHQW // Trying to find the bug in this buggy version of hypotenuse. public static double hypotenuse(double a, double b) { double c = Math.sqrt(Math.pow(2, a) + Math.pow(2, b)); System.out.println(c); // better return c; } 8VLQJ 2EMHFWV :H YH VSHQW D FRQVLGHUDEOH DPRXQW RI WLPH GLVFXVVLQJ WKH SULPLWLYH W\SHV LQ -DYD DQG KRZ WKH\ ZRUN VR LW V DERXW WLPH WKDW ZH VWDUWHG WDONLQJ DERXW REMHFWV DQG KRZ WKH\ ZRUN 144 7KH LGHD IRU REMHFWV FDPH IURP WKH REVHUYDWLRQ WKDW DV ZH VWDUW ZRUNLQJ ZLWK D QHZ NLQG RI GDWD LQWHJHUV UHDOV FKDUDFWHUV WH[W HWF ZH ILQG RXUVHOYHV ZULWLQJ D ORW RI PHWKRGV WKDW RSHUDWH RQ WKDW GDWD ,W VHHPHG RGG WR KDYH WKHVH WZR WKLQJV VHSDUDWHG ,W PDNHV PRUH VHQVH WR LQFOXGH VRPH RI WKH EDVLF RSHUDWLRQV ZLWK WKH GDWD LWVHOI 7KLV SDFNDJLQJ RI GDWD DQG RSHUDWLRQV LQWR RQH HQWLW\ LV WKH FHQWUDO LGHD EHKLQG REMHFWV $Q REMHFW VWRUHV VRPH GDWD DQG KDV PHWKRGV WKDW DFW RQ LWV GDWD 2EMHFW $ SURJUDPPLQJ HQWLW\ WKDW FRQWDLQV VWDWH GDWD DQG EHKDYLRU PHWKRGV $V ZH VDLG LQ &KDSWHU FODVVHV DUH WKH EDVLF EXLOGLQJ EORFNV RI -DYD SURJUDPV %XW FODVVHV DOVR VHUYH DQRWKHU SXUSRVH WR GHVFULEH QHZ W\SHV RI REMHFWV &ODVV $ FDWHJRU\ RU W\SH RI REMHFW :KHQ XVHG WKLV ZD\ D FODVV LV OLNH D EOXHSULQW RI ZKDW WKH REMHFW ORRNV OLNH 2QFH ZH YH JLYHQ -DYD WKH EOXHSULQW ZH FDQ DVN LW WR FUHDWH DFWXDO REMHFWV WKDW PDWFK WKH EOXHSULQW :H VRPHWLPHV UHIHU WR HDFK RI WKH LQGLYLGXDO REMHFWV DV LQVWDQFHV RI WKH FODVV :H WHQG WR XVH WKH ZRUGV LQVWDQFH DQG REMHFW LQWHUFKDQJHDEO\ 7KLV FRQFHSW LV GLIILFXOW WR XQGHUVWDQG LQ WKH DEVWUDFW VR OHW V ORRN DW VHYHUDO GLIIHUHQW FODVVHV WR EHWWHU XQGHUVWDQG ZKDW LW PHDQV DQG KRZ LW ZRUNV ,Q NHHSLQJ ZLWK RXU LGHD RI IRFXVLQJ RQ IXQGDPHQWDO FRQFHSWV ILUVW ZH UH JRLQJ WR VWXG\ KRZ WR XVH H[LVWLQJ REMHFWV WKDW DUH DOUHDG\ SDUW RI -DYD EXW ZH DUHQ W JRLQJ WR VWXG\ KRZ WR GHILQH RXU RZQ QHZ W\SHV RI REMHFWV MXVW \HW :H OO JHW WR WKDW LQ &KDSWHU DIWHU ZH YH KDG WLPH WR SUDFWLFH XVLQJ REMHFWV 8QIRUWXQDWHO\ XVLQJ REMHFWV UHTXLUHV VRPH QHZ V\QWD[ DQG FRQFHSWV WKDW GLIIHU IURP WKH SULPLWLYH W\SHV ZH YH VHHQ ,W ZRXOG EH QLFH LI -DYD KDG D FRQVLVWHQW PRGHO IRU XVLQJ DOO W\SHV RI GDWD EXW LW GRHVQ W 7KDW PHDQV WKDW ZH XQIRUWXQDWHO\ KDYH WR OHDUQ WZR VHWV RI UXOHV LI ZH ZDQW WR XQGHUVWDQG KRZ RXU SURJUDPV RSHUDWH RQH IRU SULPLWLYHV DQG RQH IRU REMHFWV 6WULQJ 2EMHFWV 6WULQJV DUH RQH RI WKH PRVW XVHIXO DQG WKH PRVW FRPPRQO\ XVHG W\SHV RI REMHFWV LQ -DYD VR ZH GHILQLWHO\ ZDQW WR VHH KRZ WKH\ ZRUN 7KH\ GRQ W PDNH WKH EHVW H[DPSOH RI REMHFWV WKRXJK EHFDXVH WKHUH DUH D ORW RI VSHFLDO UXOHV WKDW DSSO\ RQO\ WR VWULQJV VR LQ WKH QH[W VHFWLRQ ZH OO ORRN DW D PRUH W\SLFDO NLQG RI REMHFW 6WULQJV KDYH WKH VSHFLDO SURSHUW\ WKDW WKHUH DUH OLWHUDOV WKDW UHSUHVHQW 6WULQJ REMHFWV :H YH EHHQ XVLQJ WKHP LQ SULQWOQ VWDWHPHQWV VLQFH &KDSWHU :KDW ZH KDYHQ W GLVFXVVHG LV WKDW WKHVH OLWHUDO YDOXHV UHSUHVHQW REMHFWV RI W\SH 6WULQJ LQVWDQFHV RI WKH 6WULQJ FODVV )RU H[DPSOH LQ WKH VDPH ZD\ WKDW \RX FDQ VD\ int x = 8; \RX FDQ VD\ 145 String s = "hello there"; :H FDQ GHFODUH YDULDEOHV RI W\SH 6WULQJ DQG FDQ XVH WKH DVVLJQPHQW VWDWHPHQW WR JLYH D YDOXH WR WKHVH YDULDEOHV :H FDQ DOVR ZULWH FRGH WKDW LQYROYHV 6WULQJ H[SUHVVLRQV String s1 = "hello"; String s2 = "there"; String combined = s1 + " " + s2; 7KLV FRGH GHILQHV WZR 6WULQJV WKDW HDFK UHSUHVHQW D VLQJOH ZRUG DQG D WKLUG 6WULQJ WKDW UHSUHVHQWV WKH FRQFDWHQDWLRQ RI WKH WZR ZRUGV ZLWK D VSDFH LQ EHWZHHQ <RX OO QRWLFH WKDW WKH W\SH String LV FDSLWDOL]HG DV DUH WKH QDPHV RI DOO REMHFW W\SHV LQ -DYD XQOLNH WKH SULPLWLYH W\SHV VXFK DV GRXEOH DQG LQW 6R IDU ZH KDYHQ W VHHQ DQ\WKLQJ VSHFLDO DERXW 6WULQJ REMHFWV 5HPHPEHU WKDW WKH LGHD EHKLQG REMHFWV ZDV WR LQFOXGH EDVLF RSHUDWLRQV ZLWK WKH GDWD LWVHOI WKH ZD\ ZH EXLOG D FDU WKDW KDV FRQWUROV EXLOW LQ 7KH GDWD VWRUHG LQ D 6WULQJ LV D VHTXHQFH RI FKDUDFWHUV 7KHUH DUH DOO VRUWV RI RSHUDWLRQV ZH PLJKW ZDQW WR SHUIRUP RQ WKLV VHTXHQFH RI FKDUDFWHUV )RU H[DPSOH ZH PLJKW ZDQW WR NQRZ KRZ PDQ\ FKDUDFWHUV WKHUH DUH LQ WKH 6WULQJ 6WULQJ REMHFWV KDYH D OHQJWK PHWKRG WKDW UHWXUQV WKLV LQIRUPDWLRQ ,I WKH OHQJWK PHWKRG ZHUH VWDWLF \RX ZRXOG FDOO LW E\ VD\LQJ VRPHWKLQJ OLNH length(s) // this isn't legal %XW ZKHQ SHUIRUPLQJ RSHUDWLRQV RQ REMHFWV ZH XVH D GLIIHUHQW V\QWD[ 2EMHFWV VWRUH GDWD DQG PHWKRGV VR WKH PHWKRG WR UHSRUW D 6WULQJ V OHQJWK DFWXDOO\ H[LVWV LQVLGH WKDW 6WULQJ REMHFW LWVHOI 7R FDOO DQ REMHFW V PHWKRG \RX ZULWH WKH QDPH RI WKH YDULDEOH ILUVW DQG WKHQ WKH QDPH RI WKH PHWKRG ZLWK D GRW LQ EHWZHHQ s.length() 7KLQN RI LW DV WDONLQJ WR WKH 6WULQJ REMHFW :KHQ \RX DVN IRU s.length() \RX UH VD\LQJ +H\ V , P WDONLQJ WR \RX :KDW V \RXU OHQJWK" 2I FRXUVH GLIIHUHQW 6WULQJ REMHFWV KDYH GLIIHUHQW OHQJWKV VR \RX ZLOO JHW GLIIHUHQW DQVZHUV ZKHQ \RX WDON WR GLIIHUHQW 6WULQJ REMHFWV 7KH JHQHUDO V\QWD[ IRU FDOOLQJ D PHWKRG RI DQ REMHFW LV WKH IROORZLQJ <object's name> . <method name> ( <parameter(s)> ) )RU H[DPSOH VXSSRVH WKDW ZH KDYH LQLWLDOL]HG WZR VWULQJ YDULDEOHV DV IROORZV String s1 = "hello"; String s2 = "how are you?"; :H FDQ XVH D SULQWOQ WR H[DPLQH WKH OHQJWK RI HDFK VWULQJ System.out.println("Length of s1 = " + s1.length()); System.out.println("Length of s2 = " + s2.length()); ZKLFK SURGXFHV WKH IROORZLQJ RXWSXW 146 Length of s1 = 5 Length of s2 = 12 :KDW HOVH PLJKW ZH ZDQW WR GR ZLWK D 6WULQJ REMHFW" :LWK WKH OHQJWK PHWKRG ZH FDQ ILJXUH RXW KRZ PDQ\ FKDUDFWHUV WKHUH DUH EXW ZKDW DERXW JHWWLQJ WKH LQGLYLGXDO FKDUDFWHUV WKHPVHOYHV" 7KHUH DUH VHYHUDO ZD\V WR GR WKLV EXW RQH RI WKH PRVW FRPPRQ LV WR XVH D PHWKRG FDOOHG FKDU$W WKDW UHWXUQV WKH FKDUDFWHU DW D VSHFLILF ORFDWLRQ LQ WKH 6WULQJ 7KLV OHDGV XV WR WKH SUREOHP RI KRZ WR VSHFLI\ ORFDWLRQV LQ D VHTXHQFH 2EYLRXVO\ WKHUH LV D ILUVW FKDUDFWHU VHFRQG FKDUDFWHU DQG VR RQ VR LW PDNHV VHQVH WR XVH DQ LQWHJHU WR UHIHU WR D VSHFLILF ORFDWLRQ :H FDOO WKLV WKH LQGH[ DQG DV ZH OO VHH -DYD XVXDOO\ XVHV DV WKH ILUVW LQGH[ YDOXH ,QGH[ $Q LQWHJHU XVHG WR VSHFLI\ D ORFDWLRQ LQ D VHTXHQFH RI YDOXHV -DYD JHQHUDOO\ XVHV LQGH[LQJ DV WKH ILUVW LQGH[ YDOXH IROORZHG E\ DQG VR RQ 6R HDFK FKDUDFWHU RI D 6WULQJ REMHFW LV DVVLJQHG DQ LQGH[ VWDUWLQJ ZLWK LQGH[ YDULDEOH V WKDW UHIHUV WR WKH 6WULQJ KHOOR WKH LQGH[HV ZRXOG EH h | 0 e | 1 l | 2 l | 3 o | 4 EDVHG )RU H[DPSOH IRU RXU ,W PD\ VHHP LQWXLWLYH WR FRQVLGHU WKH OHWWHU K WR EH DW SRVLWLRQ EXW WKHUH DUH DGYDQWDJHV WR VWDUWLQJ ZLWK DQ LQGH[ RI DQG LW V D FRQYHQWLRQ WKDW ZDV DGRSWHG E\ WKH GHVLJQHUV RI WKH & ODQJXDJH WKDW KDV EHHQ IROORZHG DOVR E\ WKH GHVLJQHUV RI & DQG -DYD VR LW V D FRQYHQWLRQ \RX OO KDYH WR OHDUQ WR OLYH ZLWK )RU RXU ORQJHU VWULQJ V WKH SRVLWLRQV ZRXOG EH h | 0 o | 1 w | 2 | 3 a | 4 r | 5 e | 6 | 7 y | 8 o | 9 u | 10 ? | 11 1RWLFH WKDW WKH VSDFHV LQ WKH VWULQJ KDYH SRVLWLRQV DV ZHOO DV LQ SRVLWLRQV DQG DERYH $OVR QRWLFH WKDW WKH LQGH[HV IRU D JLYHQ 6WULQJ DOZD\V UDQJH IURP WR RQH OHVV WKDQ WKH OHQJWK RI WKH 6WULQJ 8VLQJ WKH FKDU$W PHWKRG ZH FDQ UHTXHVW VSHFLILF FKDUDFWHUV RI D 6WULQJ 7KH UHWXUQ W\SH LV FKDU )RU H[DPSOH LI ZH DVN IRU V FKDU$W ZH OO JHW H WKH H LQ KHOOR ,I ZH DVN IRU V FKDU$W ZH OO JHW U WKH U LQ KRZ DUH \RX" )RU DQ\ 6WULQJ LI ZH DVN IRU FKDU$W ZH OO JHW WKH ILUVW FKDUDFWHU RI WKH 6WULQJ :KHQ ZRUNLQJ ZLWK 6WULQJ REMHFWV ZH RIWHQ ILQG LW XVHIXO WR ZULWH D IRU ORRS WR KDQGOH WKH GLIIHUHQW FKDUDFWHUV RI WKH 6WULQJ %HFDXVH 6WULQJV DUH LQGH[HG VWDUWLQJ DW WKLV WDVN LV HDVLHU WR ZULWH ZLWK IRU ORRSV WKDW VWDUW ZLWK UDWKHU WKDQ &RQVLGHU IRU H[DPSOH WKH IROORZLQJ FRGH WKDW SULQWV RXW WKH LQGLYLGXDO FKDUDFWHUV RI V String s1 = "hello"; for (int i = 0; i < s1.length(); i++) { System.out.println(i + ": " + s1.charAt(i)); } 147 ZKLFK SURGXFHV WKH IROORZLQJ RXWSXW 0: 1: 2: 3: 4: h e l l o 5HPHPEHU WKDW ZKHQ ZH VWDUW ORRSV DW ZH XVXDOO\ WHVW ZLWK OHVV WKDQ UDWKHU WKDQ OHVV WKDQ RU HTXDO 2XU 6WULQJ V KDV FKDUDFWHUV LQ LW VR WKH FDOO RQ V OHQJWK ZLOO UHWXUQ %XW EHFDXVH WKH ILUVW LQGH[ LV WKH ODVW LQGH[ ZLOO EH RQH OHVV WKDQ 7KLV FRQYHQWLRQ WDNHV D ZKLOH WR JHW XVHG WR EXW EDVHG LQGH[LQJ LV XVHG WKURXJKRXW -DYD VR \RX OO HYHQWXDOO\ JHW WKH KDQJ RI LW $QRWKHU XVHIXO 6WULQJ PHWKRG LV WKH VXEVWULQJ PHWKRG ,W WDNHV WZR LQWHJHU DUJXPHQWV UHSUHVHQWLQJ D VWDUWLQJ DQG HQGLQJ LQGH[ :KHQ \RX FDOO WKH VXEVWULQJ PHWKRG \RX SURYLGH WZR RI WKHVH LQGH[HV WKH LQGH[ RI WKH ILUVW FKDUDFWHU \RX ZDQW DQG WKH LQGH[ MXVW SDVW WKH ODVW LQGH[ WKDW \RX ZDQW 5HFDOO WKDW RXU 6WULQJ V WKDW ZH VHW WR KRZ DUH \RX" KDV WKH IROORZLQJ SRVLWLRQV h | 0 o | 1 w | 2 | 3 a | 4 r | 5 e | 6 | 7 y | 8 o | 9 u | 10 ? | 11 ,I \RX ZDQW WR SXOO RXW WKH LQGLYLGXDO ZRUG KRZ IURP WKLV 6WULQJ \RX G DVN IRU s2.substring(0, 3) 5HPHPEHU WKDW WKH VHFRQG YDOXH WKDW \RX SDVV WR WKH VXEVWULQJ PHWKRG LV VXSSRVHG WR EH RQH EH\RQG WKH HQG RI WKH VXEVWULQJ \RX DUH IRUPLQJ 6R HYHQ WKRXJK WKHUH LV D VSDFH DW SRVLWLRQ LQ WKH RULJLQDO VWULQJ LW ZLOO QRW EH SDUW RI ZKDW ZH JHW IURP WKH FDOO RQ VXEVWULQJ ,QVWHDG ZH JHW DOO FKDUDFWHUV MXVW EHIRUH SRVLWLRQ )ROORZLQJ WKLV UXOH PHDQV WKDW VRPHWLPHV \RX ZLOO JLYH D SRVLWLRQ WR VXEVWULQJ DW ZKLFK WKHUH LVQ W D FKDUDFWHU 7KH ODVW FKDUDFWHU LQ WKH VWULQJ WKDW V UHIHUV WR LV DW LQGH[ WKH TXHVWLRQ PDUN ,I \RX ZDQW WR JHW WKH VXEVWULQJ \RX" LQFOXGLQJ WKH TXHVWLRQ PDUN \RX G DVN IRU s2.substring(8, 12) 7KHUH LV QR FKDUDFWHU DW SRVLWLRQ LQ V EXW WKLV FDOO DVNV IRU FKDUDFWHUV VWDUWLQJ DW SRVLWLRQ FRPH EHIRUH SRVLWLRQ VR WKLV DFWXDOO\ PDNHV VHQVH WKDW <RX KDYH WR EH FDUHIXO DERXW ZKDW LQGH[HV \RX XVH :LWK WKH VXEVWULQJ PHWKRG ZH FDQ DVN IRU WKH SRVLWLRQ MXVW EH\RQG WKH HQG RI WKH 6WULQJ EXW \RX FDQ W DVN IRU DQ\WKLQJ EH\RQG WKDW )RU H[DPSOH LI \RX DVN IRU s2.substring(8, 13) // out of bounds! <RXU SURJUDP ZLOO JHQHUDWH DQ H[HFXWLRQ HUURU 6LPLODUO\ LI \RX DVN IRU WKH FKDU$W D QRQH[LVWHQW SRVLWLRQ \RXU SURJUDP ZLOO JHQHUDWH DQ H[HFXWLRQ HUURU 7KHVH HUURUV DUH NQRZQ DV H[FHSWLRQV ([FHSWLRQV DUH UXQWLPH HUURUV DV PHQWLRQHG LQ &KDSWHU 148 ([FHSWLRQ $ UXQWLPH HUURU WKDW SUHYHQWV D SURJUDP IURP FRQWLQXLQJ LWV QRUPDO H[HFXWLRQ :H VD\ WKDW DQ H[FHSWLRQ LV WKURZQ ZKHQ DQ HUURU LV HQFRXQWHUHG :KHQ DQ H[FHSWLRQ LV WKURZQ -DYD ORRNV WR VHH LI \RX KDYH ZULWWHQ FRGH WR KDQGOH LW ,I QRW SURJUDP H[HFXWLRQ LV KDOWHG DQG \RX ZLOO VHH ZKDW LV NQRZQ DV D VWDFN WUDFH RU EDFN WUDFH 7KH VWDFN WUDFH VKRZV \RX WKH VHULHV RI PHWKRGV WKDW KDYH EHHQ FDOOHG LQ UHYHUVH RUGHU ,Q WKLV FDVH RI EDG 6WULQJ LQGH[HV WKH H[FHSWLRQ SULQWV D PHVVDJH VXFK DV WKH IROORZLQJ WR WKH FRQVROH Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of r at java.lang.String.substring(Unknown Source) at ExampleProgram.main(ExampleProgram.java:3) <RX FDQ XVH 6WULQJV DV SDUDPHWHUV WR PHWKRGV )RU H[DPSOH WKH IROORZLQJ SURJUDP XVHV 6WULQJ SDUDPHWHUV WR HOLPLQDWH VRPH RI WKH UHGXQGDQF\ LQ D SRSXODU FKLOGUHQ V VRQJ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class BusSong { public static void main(String args) { verse("wheels", "go", "round and round"); verse("wipers", "go", "swish, swish, swish"); verse("horn", "goes", "beep, beep, beep"); } public static void verse(String item, String verb, String sound) { System.out.print("The " + item + " on the bus " + verb + " "); System.out.println(sound + ","); System.out.println(sound + ","); System.out.println(sound + "."); System.out.print("The " + item + " on the bus " + verb + " "); System.out.println(sound + ","); System.out.println("All through the town."); System.out.println(); } } ,W SURGXFHV WKH IROORZLQJ RXWSXW The wheels on the bus go round and round, round and round, round and round. The wheels on the bus go round and round, All through the town. The wipers on the bus go swish, swish, swish, swish, swish, swish, swish, swish, swish. The wipers on the bus go swish, swish, swish, All through the town. The horn on beep, beep, beep, beep, The horn on All through the bus goes beep, beep, beep, beep, beep. the bus goes beep, beep, beep, the town. 149 7KH IROORZLQJ DUH VRPH RI WKH PRVW XVHIXO PHWKRGV WKDW \RX FDQ FDOO RQ 6WULQJ REMHFWV 8VHIXO 0HWKRGV RI 6WULQJ 2EMHFWV 0HWKRG charAt(index) endsWith(text) indexOf(text) 'HVFULSWLRQ FKDUDFWHU DW D VSHFLILF LQGH[ ZKHWKHU RU QRW VWULQJ HQGV ZLWK VRPH WH[W LQGH[ RI D SDUWLFXODU FKDUDFWHU RU 6WULQJ LI QRW SUHVHQW QXPEHU RI FKDUDFWHUV ZKHWKHU RU QRW VWULQJ VWDUWV ZLWK VRPH WH[W FKDUDFWHUV IURP VWDUW LQGH[ WR MXVW EHIRUH VWRS LQGH[ D QHZ VWULQJ ZLWK DOO ORZHUFDVH OHWWHUV D QHZ VWULQJ ZLWK DOO XSSHUFDVH OHWWHUV ([DPSOH DVVXPLQJ V LV KHOOR UHWXUQV 'e' s.endsWith("llo") UHWXUQV true s.charAt(1) s.indexOf("o") UHWXUQV 4 length() startsWith(text) substring(start, stop) toLowerCase() toUpperCase() s.length() UHWXUQV 5 UHWXUQV false s.startsWith("hi") s.substring(1, 3) s.toLowerCase() s.toUpperCase() UHWXUQV "el" UHWXUQV "hello" UHWXUQV "HELLO" 6WULQJV LQ -DYD DUH LPPXWDEOH ZKLFK PHDQV WKDW RQFH WKH\ DUH FRQVWUXFWHG WKH\ FDQ QHYHU EH FKDQJHG LQ YDOXH ,PPXWDEOH 2EMHFW $Q REMHFW ZKRVH YDOXH FDQQRW EH FKDQJHG ,W PD\ VHHP RGG WKDW VWULQJV DUH LPPXWDEOH DQG \HW WKH\ KDYH PHWKRGV OLNH WR8SSHU&DVH DQG WR/RZHU&DVH <RX KDYH WR UHDG WKH WDEOH GHVFULSWLRQ FDUHIXOO\ 7KHVH PHWKRGV GRQ W DFWXDOO\ FKDQJH D JLYHQ VWULQJ REMHFW WKH\ UHWXUQ D QHZ VWULQJ &RQVLGHU WKH IROORZLQJ FRGH String s = "Hello Maria"; s.toUpperCase(); System.out.println(s); <RX PLJKW WKLQN WKDW WKLV ZLOO WXUQ WKH VWULQJ WR LWV XSSHUFDVH HTXLYDOHQW EXW LW GRHVQ W 7KH VHFRQG OLQH RI FRGH FRQVWUXFWV D QHZ VWULQJ WKDW KDV WKH XSSHUFDVH HTXLYDOHQW RI WKH VWULQJ EXW ZH GRQ W GR DQ\WKLQJ ZLWK WKLV QHZ YDOXH 7KH NH\ LV WR HLWKHU VWRUH WKLV QHZ VWULQJ LQ D GLIIHUHQW YDULDEOH RU WR UHDVVLJQ WKH YDULDEOH V WR SRLQW WR WKH QHZ VWULQJ String s = "Hello Maria"; s = s.toUpperCase(); System.out.println(s); 7KLV YHUVLRQ RI WKH FRGH SURGXFHV WKH IROORZLQJ RXWSXW HELLO MARIA 150 7KH WR8SSHU&DVH DQG WR/RZHU&DVH PHWKRGV DUH SDUWLFXODUO\ KHOSIXO ZKHQ \RX ZDQW WR SHUIRUP VWULQJ FRPSDULVRQV LQ ZKLFK \RX LJQRUH WKH FDVH RI WKH OHWWHUV LQYROYHG 3RLQW 2EMHFWV 6WULQJV DUH H[WUHPHO\ XVHIXO REMHFWV EXW WKH UXOHV IRU FUHDWLQJ DQG XVLQJ WKHP GRQ W FRPSOHWHO\ PDWFK WKRVH RI RWKHU W\SHV RI REMHFWV :H OO QRZ H[DPLQH D PRUH W\SLFDO W\SH RI REMHFW 3RLQW $ 3RLQW REMHFW VWRUHV WKH [ \ FRRUGLQDWHV RI D SRLQW LQ ' VSDFH 7KHVH FRRUGLQDWHV DUH H[SUHVVHG DV LQWHJHUV DOWKRXJK WKHUH DUH DOVR YDULDWLRQV IRU VWRULQJ SRLQWV H[SUHVVHG XVLQJ IORDWLQJ SRLQW QXPEHUV 6WULQJV DUH D VSHFLDO FDVH WKDW KDYH OLWHUDO YDOXHV WKDW FDQ EH UHIHUUHG WR GLUHFWO\ 0RVW REMHFWV KDYH WR EH H[SOLFLWO\ FRQVWUXFWHG E\ FDOOLQJ D VSHFLDO PHWKRG NQRZQ DV D FRQVWUXFWRU &RQVWUXFW &RQVWUXFWRU $ PHWKRG WKDW FUHDWHV DQG LQLWLDOL]HV DQ REMHFW 2EMHFWV LQ -DYD SURJUDPV PXVW EH FRQVWUXFWHG EHIRUH WKH\ FDQ EH XVHG 5HPHPEHU WKDW D FODVV LV OLNH D EOXHSULQW IRU D IDPLO\ RI REMHFWV &DOOLQJ D FRQVWUXFWRU LV OLNH VHQGLQJ DQ RUGHU WR WKH IDFWRU\ DVNLQJ LW WR IROORZ WKH EOXHSULQW WR JHW \RX DQ DFWXDO REMHFW WKDW \RX FDQ PDQLSXODWH :KHQ \RX VHQG LQ \RXU RUGHU WR WKH IDFWRU\ \RX VRPHWLPHV VSHFLI\ FHUWDLQ SDUDPHWHUV H J ZKDW FRORU \RX ZDQW WKH REMHFW WR EH ,Q -DYD FRQVWUXFWRUV DUH FDOOHG XVLQJ WKH VSHFLDO NH\ZRUG new IROORZHG E\ WKH REMHFW V W\SH DQG DQ\ QHFHVVDU\ SDUDPHWHUV )RU H[DPSOH WR FRQVWUXFW D VSHFLILF 3RLQW REMHFW \RX KDYH WR SDVV WKH YDOXHV \RX ZDQW IRU [ DQG \ Point p = new Point(3, 8); 7KLV FRGH SHUIRUPV D FDOO RQ D FRQVWUXFWRU PHWKRG RI WKH 3RLQW FODVV &RQVWUXFWRUV DOZD\V KDYH WKH VDPH QDPH DV WKH FODVV ,Q WKLV FDVH ZH SDVV WKH FRQVWUXFWRU WZR LQWHJHU YDOXHV DV SDUDPHWHUV WR VSHFLI\ WKH [ FRRUGLQDWH DQG \ FRRUGLQDWH RI WKH SRLQW $IWHU H[HFXWLQJ WKH OLQH RI FRGH DERYH ZH ZRXOG KDYH WKH IROORZLQJ VLWXDWLRQ +--------------------+ +---+ | +----+ +----+ | p | +-+--> | x | 3 | y | 8 | | +---+ | +----+ +----+ | +--------------------+ 2QFH \RX KDYH FRQVWUXFWHG D 3RLQW REMHFW ZKDW FDQ \RX GR ZLWK LW" 2QH RI WKH PRVW FRPPRQ WKLQJV \RX GR ZLWK DQ REMHFW LV WR SULQW LW WR WKH FRQVROH $ 3RLQW REMHFW OLNH PDQ\ -DYD REMHFWV FDQ EH SULQWHG ZLWK WKH SULQWOQ VWDWHPHQW System.out.println(p); 7KH SULQWOQ VWDWHPHQW SURGXFHV WKH IROORZLQJ RXWSXW 151 java.awt.Point[x=3,y=8] 7KH RXWSXW SURGXFHG EHJLQV ZLWK WKH IXOO QDPH RI WKH FODVV MDYD DZW 3RLQW 7KH MDYD DZW SDUW RI WKLV QDPH LV H[SODLQHG LQ D PRPHQW $IWHU WKH QDPH RI WKH FODVV ZH KDYH WKH [ DQG \ FRRUGLQDWHV OLVWHG LQVLGH VTXDUH EUDFNHWV 7KLV IRUPDW LV D OLWWOH XJO\ EXW LW OHWV \RX VHH WKH [ DQG \ YDOXHV LQVLGH D JLYHQ SRLQW 3RLQW REMHFWV DOVR KDYH D PHWKRG FDOOHG WUDQVODWH WKDW FDQ EH XVHG WR VKLIW WKH FRRUGLQDWHV E\ D VSHFLILF GHOWD [ DQG GHOWD \ ZKLFK DUH SDVVHG DV SDUDPHWHUV :KHQ \RX WUDQVODWH D SRLQW \RX VKLIW LWV ORFDWLRQ E\ WKH VSHFLILHG DPRXQW )RU H[DPSOH \RX PLJKW VD\ p.translate(-1, -2); // subtract 1 from x, subtract 2 from y *LYHQ WKDW WKH SRLQW VWDUWHG RXW ZLWK FRRUGLQDWHV WKLV WUDQVODWLRQ ZRXOG OHDYH WKH SRLQW ZLWK FRRUGLQDWHV 7KXV DIWHU WKLV OLQH RI FRGH LV H[HFXWHG ZH HQG XS ZLWK WKH IROORZLQJ VLWXDWLRQ +--------------------+ +---+ | +----+ +----+ | p | +-+--> | x | 2 | y | 6 | | +---+ | +----+ +----+ | +--------------------+ +HUH LV D FRPSOHWH SURJUDP WKDW FRQVWUXFWV D 3RLQW REMHFW DQG WUDQVODWHV LWV FRRUGLQDWHV XVLQJ SULQWOQ VWDWHPHQWV WR H[DPLQH WKH FRRUGLQDWHV EHIRUH DQG DIWHU WKH FDOO 1 2 3 4 5 6 7 8 9 10 import java.awt.*; public class PointExample1 { public static void main(String args) { Point p = new Point(3, 8); System.out.println("initially p = " + p); p.translate(-1, -2); System.out.println("after translating p = " + p); } } 7KLV FRGH SURGXFHV WKH IROORZLQJ RXWSXW initially p = java.awt.Point[x=3,y=8] after translating p = java.awt.Point[x=2,y=6] 7KHUH LV VRPHWKLQJ QHZ DW WKH EHJLQQLQJ RI WKLV FODVV ILOH FDOOHG DQ LPSRUW GHFODUDWLRQ 5HPHPEHU WKDW -DYD KDV D ODUJH QXPEHU RI FODVVHV LQFOXGHG LQ ZKDW DUH FROOHFWLYHO\ NQRZQ DV WKH -DYD FODVV OLEUDULHV 7R KHOS PDQDJH WKHVH FODVVHV -DYD SURYLGHV DQ RUJDQL]DWLRQDO XQLW NQRZQ DV D SDFNDJH 5HODWHG FODVVHV DUH FRPELQHG WRJHWKHU LQWR D VLQJOH SDFNDJH 3DFNDJH $ FROOHFWLRQ RI UHODWHG -DYD FODVVHV )RU H[DPSOH WKH 3RLQW FODVV LV VWRUHG LQ D SDFNDJH NQRZQ DV MDYD DZW ZKLFK LV DQ DEEUHYLDWLRQ IRU WKH -DYD $EVWUDFW :LQGRZLQJ 7RRONLW -DYD SURJUDPV GRQ W QRUPDOO\ KDYH DFFHVV WR D SDFNDJH XQOHVV WKH\ LQFOXGH DQ LPSRUW GHFODUDWLRQ 152 ,PSRUW 'HFODUDWLRQ $ UHTXHVW WR DFFHVV D VSHFLILF -DYD SDFNDJH :H KDYHQ W QHHGHG DQ LPSRUW VWDWHPHQW \HW EHFDXVH -DYD DXWRPDWLFDOO\ LPSRUWV HYHU\ FODVV VWRUHG LQ D SDFNDJH FDOOHG MDYD ODQJ 7KH MDYD ODQJ SDFNDJH LQFOXGHV EDVLF FODVVHV WKDW PRVW -DYD SURJUDPV ZRXOG EH OLNHO\ WR XVH H J 6\VWHP 6WULQJ 0DWK %HFDXVH -DYD GRHV QRW DXWRPDWLFDOO\ LPSRUW MDYD DZW ZH KDYH WR GR LW RXUVHOYHV 7KH LPSRUW GHFODUDWLRQ DOORZV \RX WR LPSRUW MXVW D VLQJOH FODVV IURP D SDFNDJH DV LQ import java.awt.Point; 6RPH SHRSOH SUHIHU WR VSHFLILFDOO\ PHQWLRQ HDFK FODVV WKDW \RX DUH LPSRUWLQJ 7KH SUREOHP LV WKDW RQFH \RX VWDUW LPSRUWLQJ RQH FODVV IURP D SDFNDJH \RX UH OLNHO\ WR ZDQW WR LPSRUW RWKHUV DV ZHOO -DYD DOORZV \RX WR XVH DQ DVWHULVN WR LPSRUW DOO FODVVHV IURP D SDFNDJH import java.awt.*; :H ZLOO XVH WKH DVWHULVN YHUVLRQ RI LPSRUW LQ WKLV ERRN WR NHHS WKLQJV VLPSOH 5HIHUHQFH 6HPDQWLFV 2EMHFWV DUH VWRUHG LQ WKH FRPSXWHU V PHPRU\ LQ D GLIIHUHQW ZD\ WKDQ SULPLWLYH GDWD )RU H[DPSOH ZKHQ ZH GHFODUH DQ LQWHJHU YDULDEOH int x = 8; 7KH YDULDEOH VWRUHV WKH DFWXDO GDWD 6R ZH YH GUDZQ SLFWXUHV OLNH WKH IROORZLQJ +---+ x | 8 | +---+ 7KH VLWXDWLRQ LV GLIIHUHQW IRU REMHFWV :LWK REMHFWV WKH YDULDEOH GRHVQ W VWRUH WKH DFWXDO GDWD ,QVWHDG WKH GDWD LV VWRUHG LQ DQ REMHFW DQG WKH YDULDEOH VWRUHV D UHIHUHQFH WR ZKHUH WKH REMHFW LV VWRUHG 6R ZH KDYH WZR GLIIHUHQW HOHPHQWV LQ PHPRU\ WKH YDULDEOH DQG WKH REMHFW 6R ZKHQ ZH FRQVWUXFW D 3RLQW REMHFW Point p = new Point(3, 8); ZH HQG XS ZLWK WKH IROORZLQJ +--------------------+ +---+ | +----+ +----+ | p | +-+--> | x | 3 | y | 8 | | +---+ | +----+ +----+ | +--------------------+ 153 $V WKH GLDJUDP LQGLFDWHV ZH KDYH WZR GLIIHUHQW YDOXHV VWRUHG LQ PHPRU\ :H KDYH WKH 3RLQW REMHFW LWVHOI ZKLFK DSSHDUV RQ WKH ULJKW VLGH RI WKLV SLFWXUH :H DOVR KDYH D YDULDEOH FDOOHG S ZKLFK VWRUHV D UHIHUHQFH WR WKH 3RLQW REMHFW UHSUHVHQWHG LQ WKLV SLFWXUH DV DQ DUURZ :H VD\ WKDW S UHIHUV WR WKH REMHFW 7KLV FRQYHQWLRQ ZLOO WDNH VRPH WLPH WR JHW XVHG WR EHFDXVH WKHVH DUH WZR GLIIHUHQW DSSURDFKHV WR VWRULQJ GDWD 7KHVH DSSURDFKHV DUH VR FRPPRQ WKDW FRPSXWHU VFLHQWLVWV KDYH WHFKQLFDO WHUPV WR GHVFULEH WKHP 7KH V\VWHP IRU WKH SULPLWLYH W\SHV OLNH LQW LV NQRZQ DV YDOXH VHPDQWLFV DQG WKRVH W\SHV DUH RIWHQ UHIHUUHG WR DV YDOXH W\SHV 7KH V\VWHP IRU 3RLQWV DQG RWKHU REMHFWV LV NQRZQ DV UHIHUHQFH VHPDQWLFV DQG WKRVH W\SHV DUH RIWHQ UHIHUUHG WR DV UHIHUHQFH W\SHV 9DOXH 6HPDQWLFV $ V\VWHP LQ ZKLFK YDOXHV DUH VWRUHG GLUHFWO\ DQG FRS\LQJ LQYROYHV WKH FUHDWLRQ RI LQGHSHQGHQW FRSLHV RI YDOXHV 7\SHV WKDW XVH YDOXH VHPDQWLFV DUH FDOOHG YDOXH W\SHV 5HIHUHQFH 6HPDQWLFV $ V\VWHP LQ ZKLFK UHIHUHQFHV WR YDOXHV DUH VWRUHG DQG FRS\LQJ LQYROYHV FRS\LQJ UHIHUHQFHV 7\SHV WKDW XVH UHIHUHQFH VHPDQWLFV DUH FDOOHG UHIHUHQFH W\SHV ,W ZLOO WDNH XV D ZKLOH WR H[SORUH DOO RI WKH LPSOLFDWLRQV RI WKLV GLIIHUHQFH 7KH NH\ WKLQJ WR UHPHPEHU LV WKDW ZKHQ \RX DUH ZRUNLQJ ZLWK REMHFWV \RX DUH DOZD\V ZRUNLQJ ZLWK UHIHUHQFHV WR GDWD UDWKHU WKDQ WKH GDWD LWVHOI $W WKLV SRLQW \RX DUH SUREDEO\ ZRQGHULQJ ZK\ -DYD KDV WZR GLIIHUHQW V\VWHPV -DYD ZDV GHVLJQHG IRU REMHFW RULHQWHG SURJUDPPLQJ VR WKH ILUVW TXHVWLRQ WR FRQVLGHU LV ZK\ 6XQ GHFLGHG WKDW REMHFWV VKRXOG KDYH UHIHUHQFH VHPDQWLFV 7KHUH DUH WZR SULPDU\ UHDVRQV HIILFLHQF\ 2EMHFWV FDQ EH FRPSOH[ ZKLFK PHDQV WKDW WKH\ FDQ WDNH XS D ORW RI VSDFH LQ PHPRU\ :H GRQ W ZDQW WR KDYH WR PDNH FRSLHV RI VXFK REMHFWV EHFDXVH ZH ZRXOG UXQ RXW RI PHPRU\ )RU H[DPSOH D 6WULQJ REMHFW PLJKW WDNH XS D ORW RI VSDFH LQ PHPRU\ EHFDXVH LW PLJKW VWRUH D ODUJH QXPEHU RI FKDUDFWHUV %XW HYHQ LI VXFK D 6WULQJ REMHFW LV YHU\ ODUJH D UHIHUHQFH WR LW FDQ EH IDLUO\ VPDOO LQ WKH VDPH ZD\ WKDW HYHQ D KRXVH WKDW LV D PDQVLRQ ZLOO KDYH D VLPSOH VWUHHW DGGUHVV $V DQRWKHU DQDORJ\ WKLQN RI KRZ SHRSOH XVH FHOO SKRQHV 7KH SKRQHV FDQ EH YHU\ WLQ\ DQG HDV\ WR WUDQVSRUW EHFDXVH FHOO SKRQH QXPEHUV GRQ W WDNH XS PXFK VSDFH ,PDJLQH KRZ GLIIHUHQW LW ZRXOG EH LI LQVWHDG RI FDUU\LQJ DURXQG D VHW RI FHOO SKRQH QXPEHUV \RX WULHG WR FDUU\ DURXQG WKH DFWXDO SHRSOH LQVWHDG VKDULQJ ,W LV RIWHQ WKH FDVH WKDW KDYLQJ D FRS\ RI VRPHWKLQJ LV QRW JRRG HQRXJK 6XSSRVH WKDW \RXU LQVWUXFWRU WHOOV DOO RI WKH VWXGHQWV LQ WKH FODVV WR SXW WKHLU WHVWV LQWR D FHUWDLQ ER[ DIWHU WKH H[DP LV RYHU ,PDJLQH KRZ FRQIXVLQJ LW ZRXOG EH LI HDFK VWXGHQW PDGH D FRS\ RI WKH ER[ 7KH REYLRXV LQWHQW LV WKDW DOO RI WKH VWXGHQWV ZLOO EH XVLQJ WKH VDPH ER[ :LWK UHIHUHQFH VHPDQWLFV \RX FDQ KDYH PDQ\ UHIHUHQFHV WR D VLQJOH REMHFW ZKLFK DOORZV GLIIHUHQW SDUWV RI \RXU SURJUDP WR VKDUH D FHUWDLQ REMHFW 6R ZLWKRXW UHIHUHQFH VHPDQWLFV -DYD SURJUDPV ZRXOG EH PRUH GLIILFXOW WR ZULWH 7KHQ ZK\ GLG 6XQ GHFLGH WR LQFOXGH SULPLWLYH W\SHV WKDW KDYH YDOXH VHPDQWLFV" 7KH UHDVRQV DUH SULPDULO\ KLVWRULFDO 6XQ ZDQWHG WR OHYHUDJH WKH SRSXODULW\ RI & DQG & ZKLFK KDG VLPLODU W\SHV DQG 6XQ ZDQWHG WR 154 JXDUDQWHH WKDW -DYD SURJUDPV ZRXOG UXQ TXLFNO\ ZKLFK ZDV HDVLHU WR DFFRPSOLVK ZLWK WKH PRUH WUDGLWLRQDO SULPLWLYH W\SHV ,I 6XQ KDG D FKDQFH WR UHGHVLJQ -DYD WRGD\ WKH\ PLJKW YHU\ ZHOO JHW ULG RI WKH SULPLWLYH W\SHV DQG JR ZLWK D FRQVLVWHQW REMHFW PRGHO ZLWK MXVW UHIHUHQFH VHPDQWLFV 0XOWLSOH 2EMHFWV ,Q WKH ODVW VHFWLRQ ZH VDZ KRZ WR PDQLSXODWH D VLQJOH REMHFW &RQVLGHU WKH IROORZLQJ OLQHV RI FRGH WKDW FRQVWUXFW WZR GLIIHUHQW REMHFWV Point p1 = new Point(3, 8); Point p2 = new Point(); (DFK REMHFW PXVW EH FRQVWUXFWHG VHSDUDWHO\ E\ D FDOO RQ QHZ 5HPHPEHU WKDW ZH FDOO WKHVH LQVWDQFHV RI WKH FODVV 7KH ILUVW FDOO SDVVHV WKH [ FRRUGLQDWH DQG \ FRRUGLQDWH RI WKH SRLQW 7KH VHFRQG FDOO XVHV D GLIIHUHQW FRQVWUXFWRU ZLWK D GLIIHUHQW PHWKRG VLJQDWXUH ]HUR SDUDPHWHUV LQVWHDG RI WZR SDUDPHWHUV 7KLV FRQVWUXFWRU VHWV WKH [ FRRUGLQDWH DQG \ FRRUGLQDWH WR 7KH DUJXPHQW FRQVWUXFWRU LV RIWHQ UHIHUUHG WR DV WKH GHIDXOW FRQVWUXFWRU $IWHU H[HFXWLQJ WKHVH VWDWHPHQWV RXU PHPRU\ ZRXOG ORRN DV IROORZV +--------------------+ +---+ | +----+ +----+ | p1 | +-+--> | x | 3 | y | 8 | | +---+ | +----+ +----+ | +--------------------+ +--------------------+ +---+ | +----+ +----+ | p2 | +-+--> | x | 0 | y | 0 | | +---+ | +----+ +----+ | +--------------------+ 7R XQGHUVFRUH WKH IDFW WKDW YDULDEOHV DQG REMHFWV DUH VWRUHG VHSDUDWHO\ FRQVLGHU ZKDW ZRXOG KDSSHQ LI \RX QRZ H[HFXWH WKH IROORZLQJ FRGH Point p3 = p2; 7KLV GHFODUHV D WKLUG 3RLQW YDULDEOH EXW GRHVQ W LQFOXGH D WKLUG FDOO RQ QHZ 7KDW PHDQV WKDW ZH VWLOO KDYH MXVW WKH WZR 3RLQW REMHFWV HYHQ WKRXJK ZH QRZ KDYH WKUHH 3RLQW YDULDEOHV 155 +--------------------+ +---+ | +----+ +----+ | p1 | +-+--> | x | 3 | y | 8 | | +---+ | +----+ +----+ | +--------------------+ +--------------------+ +---+ | +----+ +----+ | p2 | +-+--> | x | 0 | y | 0 | | +---+ | +----+ +----+ | +--------------------+ ^ +---+ | p3 | --+--------------+ +---+ 7KH YDULDEOHV S DQG S ERWK UHIHU WR WKH VDPH 3RLQW REMHFW 7KLV VLWXDWLRQ GRHVQ W DULVH ZKHQ \RX XVH YDOXH W\SHV OLNH LQW ,W KDSSHQV RQO\ ZLWK REMHFWV /HW V ORRN DW D FRPSOHWH SURJUDP WR H[SORUH KRZ WKLV ZRUNV 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 import java.awt.*; public class PointExample2 { public static void main(String args) { // declare variables and construct objects Point p1 = new Point(3, 8); Point p2 = new Point(); Point p3 = p2; // see what is in each System.out.println("p1 System.out.println("p2 System.out.println("p3 System.out.println(); // three translations p1.translate(-1, -2); p2.translate(4, 8); p3.translate(2, 3); // see what is in each System.out.println("p1 System.out.println("p2 System.out.println("p3 } } point = " + = " + = " + now p1); p2); p3); point = " + = " + = " + initially p1); p2); p3); 7KLV SURJUDP SURGXFHV RXWSXW WKDW PLJKW EH VXUSULVLQJ p1 = java.awt.Point[x=3,y=8] p2 = java.awt.Point[x=0,y=0] p3 = java.awt.Point[x=0,y=0] p1 = java.awt.Point[x=2,y=6] p2 = java.awt.Point[x=6,y=11] p3 = java.awt.Point[x=6,y=11] 156 :H HQG XS ZLWK FRRUGLQDWHV EXW WKHUH DUHQ W DQ\ V RU V LQ WKH RULJLQDO SURJUDP /HW V VHH KRZ WKLV KDSSHQV 7KH PDQLSXODWLRQV XVLQJ YDULDEOH S DUH IDLUO\ VWUDLJKWIRUZDUG 9DULDEOHV S DQG S DUH PRUH FRPSOH[ 5HPHPEHU WKDW ZH KDYH D VLQJOH 3RLQW REMHFW WKDW ERWK RI WKHVH YDULDEOHV UHIHU WR 7KLV REMHFW LQLWLDOO\ KDV FRRUGLQDWHV 6R DIWHU H[HFXWLQJ WKH ILUVW WKUHH OLQHV RI FRGH PHPRU\ ORRNV OLNH WKLV +--------------------+ +---+ | +----+ +----+ | p1 | +-+--> | x | 3 | y | 8 | | +---+ | +----+ +----+ | +--------------------+ +--------------------+ +---+ | +----+ +----+ | p2 | +-+--> | x | 0 | y | 0 | | +---+ | +----+ +----+ | +--------------------+ ^ +---+ | p3 | --+--------------+ +---+ :KHQ ZH H[HFXWH WKH ILUVW VHW RI SULQWOQ VWDWHPHQWV ZH VHH WKDW S KDV FRRUGLQDWHV :KHQ ZH H[HFXWH WKH SULQWOQ VWDWHPHQW RQ S ZH JHW WKH FRRUGLQDWHV DQG ZKHQ ZH H[HFXWH WKH SULQWOQ VWDWHPHQW RQ S ZH JHW WKHVH FRRUGLQDWHV DJDLQ EHFDXVH HDFK YDULDEOH LV UHIHUULQJ WR WKLV REMHFW 7KHQ ZH WUDQVODWH WKH FRRUGLQDWHV IRU S E\ LQ WKH [ GLUHFWLRQ DQG LQ WKH \ GLUHFWLRQ +--------------------+ +---+ | +----+ +----+ | p1 | +-+--> | x | 2 | y | 6 | | +---+ | +----+ +----+ | +--------------------+ +--------------------+ +---+ | +----+ +----+ | p2 | +-+--> | x | 0 | y | 0 | | +---+ | +----+ +----+ | +--------------------+ ^ +---+ | p3 | --+--------------+ +---+ 7KHQ ZH WUDQVODWH WKH FRRUGLQDWHV IRU S E\ LQ WKH [ GLUHFWLRQ DQG E\ LQ WKH \ GLUHFWLRQ 157 +--------------------+ +---+ | +----+ +----+ | p1 | +-+--> | x | 2 | y | 6 | | +---+ | +----+ +----+ | +--------------------+ +--------------------+ +---+ | +----+ +----+ | p2 | +-+--> | x | 4 | y | 8 | | +---+ | +----+ +----+ | +--------------------+ ^ +---+ | p3 | --+--------------+ +---+ 7KHQ ZH WUDQVODWH WKH FRRUGLQDWHV IRU S E\ LQ WKH [ GLUHFWLRQ DQG LQ WKH \ GLUHFWLRQ %XW EHFDXVH S UHIHUV WR WKH VDPH REMHFW DV S RQH +--------------------+ +---+ | +----+ +----+ | p1 | +-+--> | x | 2 | y | 6 | | +---+ | +----+ +----+ | +--------------------+ +--------------------+ +---+ | +----+ +----+ | p2 | +-+--> | x | 6 | y | 11 | | +---+ | +----+ +----+ | +--------------------+ ^ +---+ | p3 | --+--------------+ +---+ WKLV WUDQVODWLRQ LV LQ HIIHFW DGGHG WR WKH SUHYLRXV :H VWDUWHG ZLWK WKH FRRUGLQDWHV DQG ZH WUDQVODWHG S E\ DQG S E\ EXW S DQG S UHIHU WR WKH VDPH REMHFW ZKLFK PHDQV ZH HQGHG XS WUDQVODWLQJ WKDW VLQJOH SRLQW WZLFH OHDYLQJ LW DW 7KH ILQDO SULQWOQ VWDWHPHQWV UHSRUW WKH QHZ FRRUGLQDWHV RI S DQG UHSRUW WKH QHZ FRRUGLQDWHV RI WKH RWKHU SRLQW WZLFH RQFH WKURXJK WKH SULQWOQ RQ S DQG RQFH WKURXJK WKH SULQWOQ RQ S 2EMHFWV DV 3DUDPHWHUV WR 0HWKRGV &RQVLGHU ZKDW KDSSHQV ZKHQ \RX GHILQH D VWDWLF PHWKRG WKDW XVHV D UHIHUHQFH W\SH IRU LWV SDUDPHWHU public static void manipulate(Point p) { p.translate(2, 3); } 5HPHPEHU WKDW D SDUDPHWHU EHFRPHV D FRS\ RI ZKDWHYHU LV SDVVHG LQ WKH FDOO :LWK WKH YDOXH W\SHV OLNH LQW DQ\ FKDQJHV WKDW ZH PDGH WR WKH SDUDPHWHU KDYH QR HIIHFW RQ DQ\ YDULDEOH SDVVHG WR WKH PHWKRG 7KH VLWXDWLRQ LV PRUH FRPSOLFDWHG IRU REMHFWV EHFDXVH RI WKHLU UHIHUHQFH VHPDQWLFV 6XSSRVH WKDW ZH KDYH GHILQHG D 3RLQW YDULDEOH DQG ZH SDVV LW WR WKLV PHWKRG 158 Point test = new Point(6, 15); manipulate(test); 'RHV WKH PHWKRG HQG XS WUDQVODWLQJ WKH FRRUGLQDWHV RI WKH REMHFW" 7KH DQVZHU LV \HV HYHQ WKRXJK ZH DUH XVLQJ D SDUDPHWHU WKDW FUHDWHV D FRS\ 7KLQN RI ZKDW LV KDSSHQLQJ LQ WKH FRPSXWHU V PHPRU\ :KHQ ZH GHFODUH RXU YDULDEOH WHVW ZH HQG XS ZLWK WKLV VLWXDWLRQ +--------------------+ +---+ | +----+ +----+ | test | +-+--> | x | 6 | y | 15 | | +---+ | +----+ +----+ | +--------------------+ :KHQ ZH FDOO WKH PDQLSXODWH PHWKRG -DYD PDNHV D FRS\ RI WKH YDULDEOH WHVW DQG VWRUHV WKLV LQ WKH SDUDPHWHU FDOOHG S %XW WKH YDULDEOH WHVW LVQ W LWVHOI DQ REMHFW ,W VWRUHV D UHIHUHQFH WR DQ REMHFW 6R ZKHQ ZH PDNH D FRS\ RI LW ZH HQG XS ZLWK WKLV VLWXDWLRQ LQ PHPRU\ +--------------------+ +---+ | +----+ +----+ | test | +-+--> | x | 6 | y | 15 | | +---+ | +----+ +----+ | +--------------------+ ^ +---+ | p | --+-----------------+ +---+ :H QRZ KDYH WZR YDULDEOHV WKDW UHIHU WR WKH VDPH 3RLQW REMHFW 6R LW GRHVQ W PDWWHU WKDW S LV D FRS\ EHFDXVH LW V UHIHUULQJ WR WKH VDPH REMHFW DV WKH RULJLQDO 6R DQ\ FDOOV RQ WUDQVODWH XVLQJ S ZLOO FKDQJH WKH REMHFW WKDW WHVW UHIHUV WR 7KHUH LV RQH RWKHU FDVH ZRUWK FRQVLGHULQJ :KDW LI WKH PDQLSXODWH PHWKRG KDG EHHQ ZULWWHQ WKLV ZD\ LQVWHDG public static void manipulate(Point p) { p.translate(2, 3); p = new Point(17, 45); } /HW V ORRN DW WKLV LQ GHWDLO :KHQ WKH PHWKRG LV FDOOHG ZH VHW XS WKH YDULDEOH S DV D FRS\ RI WKH YDULDEOH WHVW ZKLFK OHDGV WR WZR YDULDEOHV UHIHUULQJ WR WKH VDPH 3RLQW REMHFW +--------------------+ +---+ | +----+ +----+ | test | +-+--> | x | 6 | y | 15 | | +---+ | +----+ +----+ | +--------------------+ ^ +---+ | p | --+-----------------+ +---+ :H WUDQVODWH WKH FRRUGLQDWHV IRU S E\ YHUVLRQ RI WKH PHWKRG LQ WKH [ GLUHFWLRQ DQG LQ WKH \ GLUHFWLRQ DV LQ WKH ROG 159 +--------------------+ +---+ | +----+ +----+ | test | +-+--> | x | 8 | y | 18 | | +---+ | +----+ +----+ | +--------------------+ ^ +---+ | p | --+-----------------+ +---+ 7KHQ ZH WXUQ DURXQG DQG UHVHW WKH YDULDEOH S WR D QHZ 3RLQW REMHFW ZLWK D GLIIHUHQW VHW RI FRRUGLQDWHV 'RHV WKLV DIIHFW RXU YDULDEOH WHVW" 7KH DQVZHU LV QR %HFDXVH S LV D ORFDO FRS\ FKDQJLQJ LWV YDOXH KDV QR HIIHFW RQ RXU YDULDEOH WHVW +--------------------+ +---+ | +----+ +----+ | test | +-+--> | x | 8 | y | 18 | | +---+ | +----+ +----+ | +--------------------+ +--------------------+ +---+ | +----+ +----+ | p | +-+--> | x | 17 | y | 45 | | +---+ | +----+ +----+ | +--------------------+ 7KH ERWWRP OLQH LV WKDW ZKHQ \RX SDVV DQ REMHFW DV D SDUDPHWHU \RX FDQ FKDQJH WKH REMHFW LWVHOI EXW \RX FDQ W FKDQJH WKH YDULDEOH WKDW SRLQWV WR WKH REMHFW 3KRQH QXPEHUV SURYLGH D XVHIXO DQDORJ\ 6XSSRVH WKDW \RX KDYH D SLHFH RI SDSHU RQ ZKLFK \RX KDYH ZULWWHQ GRZQ D SHUVRQ V SKRQH QXPEHU 7KDW JLYHV \RX WKH DELOW\ WR FDOO WKDW SHUVRQ <RX FDQ PDNH D FRS\ RI WKDW SLHFH RI SDSHU DQG JLYH LW WR VRPHRQH HOVH ZKLFK LV VLPLODU WR ZKDW KDSSHQV ZKHQ \RX SDVV DQ REMHFW DV D SDUDPHWHU WR D PHWKRG %HFDXVH \RX KDYH \RXU RZQ SLHFH RI SDSHU WHOOLQJ \RX WKH SKRQH QXPEHU \RX ZLOO QHYHU ORVH LW %XW WKH RWKHU SHUVRQ KDV WKH SKRQH QXPEHU DV ZHOO ZKLFK PHDQV WKH\ FDQ PDNH D SKRQH FDOO MXVW DV \RX FDQ ,QWHUDFWLYH 3URJUDPV :H KDYH VHHQ WKDW E\ FDOOLQJ 6\VWHP RXW SULQWOQ DQG 6\VWHP RXW SULQW ZH FDQ SURGXFH RXWSXW LQ WKH FRQVROH ZLQGRZ UDWKHU HDVLO\ :H FDQ DOVR ZULWH SURJUDPV WKDW SDXVH DQG ZDLW IRU WKH XVHU WR W\SH D UHVSRQVH 6XFK SURJUDPV DUH NQRZQ DV LQWHUDFWLYH SURJUDPV DQG WKH UHVSRQVHV W\SHG E\ WKH XVHU DUH NQRZQ DV FRQVROH LQSXW &RQVROH ,QSXW 5HVSRQVHV W\SHG E\ WKH XVHU ZKHQ DQ LQWHUDFWLYH SURJUDP SDXVHV IRU LQSXW 160 :KHQ \RX UHIHU WR 6\VWHP RXW \RX DUH DFFHVVLQJ DQ REMHFW LQ WKH 6\VWHP FODVV NQRZQ DV WKH VWDQGDUG RXWSXW VWUHDP RU VWDQGDUG RXW IRU VKRUW 7KHUH LV D FRUUHVSRQGLQJ REMHFW IRU VWDQGDUG LQSXW NQRZQ DV 6\VWHP LQ %XW -DYD ZDVQ W GHVLJQHG IRU FRQVROH LQSXW DQG 6\VWHP LQ KDV QHYHU EHHQ SDUWLFXODUO\ HDV\ WR XVH IRU FRQVROH LQSXW )RUWXQDWHO\ IRU XV WKHUH LV DQ HDVLHU ZD\ WR UHDG FRQVROH LQSXW 6FDQQHU 2EMHFWV 6WDUWLQJ ZLWK YHUVLRQ -DYD KDV D FODVV FDOOHG 6FDQQHU WKDW VLPSOLILHV UHDGLQJ IURP WKH FRQVROH DQG UHDGLQJ IURP ILOHV ,W LV SDUW RI WKH MDYD XWLO SDFNDJH VR ZH ZLOO KDYH WR UHPHPEHU WR LQFOXGH WKH IROORZLQJ LPSRUW GHFODUDWLRQ LQ RXU SURJUDPV WKDW XVH 6FDQQHU import java.util.*; // for Scanner 7R XVH D 6FDQQHU \RX ILUVW KDYH WR FRQVWUXFW RQH <RX FDQ FRQVWUXFW D 6FDQQHU E\ SDVVLQJ LW D UHIHUHQFH WR DQ LQSXW VWUHDP 7R UHDG IURP WKH FRQVROH ZLQGRZ ZH SDVV LW 6\VWHP LQ Scanner console = new Scanner(System.in); 2QFH FRQVWUXFWHG \RX FDQ DVN WKH 6FDQQHU WR UHWXUQ D YDOXH RI D SDUWLFXODU W\SH 7KHUH DUH D QXPEHU RI GLIIHUHQW PHWKRGV WKDW DOO EHJLQ ZLWK WKH ZRUG QH[W WR REWDLQ WKHVH YDULRXV W\SHV RI YDOXHV 6FDQQHU 0HWKRGV 0HWKRG next() nextDouble() nextInt() nextLine() 'HVFULSWLRQ UHDGV DQG UHWXUQV WKH QH[W WRNHQ DV D 6WULQJ UHDGV DQG UHWXUQV D GRXEOH YDOXH UHDGV DQG UHWXUQV DQ LQW YDOXH UHDGV DQG UHWXUQV WKH QH[W OLQH RI LQSXW DV D 6WULQJ 7\SLFDOO\ ZH ZLOO XVH D YDULDEOH WR NHHS WUDFN RI WKH YDOXH UHWXUQHG E\ RQH RI WKHVH PHWKRGV )RU H[DPSOH ZH PLJKW VD\ int n = console.nextInt(); 7KH FDOO RQ WKH FRQVROH REMHFW V QH[W,QW PHWKRG SDXVHV IRU XVHU LQSXW :KHQHYHU WKH FRPSXWHU SDXVHV IRU LQSXW LW ZLOO SDXVH IRU DQ HQWLUH OLQH RI LQSXW ,Q RWKHU ZRUGV ZKHQHYHU WKH FRPSXWHU SDXVHV IRU LQSXW LW ZLOO ZDLW XQWLO WKH XVHU KLWV WKH (QWHU NH\ EHIRUH FRQWLQXLQJ H[HFXWLRQ <RX FDQ XVH WKH 6FDQQHU FODVV WR UHDG LQSXW OLQH E\ OLQH XVLQJ WKH QH[W/LQH PHWKRG DOWKRXJK ZH ZRQ W EH XVLQJ QH[W/LQH YHU\ PXFK IRU QRZ 7KH RWKHU QH[W PHWKRGV DUH DOO WRNHQ EDVHG 7RNHQ $ VLQJOH HOHPHQW RI LQSXW H J RQH ZRUG RQH QXPEHU %\ GHIDXOW WKH 6FDQQHU XVHV ZKLWHVSDFH WR VHSDUDWH WRNHQV 161 :KLWHVSDFH 6SDFHV WDE FKDUDFWHUV DQG QHZ OLQH FKDUDFWHUV $ 6FDQQHU REMHFW ORRNV DW ZKDW WKH XVHU W\SHV DQG XVHV WKH ZKLWHVSDFH RQ WKH LQSXW OLQH WR EUHDN LW XS LQWR LQGLYLGXDO WRNHQV )RU H[DPSOH WKH IROORZLQJ OLQH RI LQSXW hello there. how are "you?" all-one-token ZRXOG EH VSOLW LQWR VL[ WRNHQV hello there. how are "you?" all-one-token 1RWLFH WKDW WKH 6FDQQHU LQFOXGHV SXQFWXDWLRQ FKDUDFWHUV OLNH SHULRGV TXHVWLRQ PDUNV GDVKHV DQG TXRWDWLRQ PDUNV LQ WKH WRNHQV LW JHQHUDWHV $OVR QRWLFH WKDW ZH JHW MXVW RQH WRNHQ IRU DOO RQH WRNHQ 7KDW V EHFDXVH WKHUH LV QR ZKLWHVSDFH LQ WKH PLGGOH WR EUHDN LW XS LQWR GLIIHUHQW WRNHQV <RX FDQ FRQWURO KRZ D 6FDQQHU WXUQV WKLQJV LQWR WRNHQV D SURFHVV FDOOHG WRNHQL]LQJ WKH LQSXW EXW ZH ZRQ W EH GRLQJ DQ\WKLQJ WKDW IDQF\ ,W LV SRVVLEOH WR UHDG PRUH WKDQ RQH YDOXH IURP WKH 6FDQQHU DV LQ double x = console.nextDouble(); double y = console.nextDouble(); %HFDXVH ZH KDYH WZR GLIIHUHQW FDOOV RQ WKH FRQVROH REMHFW V QH[W'RXEOH PHWKRG WKLV FRGH ZLOO FDXVH WKH FRPSXWHU WR SDXVH XQWLO WKH XVHU KDV HQWHUHG WZR QXPEHU YDOXHV 7KH YDOXHV FDQ EH HQWHUHG RQ WKH VDPH OLQH RU RQ VHSDUDWH OLQHV ,Q JHQHUDO WKH FRPSXWHU FRQWLQXHV WR SDXVH IRU XVHU LQSXW XQWLO LW KDV REWDLQHG ZKDWHYHU YDOXHV \RX KDYH DVNHG WKH 6FDQQHU WR REWDLQ ,I D XVHU W\SHV VRPHWKLQJ WKDW LVQ W DQ LQWHJHU ZKHQ \RX UH FDOOLQJ QH[W,QW VXFK DV ;<==< WKH 6FDQQHU REMHFW JHQHUDWHV DQ H[FHSWLRQ 5HFDOO IURP WKH VHFWLRQ RQ 6WULQJ REMHFWV WKDW H[FHSWLRQV DUH UXQWLPH HUURUV WKDW KDOW SURJUDP H[HFXWLRQ ,Q WKLV FDVH D UXQWLPH HUURU RXWSXW VXFK DV WKH IROORZLQJ DSSHDUV Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at Example.main(Example.java:13) :H ZLOO VHH LQ D ODWHU FKDSWHU KRZ WR WHVW IRU XVHU HUURUV ,Q WKH PHDQWLPH ZH ZLOO DVVXPH WKDW WKH XVHU SURYLGHV DSSURSULDWH LQSXW 162 $ 6DPSOH ,QWHUDFWLYH 3URJUDP 8VLQJ WKH 6FDQQHU FODVV ZH FDQ ZULWH D FRPSOHWH LQWHUDFWLYH SURJUDP WKDW SHUIRUPV D XVHIXO FRPSXWDWLRQ IRU WKH XVHU ,I \RX HYHU ILQG \RXUVHOI EX\LQJ D KRXVH \RX OO ZDQW WR NQRZ ZKDW \RXU PRQWKO\ SD\PHQW IRU WKH ORDQ LV JRLQJ WR EH 7KH IROORZLQJ LV D FRPSOHWH SURJUDP WKDW DVNV IRU LQIRUPDWLRQ DERXW D ORDQ DQG SULQWV WKH PRQWKO\ SD\PHQW 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 // This program prompts for information about a loan and computes // the monthly loan payment. import java.util.*; // for Scanner public class Mortgage { public static void main(String args) { Scanner console = new Scanner(System.in); // obtain values System.out.println("This program computes monthly loan payments."); System.out.print("loan amount : "); double loan = console.nextDouble(); System.out.print("number of years : "); int years = console.nextInt(); System.out.print("interest rate : "); double rate = console.nextDouble(); System.out.println(); // compute result and report int n = 12 * years; double c = rate / 12.0 / 100.0; double payment = loan * c * Math.pow(1 + c, n) / (Math.pow(1 + c, n) - 1); System.out.println("monthly payment = $" + (int) payment); } } 7KH IROORZLQJ LV D VDPSOH H[HFXWLRQ RI WKH SURJUDP This program computes monthly loan payments. loan amount : 275000 number of years : 30 interest rate : 6.75 monthly payment = $1783 7KH ILUVW WKLQJ ZH GR LQ WKH SURJUDP LV WR FRQVWUXFW D 6FDQQHU REMHFW WKDW ZH XVH IRU FRQVROH LQSXW 7KHQ WKH SURJUDP H[SODLQV ZKDW LV JRLQJ WR KDSSHQ 7KLV LV HVVHQWLDO IRU LQWHUDFWLYH SURJUDPV <RX GRQ W ZDQW D SURJUDP WR SDXVH IRU XVHU LQSXW XQWLO \RX YH H[SODLQHG WR WKH XVHU ZKDW LV JRLQJ WR KDSSHQ 7KHQ \RX OO QRWLFH VHYHUDO SDLUV RI VWDWHPHQWV OLNH WKHVH System.out.print("loan amount : "); double loan = console.nextDouble(); 163 7KH ILUVW VWDWHPHQW LV FDOOHG D SURPSW D UHTXHVW IRU LQIRUPDWLRQ IURP WKH XVHU :H XVH D SULQW VWDWHPHQW LQVWHDG RI D SULQWOQ VR WKDW WKH XVHU ZLOO W\SH WKH YDOXHV RQ WKH VDPH OLQH DV WKH SURPSW L H WR WKH ULJKW RI WKH SURPSW 7KH VHFRQG VWDWHPHQW FDOOV WKH QH[W'RXEOH PHWKRG RI WKH FRQVROH REMHFW WR UHDG D YDOXH RI W\SH GRXEOH IURP WKH XVHU 7KLV YDOXH LV VWRUHG LQ D YDULDEOH FDOOHG ORDQ 7KLV SDWWHUQ RI SURPSW UHDG VWDWHPHQWV LV FRPPRQ LQ LQWHUDFWLYH SURJUDPV $IWHU SURPSWLQJ IRU YDOXHV WKH SURJUDP FRPSXWHV VHYHUDO YDOXHV 7KH IRUPXOD IRU FRPSXWLQJ PRQWKO\ PRUWJDJH SD\PHQWV LQYROYHV WKH ORDQ DPRXQW WKH WRWDO QXPEHU RI PRQWKV LQYROYHG D YDOXH ZH FDOO Q DQG WKH PRQWKO\ LQWHUHVW UDWH D YDOXH ZH FDOO F 7KH SD\PHQW IRUPXOD LV JLYHQ E\ WKH IROORZLQJ HTXDWLRQ <RX ZLOO QRWLFH LQ WKH SURJUDP WKDW ZH XVH WKH 0DWK SRZ PHWKRG IRU H[SRQHQWLDWLRQ WR WUDQVODWH WKLV IRUPXOD LQWR D -DYD H[SUHVVLRQ 7KH ILQDO OLQH RI WKH SURJUDP SULQWV RXW WKH PRQWKO\ SD\PHQW <RX PLJKW LPDJLQH WKDW ZH ZRXOG VLPSO\ VD\ System.out.println("monthly payment = $" + payment); %HFDXVH WKH SD\PHQW LV VWRUHG LQ D YDULDEOH RI W\SH GRXEOH WKLV ZRXOG SULQW DOO RI WKH GLJLWV RI WKH QXPEHU )RU H[DPSOH IRU WKH ORJ OLVWHG DERYH LW ZRXOG SULQW WKH IROORZLQJ monthly payment = $1783.6447655625927 7KDW LV D UDWKHU VWUDQJH ORRNLQJ RXWSXW IRU VRPHRQH XVHG WR GROODUV DQG FHQWV )RU WKH SXUSRVHV RI WKLV VLPSOH SURJUDP LW V HDV\ WR FDVW WKLV WR DQ LQW DQG UHSRUW MXVW WKH GROODU DPRXQW RI WKH SD\PHQW System.out.println("monthly payment = $" + (int) payment); 0RVW SHRSOH WU\LQJ WR ILJXUH RXW D PRUWJDJH DUHQ W WKDW LQWHUHVWHG LQ WKH SHQQLHV VR WKH SURJUDP LV VWLOO XVHIXO ,Q WKH QH[W VHFWLRQ ZH ZLOO VHH KRZ WR URXQG D QXPEHU OLNH WKLV WR WZR GHFLPDO SODFHV &DVH 6WXG\ 3URMHFWLOH 7UDMHFWRU\ ,W V WLPH WR SXOO WRJHWKHU WKH GLIIHUHQW WKUHDGV RI WKLV FKDSWHU ZLWK D PRUH FRPSOH[ H[DPSOH WKDW ZLOO LQYROYH SDUDPHWHUV PHWKRGV WKDW UHWXUQ YDOXHV PDWKHPDWLFDO FRPSXWDWLRQV DQG WKH XVH RI D 6FDQQHU REMHFW IRU FRQVROH LQSXW 3K\VLFV VWXGHQWV DUH RIWHQ DVNHG WR FRQVLGHU WKH WUDMHFWRU\ WKDW D SURMHFWLOH ZLOO IROORZ JLYHQ LWV LQLWLDO YHORFLW\ DQG LWV LQLWLDO DQJOH UHODWLYH WR WKH KRUL]RQWDO )RU H[DPSOH WKH SURMHFWLOH PLJKW EH D IRRWEDOO WKDW KDV EHHQ NLFNHG E\ D SHUVRQ :H ZDQW WR FRPSXWH WKH SDWK LW IROORZV JLYHQ (DUWK V JUDYLW\ 7R NHHS WKH FRPSXWDWLRQ UHDVRQDEOH ZH ZLOO LJQRUH DLU UHVLVWDQFH 7KHUH DUH VHYHUDO TXHVWLRQV WKDW ZH JHQHUDOO\ ZDQW WR DQVZHU IRU VXFK D SUREOHP :KHQ GRHV WKH SURMHFWLOH UHDFK LWV KLJKHVW SRLQW" +RZ KLJK GRHV LW UHDFK" 164 +RZ ORQJ GRHV LW WDNH WR FRPH EDFN WR WKH JURXQG" +RZ IDU GRHV LW ODQG IURP ZKHUH LW ZDV ODXQFKHG" <RX FRXOG LPDJLQH VHYHUDO ZD\V WR DQVZHU WKHVH TXHVWLRQV 2QH VLPSOH DSSURDFK LV WR SURYLGH D WDEOH WKDW GLVSOD\V WKH WUDMHFWRU\ VWHS E\ VWHS LQGLFDWLQJ WKH [ SRVLWLRQ \ SRVLWLRQ DQG HODSVHG WLPH 7R PDNH VXFK D WDEOH ZH QHHG WR REWDLQ WKUHH YDOXHV IURP WKH XVHU WKH LQLWLDO YHORFLW\ WKH DQJOH UHODWLYH WR WKH KRUL]RQWDO DQG WKH QXPEHU RI VWHSV WR LQFOXGH LQ WKH WDEOH ZH ZLOO SURGXFH :H FRXOG DVN IRU WKH YHORFLW\ HLWKHU LQ PHWHUV VHFRQG RU IHHW VHFRQG *LYHQ WKDW WKLV LV D 3K\VLFV SUREOHP ZH OO VWLFN WR WKH 0HWULF 6\VWHP DQG DVN IRU PHWHUV VHFRQG :H DOVR KDYH WR WKLQN DERXW KRZ WR VSHFLI\ WKH DQJOH 8QIRUWXQDWHO\ PRVW RI WKH -DYD PHWKRGV WKDW RSHUDWH RQ DQJOHV UHTXLUH DQ DQJOH LQ UDGLDQV UDWKHU WKDQ GHJUHHV :H FRXOG DVN IRU WKH XVHU WR JLYH XV WKH DQJOH LQ UDGLDQV EXW WKDW ZRXOG EH KLJKO\ LQFRQYHQLHQW IRU WKH XVHU ,QVWHDG ZH FDQ DOORZ WKH XVHU WR HQWHU WKH DQJOH LQ GHJUHHV DQG ZH FDQ FRQYHUW LW WR UDGLDQV XVLQJ WKH EXLOW LQ PHWKRG 0DWK WR5DGLDQV 6R WKH LQWHUDFWLYH SDUW RI RXU SURJUDP ZRXOG ORRN OLNH WKLV Scanner console = new Scanner(System.in); System.out.print("velocity (meters/second)? "); double velocity = console.nextDouble(); System.out.print("angle relative to horizontal (degrees)? "); double angle = Math.toRadians(console.nextDouble()); System.out.print("number of steps to display? "); int steps = console.nextInt(); 1RWLFH WKDW IRU WKH YHORFLW\ DQG DQJOH ZH FDOO WKH QH[W'RXEOH PHWKRG RI WKH FRQVROH REMHFW EHFDXVH ZH ZDQW WR OHW WKH XVHU VSHFLI\ DQ\ QXPEHU LQFOXGLQJ RQH ZLWK D GHFLPDO SRLQW EXW IRU WKH QXPEHU RI VWHSV ZH FDOO QH[W,QW EHFDXVH WKH QXPEHU RI OLQHV LQ RXU WDEOH QHHGV WR EH DQ LQWHJHU /RRN PRUH FORVHO\ DW WKLV OLQH RI FRGH double angle = Math.toRadians(console.nextDouble()); 6RPH EHJLQQHUV ZRXOG ZULWH WKLV DV WZR VHSDUDWH VWHSV double angleInDegrees = console.nextDouble(); double angle = Math.toRadians(angleInDegrees); %RWK DSSURDFKHV ZRUN DQG ERWK DSSURDFKHV DUH UHDVRQDEOH EXW NHHS LQ PLQG WKDW \RX GRQ W QHHG WR EUHDN WKLV XS LQWR WZR VHSDUDWH VWHSV <RX FDQ ZULWH LW LQ WKH PRUH FRPSDFW IRUP DV D VLQJOH OLQH RI FRGH 2QFH ZH KDYH REWDLQHG WKHVH YDOXHV IURP WKH XVHU ZH DUH UHDG\ WR EHJLQ WKH FRPSXWDWLRQV IRU RXU WUDMHFWRU\ WDEOH :H QHHG WR FRPSXWH WKH [ FRPSRQHQW RI WKH YHORFLW\ YHUVXV WKH \ FRPSRQHQW RI WKH YHORFLW\ )URP 3K\VLFV ZH NQRZ WKDW WKHVH FDQ EH FRPSXWHG DV IROORZV double xVelocity = velocity * Math.cos(angle); double yVelocity = velocity * Math.sin(angle); 165 %HFDXVH ZH DUH LJQRULQJ WKH SRVVLELOLW\ RI DLU UHVLVWDQFH WKH [ YHORFLW\ ZLOO QRW FKDQJH 7KH \ YHORFLW\ LV VXEMHFW WR WKH SXOO RI JUDYLW\ 3K\VLFV WHOOV XV WKDW RQ WKH VXUIDFH RI WKH (DUWK JUDYLW\ LV DSSUR[LPDWHO\ PHWHUV VHFRQG 7KLV LV DQ DSSURSULDWH YDOXH WR GHILQH DV D FODVV FRQVWDQW public static final double EARTH_ACCELERATION = -9.81; 1RWLFH WKDW ZH GHILQH JUDYLW\ DV D QHJDWLYH QXPEHU EHFDXVH LW GHFUHDVHV WKH \ YHORFLW\ RI DQ REMHFW SXOOLQJ LW GRZQ DV RSSRVHG WR SXVKLQJ LW DZD\ 2XU JRDO LV WR GLVSOD\ [ \ DQG HODSVHG WLPH DV WKH REMHFW JRHV XS DQG FRPHV EDFN GRZQ DJDLQ 7KH \ YHORFLW\ GHFUHDVHV VWHDGLO\ XQWLO LW EHFRPHV )URP 3K\VLFV ZH NQRZ WKDW WKHUH LV D V\PPHWU\ LQ WKLV SUREOHP 7KH SURMHFWLOH ZLOO JR XSZDUG XQWLO LWV \ YHORFLW\ UHDFKHV DQG WKHQ LW ZLOO IROORZ D VLPLODU SDWK EDFN GRZQ WKDW WDNHV DQ HTXDO DPRXQW RI WLPH 7KXV WKH WRWDO WLPH LQYROYHG LQ VHFRQGV FDQ EH FRPSXWHG DV IROORZV double totalTime = -2.0 * yVelocity / EARTH_ACCELERATION; 1RZ KRZ GR ZH FRPSXWH WKH YDOXHV RI [ \ DQG HODSVHG WLPH WR LQFOXGH LQ RXU WDEOH" 7ZR RI WKHVH DUH UHODWLYHO\ VLPSOH :H ZDQW VWHDG\ WLPH LQFUHPHQWV IRU HDFK GLIIHUHQW HQWU\ LQ WKH WDEOH VR ZH FDQ FRPSXWH WKH WLPH LQFUHPHQW E\ GLYLGLQJ WRWDO WLPH E\ WKH QXPEHU RI VWHSV ZH ZDQW WR LQFOXGH LQ RXU WDEOH double timeIncrement = totalTime / steps; $V QRWHG HDUOLHU WKH [ YHORFLW\ GRHV QRW FKDQJH VR IRU HDFK RI WKHVH WLPH LQFUHPHQWV ZH PRYH WKH VDPH GLVWDQFH LQ WKH [ GLUHFWLRQ double xIncrement = xVelocity * timeIncrement; 7KH WULFN\ YDOXH WR FRPSXWH KHUH LV WKH \ SRVLWLRQ %HFDXVH RI JUDYLW\ WKH \ YHORFLW\ FKDQJHV RYHU WLPH %XW IURP 3K\VLFV ZH KDYH WKH IROORZLQJ JHQHUDO IRUPXOD IRU FRPSXWLQJ WKH GLVSODFHPHQW RI DQ REMHFW JLYHQ WKH YHORFLW\ Y WLPH W DQG DFFHOHUDWLRQ D GLVSODFHPHQW YW DW ,Q RXU FDVH WKH YHORFLW\ ZH ZDQW LV WKH \ YHORFLW\ DQG WKH DFFHOHUDWLRQ LV (DUWK V JUDYLW\ +HUH WKHQ LV D SVHXGRFRGH GHVFULSWLRQ RI KRZ WR FUHDWH WKH WDEOH set all of x, y and t to 0 for (given number of steps) { t += timeIncrement; x += xIncrement; y = yVelocity * t + 0.5 * EARTH_ACCELERATION * t * t; report step #, x, y, t } :H DUH IDLUO\ FORVH WR KDYLQJ UHDO -DYD FRGH KHUH EXW ZH KDYH WR WKLQN DERXW KRZ WR UHSRUW WKH YDOXHV RI [ \ DQG W LQ D WDEOH 7KH\ ZLOO DOO EH RI W\SH GRXEOH 7KDW PHDQV WKH\ DUH OLNHO\ WR SURGXFH D ODUJH QXPEHU RI GLJLWV DIWHU WKH GHFLPDO SRLQW *HQHUDOO\ ZH DUHQ W LQWHUHVWHG LQ VHHLQJ VR PDQ\ GLJLWV DQG ZH SDUWLFXODUO\ DUHQ W LQWHUHVWHG LQ WKLV FDVH EHFDXVH RXU FRPSXWDWLRQV DUHQ W WKDW DFFXUDWH 166 %HIRUH ZH WU\ WR FRPSOHWH WKH FRGH IRU WKH WDEOH OHW V WKLQN DERXW WKH SUREOHP RI GLVSOD\LQJ RQO\ VRPH RI WKH GLJLWV RI D QXPEHU 7KH LGHD LV WR WUXQFDWH WKH GLJLWV VR WKDW ZH GRQ W KDYH WR VHH DOO RI WKHP :H YH VHHQ D ZD\ WR WUXQFDWH GLJLWV :H FDQ FDVW D GRXEOH WR DQ LQW DQG WKDW WUXQFDWHV DOO RI WKH GLJLWV DIWHU WKH GHFLPDO SRLQW :H FRXOG GR WKDW EXW ZH SUREDEO\ ZDQW DW OHDVW VRPH RI WKRVH GLJLWV )RU H[DPSOH KRZ FRXOG ZH WUXQFDWH DOO EXW WZR RI WKH GLJLWV" 7KH WULFN LV WR EULQJ WKH WZR GLJLWV ZH ZDQW WR WKH RWKHU VLGH RI WKH GHFLPDO SRLQW :H FDQ GR WKDW E\ PXOWLSO\LQJ E\ DQG WKHQ FDVWLQJ WR LQW (int) (n * 100.0) 7KLV H[SUHVVLRQ JHWV XV WKH GLJLWV ZH ZDQW EXW QRZ WKH GHFLPDO SRLQW LV LQ WKH ZURQJ SODFH )RU H[DPSOH LI Q LV LQWLDOO\ WKH H[SUHVVLRQ DERYH ZLOO JLYH XV :H KDYH WR GLYLGH WKLV UHVXOW E\ WR WXUQ LW EDFN LQWR WKH QXPEHU (int) (n * 100.0) / 100.0 :KLOH ZH UH DW LW ZH FDQ PDNH RQH ILQDO LPSURYHPHQW 1RWLFH KRZ RXU RULJLQDO QXPEHU ZDV ,I ZH GR VLPSOH WUXQFDWLRQ ZH JHW %XW UHDOO\ WKLV QXPEHU LV FORVHU WR :H FDQ URXQG WR WKH QHDUHVW GLJLW E\ DGGLQJ WR WKH QXPEHU EHIRUH ZH FDVW (int) (n * 100.0 + 0.5) / 100.0 7KLV LV DQ RSHUDWLRQ WKDW ZH DUH OLNHO\ WR ZDQW WR SHUIRUP RQ PRUH WKDQ RQH QXPEHU VR LW GHVHUYHV WR EH LQFOXGHG LQ D PHWKRG public static double round2(double n) { return (int) (n * 100.0 + 0.5) / 100.0; } *HWWLQJ EDFN WR RXU SVHXGRFRGH IRU WKH WDEOH ZH FDQ LQFRUSRUDWH FDOOV RQ WKH URXQG PHWKRG WR JHW D ELW FORVHU WR DFWXDO -DYD FRGH set all of x, y and t to 0 for (given number of steps) { t += timeIncrement; x += xIncrement; y = yVelocity * t + 0.5 * EARTH_ACCELERATION * t * t; report step #, round2(x), round2(y), round2(t) } ,W ZRXOG EH QLFH LI WKH YDOXHV LQ WKH WDEOH OLQHG XS VRPHZKDW 7R JHW QXPEHUV WKDW OLQH XS QLFHO\ ZH ZRXOG KDYH WR XVH IRUPDWWHG RXWSXW :H ZLOO VHH KRZ WR GR WKDW LQ &KDSWHU )RU QRZ ZH FDQ DW OHDVW JHW WKH QXPEHUV WR OLQH XS LQ FROXPQV E\ VHSDUDWLQJ WKHP ZLWK WDE FKDUDFWHUV 5HPHPEHU WKDW WKH HVFDSH VHTXHQFH ?W UHSUHVHQWV D VLQJOH WDE ,I ZH UH JRLQJ WR KDYH D WDEOH ZLWK FROXPQV LW DOVR PDNHV VHQVH WR KDYH D KHDGHU IRU WKH WDEOH $QG ZH SUREDEO\ ZDQW WR LQFOXGH D OLQH LQ WKH WDEOH VKRZLQJ WKH LQLWLDO FRQGLWLRQ ZKHUH [ \ DQG WLPH DUH DOO HTXDO WR 6R ZH FDQ H[SDQG WKH SVHXGRFRGH DERYH LQWR WKH IROORZLQJ -DYD FRGH 167 double x = 0.0; double y = 0.0; double t = 0.0; System.out.println("step\tx\ty\ttime"); System.out.println("0\t0.0\t0.0\t0.0"); for (int i = 1; i <= steps; i++) { t += timeIncrement; x += xIncrement; y = yVelocity * t + 0.5 * EARTH_ACCELERATION * t * t; System.out.println(i + "\t" + round2(x) + "\t" + round2(y) + "\t" + round2(t)); } $Q 8QVWUXFWXUHG 6ROXWLRQ :H FDQ SXW DOO RI WKHVH SLHFHV WRJHWKHU WR IRUP D FRPSOHWH SURJUDP /HW V ILUVW ORRN DW DQ XQVWUXFWXUHG YHUVLRQ WKDW LQFOXGHV PRVW RI WKH FRGH LQ PDLQ 7KLV YHUVLRQ DOVR LQFOXGHV VRPH QHZ SULQWOQ VWDWHPHQWV DW WKH EHJLQQLQJ WKDW JLYH D EULHI LQWURGXFWLRQ WR WKH XVHU 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 30 31 32 33 34 35 36 37 38 39 // This program computes the trajectory of a projectile. import java.util.*; // for Scanner public class Projectile { public static final double EARTH_ACCELERATION = -9.81; public static void main(String args) { Scanner console = new Scanner(System.in); // meters/second^2 System.out.println("This program computes the trajectory of a"); System.out.println("projectile given its initial velocity and"); System.out.println("its angle relative to the horizontal. Use"); System.out.println("an even number of steps if you want to include"); System.out.println("the high point reached by the projectile."); System.out.println(); System.out.print("velocity (meters/second)? "); double velocity = console.nextDouble(); System.out.print("angle relative to horizontal (degrees)? "); double angle = Math.toRadians(console.nextDouble()); System.out.print("number of steps to display? "); int steps = console.nextInt(); System.out.println(); double double double double double xVelocity = velocity * Math.cos(angle); yVelocity = velocity * Math.sin(angle); totalTime = - 2.0 * yVelocity / EARTH_ACCELERATION; timeIncrement = totalTime / steps; xIncrement = xVelocity * timeIncrement; double x = 0.0; double y = 0.0; double t = 0.0; System.out.println("step\tx\ty\ttime"); System.out.println("0\t0.0\t0.0\t0.0"); for (int i = 1; i <= steps; i++) { t += timeIncrement; x += xIncrement; 168 40 41 42 43 44 45 46 47 48 49 y = yVelocity * t + 0.5 * EARTH_ACCELERATION * t * t; System.out.println(i + "\t" + round2(x) + "\t" + round2(y) + "\t" + round2(t)); } } public static double round2(double n) { return (int) (n * 100.0 + 0.5) / 100.0; } } 7KH IROORZLQJ LV D VDPSOH H[HFXWLRQ RI WKH SURJUDP This program computes the trajectory of a projectile given its initial velocity and its angle relative to the horizontal. Use an even number of steps if you want to include the high point reached by the projectile. velocity (meters/second)? 30 angle relative to horizontal (degrees)? 50 number of steps to display? 10 step 0 1 2 3 4 5 6 7 8 9 10 x 0.0 9.03 18.07 27.1 36.14 45.17 54.21 63.24 72.28 81.31 90.35 y 0.0 9.69 17.23 22.61 25.84 26.92 25.84 22.61 17.23 9.69 0.0 time 0.0 0.47 0.94 1.41 1.87 2.34 2.81 3.28 3.75 4.22 4.69 )URP WKH ORJ \RX FDQ VHH WKDW WKH SURMHFWLOH UHDFKHV D PD[LPXP KHLJKW RI PHWHUV DIWHU VHFRQGV WKH ILIWK VWHS DQG WKDW LW ODQGV PHWHUV IURP ZKHUH LW EHJDQ DIWHU VHFRQGV WKH WHQWK VWHS 7KLV YHUVLRQ RI WKH SURJUDP ZRUNV EXW ZH GRQ W JHQHUDOO\ ZDQW WR LQFOXGH VR PXFK FRGH LQ PHWKRG PDLQ 7KH QH[W VHFWLRQ H[SORUHV KRZ WR EUHDN WKLV SURJUDP XS LQWR VPDOOHU SLHFHV $ 6WUXFWXUHG 6ROXWLRQ /RRNLQJ DW WKH FRGH LQ WKH PDLQ PHWKRG RI WKH 3URMHFWLOH SURJUDP WKHUH DUH WKUHH PDMRUV EORFNV RI FRGH :H KDYH D VHULHV RI SULQWOQ VWDWHPHQWV WKDW LQWURGXFH WKH SURJUDP WR WKH XVHU 7KHQ ZH KDYH D VHULHV RI VWDWHPHQWV WKDW SURPSW WKH XVHU IRU D WKH WKUHH YDOXHV XVHG WR SURGXFH WKH WDEOH 7KHQ ZH KDYH WKH FRGH WKDW SURGXFHV WKH WDEOH LWVHOI 6R WKH RYHUDOO VWUXFWXUH ORRNV OLNH WKLV give introduction. prompt for velocity, angle and steps. produce table. 169 7KH ILUVW VWHS DQG WKH WKLUG VWHS DUH HDVLO\ WXUQHG LQWR PHWKRGV EXW QRW WKH PLGGOH VWHS 7KH PLGGOH VWHS SURPSWV WKH XVHU IRU YDOXHV WKDW ZH QHHG WR SURGXFH WKH WDEOH ,I ZH WXUQ WKDW PLGGOH VWHS LQWR D PHWKRG WKHQ LW ZRXOG KDYH WR VRPHKRZ UHWXUQ WKUHH YDOXHV EDFN WR PDLQ $ PHWKRG FDQ UHWXUQ RQO\ D VLQJOH YDOXH VR ZH XQIRUWXQDWHO\ FDQ W WXUQ WKLV LQWR D PHWKRG :H FRXOG WXUQ WKH FRGH LQWR WKUHH GLIIHUHQW PHWKRGV RQH IRU HDFK RI WKH WKUHH YDOXHV EXW HDFK RI WKRVH PHWKRGV ZRXOG EH MXVW WZR OLQHV ORQJ VR LW V QRW FOHDU WKDW LW LPSURYHV WKH RYHUDOO VWUXFWXUH PXFK 7KH PDLQ LPSURYHPHQW ZH FDQ PDNH WKHQ LV WR VSOLW RII WKH LQWURGXFWLRQ DQG WKH WDEOH LQWR VHSDUDWH PHWKRGV $QRWKHU LPSURYHPHQW ZH FDQ PDNH LV WR WXUQ WKH 3K\VLFV GLVSODFHPHQW IRUPXOD LQWR LWV RZQ PHWKRG ,W LV D JRRG LGHD WR WXUQ HTXDWLRQV LQWR PHWKRGV ,QWURGXFLQJ WKRVH PHWKRGV ZH JHW WKH IROORZLQJ VWUXFWXUHG YHUVLRQ RI WKH SURJUDP 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 // This program computes the trajectory of a projectile. import java.util.*; // for Scanner public class Projectile2 { public static final double EARTH_ACCELERATION = -9.81; public static void main(String args) { Scanner console = new Scanner(System.in); giveIntro(); // meters/second^2 System.out.print("velocity (meters/second)? "); double velocity = console.nextDouble(); System.out.print("angle relative to horizontal (degrees)? "); double angle = Math.toRadians(console.nextDouble()); System.out.print("number of steps to display? "); int steps = console.nextInt(); System.out.println(); printTable(velocity, angle, steps); } // prints a table showing the trajectory of an object given its initial // velocity and angle and including the given number of steps in the // table public static void printTable(double velocity, double angle, int steps) { double xVelocity = velocity * Math.cos(angle); double yVelocity = velocity * Math.sin(angle); double totalTime = - 2.0 * yVelocity / EARTH_ACCELERATION; double timeIncrement = totalTime / steps; double xIncrement = xVelocity * timeIncrement; double x = 0.0; double y = 0.0; double t = 0.0; System.out.println("step\tx\ty\ttime"); System.out.println("0\t0.0\t0.0\t0.0"); for (int i = 1; i <= steps; i++) { t += timeIncrement; x += xIncrement; y = displacement(yVelocity, t, EARTH_ACCELERATION); System.out.println(i + "\t" + round2(x) + "\t" + round2(y) + "\t" + round2(t)); } } 170 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 // gives a brief introduction to the user public static void giveIntro() { System.out.println("This program computes the trajectory of a"); System.out.println("projectile given its initial velocity and"); System.out.println("its angle relative to the horizontal. Use"); System.out.println("an even number of steps if you want to include"); System.out.println("the high point reached by the projectile."); System.out.println(); } // returns the horizontal displacement for a body given its initial // velocity v, elapsed time t and acceleration a public static double displacement(double v, double t, double a) { return v * t + 0.5 * a * t * t; } // rounds n to 2 digits after the decimal point public static double round2(double n) { return (int) (n * 100.0 + 0.5) / 100.0; } } 7KLV YHUVLRQ H[HFXWHV WKH VDPH ZD\ DV WKH HDUOLHU YHUVLRQ &KDSWHU 6XPPDU\ 0HWKRGV PD\ EH ZULWWHQ WR DFFHSW SDUDPHWHUV ZKLFK DUH YDOXHV JLYHQ IURP WKH FDOOLQJ FRGH LQWR WKH PHWKRG 3DUDPHWHUV DOORZ GDWD YDOXHV WR IORZ LQWR WKH PHWKRG ZKLFK FDQ FKDQJH WKH ZD\ WKH PHWKRG H[HFXWHV $ PHWKRG GHFODUHG ZLWK D VHW RI SDUDPHWHUV FDQ SHUIRUP DQ HQWLUH IDPLO\ RI VLPLODU WDVNV LQVWHDG RI H[DFWO\ RQH WDVN :KHQ SULPLWLYH YDOXHV VXFK DV W\SH int RU double DUH SDVVHG DV SDUDPHWHUV WKHLU YDOXHV DUH FRSLHG LQWR WKH PHWKRG 3ULPLWLYH SDUDPHWHUV VHQG YDOXHV LQWR D PHWKRG EXW QRW RXW RI LW WKH PHWKRG FDQ XVH WKH GDWD YDOXHV EXW FDQQRW DIIHFW WKH YDOXH RI DQ\ YDULDEOHV RXWVLGH RI LW 7ZR PHWKRGV FDQ KDYH WKH VDPH QDPH LI WKH\ GHFODUH GLIIHUHQW SDUDPHWHUV 7KLV LV FDOOHG RYHUORDGLQJ 0HWKRGV FDQ EH ZULWWHQ WR UHWXUQ D YDOXH WR WKH FDOOLQJ FRGH 7KLV DOORZV PHWKRGV WR SHUIRUP D FRPSOH[ FRPSXWDWLRQ DQG WKHQ SURYLGH LWV UHVXOW EDFN WR WKH FDOOLQJ FRGH 7KH W\SH RI WKH UHWXUQ YDOXH PXVW EH GHFODUHG LQ WKH PHWKRG V KHDGHU DQG LV FDOOHG WKH PHWKRG V UHWXUQ W\SH -DYD KDV D FODVV QDPHG Math WKDW FRQWDLQV VHYHUDO XVHIXO VWDWLF PHWKRGV WKDW \RX FDQ XVH LQ \RXU SURJUDPV VXFK DV SRZHUV VTXDUH URRWV DQG ORJDULWKPV $Q REMHFW LV DQ HQWLW\ WKDW FRPELQHV GDWD DQG RSHUDWLRQV 6RPH REMHFWV LQ -DYD LQFOXGH 6WULQJV ZKLFK DUH VHTXHQFHV RI WH[W FKDUDFWHUV DQG 3RLQWV ZKLFK KROG &DUWHVLDQ [ \ FRRUGLQDWHV 171 2EMHFWV FRQWDLQ PHWKRGV WKDW LPSOHPHQW WKHLU EHKDYLRU 7R FDOO D PHWKRG RQ DQ REMHFW ZULWH LWV QDPH IROORZHG E\ D GRW IROORZHG E\ WKH PHWKRG QDPH IRU H[DPSOH objectName.methodName(); $ 6WULQJ REMHFW KROGV D VHTXHQFH RI FKDUDFWHUV 7KH FKDUDFWHUV KDYH LQGH[HV VWDUWLQJ ZLWK IRU WKH ILUVW FKDUDFWHU $ 3RLQW REMHFW KROGV WZR int YDOXHV [ DQG \ UHSUHVHQWLQJ D GLPHQVLRQDO SRLQW $ SRLQW FDQ EH FRQVWUXFWHG WUDQVODWHG PRYHG WR D QHZ ORFDWLRQ DQG FRQYHUWHG LQWR D 6WULQJ WR EH SULQWHG RQ WKH FRQVROH $Q H[FHSWLRQ LV DQ HUURU WKDW RFFXUV ZKHQ D SURJUDP KDV PDGH DQ LOOHJDO DFWLRQ DQG LV XQDEOH WR FRQWLQXH H[HFXWLQJ QRUPDOO\ -DYD REMHFWV XVH UHIHUHQFH VHPDQWLFV ZKHUH YDULDEOHV GR QRW VWRUH DFWXDO REMHFWV EXW LQVWHDG DUH VLPSO\ QDPHV WKDW UHIHU WR REMHFWV 7KLV PHDQV WKDW WZR YDULDEOHV FDQ UHIHU WR WKH VDPH REMHFW ,I DQ REMHFW LV PRGLILHG WKURXJK RQH RI LWV UHIHUHQFHV WKH PRGLILFDWLRQ ZLOO DOVR EH VHHQ LQ WKH RWKHU 2EMHFWV WKDW DUH SDVVHG DV SDUDPHWHUV WR PHWKRGV EHKDYH GLIIHUHQWO\ IURP SULPLWLYH SDUDPHWHUV EHFDXVH WKH PHWKRG UHIHUV WR WKH VDPH REMHFW WKDW ZDV SDVVHG LQ QRW WR D FRS\ RI WKH REMHFW ,I WKH PHWKRG PRGLILHV WKH REMHFW V GDWD WKH PRGLILFDWLRQ ZLOO DOVR EH VHHQ ZKHQ WKH PHWKRG UHWXUQV 6RPH SURJUDPV DUH LQWHUDFWLYH DQG UHVSRQG WR LQSXW IURP WKH XVHU 7KHVH SURJUDPV VKRXOG SULQW D PHVVDJH WR WKH XVHU DOVR FDOOHG D SURPSW DVNLQJ IRU WKH LQSXW -DYD KDV D FODVV FDOOHG 6FDQQHU WKDW UHDGV LQSXW IURP WKH NH\ERDUG $ 6FDQQHU FDQ UHDG YDULRXV SLHFHV RI LQSXW DOVR FDOOHG WRNHQV IURP DQ LQSXW VRXUFH $ 6FDQQHU FDQ UHDG HLWKHU RQH WRNHQ DW D WLPH RU DQ HQWLUH OLQH DW D WLPH 6HOI &KHFN ([HUFLVHV 6HFWLRQ 3DUDPHWHUV :KDW RXWSXW LV SURGXFHG E\ WKH IROORZLQJ SURJUDP" 172 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class Odds { public static void main(String args) { printOdds(3); printOdds(17 / 2); int x = 25; printOdds(37 - x + 1); } public static void printOdds(int n) { for (int i = 1; i <= n; i++) { int odd = 2 * i - 1; System.out.print(odd + " "); } System.out.println(); } } :KDW LV WKH RXWSXW RI WKH IROORZLQJ SURJUDP" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class Weird { public static void main(String args) { int number = 8; halfTheFun(11); halfTheFun(2 - 3 + 2 * 8); halfTheFun(number); System.out.println("number = " + number); } public static void halfTheFun(int number) { number = number / 2; for (int count = 1; count <= number; count++) { System.out.print(count + " "); } System.out.println(); } } :KDW LV WKH RXWSXW RI WKH IROORZLQJ SURJUDP" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class Params1 { public static void main(String args) { String one = "two"; String two = "three"; String three = "1"; int number = 20; sentence(one, two, 3); sentence(two, three, 14); sentence(three, three, number + 1); sentence(three, two, 1); sentence("eight", three, number / 2); } public static void sentence(String three, String one, int number) { System.out.println(one + " times " + three + " = " + (number * 2)); } } 173 :KDW RXWSXW LV SURGXFHG E\ WKH IROORZLQJ SURJUDP" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class Mystery1 { public static void main(String args) { String whom = "her"; String who = "him"; String it = "who"; String he = "it"; String she = "whom"; sentence(he, she, it); sentence(she, he, who); sentence(who, she, who); sentence(it, "stu", "boo"); sentence(it, whom, who); } public static void sentence(String she, String who, String whom) { System.out.println(who + " and " + whom + " like " + she); } } :ULWH D PHWKRG QDPHG printStrings WKDW DFFHSWV D 6WULQJ DQG D QXPEHU RI UHSHWLWLRQV DV SDUDPHWHUV DQG SULQWV WKDW 6WULQJ WKH JLYHQ QXPEHU RI WLPHV )RU H[DPSOH WKH FDOO printStrings("abc", 5) ZLOO SULQW WKH IROORZLQJ RXWSXW abcabcabcabcabc 6HFWLRQ 0HWKRGV WKDW 5HWXUQ 9DOXHV 7KH 6\VWHP RXW SULQWOQ FRPPDQG ZRUNV RQ PDQ\ GLIIHUHQW W\SHV RI YDOXHV VXFK DV LQW RU GRXEOH :KDW LV WKH WHUP IRU VXFK D PHWKRG" :KDW LV ZURQJ ZLWK WKH IROORZLQJ SURJUDP" 1 2 3 4 5 6 7 8 9 10 11 12 13 public class Temperature { public static void main(String args) { double tempf = 98.6; double tempc = 0.0; ftoc(tempf, tempc); System.out.println("Body temp in C is: " + tempc); } // Converts Fahrenheit temperatures to Celsius. public static void ftoc(double tempf, double tempc) { tempc = (tempf - 32) * 5 / 9; } } (YDOXDWH WKH IROORZLQJ H[SUHVVLRQV Math.abs(-1.6) 174 Math.abs(2 + -4) Math.pow(6, 2) Math.pow(5 / 2, 6) Math.ceil(9.1) Math.ceil(115.8) Math.max(7, 4) Math.min(8, 3 + 2) Math.min(-2, -5) Math.sqrt(64) Math.sqrt(76 + 45) 100 + Math.log10(100) 13 + Math.abs(-7) - Math.pow(2, 3) + 5 Math.sqrt(16) * Math.max(Math.abs(-5), Math.abs(-3)) 7 - 2 + Math.log10(1000) + Math.log(Math.pow(Math.E, 5)) Math.max(18 - 5 + Math.random(), Math.ceil(4.6 * 3)) :KDW RXWSXW LV SURGXFHG E\ WKH IROORZLQJ SURJUDP" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class Mystery6 { public static void main(String int x = 1, y = 2, z = 3; z = mystery(x, z, y); System.out.println(x + " " + x = mystery(z, z, x); System.out.println(x + " " + y = mystery(y, y, z); System.out.println(x + " " + } args) { y + " " + z); y + " " + z); y + " " + z); public static int mystery(int z, int x, int y) { z--; x = 2 * y + z; y = x - 1; System.out.println(y + " " + z); return x; } } :ULWH D PHWKRG QDPHG min WKDW WDNHV WKUHH LQWV DV SDUDPHWHUV DQG UHWXUQV WKH YDOXH RXW RI WKH WKUHH WKDW LV WKH VPDOOHVW $ FDOO RI min(3, -2, 7) ZRXOG UHWXUQ DQG D FDOO RI min(19, 27, 6) ZRXOG UHWXUQ 8VH Math.min WR ZULWH \RXU VROXWLRQ :ULWH D PHWKRG QDPHG countQuarters WKDW WDNHV DQ LQW UHSUHVHQWLQJ D QXPEHU RI FHQWV DV D SDUDPHWHU DQG UHWXUQV WKH QXPEHU RI TXDUWHU FRLQV UHSUHVHQWHG E\ WKDW PDQ\ FHQWV 'RQ W FRXQW DQ\ ZKROH GROODUV EHFDXVH WKRVH ZRXOG EH GLVSHQVHG DV GROODU ELOOV )RU H[DPSOH countQuarters(64) ZRXOG UHWXUQ EHFDXVH WZR TXDUWHUV PDNH FHQWV ZLWK H[WUD OHIW countQuarters(1278) ZRXOG UHWXUQ RYHU $ FDOO RI EHFDXVH DIWHU WKH GROODUV DUH WDNHQ RXW TXDUWHUV UHPDLQ LQ WKH FHQWV OHIW 175 6HFWLRQ 8VLQJ 2EMHFWV $VVXPLQJ WKDW WKH IROORZLQJ YDULDEOHV KDYH EHHQ GHFODUHG String str1 = "Q.E.D."; String str2 = "Arcturan Megadonkey"; String str3 = "Sirius Cybernetics Corporation"; (YDOXDWH WKH IROORZLQJ H[SUHVVLRQV str1.length() str2.length() str1.toLowerCase() str2.toUpperCase() str1.substring(8, 11) str2.substring(10, 14) str1.indexOf("D") str1.indexOf(".") str2.indexOf("donkey") str3.indexOf("X") str2 + str3.charAt(17) str3.substring(9, str3.indexOf("e")) str3.substring(7, 12) str2.toLowerCase().substring(9, 13) + str3.substring(18, str3.length() - 7) %DVHG RQ WKH IROORZLQJ 6WULQJ String quote = "Four score and seven years ago"; :KDW H[SUHVVLRQ SURGXFHV WKH QHZ 6WULQJ "SCORE" " :KDW H[SUHVVLRQ SURGXFHV "four years" " :KDW DUH WKH [ \ FRRUGLQDWHV RI WKH 3RLQWV UHIHUUHG WR DV S FRGH" Point p1 = new Point(17, 9); Point p2 = new Point(4, -1); Point p3 = p2; p1.translate(3, 1); p2.x = 50; p3.translate(-4, 5); S DQG S DIWHU WKH IROORZLQJ :KDW LV WKH RXWSXW RI WKH IROORZLQJ SURJUDP" 176 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import java.awt.*; public class PointMystery { public static void main(String args) { Point p1 = new Point(5, 2); Point p2 = new Point(-3, 6); silly(p1); silly(p2); Point p3 = p1; silly(p3); System.out.println("(" + p1.x + ", " + p1.y + ")"); System.out.println("(" + p2.x + ", " + p2.y + ")"); System.out.println("(" + p3.x + ", " + p3.y + ")"); } public static void silly(Point p) { int temp = p.x; p.x = p.y; p.y = temp; } } 6HFWLRQ ,QWHUDFWLYH 3URJUDPV ,Q D SURJUDP UHDGLQJ XVHU LQSXW XVLQJ D 6FDQQHU LI WKH XVHU W\SHV WKH IROORZLQJ OLQH RI LQSXW Hello there. 1+2 is 3 and 1.5 squared is 2.25! +RZ PDQ\ WRNHQV DUH LQ WKH SUHFHGLQJ OLQH" :KDW W\SHV FDQ WKH 6FDQQHU OHJDOO\ UHDG HDFK WRNHQ DV" *LYHQ WKH IROORZLQJ FRGH IUDJPHQW Scanner console = new Scanner(System.in); System.out.print("How much money do you have? "); double money = console.nextDouble(); 'HVFULEH ZKDW ZLOO KDSSHQ ZKHQ WKH XVHU W\SHV HDFK RI WKH IROORZLQJ YDOXHV ,I WKH FRGH ZLOO UXQ VXFFHVVIXOO\ GHVFULEH WKH YDOXH WKDW ZLOO EH VWRUHG LQ WKH YDULDEOH money PLOOLRQ QRQH 177 :ULWH -DYD FRGH WR UHDG DQ LQWHJHU IURP WKH XVHU WKHQ SULQW WKDW QXPEHU PXOWLSOLHG E\ PD\ DVVXPH WKDW WKH XVHU W\SHV D YDOLG LQWHJHU :ULWH -DYD FRGH WKDW SURPSWV WKH XVHU IRU D SKUDVH DQG D QXPEHU RI WLPHV WKHQ SULQWV WKH SKUDVH WKDW PDQ\ WLPHV +HUH LV DQ H[DPSOH GLDORJXH ZLWK WKH XVHU What is your phrase? His name is Robert Paulson. How many times should I repeat the phrase? 3 His name is Robert Paulson. His name is Robert Paulson. His name is Robert Paulson. <RX ([HUFLVHV :ULWH D PHWKRG QDPHG printNumbers WKDW DFFHSWV D PD[LPXP QXPEHU DV DQ DUJXPHQW DQG SULQWV HDFK QXPEHU IURP XS WR WKDW PD[LPXP LQFOXVLYH ER[HG E\ VTXDUH EUDFNHWV )RU H[DPSOH FRQVLGHU WKH IROORZLQJ FDOOV printNumbers(15); printNumbers(5); 7KH SUHFHGLQJ FDOOV VKRXOG SURGXFH WKH IROORZLQJ RXWSXW [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [1] [2] [3] [4] [5] <RX PD\ DVVXPH WKDW WKH YDOXH SDVVHG WR printNumbers KDV D YDOXH RI RU JUHDWHU :ULWH D PHWKRG QDPHG printPowersOf2 WKDW DFFHSWV D PD[LPXP QXPEHU DV DQ DUJXPHQW DQG SULQWV HDFK SRZHU RI IURP A XS WR WKDW PD[LPXP SRZHU LQFOXVLYH )RU H[DPSOH FRQVLGHU WKH IROORZLQJ FDOOV printPowersOf2(3); printPowersOf2(10); 7KH SUHFHGLQJ FDOOV VKRXOG SURGXFH WKH IROORZLQJ RXWSXW 1 2 4 8 1 2 4 8 16 32 64 128 256 512 1024 <RX PD\ DVVXPH WKDW WKH YDOXH SDVVHG WR printPowersOf2 KDV D YDOXH RI RU JUHDWHU 7KH Math FODVV PD\ KHOS \RX ZLWK WKLV SUREOHP ,I \RX XVH LW \RX PD\ QHHG WR FDVW LWV UHVXOWV IURP GRXEOH WR LQW VR WKDW \RX GRQ W VHH D DIWHU HDFK QXPEHU LQ \RXU RXWSXW $OVR FDQ \RX ZULWH WKLV SURJUDP ZLWKRXW XVLQJ WKH Math FODVV" :ULWH D PHWKRG QDPHG printPowersOfN WKDW DFFHSWV D EDVH DQG DQ H[SRQHQW DV DUJXPHQWV DQG SULQWV HDFK SRZHU RI WKH EDVH IURP EDVHA XS WR WKDW PD[LPXP SRZHU LQFOXVLYH )RU H[DPSOH FRQVLGHU WKH IROORZLQJ FDOOV printPowersOfN(4, 3); printPowersOfN(5, 6); printPowersOfN(-2, 8); 178 7KH SUHFHGLQJ FDOOV VKRXOG SURGXFH WKH IROORZLQJ RXWSXW 1 4 16 64 1 5 25 125 625 3125 15625 1 -2 4 -8 16 -32 64 -128 256 <RX PD\ DVVXPH WKDW WKH H[SRQHQW SDVVHG WR printPowersOfN KDV D YDOXH RI RU JUHDWHU 7KH Math FODVV PD\ KHOS \RX ZLWK WKLV SUREOHP ,I \RX XVH LW \RX PD\ QHHG WR FDVW LWV UHVXOWV IURP GRXEOH WR LQW VR WKDW \RX GRQ W VHH D DIWHU HDFK QXPEHU LQ \RXU RXWSXW $OVR FDQ \RX ZULWH WKLV SURJUDP ZLWKRXW XVLQJ WKH Math FODVV" :ULWH D PHWKRG QDPHG largerAbsVal WKDW WDNHV WZR LQWV DV SDUDPHWHUV DQG UHWXUQV WKH ODUJHU RI WKH WZR DEVROXWH YDOXHV $ FDOO RI largerAbsVal(11, 2) ZRXOG UHWXUQ DQG D FDOO RI largerAbsVal(4, -5) ZRXOG UHWXUQ :ULWH D YDULDWLRQ RI WKH PHWKRG IURP WKH ODVW H[HUFLVH FDOOHG largestAbsVal WKDW WDNHV WKUHH LQWV UHWXUQV WKH ODUJHVW RI WKHLU WKUHH DEVROXWH YDOXHV )RU H[DPSOH D FDOO RI largestAbsVal(7, -2, -11) ZRXOG UHWXUQ DQG D FDOO RI largestAbsVal(-4, 5, 2) ZRXOG UHWXUQ :ULWH D PHWKRG QDPHG quadratic WKDW VROYHV TXDGUDWLF HTXDWLRQV DQG SULQWV WKHLU URRWV 5HFDOO WKDW D TXDGUDWLF HTXDWLRQ LV D SRO\QRPLDO HTXDWLRQ LQ WHUPV RI D YDULDEOH [ RI WKH IRUP D[ E[ F 7KH IRUPXOD IRU VROYLQJ D TXDGUDWLF HTXDWLRQ LV +HUH DUH VRPH H[DPSOH HTXDWLRQV DQG WKHLU URRWV [ [ [ [ [ [ [ [ <RXU PHWKRG VKRXOG DFFHSW WKH FRHIILFLHQWV D E DQG F DV SDUDPHWHUV DQG VKRXOG SULQW WKH URRWV RI WKH HTXDWLRQ <RX PD\ DVVXPH WKDW WKH HTXDWLRQ KDV WZR UHDO URRWV WKRXJK PDWKHPDWLFDOO\ WKLV LV QRW DOZD\V WKH FDVH :ULWH D PHWKRG QDPHG padString WKDW DFFHSWV WZR SDUDPHWHUV D 6WULQJ DQG DQ LQWHJHU UHSUHVHQWLQJ D OHQJWK 7KH PHWKRG VKRXOG SDG WKH SDUDPHWHU VWULQJ ZLWK VSDFHV XQWLO LWV OHQJWK LV WKH JLYHQ OHQJWK )RU H[DPSOH padString("hello", 8) VKRXOG UHWXUQ " hello" 7KLV VRUW RI PHWKRG LV XVHIXO ZKHQ WU\LQJ WR SULQW RXWSXW WKDW OLQHV XS KRUL]RQWDOO\ ,I WKH VWULQJ V OHQJWK LV DOUHDG\ DW OHDVW DV ORQJ DV WKH OHQJWK SDUDPHWHU \RXU PHWKRG VKRXOG UHWXUQ WKH RULJLQDO VWULQJ )RU H[DPSOH padString("congratulations", 10) VKRXOG UHWXUQ "congratulations" :ULWH D PHWKRG QDPHG vertical WKDW DFFHSWV D 6WULQJ DV LWV SDUDPHWHU DQG SULQWV HDFK OHWWHU RI WKH VWULQJ RQ VHSDUDWH OLQHV )RU H[DPSOH D FDOO RI vertical("hey now") VKRXOG SURGXFH WKH IROORZLQJ RXWSXW 179 h e y n o w :ULWH D PHWKRG QDPHG reverse WKDW DFFHSWV D 6WULQJ DV LWV SDUDPHWHU DQG UHWXUQV D 6WULQJ WKDW LV WKH SDUDPHWHU UHYHUVHG ZLWK WKH FKDUDFWHUV LQ RSSRVLWH RUGHU )RU H[DPSOH D FDOO RI reverse("hello there!") VKRXOG UHWXUQ "!ereht olleh" ,I WKH HPSW\ VWULQJ LV SDVVHG WR reverse WKH HPSW\ VWULQJ VKRXOG EH UHWXUQHG :ULWH D PHWKRG QDPHG swapPoints WKDW WDNHV WZR 3RLQWV DV DUJXPHQWV DQG VZDSV WKHLU YDOXHV ZLWK HDFK RWKHU &RQVLGHU WKH IROORZLQJ H[DPSOH FRGH WKDW FDOOV swapPoints Point p1 = new Point(5, 2); Point p2 = new Point(-3, 6); swapPoints(p1, p2); System.out.println("(" + p1.x + ", " + p1.y + ")"); System.out.println("(" + p2.x + ", " + p2.y + ")"); 7KH RXWSXW SURGXFHG IURP WKH DERYH FRGH VKRXOG EH (-3, 6) (5, 2) :ULWH -DYD FRGH WKDW SURPSWV WKH XVHU WR HQWHU KLV KHU IXOO QDPH WKHQ SULQWV WKH QDPH LQ WKH RSSRVLWH RUGHU +HUH LV DQ H[DPSOH GLDORJXH ZLWK WKH XVHU Please enter your full name: Sammy Jankis Your name in reverse order is Jankis, Sammy 3URJUDPPLQJ 3URMHFWV :ULWH D SURJUDP WKDW SURGXFHV &KULVWPDV WUHHV DV RXWSXW ,W VKRXOG KDYH D PHWKRG ZLWK WZR SDUDPHWHUV RQH IRU WKH QXPEHU RI VHJPHQWV LQ WKH WUHH DQG RQH IRU WKH KHLJKW RI HDFK VHJPHQW )RU H[DPSOH WKH IROORZLQJ WUHH RQ WKH OHIW KDV VHJPHQWV RI KHLJKW DQG WKH RQH RQ WKH ULJKW KDV VHJPHQWV RI KHLJKW 180 * *** ***** ******* *** ***** ******* ********* ***** ******* ********* *********** * * ******* * *** ***** ******* ********* *** ***** ******* ********* *********** * * ******* $ FHUWDLQ EDQN RIIHUV LQWHUHVW RQ ORDQV FRPSRXQGHG DQQXDOO\ &UHDWH D WDEOH WKDW VKRZV KRZ PXFK PRQH\ D SHUVRQ ZLOO DFFXPXODWH RYHU D SHULRG RI \HDUV DVVXPLQJ DQ LQLWLDO LQYHVWPHQW RI DQG DVVXPLQJ WKDW LV GHSRVLWHG HDFK \HDU DIWHU WKH ILUVW <RXU WDEOH VKRXOG LQGLFDWH IRU HDFK \HDU WKH FXUUHQW EDODQFH WKH LQWHUHVW WKH QHZ GHSRVLW DQG WKH QHZ EDODQFH :ULWH D SURJUDP WKDW VKRZV WKH WRWDO QXPEHU RI SUHVHQWV UHFHLYHG RQ HDFK GD\ DFFRUGLQJ WR WKH VRQJ 7KH 7ZHOYH 'D\V RI &KULVWPDV DV LQGLFDWHG LQ WKH IROORZLQJ WDEOH 'D\ 3UHVHQWV 5HFHLYHG 7RWDO 3UHVHQWV :ULWH D SURJUDP WKDW SURPSWV IRU WKH OHQJWKV RI WKH VLGHV RI D WULDQJOH DQG UHSRUWV WKH WKUHH DQJOHV :ULWH D SURJUDP WKDW FRPSXWHV WKH VSKHULFDO GLVWDQFH EHWZHHQ WZR SRLQWV RQ WKH VXUIDFH RI WKH (DUWK 7KLV LV D XVHIXO RSHUDWLRQ EHFDXVH LW WHOOV \RX KRZ DSDUW WZR FLWLHV DUH LI \RX PXOWLSO\ LW E\ WKH UDGLXV RI WKH (DUWK ZKLFK LV URXJKO\ NP /HW EH WKH ODWLWXGH DQG ORQJLWXGH RI WZR SRLQWV UHVSHFWLYHO\ WKH ORQJLWXGH GLIIHUHQFH DQG WKH DQJXODU GLIIHUHQFH GLVWDQFH LQ UDGLDQV FDQ EH GHWHUPLQHG IURP WKH VSKHULFDO ODZ RI FRVLQHV DV DUFFRV VLQ VLQ FRV FRV FRV )RU H[DPSOH FRQVLGHU WKH ODWLWXGH DQG ORQJLWXGH RI WZR PDMRU FLWLHV 1DVKYLOOH 71 86$ 1 : 181 /RV $QJHOHV &$ 86$ 1 : <RX PXVW FRQYHUW WKHVH FRRUGLQDWHV WR UDGLDQV EHIRUH \RX FDQ XVH WKHP HIIHFWLYHO\ LQ WKH IRUPXOD $IWHU FRQYHUVLRQ WKH FRRUGLQDWHV EHFRPH 1DVKYLOOH /RV $QJHOHV UDG UDG UDG UDG 8VLQJ WKHVH YDOXHV LQ WKH DQJXODU GLVWDQFH HTXDWLRQ U [ NP 7KXV WKH GLVWDQFH EHWZHHQ WKHVH FLWLHV LV DERXW NP RU PLOHV 1RWH 7R VROYH WKLV SUREOHP \RX ZLOO QHHG WR XVH WKH Math.acos PHWKRG WKDW UHWXUQV DQ DUF FRVLQH DQJOH LQ UDGLDQV 6WXDUW 5HJHV 0DUW\ 6WHSS 182 6XSSOHPHQW * *UDSKLFV RSWLRQDO &RS\ULJKW E\ 6WXDUW 5HJHV DQG 0DUW\ 6WHSS * ,QWURGXFWLRQ WR *UDSKLFV 'UDZLQJ3DQHO /LQHV DQG 6KDSHV &RORUV 7H[W DQG )RQWV * 3URFHGXUDO 'HFRPSRVLWLRQ ZLWK *UDSKLFV $ /DUJHU ([DPSOH 'UDZ'LDPRQGV 6XPPDU\ RI *UDSKLFV 0HWKRGV * &DVH 6WXG\ 3\UDPLGV $Q 8QVWUXFWXUHG 6ROXWLRQ $ 6WUXFWXUHG 6ROXWLRQ ,QWURGXFWLRQ 2QH RI WKH PRVW FRPSHOOLQJ UHDVRQV WR OHDUQ DERXW XVLQJ REMHFWV LV WKDW WKH\ DOORZ XV WR GUDZ JUDSKLFV LQ -DYD *UDSKLFV DUH XVHG IRU JDPHV UHQGHULQJ FRPSOH[ LPDJHV FRPSXWHU DQLPDWLRQ DQG PRGHUQ XVHU LQWHUIDFHV FDOOHG *8,V *UDSKLFDO 8VHU ,QWHUIDFHV ,Q WKLV RSWLRQDO VXSSOHPHQW VHFWLRQ ZH ZLOO H[DPLQH D IHZ RI WKH EDVLF FODVVHV IURP -DYD V JUDSKLFDO IUDPHZRUN DQG XVH WKHP WR GUDZ SDWWHUQHG WZR GLPHQVLRQDO ILJXUHV RI VKDSHV DQG WH[W RQWR WKH VFUHHQ 183 * ,QWURGXFWLRQ WR *UDSKLFV -DYD V RULJLQDO JUDSKLFDO WRROV ZHUH FROOHFWLYHO\ NQRZQ DV $:7 ZKLFK LV VKRUW IRU $EVWUDFW :LQGRZLQJ 7RRONLW 7KH FODVVHV DVVRFLDWHG ZLWK $:7 UHVLGH LQ SDFNDJH MDYD DZW $V ZH VDZ HDUOLHU LQ &KDSWHU WKH 3RLQW FODVV LV GHILQHG LQ WKLV SDFNDJH ,Q RUGHU WR FUHDWH JUDSKLFDO SURJUDPV OLNH WKRVH VHHQ LQ WKLV VHFWLRQ ZH OO QHHG WKH IROORZLQJ LPSRUW VWDWHPHQW DW WKH WRS RI RXU SURJUDPV import java.awt.*; // for graphics 7R NHHS WKLQJV VLPSOH ZH ZLOO EH XVLQJ D FXVWRP FODVV FDOOHG 'UDZLQJ3DQHO WKDW ZDV ZULWWHQ E\ WKH DXWKRUV RI WKLV WH[WERRN WR VLPSOLI\ VRPH RI WKH PRUH HVRWHULF GHWDLOV RI -DYD JUDSKLFV ,W LV OHVV WKDQ D SDJH LQ OHQJWK VR ZH DUHQ W KLGLQJ PXFK 'UDZLQJ3DQHO GRHV QRW QHHG WR EH LPSRUWHG EXW WKH ILOH 'UDZLQJ3DQHO MDYD PXVW EH SODFHG LQWR WKH VDPH IROGHU DV \RXU SURJUDP 7KH DFWXDO GUDZLQJ ZLOO EH GRQH ZLWK DQ REMHFW RI W\SH *UDSKLFV 7KH *UDSKLFV FODVV LV SDUW RI WKH MDYD DZW OLEUDU\ 7KH GUDZLQJ SDQHO LV OLNH D SLHFH RI SDSHU DQG WKH *UDSKLFV REMHFW LV OLNH D SHQFLO RU SDLQWEUXVK 7KH SDQHO NHHSV WUDFN RI WKH RYHUDOO LPDJH EXW WKH *UDSKLFV REMHFW GRHV WKH DFWXDO GUDZLQJ RI LQGLYLGXDO VKDSHV ZLWKLQ WKH SDQHO :H ZLOO ODWHU H[SORUH WKH RSWLRQ RI XVLQJ GLIIHUHQW FRORUV XVLQJ &RORU REMHFWV 'UDZLQJ3DQHO <RX FDQ FUHDWH D JUDSKLFDO ZLQGRZ RQ \RXU VFUHHQ E\ FRQVWUXFWLQJ D 'UDZLQJ3DQHO REMHFW <RX PXVW VSHFLI\ WKH ZLGWK DQG KHLJKW RI WKH GUDZLQJ DUHD DrawingPanel <name> = new DrawingPanel(<width>, <height>); 2QFH \RX KDYH FRQVWUXFWHG RQH D 'UDZLQJ3DQHO REMHFW KDV WKH IROORZLQJ SXEOLF PHWKRGV 8VHIXO 0HWKRGV RI 'UDZLQJ3DQHO 2EMHFWV 0HWKRG getGraphics() 'HVFULSWLRQ UHWXUQV D UHIHUHQFH WR WKH *UDSKLFV REMHFW WKDW FDQ EH XVHG WR GUDZ RQWR WKH SDQHO setBackground(Color c) VHWV WKH EDFNJURXQG FRORU RI WKH SDQHO WR WKH JLYHQ FRORU GHIDXOW LV ZKLWH 7KH W\SLFDO XVDJH RI WKH 'UDZLQJ3DQHO ZLOO EH WR FRQVWUXFW RQH ZLWK D SDUWLFXODU KHLJKW DQG ZLGWK WR VHW LWV EDFNJURXQG FRORU LI \RX GRQ W ZDQW WKH GHIDXOW ZKLWH EDFNJURXQG DQG WKHQ WR GUDZ VRPHWKLQJ XVLQJ LWV *UDSKLFV REMHFW 7KH 'UDZLQJ3DQHO DSSHDUV RQ WKH VFUHHQ DW WKH WLPH WKDW \RX FRQVWUXFW LW $OO FRRUGLQDWHV DUH VSHFLILHG DV LQWHJHUV (DFK [ \ SRVLWLRQ FRUUHVSRQGV WR D GLIIHUHQW SL[HO RQ WKH FRPSXWHU VFUHHQ 7KH ZRUG SL[HO LV D VKRUWKDQG IRU SLFWXUH HOHPHQW DQG UHSUHVHQWV D VLQJOH GRW RQ WKH FRPSXWHU VFUHHQ 184 7KH FRRUGLQDWH V\VWHP DVVLJQV WKH XSSHU OHIW FRUQHU RI D SDQHO WR $V ZH PRYH WR WKH ULJKW RI WKLV SRVLWLRQ WKH [ YDOXH LQFUHDVHV $V ZH JR GRZQ IURP WKLV SRVLWLRQ WKH \ YDOXH LQFUHDVHV )RU H[DPSOH VXSSRVH WKDW \RX FRQVWUXFW D 'UDZLQJ3DQHO ZLWK D ZLGWK RI SL[HOV DQG D KHLJKW RI SL[HOV 7KHQ WKH XSSHU OHIW FRUQHU KDV FRRUGLQDWHV DQG WKH ORZHU ULJKW FRUQHU KDV FRRUGLQDWHV (0, 0) +-----------+ | | | | | | +-----------+ (199, 99) 7KLV LV OLNHO\ WR EH FRQIXVLQJ DW ILUVW EHFDXVH LQ PDWK FODVVHV \RX SUREDEO\ XVHG FRRUGLQDWH V\VWHPV ZKHUH \ YDOXHV ZHQW GRZQ DV \RX PRYHG GRZQ /LQHV DQG 6KDSHV 6R KRZ GR \RX DFWXDOO\ GUDZ VRPHWKLQJ" 7R GUDZ VKDSHV DQG OLQHV ZH GRQ W WDON GLUHFWO\ WR WKH 'UDZLQJ3DQHO EXW LQVWHDG WR D UHODWHG REMHFW RI D W\SH QDPHG *UDSKLFV 7KLQN RI WKH 'UDZLQJ3DQHO DV D FDQYDV DQG WKH *UDSKLFV REMHFW DV WKH SDLQWEUXVK 7KH 'UDZLQJ3DQHO FODVV KDV D PHWKRG QDPHG getGraphics WKDW UHWXUQV D UHIHUHQFH WR LWV *UDSKLFV REMHFW 2QH RI WKH VLPSOHVW GUDZLQJ FRPPDQGV LV GUDZ/LQH ZKLFK WDNHV IRXU LQWHJHU DUJXPHQWV g.drawLine(<x1>, <y1>, <x2>, <y2>); GUDZV D OLQH IURP WKH SRLQW [ \ WR WKH SRLQW [ \ +HUH LV D VDPSOH SURJUDP WKDW SXWV WKHVH SLHFHV WRJHWKHU 1 2 3 4 5 6 7 8 9 10 11 12 import java.awt.*; // for Graphics, Color, etc public class DrawSample1 { public static void main(String args) { // create the drawing panel DrawingPanel panel = new DrawingPanel(200, 100); // draw a line on the panel using the Graphics paintbrush Graphics g = panel.getGraphics(); g.drawLine(25, 75, 175, 25); } } ZKLFK SURGXFHV WKH IROORZLQJ RXWSXW 185 7KH ILUVW VWDWHPHQW LQ PDLQ FRQVWUXFWV D 'UDZLQJ3DQHO ZLWK D ZLGWK RI DQG D KHLJKW RI 2QFH FRQVWUXFWHG WKH ZLQGRZ ZLOO SRS XS RQ WKH VFUHHQ 7KH VHFRQG VWDWHPHQW GUDZV D OLQH IURP WR 7KH ILUVW SRLQW LV LQ WKH ORZHU OHIW SDUW RI WKH ZLQGRZ RYHU IURP WKH OHIW GRZQ IURP WKH WRS 7KH VHFRQG SRLQW LV LQ WKH XSSHU ULJKW FRUQHU RYHU IURP WKH OHIW RQO\ GRZQ IURP WKH WRS 1RWLFH WKHVH SDUWLFXODU OLQHV RI FRGH Graphics g = panel.getGraphics(); g.drawLine(25, 75, 175, 25); <RX PLJKW ZRQGHU ZK\ \RX FDQ W MXVW VD\ panel.drawLine(25, 75, 175, 25); // this is illegal 7KH SRLQW LV WKDW WKHUH DUH WZR GLIIHUHQW REMHFWV LQYROYHG LQ WKLV SURJUDP WKH GUDZLQJ SDQHO LWVHOI WKH FDQYDV DQG WKH *UDSKLFV REMHFW DVVRFLDWHG ZLWK WKH SDQHO WKH SDLQWEUXVK 7KH SDQHO GRHVQ W NQRZ KRZ WR GUDZ D OLQH 2QO\ WKH *UDSKLFV REMHFW NQRZV KRZ WR GUDZ D OLQH <RX KDYH WR EH FDUHIXO WR PDNH VXUH WKDW \RX DUH WDONLQJ WR WKH ULJKW REMHFW ZKHQ \RX JLYH D FRPPDQG 7KLV FDQ EH FRQIXVLQJ EXW LW LV D FRPPRQ RFFXUUHQFH LQ -DYD SURJUDPV ,Q IDFW D W\SLFDO -DYD SURJUDP KDV KXQGUHGV LI QRW WKRXVDQGV RI REMHFWV WKDW DUH LQWHUDFWLQJ ZLWK HDFK RWKHU 7KLV LVQ W VR XQOLNH ZKDW ZH GR DV SHRSOH ,I \RX ZDQW WR VFKHGXOH D PHHWLQJ ZLWK D EXV\ FRUSRUDWH H[HFXWLYH VKH PLJKW WHOO \RX WR 7DON WR P\ VHFUHWDU\ DERXW WKDW 2U LI \RX UH DVNLQJ GLIILFXOW OHJDO TXHVWLRQV D SHUVRQ PLJKW WHOO \RX WR 7DON WR P\ ODZ\HU DERXW WKDW ,Q WKLV FDVH WKH 'UDZLQJ3DQHO GRHVQ W NQRZ KRZ WR GUDZ ,I LW FRXOG WDON LW ZRXOG VD\ 7DON WR P\ *UDSKLFV REMHFW DERXW WKDW ,W ZRXOG DOVR KDYH EHHQ OHJDO WR XVH WKH *UDSKLFV ZLWKRXW VWRULQJ LW LQ D YDULDEOH OLNH WKLV panel.getGraphics().drawLine(25, 75, 175, 25); // this is also legal %XW LQ PDQ\ FDVHV ZH OO ZDQW WR VHQG VHYHUDO FRPPDQGV WR WKH *UDSKLFV REMHFW VR LW V PRUH FRQYHQLHQW WR JLYH LW D QDPH DQG VWRUH LW LQ D YDULDEOH /HW V ORRN DW D PRUH FRPSOLFDWHG H[DPSOH 1 2 3 4 5 6 7 8 9 10 11 12 13 import java.awt.*; // for Graphics, Color, etc public class DrawSample2 { public static void main(String args) { DrawingPanel panel = new DrawingPanel(200, 100); // draw a triangle on the panel Graphics g = panel.getGraphics(); g.drawLine(25, 75, 100, 25); g.drawLine(100, 25, 175, 75); g.drawLine(25, 75, 175, 75); } } 7KLV SURJUDP GUDZV WKUHH GLIIHUHQW OLQHV WR IRUP D WULDQJOH 186 7KH OLQHV DUH GUDZQ EHWZHHQ WKUHH GLIIHUHQW SRLQWV ,Q WKH ORZHU OHIW FRUQHU ZH KDYH WKH SRLQW ,Q WKH PLGGOH DW WKH WRS ZH KDYH WKH SRLQW $QG LQ WKH ORZHU ULJKW FRUQHU ZH KDYH WKH SRLQW 7KH YDULRXV FDOOV RQ GUDZ/LQH VLPSO\ GUDZ WKH OLQHV WKDW FRQQHFW WKHVH WKUHH SRLQWV 7KH *UDSKLFV REMHFW DOVR KDV PHWKRGV IRU GUDZLQJ SDUWLFXODU VKDSHV )RU H[DPSOH ZH RIWHQ ZDQW WR GUDZ UHFWDQJOHV DQG ZH FDQ GR VR ZLWK WKH GUDZ5HFW PHWKRG g.drawRect(<x>, <y>, <width>, <height>); GUDZV D UHFWDQJOH ZLWK XSSHU OHIW FRRUGLQDWHV [ \ DQG JLYHQ KHLJKW DQG ZLGWK $QRWKHU ILJXUH ZH RIWHQ ZDQW WR GUDZ LV D FLUFOH RU PRUH JHQHUDOO\ DQ RYDO :H DUH LPPHGLDWHO\ IDFHG ZLWK WKH SUREOHP RI KRZ WR VSHFLI\ ZKHUH LW DSSHDUV DQG KRZ ELJ LW LV :H GR VR E\ VSHFLI\LQJ ZKDW LV NQRZQ DV WKH ERXQGLQJ UHFWDQJOH RI WKH FLUFOH RU RYDO ,Q RWKHU ZRUGV ZH VSHFLI\ LW WKH VDPH ZD\ ZH VSHFLI\ D UHFWDQJOH ZLWK WKH XQGHUVWDQGLQJ WKDW -DYD ZLOO GUDZ WKH ODUJHVW RYDO SRVVLEOH WKDW ILWV LQVLGH WKDW UHFWDQJOH g.drawOval(<x>, <y>, <width>, <height>); GUDZV WKH ODUJHVW RYDO WKDW ILWV ZLWKLQ WKH UHFWDQJOH ZLWK XSSHU OHIW FRRUGLQDWHV [ \ DQG JLYHQ KHLJKW DQG ZLGWK 1RWLFH WKDW WKH ILUVW WZR YDOXHV SDVVHG WR GUDZ5HFW DQG GUDZ2YDO DUH FRRUGLQDWHV 7KH QH[W WZR YDOXHV DUH D ZLGWK DQG D KHLJKW QRW FRRUGLQDWHV )RU H[DPSOH KHUH LV D VKRUW SURJUDP WKDW GUDZV WZR UHFWDQJOHV DQG WZR RYDOV 1 2 3 4 5 6 7 8 9 10 11 12 13 import java.awt.*; public class DrawSample3 { public static void main(String args) { DrawingPanel panel = new DrawingPanel(200, 100); Graphics g = panel.getGraphics(); g.drawRect(25, 50, 20, 20); g.drawRect(150, 10, 40, 20); g.drawOval(50, 25, 20, 20); g.drawOval(150, 50, 40, 20); } } %HORZ LV WKH RXWSXW RI WKH SURJUDP 7KH ILUVW UHFWDQJOH KDV XSSHU OHIW FRUQHU ,WV ZLGWK DQG KHLJKW DUH HDFK VR WKLV LV D VTXDUH 7KH FRRUGLQDWHV RI LWV ORZHU ULJKW FRUQHU ZRXOG EH PRUH WKDQ WKH [ \ FRRUGLQDWHV RI WKH XSSHU OHIW FRUQHU 7KLV SURJUDP DOVR GUDZV D UHFWDQJOH ZLWK XSSHU OHIW FRUQHU WKDW KDV D ZLGWK RI DQG D KHLJKW RI ZLGHU WKDQ LW LV WDOO 7KH 187 ERXQGLQJ UHFWDQJOH RI WKH ILUVW RYDO KDV XSSHU OHIW FRRUGLQDWHV DQG D ZLGWK DQG KHLJKW RI ,Q RWKHU ZRUGV LW V D FLUFOH 7KH ERXQGLQJ UHFWDQJOH RI WKH VHFRQG RYDO KDV XSSHU OHIW FRRUGLQDWHV DQG D ZLGWK RI DQG KHLJKW RI DQ RYDO WKDW LV ZLGHU WKDQ LW LV WDOO 6RPHWLPHV ZH GRQ W MXVW ZDQW WR GUDZ WKH RXWOLQH RI D VKDSH ZH ZDQW WR SDLQW WKH HQWLUH DUHD ZLWK D SDUWLFXODU FRORU 7KHUH DUH YDULDWLRQV RI WKH GUDZ5HFW DQG GUDZ2YDO PHWKRGV NQRZQ DV ILOO5HFW DQG ILOO2YDO WKDW GR H[DFWO\ WKLV 7KH\ ILOO LQ WKH JLYHQ UHFWDQJOH RU RYDO ZLWK WKH FXUUHQW FRORU RI SDLQW )RU H[DPSOH LI ZH FKDQJH WZR RI WKH FDOOV LQ WKH SUHYLRXV SURJUDP WR EH ILOO RSHUDWLRQV LQVWHDG RI GUDZ RSHUDWLRQV 1 2 3 4 5 6 7 8 9 10 11 12 13 import java.awt.*; public class DrawSample4 { public static void main(String args) { DrawingPanel panel = new DrawingPanel(200, 100); Graphics g = panel.getGraphics(); g.fillRect(25, 50, 20, 20); g.drawRect(150, 10, 40, 20); g.drawOval(50, 25, 20, 20); g.fillOval(150, 50, 40, 20); } } ZH JHW WKLV RXWSXW LQVWHDG &RORUV $OO RI WKH VKDSHV DQG OLQHV LQ WKH SUHFHGLQJ SURJUDPV ZHUH EODFN DQG DOO RI WKH SDQHOV KDG D ZKLWH EDFNJURXQG 7KHVH DUH WKH GHIDXOW FRORUV %XW LW V SRVVLEOH WR VHW WKH EDFNJURXQG FRORU RI WKH SDQHO DQG \RX FDQ DOVR FKDQJH WKH FXUUHQW FRORU EHLQJ XVHG E\ WKH *UDSKLFV REMHFW OLNH GLSSLQJ \RXU SDLQWEUXVK LQ D GLIIHUHQW FRQWDLQHU RI SDLQW :H ZLOO XVH WKH VWDQGDUG &RORU FODVV WKDW LV DOVR SDUW RI WKH VWDQGDUG MDYD DZW SDFNDJH 188 7KHUH DUH D QXPEHU RI SUHGHILQHG FRORUV WKDW ZH FDQ UHIHU WR GLUHFWO\ 7KH\ DUH GHILQHG DV FODVV FRQVWDQWV MXVW OLNH WKH FRQVWDQWV ZH ZHUH XVLQJ LQ &KDSWHU 7KH QDPHV RI WKHVH FRQVWDQWV DUH LQ DOO XSSHUFDVH DQG DUH VHOI H[SODQDWRU\ 7R UHIHU WR RQH RI WKHVH FRORUV \RX KDYH WR SUHFHGH LW ZLWK WKH FODVV QDPH DQG D GRW DV LQ &RORU *5((1 RU &RORU %/8( %HORZ LV D WDEOH RI DOO RI WKH SUHGHILQHG &RORU FRQVWDQWV &RORU &RQVWDQWV Color.BLACK Color.GREEN Color.RED Color.BLUE Color.LIGHT_GRAY Color.WHITE Color.CYAN Color.MAGENTA Color.YELLOW Color.DARK_GRAY Color.ORANGE Color.GRAY Color.PINK 7KH 'UDZLQJ3DQHO REMHFW KDV D PHWKRG WKDW FDQ EH XVHG WR FKDQJH WKH FXUUHQW EDFNJURXQG FRORU WKDW FRYHUV WKH HQWLUH SDQHO <panel>.setBackground(<color>); VHWV WKH SDQHO V EDFNJURXQG WR EH WKH JLYHQ FRORU 7KH *UDSKLFV REMHFW KDV D PHWKRG WKDW FDQ EH XVHG WR FKDQJH WKH FXUUHQW GUDZLQJ FRORU IRU VKDSHV DQG OLQHV g.setColor(<color>); VHWV WKH FXUUHQW GUDZLQJ ILOOLQJ FRORU WR EH WKH JLYHQ FRORU &DOOLQJ VHW&RORU LV OLNH GLSSLQJ \RXU SDLQWEUXVK LQ D GLIIHUHQW FRORU SDLQW ,W PHDQV WKDW IURP WKDW SRLQW RQ DOO GUDZLQJ DQG ILOOLQJ ZLOO EH GRQH LQ WKDW FRORU )RU H[DPSOH KHUH LV DQRWKHU YHUVLRQ RI WKH SURJUDP IURP WKH SUHYLRXV WKDW XVHV D F\DQ OLJKW EOXH EDFNJURXQG FRORU DQG ILOOV LQ WKH RYDO DQG UHFWDQJOH ZLWK ZKLWH LQVWHDG RI EODFN 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import java.awt.*; public class DrawSample5 { public static void main(String args) { DrawingPanel panel = new DrawingPanel(200, 100); panel.setBackground(Color.CYAN); Graphics g = panel.getGraphics(); g.drawRect(150, 10, 40, 20); g.drawOval(50, 25, 20, 20); g.setColor(Color.WHITE); g.fillOval(150, 50, 40, 20); g.fillRect(25, 50, 20, 20); } } ,W SURGXFHV WKH IROORZLQJ RXWSXW 189 ,W V VRPHZKDW RGG WKDW \RX WHOO WKH SDQHO WR VHW WKH EDFNJURXQG FRORU ZKLOH \RX WHOO WKH *UDSKLFV WR VHW WKH IRUHJURXQG FRORU 7KH UHDVRQLQJ LV WKDW WKH EDFNJURXQG FRORU LV D SURSHUW\ RI WKH HQWLUH ZLQGRZ ZKLOH WKH IRUHJURXQG FRORU RQO\ DIIHFWV WKH SDUWLFXODU VKDSHV WKDW \RX WKHQ GUDZ ZLWK WKDW FRORU 1RWLFH WKDW ZH KDYH UHDUUDQJHG WKH RUGHU RI WKH FDOOV 7KH WZR GUDZLQJ FRPPDQGV DSSHDU ILUVW WKHQ WKH FDOO RQ VHW&RORU WKDW FKDQJHV WKH FRORU WR ZKLWH WKHQ WKH WZR ILOOLQJ FRPPDQGV :H GR WKLQJV LQ WKLV RUGHU VR WKDW WKH GUDZLQJ LV GRQH LQ EODFN DQG WKH ILOOLQJ LV GRQH LQ ZKLWH 7KH RUGHU RI RSHUDWLRQV LV YHU\ LPSRUWDQW LQ WKHVH GUDZLQJ SURJUDPV VR \RX OO KDYH WR NHHS WUDFN RI ZKDW \RXU FXUUHQW FRORU LV HDFK WLPH \RX JLYH D QHZ FRPPDQG WR GUDZ RU ILOO VRPHWKLQJ ,Q HDFK RI WKH SUHFHGLQJ H[DPSOHV ZH XVHG VLPSOH FRQVWDQWV IRU WKH GUDZLQJ DQG ILOOLQJ FRPPDQGV ,W LV SRVVLEOH WR XVH H[SUHVVLRQV )RU H[DPSOH VXSSRVH WKDW ZH VWLFN ZLWK RXU 'UDZLQJ3DQHO VL]H RI ZLGH DQG WDOO DQG ZH ZDQW WR KDYH D GLDJRQDO VHULHV RI IRXU UHFWDQJOHV WKDW H[WHQG IURP WKH XSSHU OHIW FRUQHU WR WKH ORZHU ULJKW FRUQHU HDFK ZLWK D ZKLWH RYDO LQVLGH ,Q RWKHU ZRUGV ZH ZDQW WR SURGXFH WKLV RXWSXW 7KH RYHUDOO ZLGWK RI DQG RYHUDOO KHLJKW RI DUH GLYLGHG HYHQO\ LQWR IRXU UHFWDQJOHV ZKLFK PHDQV WKDW WKH\ DOO KDYH WR EH ZLGH DQG KLJK 6R WKHLU ZLGWK DQG KHLJKW GRQ W FKDQJH EXW WKH SRVLWLRQ RI WKHLU XSSHU OHIW FRUQHUV DUH GLIIHUHQW 7KH ILUVW UHFWDQJOH KDV XSSHU OHIW FRUQHU 7KH VHFRQG RQH KDV XSSHU OHIW FRUQHU 7KH WKLUG KDV XSSHU OHIW FRUQHU $QG WKH IRXUWK KDV XSSHU OHIW FRUQHU :H QHHG WR ZULWH FRGH WR JHQHUDWH WKHVH GLIIHUHQW FRRUGLQDWHV 7KLV LV D JUHDW SODFH WR XVH D IRU ORRS 8VLQJ WKH WHFKQLTXHV RI FKDSWHU ZH FDQ PDNH D WDEOH DQG GHYHORS D IRUPXOD IRU WKH FRRUGLQDWHV ,Q WKLV FDVH LW LV HDVLHU WR KDYH WKH ORRS VWDUW ZLWK UDWKHU WKDQ ZKLFK ZLOO RIWHQ EH WKH FDVH ZLWK GUDZLQJ SURJUDPV %HORZ LV D SURJUDP WKDW LV D JRRG ILUVW VWDE DW JHQHUDWLQJ WKLV RXWSXW 190 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import java.awt.*; public class DrawSample8 { public static void main(String args) { DrawingPanel panel = new DrawingPanel(200, 100); panel.setBackground(Color.CYAN); Graphics g = panel.getGraphics(); for (int i = 0; i < 4; i++) { g.drawRect(i * 50, i * 25, 50, 25); g.setColor(Color.WHITE); g.fillOval(i * 50, i * 25, 50, 25); } } } ,W SURGXFHV WKH IROORZLQJ RXWSXW :H YH JRW RXU FRRUGLQDWHV DQG VL]HV ULJKW EXW WKH FRORUV DUHQ W TXLWH ULJKW ,QVWHDG RI JHWWLQJ EODFN UHFWDQJOHV ZLWK ZKLWH RYDOV LQVLGH ZH UH JHWWLQJ RQH EODFN UHFWDQJOH DQG WKUHH ZKLWH UHFWDQJOHV 7KDW V EHFDXVH ZH RQO\ KDYH RQH FDOO RQ VHW&RORU LQVLGH WKH ORRS ,QLWLDOO\ WKH FRORU ZLOO EH VHW WR EODFN ZKLFK LV ZK\ WKH ILUVW UHFWDQJOH FRPHV RXW EODFN %XW RQFH ZH PDNH D FDOO RQ VHW&RORU FKDQJLQJ WKH FRORU WR ZKLWH WKHQ HYHU\ VXEVHTXHQW GUDZLQJ DQG ILOOLQJ FRPPDQG LV GRQH LQ ZKLWH LQFOXGLQJ WKH VHFRQG WKLUG DQG IRXUWK UHFWDQJOHV 6R ZH QHHG WR LQFOXGH D FDOO WR VHW WKH FRORU WR EODFN IRU WKH UHFWDQJOH GUDZLQJ DQG WR VHW WKH FRORU WR ZKLWH IRU WKH RYDO :KLOH ZH UH DW LW LW V D JRRG LGHD WR VZLWFK WKH RUGHU RI WKHVH WKLQJV 7KH UHFWDQJOH DQG RYDO RYHUODS VOLJKWO\ DQG ZH ZRXOG UDWKHU KDYH WKH UHFWDQJOH GUDZQ RYHU WKH RYDO WKDQ WKH RWKHU ZD\ DURXQG %HORZ LV D SURJUDP WKDW SURGXFHV WKH FRUUHFW RXWSXW 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import java.awt.*; public class DrawSample7 { public static void main(String args) { DrawingPanel panel = new DrawingPanel(200, 100); panel.setBackground(Color.CYAN); Graphics g = panel.getGraphics(); for (int i = 0; i < 4; i++) { g.setColor(Color.WHITE); g.fillOval(i * 50, i * 25, 50, 25); g.setColor(Color.BLACK); g.drawRect(i * 50, i * 25, 50, 25); } } } 191 ,W V DOVR SRVVLEOH WR FUHDWH FXVWRP &RORU REMHFWV RI \RXU RZQ UDWKHU WKDQ XVLQJ WKH FRQVWDQW FRORUV SURYLGHG LQ WKH &RORU FODVV &RPSXWHU PRQLWRUV XVH UHG JUHHQ DQG EOXH 5*% DV WKHLU SULPDU\ FRORUV VR ZKHQ \RX FRQVWUXFW D &RORU REMHFW \RX SDVV \RXU RZQ SDUDPHWHU YDOXHV IRU WKH UHGQHVV JUHHQQHVV DQG EOXHQHVV RI WKH FRORU new Color(<red>, <green>, <blue>) (DFK RI WKH UHG JUHHQ EOXH FRPSRQHQWV VKRXOG EH LQWHJHU YDOXHV EHWZHHQ DQG 7KH KLJKHU WKH YDOXH WKH PRUH RI WKDW FRORU LV PL[HG LQ $OO YDOXHV ZRXOG SURGXFH EODFN DQG DOO YDOXHV ZRXOG SURGXFH ZKLWH 9DOXHV RI ZRXOG SURGXFH IXOO\ JUHHQ ZKLOH YDOXHV RI PDNH D GDUN SXUSOH FRORU EHFDXVH UHG DQG EOXH DUH PL[HG 6HDUFK IRU 5*% WDEOH LQ \RXU IDYRULWH VHDUFK HQJLQH WR ILQG WDEOHV RI PDQ\ FRPPRQ FRORUV 7KH IROORZLQJ SURJUDP GHPRQVWUDWHV FXVWRP FRORUV ,W XVHV D FODVV FRQVWDQW IRU WKH QXPEHU RI UHFWDQJOHV WR GUDZ DQG SURGXFHV D EOHQG RI FRORUV IURP EODFN WR ZKLWH 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import java.awt.*; public class DrawColorGradient { public static final int RECTS = 32; public static void main(String args) { DrawingPanel panel = new DrawingPanel(256, 256); panel.setBackground(new Color(255, 128, 0)); // orange Graphics g = panel.getGraphics(); // from black to white, top/left to bottom/right for (int i = 0; i < RECTS; i++) { int shift = i * 256 / RECTS; g.setColor(new Color(shift, shift, shift)); g.fillRect(shift, shift, 20, 20); } } } ,W SURGXFHV WKH IROORZLQJ RXWSXW 192 7H[W DQG )RQWV 7KHUH LV DQRWKHU GUDZLQJ FRPPDQG ZRUWK PHQWLRQLQJ ,W FDQ EH XVHG WR LQFOXGH WH[W LQ \RXU GUDZLQJ g.drawString(<message>, <x>, <y>); GUDZV WKH JLYHQ 6WULQJ ZLWK ORZHU OHIW FRUQHU [ \ 7KLV LV D VOLJKWO\ GLIIHUHQW FRQYHQWLRQ WKDQ ZKDW ZH XVHG IRU GUDZ5HFW :LWK GUDZ5HFW ZH VSHFLILHG WKH FRRUGLQDWHV RI WKH XSSHU OHIW FRUQHU +HUH ZH VSHFLI\ WKH FRRUGLQDWHV RI WKH ORZHU OHIW FRUQHU %\ GHIDXOW WH[W LV GUDZQ DSSUR[LPDWHO\ SL[HOV KLJK %HORZ LV D VDPSOH SURJUDP WKDW XVHV D ORRS WR GUDZ D SDUWLFXODU 6WULQJ WHQ GLIIHUHQW WLPHV LQGHQWLQJ HDFK WLPH PRUH WR WKH ULJKW DQG PRYLQJ GRZQ IURP WKH WRS 1 2 3 4 5 6 7 8 9 10 11 12 13 import java.awt.*; public class DrawStringMessage1 { public static void main(String args) { DrawingPanel panel = new DrawingPanel(200, 100); panel.setBackground(Color.YELLOW); Graphics g = panel.getGraphics(); for (int i = 0; i < 10; i++) { g.drawString("There is no place like home", i * 5, 10 + i * 10); } } } ,W SURGXFHV WKH IROORZLQJ RXWSXW 193 ,I \RX G OLNH WR FKDQJH WKH VW\OH RU VL]H LQ ZKLFK WKH RQVFUHHQ WH[W LV GUDZQ \RX FDQ XVH WKH VHW)RQW FRPPDQG RQ WKH *UDSKLFV REMHFW g.setFont(<font>) FKDQJHV WKH WH[W VL]H DQG VW\OH LQ ZKLFK VWULQJV DUH GUDZQ 7KH SDUDPHWHU WR VHW)RQW LV D )RQW REMHFW $ )RQW REMHFW LV FRQVWUXFWHG E\ SDVVLQJ WKUHH SDUDPHWHUV 7KH IRQW V QDPH DV D 6WULQJ LWV VW\OH VXFK DV EROG RU LWDOLF DQG LWV VL]H DV DQ LQWHJHU 7KH IRQW VW\OHV VXFK DV EROG DUH LPSOHPHQWHG DV FRQVWDQWV LQ WKH )RQW FODVV new Font(<name>, <style>, <size>) 8VHIXO &RQVWDQWV RI WKH )RQW &ODVV Font.BOLD %ROG WH[W ,WDOLF WH[W Font.BOLD + Font.ITALIC %ROG ,WDOLF WH[W Font.PLAIN 3ODLQ WH[W Font.ITALIC &RPPRQ )RQW 1DPHV "Monospaced" "SansSerif" "Serif" $ W\SHZULWHU IRQW VXFK DV Courier New $ IRQW ZLWKRXW FXUYHV VHULIV DW OHWWHU HGJHV VXFK DV Arial $ IRQW ZLWK FXUYHG HGJHV VXFK DV Times New Roman $V ZLWK FRORUV VHWWLQJ WKH IRQW RQO\ DIIHFWV VWULQJV GUDZQ DIWHU WKH IRQW LV VHW 7KH IROORZLQJ SURJUDP VHWV VHYHUDO IRQWV DQG XVHV WKHP WR GUDZ VWULQJV 194 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import java.awt.*; public class DrawFonts { public static void main(String args) { DrawingPanel panel = new DrawingPanel(200, 100); panel.setBackground(Color.PINK); Graphics g = panel.getGraphics(); g.setFont(new Font("Monospaced", Font.BOLD + Font.ITALIC, 36)); g.drawString("Too big", 20, 40); g.setFont(new Font("SansSerif", Font.PLAIN, 10)); g.drawString("Too small", 30, 60); g.setFont(new Font("Serif", Font.ITALIC, 18)); g.drawString("Just right", 40, 80); } } ,W SURGXFHV WKH IROORZLQJ RXWSXW * 3URFHGXUDO 'HFRPSRVLWLRQ ZLWK *UDSKLFV ,I \RX ZULWH FRPSOH[ GUDZLQJ SURJUDPV \RX ZLOO ZDQW WR EUHDN WKH SURJUDP GRZQ LQWR VHYHUDO VWDWLF PHWKRGV WR VWUXFWXUH WKH FRGH DQG WR UHPRYH UHGXQGDQF\ ,Q GRLQJ VR \RX OO KDYH WR SDVV D UHIHUHQFH WR WKH *UDSKLFV REMHFW WR HDFK VWDWLF PHWKRG WKDW \RX LQWURGXFH )RU D TXLFN H[DPSOH WKH SURJUDP IURP WKH SUHYLRXV VHFWLRQ FRXOG EH VSOLW LQWR D PDLQ PHWKRG DQG D GUDZLQJ PHWKRG DV IROORZV 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import java.awt.*; public class DrawStringMessage2 { public static void main(String args) { DrawingPanel panel = new DrawingPanel(200, 100); panel.setBackground(Color.YELLOW); Graphics g = panel.getGraphics(); drawText(g); } public static void drawText(Graphics g) { for (int i = 0; i < 10; i++) { g.drawString("There is no place like home", i * 5, 10 + i * 10); } } } 195 ,W SURGXFHV WKH IROORZLQJ RXWSXW 7KH SURJUDP ZRXOGQ W FRPSLOH ZLWKRXW SDVVLQJ *UDSKLFV J WR WKH GUDZ7H[W PHWKRG EHFDXVH J LV QHHGHG WR FDOO DQ\ GUDZLQJ PHWKRGV OLNH GUDZ6WULQJ RU ILOO5HFW $ /DUJHU ([DPSOH 'UDZ'LDPRQGV 1RZ OHW V FRQVLGHU D VOLJKWO\ PRUH FRPSOLFDWHG WDVN GUDZLQJ WKH ODUJHVW GLDPRQG ILJXUH WKDW ZLOO ILW LQWR D ER[ RI D SDUWLFXODU VL]H 7KH ODUJHVW GLDPRQG WKDW FDQ ILW LQWR D ER[ RI VL]H [ ORRNV D ELW OLNH WKH IROORZLQJ GLDJUDP (0, 0) (25, 0) +-----------+ | / \ | | / \ | (25, 0) |/ \| (50, 25) |\ /| | \ / | | \ / | +-----------+ (25, 50) 7KH FRGH WR GUDZ VXFK D GLDPRQG ZRXOG EH WKH IROORZLQJ g.drawRect(0, 0, 50, 50); g.drawLine(0, 25, 25, 0); g.drawLine(25, 0, 50, 25); g.drawLine(50, 25, 25, 50); g.drawLine(25, 50, 0, 25); 1RZ LPDJLQH WKDW ZH ZLVK WR GUDZ [ GLDPRQGV DW GLIIHUHQW ORFDWLRQV :H FDQ WXUQ RXU GLDPRQG GUDZLQJ FRGH LQWR D GUDZ'LDPRQG PHWKRG ZH OO FDOO WKUHH WLPHV 6LQFH HDFK GLDPRQG ZLOO EH LQ D GLIIHUHQW SRVLWLRQ ZH FDQ SDVV WKH [ DQG \ FRRUGLQDWHV DV SDUDPHWHUV WR RXU GUDZ'LDPRQG PHWKRG 7KH SDUDPHWHUV WR WKH JUDSKLFDO FDOOV OLNH GUDZ/LQH DQG GUDZ5HFW ZLOO KDYH WR EH FKDQJHG $ GLDPRQG ZLWK WRS OHIW FRUQHU DW ORFDWLRQ ZRXOG ORRN OLNH WKLV 196 (78, 22) (103, 22) +-----------+ | / \ | | / \ | (78, 47) |/ \| (128, 47) |\ /| | \ / | | \ / | +-----------+ (103, 72) 7KH FRGH WR GUDZ WKLV GLDPRQG ZRXOG EH WKH IROORZLQJ g.drawRect(78, 22, 50, 50); g.drawLine(78, 47, 103, 22); g.drawLine(103, 22, 128, 47); g.drawLine(128, 47, 103, 72); g.drawLine(103, 72, 78, 47); /RRNLQJ DW WKH FRGH IRU WKH WZR GLDPRQGV WKH SDUDPHWHUV SDVVHG WR WKH GUDZ5HFW DQG GUDZ/LQH FRPPDQGV DUH YHU\ VLPLODU WR WKRVH RI WKH ILUVW GLDPRQG H[FHSW WKDW WKH\ UH VKLIWHG E\ LQ WKH [ GLUHFWLRQ DQG LQ WKH \ GLUHFWLRQ H[FHSW IRU WKH WKLUG DQG IRXUWK SDUDPHWHUV WR GUDZ5HFW VLQFH WKHVH DUH WKH UHFWDQJOH V ZLGWK DQG KHLJKW 7KLV VKLIW LV FDOOHG DQ RIIVHW :H FDQ JHQHUDOL]H WKH FRRUGLQDWHV WR SDVV WR *UDSKLFV J V GUDZLQJ FRPPDQGV VR WKDW WKH\ OO ZRUN ZLWK DQ\ GLDPRQG LI ZH SDVV WKDW GLDPRQG V WRS OHIW [ DQG \ RIIVHW )RU H[DPSOH ZH OO JHQHUDOL]H WKH OLQH IURP WR LQ WKH ILUVW GLDPRQG DQG IURP WR LQ WKH VHFRQG \ ZKHUH [ \ LV WKH RIIVHW RI WKH JLYHQ GLDPRQG E\ VD\LQJ WKDW LW LV D OLQH IURP [ \ WR [ GLDPRQG 7KH IROORZLQJ SURJUDP XVHV WKH GUDZ'LDPRQG PHWKRG WR GUDZ GLDPRQGV ZLWKRXW UHGXQGDQF\ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // This program draws several diamond figures of size 50x50. import java.awt.*; public class DrawDiamonds { public static void main(String args) { DrawingPanel panel = new DrawingPanel(250, 150); Graphics g = panel.getGraphics(); drawDiamond(g, 0, 0); drawDiamond(g, 78, 22); drawDiamond(g, 19, 81); } // draws a diamond in 50x50 box public static void drawDiamond(Graphics g, int x, int y) { g.drawRect(x, y, 50, 50); g.drawLine(x, y + 25, x + 25, y); g.drawLine(x + 25, y, x + 50, y + 25); g.drawLine(x + 50, y + 25, x + 25, y + 50); g.drawLine(x + 25, y + 50, x, y + 25); } } 197 ,W SURGXFHV WKH IROORZLQJ RXWSXW $QRWKHU FKDOOHQJH ZRXOG EH WR PDNH WKH VL]H D SDUDPHWHU WR GUDZ GLDPRQGV RI GLIIHUHQW VL]HV ,W V SRVVLEOH WR GUDZ SDWWHUQHG ILJXUHV LQ ORRSV DQG WR KDYH RQH RI RXU GUDZLQJ PHWKRGV FDOO DQRWKHU )RU H[DPSOH LI ZH ZDQW WR GUDZ GLDPRQGV VWDUWLQJ DW DQG VSDFHG SL[HOV DSDUW ZH MXVW QHHG D IRU ORRS WKDW UHSHDWV WLPHV DQG VKLIWV WKH [ FRRUGLQDWH E\ HDFK WLPH +HUH V DQ H[DPSOH ORRS for (int i = 0; i < 5; i++) { drawDiamond(g, 12 + 60*i, 15); } ,I ZH ZDQWHG WR WXUQ RXU OLQH RI GLDPRQGV LQWR DQRWKHU PHWKRGV ZH FRXOG FDOO LW IURP PDLQ WR GUDZ PDQ\ OLQHV RI GLDPRQGV +HUH V D PRGLILHG YHUVLRQ RI WKH 'UDZ'LDPRQGV SURJUDP ZLWK WZR JUDSKLFDO PHWKRGV 198 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 30 // This program draws several diamond figures of size 50x50. import java.awt.*; public class DrawDiamonds2 { public static void main(String args) { DrawingPanel panel = new DrawingPanel(360, 160); Graphics g = panel.getGraphics(); drawManyDiamonds(g, 12, 15); g.setColor(Color.RED); drawManyDiamonds(g, 55, 100); } // Draws five diamonds in a horizontal line. public static void drawManyDiamonds(Graphics g, int x, int y) { for (int i = 0; i < 5; i++) { drawDiamond(g, x + 60*i, y); } } // draws a diamond in 50x50 box public static void drawDiamond(Graphics g, int x, int y) { g.drawRect(x, y, 50, 50); g.drawLine(x, y + 25, x + 25, y); g.drawLine(x + 25, y, x + 50, y + 25); g.drawLine(x + 50, y + 25, x + 25, y + 50); g.drawLine(x + 25, y + 50, x, y + 25); } } ,W SURGXFHV WKH IROORZLQJ RXWSXW 6XPPDU\ RI *UDSKLFV 0HWKRGV 7KH *UDSKLFV REMHFW KDV PDQ\ PRUH PHWKRGV WKDQ WKRVH VKRZQ LQ WKLV VHFWLRQ ,I \RX DUH LQWHUHVWHG \RX FDQ UHDG DERXW WKHP LQ WKH -DYD DSL GRFXPHQWDWLRQ %HORZ LV D OLVW RI WKH EDVLF RSHUDWLRQV FRYHUHG 199 6RPH 8VHIXO 0HWKRGV RI *UDSKLFV 2EMHFWV 0HWKRG drawLine(x1, y1, x2, y2) drawOval(x, y, width, height) 'HVFULSWLRQ 'UDZV D OLQH EHWZHHQ WKH SRLQWV [ \ DQG [ \ 'UDZV WKH RXWOLQH RI WKH ODUJHVW RYDO WKDW ILWV ZLWKLQ WKH VSHFLILHG UHFWDQJOH drawRect(x, y, width, height) 'UDZV WKH RXWOLQH RI WKH VSHFLILHG UHFWDQJOH drawString(message, x, y) 'UDZV WKH JLYHQ WH[W ZLWK ORZHU OHIW FRUQHU [ \ fillOval(x, y, width, height) )LOOV WKH ODUJHVW RYDO WKDW ILWV ZLWKLQ WKH VSHFLILHG UHFWDQJOH XVLQJ WKH FXUUHQW FRORU fillRect(x, y, width, height) )LOOV WKH VSHFLILHG UHFWDQJOH XVLQJ WKH FXUUHQW FRORU setColor(Color) 6HWV WKLV JUDSKLFV FRQWH[W V FXUUHQW FRORU WR WKH VSHFLILHG FRORU $OO VXEVHTXHQW JUDSKLFV RSHUDWLRQV XVLQJ WKLV JUDSKLFV FRQWH[W XVH WKLV VSHFLILHG FRORU setFont(Font) 6HWV WKLV JUDSKLFV FRQWH[W V FXUUHQW IRQW WR WKH VSHFLILHG IRQW $OO VXEVHTXHQW VWULQJV GUDZQ XVLQJ WKLV JUDSKLFV FRQWH[W XVH WKLV VSHFLILHG IRQW * &DVH 6WXG\ 3\UDPLGV ,PDJLQH WKDW ZH YH EHHQ DVNHG WR ZULWH D SURJUDP WKDW ZLOO GUDZ WKH IROORZLQJ ILJXUH RQWR D 'UDZLQJ3DQHO 7KH RYHUDOO GUDZLQJ SDQHO KDV D VL]H RI E\ (DFK S\UDPLG LV SL[HOV KLJK DQG SL[HOV ZLGH 7KH S\UDPLGV FRQVLVW RI FHQWHUHG FRORUHG VWDLUV WKDW ZLGHQ WRZDUG WKH ERWWRP ZLWK EODFN RXWOLQHV DURXQG HDFK VWDLU +HUH LV D WDEOH RI WKH DWWULEXWHV RI HDFK S\UDPLG 200 ILOO FRORU WRS OHIW FRUQHU ZKLWH UHG EOXH RI VWDLUV KHLJKW RI HDFK VWDLU VWDLUV VWDLUV VWDLUV SL[HOV SL[HOV SL[HOV $Q 8QVWUXFWXUHG 3DUWLDO 6ROXWLRQ :KHQ WU\LQJ WR VROYH D ODUJHU DQG PRUH FRPSOH[ SUREOHP OLNH WKLV LW V LPSRUWDQW WR WDFNOH LW SLHFH E\ SLHFH DQG PDNH LWHUDWLYH HQKDQFHPHQWV WRZDUG D ILQDO VROXWLRQ /HW V EHJLQ E\ WU\LQJ WR GUDZ WKH WRS OHIW ZKLWH S\UDPLG FRUUHFWO\ (DFK VWDLU LV FHQWHUHG KRUL]RQWDOO\ ZLWKLQ WKH S\UDPLG 7KH WRS VWDLU LV SL[HOV ZLGH 7KHUHIRUH LW LV VXUURXQGHG E\ RU SL[HOV RI HPSW\ VSDFH RQ HLWKHU VLGH 7KDW PHDQV WKDW WKH [ UHFWDQJOH V WRS OHIW FRUQHU LV DW 7KH VHFRQG VWDLU LV SL[HOV ZLGH PHDQLQJ WKDW LW V VXUURXQGHG E\ RU SL[HOV RQ HDFK VLGH (0, 0) +--+ <=-- 45 -=> |10| <=- 45 ---=> +-+--+-+ <=- 40 => | 20 | <= 40 --=> +------+ 7KH IROORZLQJ SURJUDP GUDZV WKH ZKLWH S\UDPLG DW LWV FRUUHFW SRVLWLRQ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import java.awt.*; // Draws the first pyramid only, with a lot of redundancy. public class Pyramids1 { public static void main(String args) { DrawingPanel panel = new DrawingPanel(350, 250); Graphics g = panel.getGraphics(); // border rectangle g.drawRect(0, 0, 100, 100); // 10 "stairs" g.drawRect(45, g.drawRect(40, g.drawRect(35, g.drawRect(30, g.drawRect(25, g.drawRect(20, g.drawRect(15, g.drawRect(10, g.drawRect( 5, g.drawRect( 0, } } in pyramid 0, 10, 10); 10, 20, 10); 20, 30, 10); 30, 40, 10); 40, 50, 10); 50, 60, 10); 60, 70, 10); 70, 80, 10); 80, 90, 10); 90, 100, 10); 201 /RRNLQJ DW WKH FRGH LW V FOHDU WKDW WKH WHQ OLQHV WR GUDZ WKH VWDLUV KDYH D ORW RI UHGXQGDQF\ EHWZHHQ WKHP ([DPLQLQJ WKH SDWWHUQV RI QXPEHUV LQ HDFK FROXPQ WKH [ YDOXHV GHFUHDVH E\ HDFK WLPH WKH \ YDOXH LQFUHDVHV E\ HDFK WLPH WKH ZLGWK LQFUHDVHV E\ HDFK WLPH DQG WKH KHLJKW VWD\V WKH VDPH $QRWKHU ZD\ RI GHVFULELQJ WKH VWDLU V [ YDOXH LV WKDW LW LV KDOI RI WKH RYHUDOO PLQXV WKH VWDLU V ZLGWK 7KH IROORZLQJ IRU ORRS GUDZV WKH WHQ VWDLUV ZLWKRXW WKH SUHYLRXV UHGXQGDQF\ for (int i = 0; i < 10; i++) { int stairWidth = 10 * (i + 1); int stairHeight = 10; int stairX = (100 - stairWidth) / 2; int stairY = 10 * i; g.drawRect(stairX, stairY, stairWidth, stairHeight); } *HQHUDOL]LQJ WKH 'UDZLQJ RI 3\UDPLGV 1H[W OHW V DGG FRGH WR GUDZ WKH ERWWRP UHG S\UDPLG ,WV [ \ SRVLWLRQ LV VWDLUV 7KDW PHDQV HDFK VWDLU LV WZLFH DV WDOO DQG ZLGH DV EHIRUH (80, 140) +----+ <=- 40 --=> | 20 | <=-- 40 -=> +---+----+---+ <= 30=> | 40 | <=30 => +------------+ DQG LW KDV RQO\ %DVHG RQ WKLV WKH WRS VWDLU V XSSHU OHIW FRUQHU LV DW DQG LWV VL]H LV XSSHU OHIW FRUQHU LV DW DQG LWV VL]H LV [ DQG VR RQ [ 7KH VHFRQG VWDLU V )RU WKH PRPHQW OHW V IRFXV RQ JHWWLQJ WKH FRRUGLQDWHV RI WKH VWDLUV ULJKW DQG QRW RQ WKH UHG ILOO FRORU +HUH LV D UHGXQGDQW ELW RI FRGH WR GUDZ WKH UHG S\UDPLG V VWDLUV ZLWKRXW WKH FRORULQJ // bottom red pyramid g.drawRect(80, 140, 100, 100); // 5 "stairs" of red pyramid g.drawRect(120, 140, 20, 20); g.drawRect(110, 160, 40, 20); g.drawRect(100, 180, 60, 20); g.drawRect( 90, 200, 80, 20); g.drawRect( 80, 220, 100, 20); 1RWLFLQJ WKDW ZH DJDLQ KDYH UHGXQGDQF\ EHWZHHQ WKH ILYH OLQHV WR GUDZ WKH VWDLUV OHW V ORRN IRU D SDWWHUQ EHWZHHQ WKHP :H OO XVH D ORRS OLNH ZH GLG LQ WKH ODVW S\UDPLG EXW ZLWK DSSURSULDWH PRGLILFDWLRQV (DFK VWDLU V KHLJKW LV QRZ SL[HOV DQG HDFK VWDLU V ZLGWK LV QRZ WLPHV WKH QXPEHU IRU WKDW VWDLU 7KH [ DQG \ FRRUGLQDWHV DUH D ELW WULFNLHU 7KH [ FRRUGLQDWH IRUPXOD LV VLPLODU WR DFFRXQW WR WKH (100 - stairWidth) / 2 IURP EHIRUH EXW WKLV WLPH LW PXVW EH VKLIWHG ULJKW E\ IRU WKH S\UDPLG V WRS OHIW FRUQHU 7KH \ FRRUGLQDWH PXVW VLPLODUO\ EH VKLIWHG GRZQZDUG E\ SL[HOV +HUH V WKH FRUUHFW ORRS 202 // 5 "stairs" of red pyramid for (int i = 0; i < 5; i++) { int stairWidth = 20 * (i + 1); int stairHeight = 20; int stairX = 80 + (100 - stairWidth) / 2; int stairY = 140 + 20 * i; g.drawRect(stairX, stairY, stairWidth, stairHeight); } &DQ ZH VSRW WKH SDWWHUQ EHWZHHQ WKHVH WZR ORRSV" (DFK ORRS V [ DQG \ FRRUGLQDWHV GLIIHU RQO\ E\ WKH IDFW WKDW ZH KDYH WR DGG WKH VWDUWLQJ SRLQW WR WKH VHFRQG ORRS 7KH VWDLUV ZLGWKV DQG KHLJKWV GLIIHU RQO\ E\ WKH IDFW WKDW RQH S\UDPLG V VWDLUV DUH SL[HOV WDOO DQG WKH RWKHU V DUH ,Q IDFW WKH UHDVRQ WKH\ UH WKDW WDOO LV EHFDXVH WKH RYHUDOO VL]H RI GLYLGHG E\ WKH QXPEHU RI VWDLUV RU \LHOGV WKH KHLJKW RI HDFK VWDLU RU UHVSHFWLYHO\ 8VLQJ WKH SUHFHGLQJ LQIRUPDWLRQ OHW V WXUQ WKH FRGH IRU GUDZLQJ D S\UDPLG LQWR D PHWKRG WKDW ZH FDQ FDOO WLPHV WR DYRLG UHGXQGDQF\ $V SDUDPHWHUV ZH OO SDVV WKH [ \ FRRUGLQDWHV RI WKH WRS OHIW FRUQHU RI WKH S\UDPLG DQG WKH QXPEHU RI VWDLUV LQ WKH S\UDPLG :H OO DOVR QHHG WR SDVV WKH *UDSKLFV J DV D SDUDPHWHU VR WKDW ZH FDQ GUDZ RQWR WKH 'UDZLQJ3DQHO 7KH PDLQ PRGLILFDWLRQ ZH OO PDNH WR WKH FRGH LV WR FRPSXWH WKH VWDLU KHLJKW ILUVW WKHQ XVH WKLV WR FRPSXWH WKH VWDLU ZLGWK WKHQ XVH WKH ZLGWK DQG KHLJKW WR KHOS XV FRPSXWH WKH [ \ FRRUGLQDWHV RI WKH VWDLU +HUH V WKH FRGH public static void drawPyramid(Graphics g, int x, int y, int stairs) { // bounding rectangle g.drawRect(x, y, 100, 100); // stairs of the pyramid for (int i = 0; i < stairs; i++) { int stairHeight = 100 / stairs; int stairWidth = stairHeight * (i + 1); int stairX = x + (100 - stairWidth) / 2; int stairY = y + stairHeight * i; g.drawRect(stairX, stairY, stairWidth, stairHeight); } } 7KH SUHFHGLQJ FRGH LV QRZ JHQHUDOL]HG WR GUDZ D S\UDPLG DW DQ\ ORFDWLRQ ZLWK DQ\ QXPEHU RI VWDLUV 2QH ILQDO LQJUHGLHQW LV PLVVLQJ WKH DELOLW\ WR JLYH D GLIIHUHQW FRORU WR HDFK S\UDPLG $ &RPSOHWH 6WUXFWXUHG 6ROXWLRQ 7KH SUHFHGLQJ FRGH LV FRUUHFW H[FHSW WKDW LW GRHVQ W DOORZ XV WR GUDZ WKH S\UDPLG LQ WKH SURSHU FRORU /HW V DGG DQ DGGLWLRQDO SDUDPHWHU D &RORU WR RXU PHWKRG DQG XVH LW WR ILOO WKH S\UDPLG VWDLUV DV QHHGHG :H OO SDVV &RORU :+,7( DV WKLV SDUDPHWHU V YDOXH IRU WKH ILUVW ZKLWH S\UDPLG LW OO ILOO WKH VWDLUV ZLWK ZKLWH HYHQ WKRXJK LW GRHVQ W KDYH WR 7KH ZD\ WR GUDZ D ILOOHG VKDSH ZLWK DQ RXWOLQH RI D GLIIHUHQW FRORU LV WR ILUVW ILOO WKH VKDSH WKHQ XVH WKH RXWOLQH FRORU WR GUDZ WKH VDPH VKDSH )RU H[DPSOH WR JHW UHG UHFWDQJOHV ZLWK EODFN RXWOLQHV ILUVW ZH OO XVH ILOO5HFW ZLWK UHG WKHQ ZH OO XVH GUDZ5HFW ZLWK EODFN ZLWK WKH VDPH SDUDPHWHUV +HUH V WKH QHZ YHUVLRQ RI WKH GUDZ3\UDPLG PHWKRG WKDW XVHV WKH ILOO FRORU DV D SDUDPHWHU 203 public static void drawPyramid(Graphics g, Color c, int x, int y, int stairs) { g.drawRect(x, y, 100, 100); for (int i = 0; i < stairs; i++) { int stairHeight = 100 / stairs; int stairWidth = stairHeight * (i + 1); int stairX = x + (100 - stairWidth) / 2; int stairY = y + stairHeight * i; g.setColor(c); g.fillRect(stairX, stairY, stairWidth, stairHeight); g.setColor(Color.BLACK); g.drawRect(stairX, stairY, stairWidth, stairHeight); } } 8VLQJ WKLV PHWKRG ZH FDQ QRZ GUDZ DOO S\UDPLGV HDVLO\ E\ FDOOLQJ GUDZ3\UDPLG DSSURSULDWH SDUDPHWHUV drawPyramid(g, Color.WHITE, 0, 0, 10); drawPyramid(g, Color.RED, 80, 140, 5); drawPyramid(g, Color.BLUE, 220, 50, 20); WLPHV ZLWK WKH 2QH ODVW LPSURYHPHQW ZH FDQ PDNH WR RXU 3\UDPLGV SURJUDP LV WR WXUQ WKH RYHUDOO S\UDPLG VL]H RI LQWR D FRQVWDQW VR WKHUH DUHQ W VR PDQ\ V O\LQJ DURXQG LQ WKH FRGH +HUH LV WKH FRPSOHWH SURJUDP 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 30 // This program uses DrawingPanel to draw three colored // pyramid figures onto the screen. import java.awt.*; public class Pyramids { public static final int SIZE = 100; public static void main(String args) { DrawingPanel panel = new DrawingPanel(350, 250); Graphics g = panel.getGraphics(); drawPyramid(g, Color.WHITE, 0, 0, 10); drawPyramid(g, Color.RED, 80, 140, 5); drawPyramid(g, Color.BLUE, 220, 50, 20); } // Draws one pyramid figure with the given number of // stairs at the given x/y position with the given color. public static void drawPyramid(Graphics g, Color c, int x, int y, int stairs) { // bounding rectangle g.drawRect(x, y, SIZE, SIZE); // stairs of the pyramid for (int i = 0; i < stairs; i++) { int stairHeight = SIZE / stairs; int stairWidth = stairHeight * (i + 1); int stairX = x + (SIZE - stairWidth) / 2; 204 31 32 33 34 35 36 37 38 39 40 41 42 int stairY = y + stairHeight * i; // fill rectangle with fill color g.setColor(c); g.fillRect(stairX, stairY, stairWidth, stairHeight); // draw black rectangle outline g.setColor(Color.BLACK); g.drawRect(stairX, stairY, stairWidth, stairHeight); } } } &KDSWHU 6XPPDU\ 'UDZLQJ3DQHO LV D FXVWRP FODVV SURYLGHG E\ WKH DXWKRUV WR HDVLO\ VKRZ D JUDSKLFDO ZLQGRZ RQ WKH VFUHHQ $ 'UDZLQJ3DQHO FRQWDLQV D *UDSKLFV REMHFW ZKLFK FDQ EH XVHG WR GUDZ OLQHV WH[W DQG VKDSHV RQ WKH VFUHHQ XVLQJ GLIIHUHQW FRORUV $ *UDSKLFV REMHFW KDV PDQ\ XVHIXO PHWKRGV IRU GUDZLQJ VKDSHV DQG OLQHV VXFK DV GUDZ/LQH ILOO5HFW DQG VHW&RORU 6KDSHV FDQ EH GUDZQ RQO\ GUDZLQJ WKHLU RXWOLQH RU ILOOHG FRORULQJ WKH HQWLUH VKDSH 7KH *UDSKLFV REMHFW FDQ ZULWH WH[W RQ WKH VFUHHQ ZLWK LWV GUDZ6WULQJ PHWKRG 'LIIHUHQW IRQW VW\OHV DQG VL]HV FDQ EH VSHFLILHG ZLWK WKH VHW)RQW PHWKRG *UDSKLFDO SURJUDPV WKDW DUH GHFRPSRVHG LQWR PHWKRGV PXVW SDVV DSSURSULDWH SDUDPHWHUV 7KH *UDSKLFV REMHFW PXVW EH SDVVHG DV ZHOO DV DQ\ [ \ FRRUGLQDWHV VL]HV RU RWKHU YDOXHV WKDW JXLGH WKH ILJXUHV WR EH GUDZQ 6HOI &KHFN ([HUFLVHV 6HFWLRQ * ,QWURGXFWLRQ WR *UDSKLFV 7KHUH DUH WZR PLVWDNHV LQ WKH IROORZLQJ FRGH DWWHPSWLQJ WR GUDZ D OLQH IURP FRRUGLQDWHV WR :KDW DUH WKH\" DrawingPanel panel = new DrawingPanel(200, 200); panel.drawLine(50, 20, 86, 35); 7KH IROORZLQJ FRGH DWWHPSWV WR GUDZ D ILOOHG EODFN RXWHU UHFWDQJOH ZLWK D ZKLWH ILOOHG LQQHU FLUFOH LQVLGH LW DrawingPanel panel = new DrawingPanel(200, 100); Graphics g = panel.getGraphics(); g.setColor(Color.WHITE); g.fillOval(10, 10, 50, 50); g.setColor(Color.BLACK); g.fillRect(10, 10, 50, 50); 205 +RZHYHU LW KDV WKH IROORZLQJ LQFRUUHFW DSSHDUDQFH :KDW PXVW EH FKDQJHG IRU LW WR ORRN FRUUHFWO\" 7KH IROORZLQJ FRGH DWWHPSWV WR GUDZ D EODFN UHFWDQJOH IURP DFURVV LWV GLDJRQDO DrawingPanel panel = new DrawingPanel(200, 100); Graphics g = panel.getGraphics(); g.drawRect(10, 20, 50, 40); g.drawLine(10, 20, 50, 40); WR ZLWK D OLQH +RZHYHU LW KDV WKH IROORZLQJ LQFRUUHFW DSSHDUDQFH :KDW PXVW EH FKDQJHG IRU LW WR ORRN FRUUHFWO\" :KDW VRUW RI ILJXUH ZLOO EH GUDZQ E\ WKH IROORZLQJ SURJUDP" &DQ \RX GUDZ DQ DSSUR[LPDWH SLFWXUH WKDW ZLOO PDWFK LWV DSSHDUDQFH ZLWKRXW UXQQLQJ LW ILUVW" 1 2 3 4 5 6 7 8 9 10 11 import java.awt.*; public class Draw7 { public static void main(String args) { DrawingPanel panel = new DrawingPanel(200, 200); Graphics g = panel.getGraphics(); for (int i = 0; i < 20; i++) { g.drawOval(i * 10, i * 10, size - (i * 10), size - (i * 10)); } } } ([HUFLVHV :ULWH D SURJUDP WKDW XVHV WKH 'UDZLQJ3DQHO WR GUDZ WKH IROORZLQJ ILJXUH 206 7KH ZLQGRZ LV SL[HOV ZLGH DQG SL[HOV WDOO 7KH EDFNJURXQG LV \HOORZ 7KHUH DUH WZR EOXH RYDOV RI VL]H [ SL[HOV 7KH\ DUH SL[HOV DSDUW DQG WKH OHIW RYDO LV ORFDWHG DW SRVLWLRQ 7KHUH LV D UHG VTXDUH ZKRVH WRS WZR FRUQHUV H[DFWO\ LQWHUVHFW WKH FHQWHUV RI WKH WZR RYDOV /DVWO\ WKHUH LV D EODFN KRUL]RQWDO OLQH WKURXJK WKH FHQWHU RI WKH VTXDUH 0RGLI\ \RXU SURJUDP IURP WKH SUHYLRXV H[HUFLVH VR WKDW WKH ILJXUH LV GUDZQ E\ D PHWKRG FDOOHG drawFigure 7KH PHWKRG VKRXOG DFFHSW WZR DUJXPHQWV 7KH Graphics g RI WKH 'UDZLQJ3DQHO RQ ZKLFK WR GUDZ DQG D Point VSHFLI\LQJ WKH ORFDWLRQ RI WKH WRS OHIW FRUQHU RI WKH ILJXUH 8VH WKH IROORZLQJ KHDGLQJ IRU \RXU PHWKRG public static void drawFigure(Graphics g, Point location) 6HW \RXU 'UDZLQJ3DQHO V VL]H WR [ SL[HOV DQG XVH \RXU drawFigure PHWKRG WR SODFH WZR ILJXUHV RQ LW 2QH ILJXUH VKRXOG EH DW SRVLWLRQ DQG WKH RWKHU VKRXOG EH DW SRVLWLRQ :ULWH D SURJUDP ZLWK D VWDWLF PHWKRG FDOOHG showDesign WKDW XVHV WKH 'UDZLQJ3DQHO WR GUDZ WKH IROORZLQJ ILJXUH 207 7KH ZLQGRZ LV SL[HOV ZLGH DQG SL[HOV WDOO 7KH EDFNJURXQG LV ZKLWH DQG WKH IRUHJURXQG LV EODFN 7KHUH DUH SL[HOV EHWZHHQ HDFK RI WKH UHFWDQJOHV DQG WKH UHFWDQJOHV DUH FRQFHQWULF WKHLU FHQWHUV DUH DW WKH VDPH SRLQW 8VH D ORRS WR GUDZ WKH UHSHDWHG UHFWDQJOHV 0RGLI\ \RXU showDesign PHWKRG IURP WKH SUHYLRXV H[HUFLVH VR WKDW LW DFFHSWV SDUDPHWHUV IRU WKH ZLQGRZ ZLGWK DQG KHLJKW DQG GLVSOD\V WKH UHFWDQJOHV DW WKH DSSURSULDWH VL]HV )RU H[DPSOH LI \RXU showDesign PHWKRG ZDV FDOOHG ZLWK YDOXHV RI DQG WKH ZLQGRZ ZRXOG KDYH WKH IROORZLQJ DSSHDUDQFH :ULWH D SURJUDP WKDW XVHV WKH 'UDZLQJ3DQHO WR GUDZ WKH IROORZLQJ ILJXUH 7KH GUDZLQJ SDQHO LV SL[HOV ZLGH E\ SL[HOV KLJK ,WV EDFNJURXQG LV F\DQ 7KH KRUL]RQWDO DQG YHUWLFDO OLQHV DUH GUDZQ LQ UHG DQG WKH GLDJRQDO OLQH LV GUDZQ LQ EODFN 7KH GLDJRQDO OLQH KDV XSSHU OHIW FRUQHU 6XFFHVVLYH KRUL]RQWDO DQG YHUWLFDO OLQHV DUH VSDFHG SL[HOV DSDUW 208 0RGLI\ \RXU FRGH IURP WKH SUHYLRXV H[HUFLVH WR SURGXFH WKH IROORZLQJ ILJXUHV 7KH GUDZLQJ SDQHO LV QRZ [ SL[HOV LQ VL]H 7KH ILUVW ILJXUH LV DW WKH VDPH SRVLWLRQ RI 7KH RWKHU ILJXUHV DUH DW SRVLWLRQV DQG UHVSHFWLYHO\ 8VH RQH RU PRUH SDUDPHWHUL]HG VWDWLF PHWKRGV WR UHGXFH WKH UHGXQGDQF\ RI \RXU VROXWLRQ :ULWH D SURJUDP WKDW XVHV WKH 'UDZLQJ3DQHO WR GUDZ WKH IROORZLQJ VSLUDO ILJXUH 7KH ZLQGRZ LV SL[HOV ZLGH DQG SL[HOV WDOO 7KH EDFNJURXQG LV ZKLWH DQG WKH IRUHJURXQG LV EODFN 7KH VSLUDO OLQH EHJLQV DW SRLQW DQG VSLUDOV LQZDUG 7KHUH DUH SL[HOV EHWZHHQ HDFK DUP RI WKH VSLUDO VSLUDOV DUH PDGH LQ WRWDO 7KH LQLWLDO VSLUDO WRXFKHV SRLQWV DQG )RU DGGLWLRQDO FKDOOHQJH SDUDPHWHUL]H \RXU SURJUDP ZLWK SDUDPHWHUV VXFK DV WKH ZLQGRZ VL]H DQG WKH QXPEHU RI VSLUDO ORRSV GHVLUHG 3URJUDPPLQJ 3URMHFWV :ULWH D SURJUDP WKDW GUDZV WKH IROORZLQJ ILJXUH RQWR D 'UDZLQJ3DQHO 209 7KH GUDZLQJ SDQHO V VL]H LV [ DQG LWV EDFNJURXQG FRORU LV F\DQ ,W FRQWDLQV IRXU ILJXUHV RI FRQFHQWULF \HOORZ FLUFOHV ZLWK EODFN RXWOLQHV DOO VXUURXQGHG E\ D JUHHQ UHFWDQJOH ZLWK D EODFN RXWOLQH 7KH IRXU ILJXUHV RQ \RXU GUDZLQJ SDQHO VKRXOG KDYH WKH IROORZLQJ SURSHUWLHV 'HVFULSWLRQ WRS OHIW ERWWRP OHIW WRS ULJKW ERWWRP ULJKW [ \ SRVLWLRQ VL]H RI VXEILJXUHV QXPEHU RI FLUFOHV QXPEHU RI URZV FROV [ [ [ [ [ [ [ [ %UHDN GRZQ \RXU SURJUDP LQWR KHOSLQJ PHWKRGV IRU GUDZLQJ RQH VXEILJXUH DV ZHOO DV GUDZLQJ D ODUJHU JULG RI VXEILJXUHV VXFK DV WKH [ JULG DW :ULWH D PRGLILHG YHUVLRQ RI WKH 3URMHFWLOH FDVH VWXG\ SURJUDP IURP &KDSWHU WKDW GUDZV D JUDSK RI WKH SURMHFWLOH V IOLJKW RQWR D 'UDZLQJ3DQHO RI VL]H [ )RU H[DPSOH WKH IROORZLQJ SDQHO GUDZV WKH SURMHFWLOH LI LW KDG DQ LQLWLDO YHORFLW\ RI PHWHUV SHU VHFRQG GHJUHHV DQJOH DQG VWHSV 210 6WXDUW 5HJHV 0DUW\ 6WHSS 211 212 ...
View Full Document

This note was uploaded on 05/18/2008 for the course CSE 142 taught by Professor Martystepp during the Spring '07 term at University of Washington.

Ask a homework question - tutors are online