Chapter 01 - Introduction to Java Programming

Chapter 01 - Introduction to Java Programming -...

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 -DYD 3URJUDPPLQJ &RS\ULJKW E\ 6WXDUW 5HJHV DQG 0DUW\ 6WHSS 3URJUDP (UURUV 6\QWD[ (UURUV /RJLF (UURUV EXJV 3URFHGXUDO 'HFRPSRVLWLRQ 6WDWLF 0HWKRGV )ORZ RI &RQWURO 0HWKRGV 7KDW &DOO 2WKHU 0HWKRGV $Q ([DPSOH 5XQWLPH (UURU &DVH 6WXG\ 'UDZ)LJXUHV 6WUXFWXUHG 9HUVLRQ )LQDO 9HUVLRQ ZLWKRXW 5HGXQGDQF\ $QDO\VLV RI )ORZ RI ([HFXWLRQ %DVLF &RPSXWLQJ &RQFHSWV +DUGZDUH DQG 6RIWZDUH 7KH 'LJLWDO 5HDOP :K\ 3URJUDPPLQJ" :K\ -DYD" 7KH 3URFHVV RI 3URJUDPPLQJ 7KH -DYD 3URJUDPPLQJ (QYLURQPHQW $QG 1RZ -DYD 6WULQJ /LWHUDOV 6WULQJV 6\VWHP RXW SULQWOQ (VFDSH 6HTXHQFHV ,GHQWLILHUV DQG .H\ZRUGV &RPPHQWV DQG 5HDGDELOLW\ $ &RPSOH[ ([DPSOH 'UDZ)LJXUHV ,QWURGXFWLRQ ,Q WKLV FKDSWHU ZH LQWURGXFH VRPH EDVLF WHUPLQRORJ\ DERXW FRPSXWHUV DQG SURJUDPPLQJ :H GLVFXVV WKH -DYD ODQJXDJH DQG LWV SURJUDPPLQJ HQYLURQPHQW :H WDNH D EULHI ORRN DW ZULWLQJ VLPSOH EXW VWUXFWXUHG -DYD SURJUDPV WKDW SURGXFH RXWSXW 11 %DVLF &RPSXWLQJ &RQFHSWV &RPSXWHUV DUH SHUYDVLYH LQ RXU GDLO\ OLYHV JLYLQJ XV DFFHVV WR QHDUO\ OLPLWOHVV LQIRUPDWLRQ 6RPH RI WKLV LQIRUPDWLRQ LV HVVHQWLDO QHZV OLNH WKH KHDGOLQHV DW FQQ FRP 6RPH RI LW LV PRUH IULYRORXV ,I \RX UH FRQFHUQHG DERXW ZKHWKHU WKH JX\ \RX PHW ODVW QLJKW FKHDWV RQ KLV JLUOIULHQGV SHUKDSV \RX YH YLVLWHG GRQWGDWHKLPJLUO FRP &RPSXWHUV OHW XV VKDUH SKRWRV ZLWK RXU IDPLOLHV DQG PDS GLUHFWLRQV WR WKH QHDUHVW SL]]D SODFH IRU GLQQHU /RWV RI UHDO ZRUOG SUREOHPV DUH EHLQJ VROYHG E\ FRPSXWHUV VRPH RI ZKLFK GRQ W PXFK UHVHPEOH WKH RQH RQ \RXU GHVN RU ODS 7KH KXPDQ JHQRPH LV VHTXHQFHG DQG VHDUFKHG IRU '1$ SDWWHUQV XVLQJ SRZHUIXO FRPSXWHUV 7KHUH DUH FRPSXWHUV LQ UHFHQWO\ PDQXIDFWXUHG FDUV PRQLWRULQJ HDFK YHKLFOH V VWDWXV DQG PRWLRQ 'LJLWDO PXVLF SOD\HUV VXFK DV $SSOH V L3RG DUH DFWXDOO\ FRPSXWHUV XQGHUQHDWK WKHLU VPDOO FDVLQJ (YHQ WKH 5RRPED YDFXXP FOHDQLQJ URERW KRXVHV D FRPSXWHU ZLWK FRPSOH[ LQVWUXFWLRQV DERXW KRZ WR GRGJH IXUQLWXUH ZKLOH FOHDQLQJ \RXU IORRUV %XW ZKDW PDNHV D FRPSXWHU D FRPSXWHU" ,V D FDOFXODWRU D FRPSXWHU" ,V D KXPDQ EHLQJ ZLWK D SDSHU DQG SHQFLO D FRPSXWHU" 7KH QH[W VHYHUDO VHFWLRQV DWWHPSW WR DGGUHVV WKLV TXHVWLRQ ZKLOH OHDGLQJ XV WRZDUG SXWWLQJ FRPSXWHUV LQ RXU FRPPDQG WKURXJK SURJUDPPLQJ +DUGZDUH DQG 6RIWZDUH $ FRPSXWHU LV D PDFKLQH WKDW PDQLSXODWHV GDWD DQG H[HFXWHV OLVWV RI LQVWUXFWLRQV NQRZQ DV SURJUDPV 3URJUDP $ OLVW RI LQVWUXFWLRQV WR EH FDUULHG RXW E\ D FRPSXWHU 2QH NH\ IHDWXUH WKDW GLIIHUHQWLDWHV D FRPSXWHU IURP D VLPSOHU PDFKLQH OLNH D FDOFXODWRU LV LWV YHUVDWLOLW\ 7KH VDPH FRPSXWHU FDQ SHUIRUP PDQ\ GLIIHUHQW WDVNV SOD\LQJ JDPHV FRPSXWLQJ LQFRPH WD[HV FRQQHFWLQJ WR RWKHU FRPSXWHUV DURXQG WKH ZRUOG GHSHQGLQJ RQ ZKDW SURJUDP LW LV UXQQLQJ DW D JLYHQ PRPHQW $ FRPSXWHU FDQ UXQ QRW RQO\ WKH SURJUDPV WKDW H[LVW RQ LW FXUUHQWO\ EXW DOVR QHZ SURJUDPV WKDW KDYHQ W HYHQ EHHQ ZULWWHQ \HW 7KH SK\VLFDO FRPSRQHQWV WKDW PDNH XS D FRPSXWHU DUH FROOHFWLYHO\ FDOOHG KDUGZDUH 2QH RI WKH PRVW LPSRUWDQW SLHFHV RI KDUGZDUH LV WKH FHQWUDO SURFHVVLQJ XQLW RU &38 ZKLFK LV WKH EUDLQ RI WKH FRPSXWHU WKDW H[HFXWHV LQVWUXFWLRQV $OVR LPSRUWDQW LV WKH FRPSXWHU V PHPRU\ RIWHQ FDOOHG UDQGRP DFFHVV PHPRU\ RU 5$0 EHFDXVH WKH FRPSXWHU FDQ DFFHVV DQ\ SDUW RI WKDW PHPRU\ DW DQ\ WLPH 7KH FRPSXWHU XVHV LWV PHPRU\ WR VWRUH SURJUDPV WKDW DUH EHLQJ H[HFXWHG DORQJ ZLWK WKHLU GDWD 5$0 LV OLPLWHG LQ VL]H DQG GRHV QRW UHWDLQ LWV FRQWHQWV ZKHQ WKH FRPSXWHU LV WXUQHG RII 7KHUHIRUH FRPSXWHUV JHQHUDOO\ DOVR XVH D KDUG GLVN DV D ODUJHU SHUPDQHQW VWRUDJH DUHD 12 &RPSXWHU SURJUDPV DUH FROOHFWLYHO\ FDOOHG VRIWZDUH 7KH SULPDU\ SLHFH RI VRIWZDUH UXQQLQJ RQ D FRPSXWHU LV LWV RSHUDWLQJ V\VWHP $Q RSHUDWLQJ V\VWHP SURYLGHV DQ HQYLURQPHQW ZKHUH PDQ\ DSSOLFDWLRQ SURJUDPV PD\ EH UXQ DW WKH VDPH WLPH DV ZHOO DV SURYLGLQJ D EULGJH EHWZHHQ WKRVH SURJUDPV DQG WKH KDUGZDUH DQG XVHU 7KH SURJUDPV WKDW UXQ LQVLGH WKH RSHUDWLQJ V\VWHP DUH RIWHQ FDOOHG DSSOLFDWLRQV :KHQ WKH XVHU VHOHFWV D SURJUDP WR EH UXQ E\ WKH RSHUDWLQJ V\VWHP VXFK DV E\ GRXEOH FOLFNLQJ WKH LFRQ RI WKDW SURJUDP RQ WKH GHVNWRS VHYHUDO WKLQJV KDSSHQ 7KH LQVWUXFWLRQV IRU WKDW SURJUDP DUH ORDGHG LQWR WKH FRPSXWHU V PHPRU\ IURP WKH KDUG GLVN 7KH RSHUDWLQJ V\VWHP DOORFDWHV PHPRU\ IRU WKDW SURJUDP WR XVH 7KH LQVWUXFWLRQV RI WKH SURJUDP DUH IHG IURP WKH PHPRU\ WR WKH &38 DQG H[HFXWHG VHTXHQWLDOO\ 7KH 'LJLWDO 5HDOP ,Q WKH ODVW VHFWLRQ ZH VDZ WKDW D FRPSXWHU LV D JHQHUDO SXUSRVH GHYLFH WKDW FDQ EH SURJUDPPHG <RX ZLOO RIWHQ KHDU SHRSOH UHIHU WR PRGHUQ FRPSXWHUV DV GLJLWDO FRPSXWHUV EHFDXVH RI WKH ZD\ WKDW WKH\ RSHUDWH 'LJLWDO %DVHG RQ QXPEHUV WKDW LQFUHDVH LQ GLVFUHWH LQFUHPHQWV VXFK DV WKH LQWHJHUV HWF %HFDXVH FRPSXWHUV DUH GLJLWDO HYHU\WKLQJ WKDW LV VWRUHG RQ D FRPSXWHU LV VWRUHG DV D VHTXHQFH RI LQWHJHUV 7KLV LQFOXGHV HYHU\ SURJUDP DQG HYHU\ SLHFH RI GDWD 7KH LGHD RI UHSUHVHQWLQJ HYHU\WKLQJ DV DQ LQWHJHU ZDV IDLUO\ XQXVXDO LQ WKH V ZKHQ WKH ILUVW FRPSXWHUV ZHUH EXLOW 7KLV LGHD VHHPV OHVV XQXVXDO WRGD\ QRZ WKDW ZH KDYH GLJLWDO PXVLF GLJLWDO SLFWXUHV DQG GLJLWDO PRYLHV $Q PS ILOH IRU H[DPSOH LV VLPSO\ D ORQJ VHTXHQFH RI LQWHJHUV WKDW VWRUHV DXGLR LQIRUPDWLRQ 1RW RQO\ DUH FRPSXWHUV GLJLWDO VWRULQJ DOO LQIRUPDWLRQ DV LQWHJHUV WKH\ DUH DOVR ELQDU\ ZKLFK PHDQV WKDW WKRVH LQWHJHUV DUH VWRUHG DV ELQDU\ QXPEHUV %LQDU\ 1XPEHU $ QXPEHU FRPSRVHG RI MXVW V DQG V DOVR NQRZQ DV D EDVH QXPEHU +XPDQV JHQHUDOO\ ZRUN ZLWK GHFLPDO RU EDVH QXPEHUV ,W PLJKW VHHP RGG WKDW FRPSXWHUV XVH ELQDU\ QXPEHUV ZKHQ SHRSOH DUH VR XVHG WR EDVH QXPEHUV EXW ZH XVH EDVH EHFDXVH LW PDWFKHV RXU SK\VLRORJ\ ILQJHUV DQG WRHV ,Q WKH FDVH RI FRPSXWHUV ZH ZDQW D V\VWHP WKDW ZLOO EH HDV\ WR FUHDWH DQG WKDW ZLOO EH YHU\ UHOLDEOH ,W WXUQV RXW WR EH VLPSOHU WR EXLOG D V\VWHP RQ WRS RI ELQDU\ SKHQRPHQD H J D FLUFXLW EHLQJ RSHQ RU FORVHG WKDQ WR EXLOG LW RQ D V\VWHP ZLWK WHQ GLIIHUHQW VWDWHV WR GLVWLQJXLVK H J GLIIHUHQW YROWDJH OHYHOV )URP D PDWKHPDWLFDO SRLQW RI YLHZ \RX FDQ VWRUH WKLQJV MXVW DV HDVLO\ XVLQJ ELQDU\ QXPEHUV DV \RX FDQ XVLQJ EDVH QXPEHUV 6LQFH LW LV HDVLHU WR FRQVWUXFW WKH SK\VLFDO GHYLFH XVLQJ ELQDU\ QXPEHUV WKDW V ZKDW FRPSXWHUV XVH 13 ,W GRHV PHDQ KRZHYHU WKDW SHRSOH ZKR DUHQ W XVHG WR FRPSXWHUV RIWHQ HQFRXQWHU XQIDPLOLDU FRQYHQWLRQV $V D UHVXOW LW LV ZRUWK VSHQGLQJ D OLWWOH WLPH UHYLHZLQJ KRZ ELQDU\ QXPEHUV ZRUN ,Q ELQDU\ \RX VWDUW ZLWK DQG \RX FDQ FRXQW XS MXVW OLNH \RX GR LQ EDVH EXW \RX UXQ RXW RI GLJLWV PXFK IDVWHU 6R FRXQWLQJ LQ ELQDU\ \RX VD\ 0 1 $QG DOUHDG\ \RX YH UXQ RXW RI GLJLWV 7KLV LV OLNH UHDFKLQJ ZKHQ \RX FRXQW LQ EDVH UXQ RXW RI GLJLWV \RX FDUU\ RYHU WR WKH QH[W GLJLW 6R WKH QH[W WZR QXPEHUV DUH 10 11 $IWHU \RX $QG DJDLQ ZH KDYH UXQ RXW RI GLJLWV 7KLV LV OLNH UHDFKLQJ LQ EDVH 6R DJDLQ ZH FDUU\ LQWR WKH QH[W GLJLW WR IRUP WKH GLJLW QXPEHU ,Q ELQDU\ ZKHQHYHU \RX VHH D VHULHV RI RQHV DV LQ \RX NQRZ \RX UH MXVW RQH DZD\ IURP WKH GLJLWV DOO IOLSSLQJ WR ZLWK D LQ IURQW LQ WKH VDPH ZD\ WKDW LQ EDVH ZKHQ \RX VHH D QXPEHU OLNH \RX NQRZ WKDW \RX DUH RQH DZD\ IURP WKRVH GLJLWV WXUQLQJ WR ZLWK D LQ IURQW 7KH IROORZLQJ WDEOH VKRZV KRZ ZH ZRXOG FRXQW XS WR WKH EDVH QXPEHU XVLQJ ELQDU\ decimal binary ----------------0 0 1 1 2 10 3 11 4 100 5 101 6 110 7 111 8 1000 decimal binary ----------------9 10 11 12 13 14 15 16 1001 1010 1011 1100 1101 1110 1111 10000 7KHUH DUH VHYHUDO XVHIXO REVHUYDWLRQV WR PDNH DERXW ELQDU\ QXPEHUV 1RWLFH LQ WKH WDEOH DERYH WKDW WKH ELQDU\ QXPEHUV DUH DOO SHUIHFW SRZHUV RI ,Q WKH VDPH ZD\ WKDW LQ EDVH ZH WDON DERXW D RQH V GLJLW WHQ V GLJLW KXQGUHG V GLJLW DQG VR RQ ZH FDQ WKLQN LQ ELQDU\ RI D RQH V GLJLW WZR V GLJLW IRXU V GLJLW HLJKW V GLJLW VL[WHHQ V GLJLW DQG VR RQ &RPSXWHU VFLHQWLVWV TXLFNO\ IRXQG WKHPVHOYHV QHHGLQJ WR UHIHU WR WKH VL]HV RI GLIIHUHQW ELQDU\ TXDQWLWLHV VR ZH LQYHQWHG WKH WHUP ELW WR UHIHU WR D VLQJOH ELQDU\ GLJLW DQG WKH WHUP E\WH WR UHIHU WR ELWV 7R WDON DERXW ODUJH DPRXQWV RI PHPRU\ ZH WDON DERXW NLORE\WHV .% PHJDE\WHV 0% JLJDE\WHV *% DQG VR RQ 0DQ\ SHRSOH WKLQN WKDW WKHVH FRUUHVSRQG WR WKH PHWULF V\VWHP ZKHUH NLOR PHDQV HTXDO WR EXW WKDW LV RQO\ DSSUR[LPDWHO\ WUXH :H XVH WKH IDFW WKDW LW DFWXDOO\ HTXDOV E\WHV D JLJDE\WH LV 6R D NLORE\WH LV E\WHV E\WHV E\WHV DQG VR RQ LV DSSURPL[DWHO\ E\WHV E\WHV D PHJDE\WH LV 14 :K\ 3URJUDPPLQJ" $W PRVW XQLYHUVLWLHV WKH ILUVW FRXUVH LQ FRPSXWHU VFLHQFH LV SUHGRPLQDQWO\ D SURJUDPPLQJ FRXUVH 0DQ\ FRPSXWHU VFLHQWLVWV DUH ERWKHUHG E\ WKLV EHFDXVH LW OHDYHV SHRSOH ZLWK WKH LPSUHVVLRQ WKDW FRPSXWHU VFLHQFH SURJUDPPLQJ :KLOH LW LV WUXH WKDW PDQ\ WUDLQHG FRPSXWHU VFLHQWLVWV VSHQG WLPH SURJUDPPLQJ WKHUH LV D ORW PRUH WR WKH GLVFLSOLQH WKDQ MXVW SURJUDPPLQJ 6R ZK\ GR ZH VWXG\ SURJUDPPLQJ ILUVW" $ 6WDQIRUG FRPSXWHU VFLHQWLVW QDPHG 'RQ .QXWK DQVZHUV WKLV TXHVWLRQ E\ VD\LQJ WKDW WKH FRPPRQ WKUHDG WR PRVW RI FRPSXWHU VFLHQFH LV WKDW ZH DOO LQ VRPH ZD\ ZRUN ZLWK DOJRULWKPV $OJRULWKP $ VWHS E\ VWHS GHVFULSWLRQ RI KRZ WR DFFRPSOLVK D WDVN .QXWK LV DQ H[SHUW LQ DOJRULWKPV VR KH ZRXOG QDWXUDOO\ EH ELDVHG WR WKLQN RI WKHP DV WKH FHQWHU RI FRPSXWHU VFLHQFH +H FODLPV WKDW ZKDW LV PRVW LPSRUWDQW LV QRW WKH DOJRULWKPV WKHPVHOYHV EXW UDWKHU WKH WKRXJKW SURFHVV WKDW FRPSXWHU VFLHQWLVWV HPSOR\ .QXWK KDV VDLG ,W KDV RIWHQ EHHQ VDLG WKDW D SHUVRQ GRHV QRW UHDOO\ XQGHUVWDQG VRPHWKLQJ XQWLO DIWHU WHDFKLQJ LW WR VRPHRQH HOVH $FWXDOO\ D SHUVRQ GRHV QRW UHDOO\ XQGHUVWDQG VRPHWKLQJ XQWLO DIWHU WHDFKLQJ LW WR D FRPSXWHU L H H[SUHVVLQJ LW DV DQ DOJRULWKP .QXWK LV GHVFULELQJ D WKRXJKW SURFHVV WKDW LV FRPPRQ WR PRVW RI FRPSXWHU VFLHQFH ZKLFK KH UHIHUV WR DV DOJRULWKPLF WKLQNLQJ 6R ZH VWXG\ SURJUDPPLQJ QRW EHFDXVH LW LV WKH PRVW LPSRUWDQW DVSHFW RI FRPSXWHU VFLHQFH EXW EHFDXVH LW LV WKH EHVW ZD\ WR H[SODLQ WKH DSSURDFK FRPSXWHU VFLHQWLVWV WDNH WR VROYLQJ SUREOHPV $OJRULWKPV DUH H[SUHVVHG DV FRPSXWHU SURJUDPV 7KH 3URFHVV RI 3URJUDPPLQJ 7KH ZRUG FRGH GHVFULEHV SURJUDP IUDJPHQWV WKHVH IRXU OLQHV RI FRGH RU WKH DFW RI SURJUDPPLQJ /HW V FRGH WKLV LQWR -DYD 2QFH D SURJUDP KDV EHHQ ZULWWHQ \RX FDQ H[HFXWH LW 3URJUDP ([HFXWLRQ 7KH DFW RI FDUU\LQJ RXW WKH LQVWUXFWLRQV FRQWDLQHG LQ D SURJUDP 7KH SURFHVV RI H[HFXWLRQ LV RIWHQ FDOOHG UXQQLQJ ,W FDQ EH XVHG DV D YHUE :KHQ P\ SURJUDP UXQV LW GRHV VRPHWKLQJ VWUDQJH RU DV D QRXQ 7KH ODVW UXQ RI P\ SURJUDP SURGXFHG WKHVH UHVXOWV &RPSXWHU SURJUDPV DUH VWRUHG LQWHUQDOO\ DV D VHULHV RI ELQDU\ QXPEHUV NQRZQ DV WKH PDFKLQH ODQJXDJH RI WKH FRPSXWHU ,Q WKH HDUO\ GD\V SURJUDPPHUV HQWHUHG QXPEHUV OLNH WKHVH GLUHFWO\ LQWR WKH FRPSXWHU 2EYLRXVO\ WKLV LV D WHGLRXV DQG FRQIXVLQJ ZD\ WR SURJUDP D FRPSXWHU DQG ZH KDYH LQYHQWHG DOO VRUWV RI PHFKDQLVPV WR VLPSOLI\ WKLV SURFHVV 15 0RGHUQ SURJUDPPHUV ZULWH LQ ZKDW DUH NQRZQ DV KLJK OHYHO SURJUDPPLQJ ODQJXDJHV OLNH -DYD 6XFK SURJUDPV FDQQRW EH UXQ GLUHFWO\ RQ D FRPSXWHU 7KH\ ILUVW KDYH WR EH WUDQVODWHG LQWR D GLIIHUHQW IRUP E\ D VSHFLDO SURJUDP NQRZQ DV D FRPSLOHU &RPSLOHU $ SURJUDP WKDW WUDQVODWHV D FRPSXWHU SURJUDP ZULWWHQ LQ RQH ODQJXDJH LQWR DQ HTXLYDOHQW SURJUDP LQ DQRWKHU ODQJXDJH RIWHQ EXW QRW DOZD\V WUDQVODWLQJ LQWR PDFKLQH ODQJXDJH $ FRPSLOHU WKDW WUDQVODWHV GLUHFWO\ LQWR PDFKLQH ODQJXDJH FUHDWHV D SURJUDP WKDW FDQ EH H[HFXWHG GLUHFWO\ RQ WKH FRPSXWHU :H UHIHU WR VXFK D SURJUDP DV DQ H[HFXWDEOH DQG ZH UHIHU WR VXFK FRPSLOHUV DV QDWLYH FRPSLOHUV EHFDXVH WKH\ FRPSLOH FRGH WR WKH ORZHVW SRVVLEOH OHYHO WKH QDWLYH PDFKLQH ODQJXDJH RI WKH FRPSXWHU 7KLV DSSURDFK ZRUNV ZHOO ZKHQ \RX NQRZ H[DFWO\ ZKDW FRPSXWHU \RX ZDQW \RXU SURJUDP WR H[HFXWH RQ %XW ZKDW LI \RX ZDQW WR H[HFXWH D SURJUDP RQ PDQ\ GLIIHUHQW FRPSXWHUV" 8VLQJ WKLV DSSURDFK \RX G QHHG D FRPSLOHU WKDW JHQHUDWHV GLIIHUHQW PDFKLQH ODQJXDJH RXWSXW IRU HDFK GLIIHUHQW FRPSXWHU 7KH GHVLJQHUV RI -DYD GHFLGHG WR XVH D GLIIHUHQW DSSURDFK 7KH\ FDUHG D ORW DERXW EHLQJ DEOH WR UXQ RQ PDQ\ GLIIHUHQW FRPSXWHUV EHFDXVH WKH\ ZDQWHG WR KDYH D ODQJXDJH WKDW ZRUNHG ZHOO IRU WKH ZHE 3HRSOH ZKR ZULWH DSSOHWV -DYD SURJUDPV WKDW OLYH LQVLGH ZHE SDJHV ZDQW WKRVH SURJUDPV WR UXQ RQ PDQ\ GLIIHUHQW FRPSXWHUV ,QVWHDG RI FRPSLOLQJ LQWR PDFKLQH ODQJXDJH -DYD SURJUDPV DUH FRPSLOHG LQWR ZKDW DUH NQRZQ DV -DYD E\WHFRGHV 7KHVH E\WHFRGHV UHSUHVHQW DQ LQWHUPHGLDWH OHYHO 7KH\ DUHQ W TXLWH DV KLJK OHYHO DV -DYD EXW WKH\ DOVR DUHQ W TXLWH DV ORZ OHYHO DV PDFKLQH ODQJXDJH 7KH NH\ WKLQJ LV WKDW RQH VHW RI E\WHFRGHV FDQ H[HFXWH RQ PDQ\ GLIIHUHQW PDFKLQHV -DYD E\WHFRGHV DUH VLPLODU WR PDFKLQH ODQJXDJH ,Q IDFW WKH\ DUH WKH PDFKLQH ODQJXDJH RI D WKHRUHWLFDO FRPSXWHU NQRZQ DV WKH -DYD 9LUWXDO 0DFKLQH RU -90 -DYD 9LUWXDO 0DFKLQH -90 $ WKHRUHWLFDO FRPSXWHU ZKRVH PDFKLQH ODQJXDJH LV WKH VHW RI -DYD E\WHFRGHV 7KLV LVQ W DQ DFWXDO PDFKLQH EXW LW V VLPLODU WR DFWXDO PDFKLQHV %\ FRPSLOLQJ GRZQ WR WKLV OHYHO WKHUH LVQ W DV PXFK ZRUN OHIW WR WXUQ WKH -DYD E\WHFRGHV LQWR DFWXDO PDFKLQH LQVWUXFWLRQV ,Q WKH -DYD SURJUDPPLQJ ODQJXDJH QRWKLQJ FDQ H[LVW RXWVLGH RI ZKDW LV FDOOHG D FODVV &ODVV $ XQLW RI FRGH WKDW LV WKH EDVLF EXLOGLQJ EORFN RI -DYD SURJUDPV 7KH QRWLRQ RI D FODVV LV PXFK ULFKHU WKDQ WKLV DV ZH OO VHH ZKHQ ZH JHW WR &KDSWHU ZH QHHG WR NQRZ LV WKDW HDFK RI RXU -DYD SURJUDPV ZLOO EH VWRUHG LQ D FODVV EXW IRU QRZ DOO 7R DFWXDOO\ H[HFXWH D -DYD FODVV ILOH \RX QHHG DQRWKHU SURJUDP WKDW ZLOO H[HFXWH WKH -DYD E\WHFRGHV 6XFK SURJUDPV DUH NQRZQ JHQHULFDOO\ DV -DYD UXQWLPHV DQG WKH VWDQGDUG HQYLURQPHQW GLVWULEXWHG E\ 6XQ LV NQRZQ DV WKH -DYD 5XQWLPH (QYLURQPHQW 16 -DYD 5XQWLPH (QYLURQPHQW -5( $ SURJUDP WKDW H[HFXWHV FRPSLOHG -DYD FODVV ILOHV 0RVW SHRSOH KDYH D -DYD UXQWLPH RQ WKHLU FRPSXWHU HYHQ LI WKH\ GRQ W NQRZ DERXW LW )RU H[DPSOH $SSOH V 26 ; LQFOXGHV D -DYD UXQWLPH DQG WKH VWDQGDUG :LQGRZV LQVWDOOHU IURP 0LFURVRIW LQVWDOOV D -DYD UXQWLPH :K\ -DYD" :KHQ 6XQ 0LFURV\VWHPV UHOHDVHG -DYD LQ WKH\ SXEOLVKHG D GRFXPHQW FDOOHG D ZKLWH SDSHU GHVFULELQJ WKHLU QHZ SURJUDPPLQJ ODQJXDJH 3HUKDSV WKH NH\ VHQWHQFH IURP WKDW SDSHU LV WKH IROORZLQJ -DYD $ VLPSOH REMHFW RULHQWHG QHWZRUN VDYY\ LQWHUSUHWHG UREXVW VHFXUH DUFKLWHFWXUH QHXWUDO SRUWDEOH KLJK SHUIRUPDQFH PXOWLWKUHDGHG G\QDPLF ODQJXDJH 7KLV VHQWHQFH FRYHUV PDQ\ RI WKH UHDVRQV ZH OO EH XVLQJ -DYD DV RXU SURJUDPPLQJ ODQJXDJH LQ WKLV WH[WERRN -DYD LV UHDVRQDEO\ VLPSOH IRU EHJLQQHUV WR OHDUQ -DYD HPEUDFHV REMHFW RULHQWHG SURJUDPPLQJ D VW\OH RI ZULWLQJ SURJUDPV WKDW KDV EHHQ VKRZQ WR EH YHU\ VXFFHVVIXO IRU FUHDWLQJ ODUJH DQG FRPSOH[ VRIWZDUH -DYD DOVR LQFOXGHV D ODUJH DPRXQW RI SUH ZULWWHQ VRIWZDUH WKDW SURJUDPPHUV FDQ XWLOL]H WR HQKDQFH WKHLU SURJUDPV 6XFK RII WKH VKHOI VRIWZDUH FRPSRQHQWV DUH RIWHQ FDOOHG OLEUDULHV )RU H[DPSOH LI \RX ZLVK WR ZULWH D SURJUDP WKDW FRQQHFWV WR D VLWH RQ WKH LQWHUQHW -DYD FRQWDLQV D OLEUDU\ WR VLPSOLI\ WKH FRQQHFWLRQ IRU \RX -DYD FRQWDLQV OLEUDULHV WR GUDZ JUDSKLFDO XVHU LQWHUIDFHV UHWULHYH GDWD IURP GDWDEDVHV DQG SHUIRUP FRPSOH[ PDWKHPDWLFDO FRPSXWDWLRQV DPRQJ PDQ\ RWKHU WKLQJV 7KHVH OLEUDULHV FROOHFWLYHO\ DUH FDOOHG WKH -DYD FODVV OLEUDULHV -DYD &ODVV /LEUDULHV -DYD V FROOHFWLRQ RI SUH H[LVWLQJ FRGH WKDW SURYLGHV VROXWLRQV WR FRPPRQ SURJUDPPLQJ SUREOHPV 7KH ULFKQHVV RI -DYD V FODVV OLEUDULHV DUH DQ H[WUHPHO\ LPSRUWDQW IDFWRU LQ WKH ULVH RI -DYD DV D SRSXODU ODQJXDJH $V RI YHUVLRQ WKH FODVV OLEUDULHV LQFOXGH RYHU HQWULHV $QRWKHU UHDVRQ WR XVH -DYD LV WKDW LW KDV D YLEUDQW SURJUDPPHU FRPPXQLW\ 7KHUH LV D ODUJH DPRXQW RI RQOLQH GRFXPHQWDWLRQ DQG WXWRULDOV DYDLODEOH WR KHOS SURJUDPPHUV OHDUQ QHZ VNLOOV 0DQ\ RI WKHVH GRFXPHQWV DUH ZULWWHQ E\ 6XQ WKHPVHOYHV VXFK DV DQ H[WHQVLYH UHIHUHQFH WR -DYD V FODVV OLEUDULHV FDOOHG WKH $3, 6SHFLILFDWLRQ ZKHUH $3, VWDQGV IRU $SSOLFDWLRQ 3URJUDPPLQJ ,QWHUIDFH -DYD LV H[WUHPHO\ SODWIRUP LQGHSHQGHQW XQOLNH SURJUDPV ZULWWHQ LQ PDQ\ RWKHU ODQJXDJHV WKH VDPH -DYD SURJUDP FDQ EH H[HFXWHG RQ PDQ\ GLIIHUHQW RSHUDWLQJ V\VWHPV VXFK DV :LQGRZV /LQX[ DQG 0DFLQWRVK 17 -DYD LV XVHG H[WHQVLYHO\ E\ SURJUDPPHUV IRU ERWK UHVHDUFK DQG EXVLQHVV DSSOLFDWLRQV 7KLV PHDQV WKDW D ODUJH QXPEHU RI SURJUDPPLQJ MREV H[LVW LQ WKH PDUNHWSODFH WRGD\ IRU VNLOOHG -DYD SURJUDPPHUV $ VDPSOH *RRJOH VHDUFK IRU WKH SKUDVH -DYD MREV UHWXUQV KLWV 7KH -DYD 3URJUDPPLQJ (QYLURQPHQW <RX PXVW EHFRPH IDPLOLDU ZLWK \RXU FRPSXWHU VHWXS EHIRUH \RX VWDUW SURJUDPPLQJ (DFK FRPSXWHU SURYLGHV D GLIIHUHQW HQYLURQPHQW IRU SURJUDP GHYHORSPHQW EXW WKHUH DUH VRPH FRPPRQ HOHPHQWV WKDW GHVHUYH FRPPHQW 1R PDWWHU ZKDW HQYLURQPHQW \RX XVH \RX ZLOO IROORZ WKH VDPH EDVLF WKUHH VWHSV W\SH LQ D SURJUDP DV D -DYD FODVV FRPSLOH WKH SURJUDP ILOH UXQ WKH FRPSLOHG YHUVLRQ RI WKH SURJUDP 7KH EDVLF XQLW RI VWRUDJH RQ PRVW FRPSXWHUV LV D ILOH (YHU\ ILOH KDV D QDPH $ ILOH QDPH HQGV ZLWK DQ H[WHQVLRQ ZKLFK LV WKH SDUW RI D ILOH V QDPH WKDW IROORZV WKH SHULRG $ ILOH V H[WHQVLRQ LQGLFDWHV WKH W\SH RI GDWD FRQWDLQHG LQ WKH ILOH )RU H[DPSOH ILOHV ZLWK H[WHQVLRQ .doc DUH 0LFURVRIW :RUG GRFXPHQWV DQG ILOHV ZLWK H[WHQVLRQ .mp3 DUH 03 DXGLR ILOHV <RX ZLOO FUHDWH ILOHV ZKRVH FRQWHQWV DUH -DYD SURJUDPV -DYD SURJUDP ILOHV PXVW XVH WKH H[WHQVLRQ .java :KHQ \RX FRPSLOH D -DYD SURJUDP WKH UHVXOWLQJ -DYD E\WHFRGHV DUH VWRUHG LQ D ILOH ZLWK WKH VDPH QDPH DQG WKH H[WHQVLRQ .class 0RVW -DYD SURJUDPPHUV XVH ZKDW DUH NQRZQ DV ,QWHJUDWHG 'HYHORSPHQW (QYLURQPHQWV RU ,'(V WKDW SURYLGH DQ DOO LQ RQH HQYLURQPHQW IRU FUHDWLQJ HGLWLQJ FRPSLOLQJ DQG H[HFXWLQJ SURJUDP ILOHV 6RPH RI WKH PRUH SRSXODU FKRLFHV IRU LQWURGXFWRU\ FRPSXWHU VFLHQFH FODVVHV DUH (FOLSVH 'U-DYD %OXH- DQG 7H[W3DG <RXU LQVWUXFWRU ZLOO WHOO \RX ZKDW HQYLURQPHQW \RX VKRXOG XVH )RU H[DPSOH \RX PLJKW W\SH LQ WKH IROORZLQJ SURJUDP ILOH 1 2 3 4 5 public class Hello { public static void main(String args) { System.out.println("Hello, world!"); } } 'RQ W ZRUU\ DERXW WKH GHWDLOV RI WKLV SURJUDP ULJKW QRZ :H ZLOO H[SORUH WKDW LQ WKH QH[W VHFWLRQ 2QFH \RX KDYH W\SHG LQ D SURJUDP ILOH \RX PRYH WR VWHS DQG FRPSLOH LW 7KH FRPPDQG WR FRPSLOH ZLOO EH GLIIHUHQW LQ HDFK GLIIHUHQW GHYHORSPHQW HQYLURQPHQW EXW WKH SURFHVV LV WKH VDPH <RX KDYH WR VXEPLW \RXU FODVV ILOH WR WKH FRPSLOHU IRU WUDQVODWLRQ W\SLFDO FRPPDQGV DUH FRPSLOH RU EXLOG 7KHUH PLJKW EH HUURUV LQ ZKLFK FDVH \RX G KDYH WR JR EDFN WR WKH HGLWRU DQG IL[ WKH HUURUV DQG WU\ WR FRPSLOH DJDLQ :H OO GLVFXVV HUURUV LQ PRUH GHWDLO ODWHU LQ WKLV FKDSWHU 2QFH \RX KDYH VXFFHVVIXOO\ FRPSLOHG \RXU SURJUDP \RX DUH UHDG\ WR PRYH WR VWHS E\ UXQQLQJ WKH SURJUDP $JDLQ WKH FRPPDQG WR GR WKLV ZLOO GLIIHU IURP RQH HQYLURQPHQW WR WKH QH[W EXW WKH SURFHVV LV VLPLODU WKH W\SLFDO FRPPDQG LV UXQ 18 7KH IROORZLQJ GLDJUDP VXPPDUL]HV WKH VWHSV ZH ZRXOG IROORZ LQ FUHDWLQJ D SURJUDP QDPHG +HOOR MDYD 7KH +HOOR MDYD SURJUDP LQYROYHV DQ RQVFUHHQ ZLQGRZ NQRZQ DV WKH FRQVROH &RQVROH :LQGRZ $ VSHFLDO WH[W RQO\ ZLQGRZ LQ ZKLFK -DYD SURJUDPV LQWHUDFW ZLWK WKH XVHU 7KH FRQVROH ZLQGRZ LV D FODVVLF ZD\ WR LQWHUDFW ZLWK FRPSXWHUV ZKHUH WKH FRPSXWHU GLVSOD\V WH[W RQ WKH VFUHHQ DQG VRPHWLPHV ZDLWV IRU WKH XVHU WR W\SH UHVSRQVHV 7KLV LV NQRZQ DV FRQVROH RU WHUPLQDO LQWHUDFWLRQ 7KH WH[W W\SHG E\ WKH FRPSXWHU LQ WKH FRQVROH ZLQGRZ LV NQRZQ DV WKH RXWSXW RI WKH SURJUDP $Q\WKLQJ W\SHG E\ WKH XVHU LV NQRZQ DV WKH FRQVROH LQSXW 7R NHHS WKLQJV VLPSOH PRVW RI WKH VDPSOH SURJUDPV LQ WKLV ERRN LQYROYH FRQVROH LQWHUDFWLRQ .HHSLQJ WKH LQWHUDFWLRQ VLPSOH ZLOO DOORZ XV WR IRFXV RXU DWWHQWLRQ DQG HIIRUW RQ RWKHU DVSHFWV RI SURJUDPPLQJ )RU WKRVH ZKR DUH LQWHUHVWHG &KDSWHU GHVFULEHV KRZ WR ZULWH SURJUDPV WKDW XVH D PRUH PRGHUQ NLQG RI LQWHUIDFH NQRZQ DV D *UDSKLFDO 8VHU ,QWHUIDFH RU *8, 19 $QG 1RZ -DYD ,W V WLPH WR ORRN DW D FRPSOHWH -DYD SURJUDP ,W LV D WUDGLWLRQ LQ FRPSXWHU VFLHQFH WKDW ZKHQ \RX GHVFULEH D QHZ SURJUDPPLQJ ODQJXDJH \RX VKRXOG VWDUW ZLWK D SURJUDP WKDW SURGXFHV D VLQJOH OLQH RI RXWSXW ZLWK WKH ZRUGV +HOOR ZRUOG 7KH KHOOR ZRUOG WUDGLWLRQ KDV EHHQ EURNHQ E\ PDQ\ DXWKRUV RI -DYD ERRNV EHFDXVH WKH SURJUDP WXUQV RXW QRW WR EH DV VKRUW DQG VLPSOH ZKHQ ZULWWHQ LQ -DYD +HUH LV RXU KHOOR ZRUOG SURJUDP 1 2 3 4 5 public class Hello { public static void main(String args) { System.out.println("Hello, world!"); } } 7KLV GHILQHV D FODVV FDOOHG +HOOR 6XQ KDV HVWDEOLVKHG WKH FRQYHQWLRQ WKDW FODVV QDPHV DOZD\V EHJLQ ZLWK D FDSLWDO OHWWHU ZKLFK PDNHV LW HDV\ WR UHFRJQL]H WKDW VRPHWKLQJ LV WKH QDPH RI D FODVV -DYD UHTXLUHV WKDW WKH FODVV QDPH DQG WKH ILOH QDPH PDWFK VR WKLV ZRXOG KDYH WR EH VWRUHG LQ D ILOH FDOOHG +HOOR MDYD )RUWXQDWHO\ \RX GRQ W KDYH WR XQGHUVWDQG DOO RI WKH GHWDLOV RI WKLV SURJUDP MXVW \HW %XW \RX GR QHHG WR XQGHUVWDQG WKH EDVLF VWUXFWXUH 5HPHPEHU WKDW WKH EDVLF XQLW RI FRGH LQ -DYD LV WKH FODVV (YHU\ SURJUDP PXVW EH FRQWDLQHG ZLWKLQ D -DYD FODVV 7KH EDVLF IRUP RI D -DYD FODVV LV DV IROORZV public class <name> { <method> <method> ... <method> } 7KH GHVFULSWLRQ DERYH LV NQRZQ DV D V\QWD[ WHPSODWH EHFDXVH LW GHVFULEHV WKH EDVLF IRUP RI D -DYD FRQVWUXFW -DYD KDV UXOHV WKDW GHWHUPLQH LWV OHJDO V\QWD[ RU JUDPPDU (DFK WLPH ZH VHH D QHZ HOHPHQW RI -DYD ZH OO EHJLQ E\ ORRNLQJ DW LWV V\QWD[ WHPSODWH %\ FRQYHQWLRQ ZH XVH WKH FKDUDFWHUV OHVV WKDQ DQG JUHDWHU WKDQ ! LQ D V\QWD[ WHPSODWH WR LQGLFDWH LWHPV WKDW QHHG WR EH ILOOHG LQ ,Q WKLV FDVH WKH QDPH RI WKH FODVV DQG WKH PHWKRGV ERWK QHHG WR EH ILOOHG LQ :KHQ ZH ZULWH LQ D OLVW RI HOHPHQWV ZH UH LQGLFDWLQJ WKDW DQ\ QXPEHU RI WKRVH HOHPHQWV PD\ EH LQFOXGHG 7KH ILUVW OLQH RI WKH FODVV LV NQRZQ DV WKH FODVV KHDGHU 7KH ZRUG public LQ WKH KHDGHU LQGLFDWHV WKDW WKLV FODVV LV DYDLODEOH WR DQ\RQH WR XVH 1RWLFH WKDW WKH SURJUDP FRGH LQ D FODVV LV HQFORVHG LQ FXUO\ EUDFH FKDUDFWHUV 7KH FXUO\ EUDFH FKDUDFWHUV DUH XVHG LQ -DYD WR JURXS WRJHWKHU UHODWHG ELWV RI FRGH *URXSLQJ &KDUDFWHUV 7KH FXUO\ EUDFH FKDUDFWHUV { DQG } XVHG LQ -DYD WR JURXS WRJHWKHU UHODWHG OLQHV RI FRGH ,Q WKLV FDVH WKH FXUO\ EUDFHV DUH LQGLFDWLQJ WKDW HYHU\WKLQJ GHILQHG LQVLGH WKHVH EUDFHV LV SDUW RI WKLV SXEOLF FODVV 20 6R ZKDW H[DFWO\ FDQ DSSHDU LQVLGH WKH FXUO\ EUDFHV" :KDW FDQ EH FRQWDLQHG LQ D FODVV" $OO VRUWV RI WKLQJV EXW IRU QRZ ZH OO OLPLW RXUVHOYHV WR ZKDW DUH NQRZQ DV PHWKRGV 0HWKRGV DUH WKH QH[W VPDOOHVW XQLW RI FRGH LQ -DYD DQG WKH\ UHSUHVHQW D VLQJOH DFWLRQ RU FDOFXODWLRQ WR EH SHUIRUPHG 0HWKRG $ SURJUDP XQLW WKDW UHSUHVHQWV D SDUWLFXODU DFWLRQ RU FRPSXWDWLRQ 6LPSOH PHWKRGV DUH OLNH YHUEV 7KH\ FRPPDQG WKH FRPSXWHU WR SHUIRUP VRPH DFWLRQ ,QVLGH WKH FXUO\ EUDFHV IRU D FODVV \RX FDQ GHILQH VHYHUDO GLIIHUHQW PHWKRGV $W D PLQLPXP D FRPSOHWH SURJUDP UHTXLUHV D VSHFLDO PHWKRG WKDW LV NQRZQ DV WKH main PHWKRG ,W KDV WKH IROORZLQJ V\QWD[ public static void main(String args) { <statement>; <statement>; ... <statement>; } -XVW DV WKH ILUVW OLQH RI D FODVV LV NQRZQ DV D FODVV KHDGHU WKH ILUVW OLQH RI D PHWKRG LV NQRZQ DV D PHWKRG KHDGHU 7KH KHDGHU IRU PDLQ LV UDWKHU FRPSOLFDWHG 0RVW SHRSOH PHPRUL]H WKLV DV D NLQG RI PDJLFDO LQFDQWDWLRQ <RX ZDQW WR RSHQ WKH GRRU WR $OL %DED V FDYH" <RX VD\ 2SHQ VHVDPH <RX ZDQW WR FUHDWH DQ H[HFXWDEOH -DYD SURJUDP" <RX VD\ public static void main(String args) $ JURXS RI -DYD WHDFKHUV PDNHV IXQ RI WKLV ZLWK D ZHE VLWH FDOOHG ZZZ SXEOLFVWDWLFYRLGPDLQ FRP 0HPRUL]LQJ PDJLFDO LQFDQWDWLRQV LV QHYHU VDWLVI\LQJ HVSHFLDOO\ IRU FRPSXWHU VFLHQWLVWV ZKR OLNH WR NQRZ HYHU\WKLQJ WKDW LV JRLQJ RQ LQ WKHLU SURJUDPV %XW WKLV LV D SODFH ZKHUH -DYD VKRZV LWV XJO\ VLGH DQG ZH MXVW KDYH WR OLYH ZLWK LW 1HZ SURJUDPPHUV OLNH QHZ GULYHUV PXVW OHDUQ WR XVH VRPHWKLQJ FRPSOH[ ZLWKRXW IXOO\ XQGHUVWDQGLQJ KRZ LW ZRUNV )RUWXQDWHO\ E\ WKH WLPH \RX ILQLVK WKLV ERRN \RX OO XQGHUVWDQG HYHU\ SDUW RI WKH LQFDQWDWLRQ 1RWLFH WKDW WKH PDLQ PHWKRG KDV D VHW RI FXUO\ EUDFHV RI LWV RZQ 7KH\ DUH DJDLQ XVHG IRU JURXSLQJ VD\LQJ WKDW HYHU\WKLQJ WKDW DSSHDUV EHWZHHQ WKH EUDFHV LV SDUW RI WKH PDLQ PHWKRG 7KH OLQHV LQ EHWZHHQ WKH FXUO\ EUDFHV VSHFLI\ WKH VHULHV RI DFWLRQV WR SHUIRUP LQ H[HFXWLQJ WKH SURJUDP :H UHIHU WR WKHVH DV WKH VWDWHPHQWV RI WKH SURJUDP -XVW DV \RX SXW WRJHWKHU DQ HVVD\ E\ VWULQJLQJ WRJHWKHU FRPSOHWH VHQWHQFHV \RX SXW WRJHWKHU D PHWKRG E\ VWULQJLQJ WRJHWKHU VWDWHPHQWV 6WDWHPHQW $Q H[HFXWDEOH VQLSSHW RI FRGH WKDW UHSUHVHQWV D FRPSOHWH FRPPDQG 7KH VDPSOH KHOOR ZRUOG SURJUDP KDV MXVW D VLQJOH VWDWHPHQW WKDW LV NQRZQ DV D println VWDWHPHQW System.out.println("Hello, world!"); 1RWLFH WKDW WKLV VWDWHPHQW HQGV ZLWK D VHPLFRORQ 7KH VHPLFRORQ KDV D VSHFLDO VWDWXV LQ -DYD ,W LV XVHG WR WHUPLQDWH VWDWHPHQWV LQ WKH VDPH ZD\ WKDW SHULRGV WHUPLQDWH VHQWHQFHV LQ (QJOLVK 21 6WDWHPHQW 7HUPLQDWRU 7KH VHPLFRORQ FKDUDFWHU ; XVHG LQ -DYD WR WHUPLQDWH VWDWHPHQWV ,Q WKH KHOOR ZRUOG SURJUDP WKHUH LV MXVW D VLQJOH FRPPDQG WR SURGXFH D OLQH RI RXWSXW EXW FRQVLGHU WKH IROORZLQJ YDULDWLRQ FDOOHG +HOOR WKDW KDV WKUHH OLQHV RI FRGH WR EH H[HFXWHG LQ WKH PDLQ PHWKRG 1 2 3 4 5 6 7 public class Hello3 { public static void main(String args) { System.out.println("Hello, world!"); System.out.println(); System.out.println("This program produces three lines of output."); } } 1RWLFH WKDW WKHUH DUH WKUHH VHPLFRORQV LQ WKH PDLQ PHWKRG RQH DW WKH HQG RI HDFK RI WKH WKUHH SULQWOQ VWDWHPHQWV 7KH VWDWHPHQWV DUH H[HFXWHG LQ WKH RUGHU LQ ZKLFK WKH\ DSSHDU IURP ILUVW WR ODVW VR WKH SURJUDP DERYH SURGXFHV WKH IROORZLQJ RXWSXW Hello, world! This program produces three lines of output. /HW V VXPPDUL]H WKH GLIIHUHQW OHYHOV ZH MXVW ORRNHG DW $ -DYD SURJUDP LV VWRUHG LQ D FODVV ,QVLGH WKH FODVV \RX FDQ LQFOXGH PHWKRGV $W D PLQLPXP D FRPSOHWH SURJUDP UHTXLUHV WKDW \RX KDYH D VSHFLDO PHWKRG FDOOHG PDLQ ,QVLGH D PHWKRG OLNH PDLQ \RX LQFOXGH D VHULHV RI VWDWHPHQWV WKDW HDFK UHSUHVHQW D VLQJOH FRPPDQG IRU WKH FRPSXWHU WR H[HFXWH ,W PD\ VHHP RGG WR SXW WKH RSHQLQJ FXUO\ EUDFH DW WKH HQG RI D OLQH UDWKHU WKDQ RQ D OLQH E\ LWVHOI 6RPH SHRSOH ZRXOG XVH WKLV VW\OH RI LQGHQWDWLRQ IRU WKH SURJUDP LQVWHDG 1 2 3 4 5 6 7 public class Hello2 { public static void main(String args) { System.out.println("Hello, world!"); } } 'LIIHUHQW SHRSOH ZLOO PDNH GLIIHUHQW FKRLFHV DERXW WKH SODFHPHQW RI FXUO\ EUDFHV 7KH VW\OH ZH XVH IROORZV 6XQ V RIILFLDO -DYD FRGLQJ FRQYHQWLRQV %XW WKH RWKHU VW\OH KDV LWV DGYRFDWHV 2IWHQ SHRSOH ZLOO SDVVLRQDWHO\ DUJXH WKDW RQH ZD\ LV PXFK EHWWHU WKDQ WKH RWKHU EXW UHDOO\ WKHVH DUH PDWWHUV RI SHUVRQDO WDVWH EHFDXVH HDFK FKRLFH KDV VRPH DGYDQWDJHV DQG VRPH GLVDGYDQWDJHV <RXU LQVWUXFWRU PD\ UHTXLUH D SDUWLFXODU VW\OH LI QRW \RX VKRXOG FKRRVH D VW\OH WKDW \RX DUH FRPIRUWDEOH ZLWK DQG WKHQ XVH LW FRQVLVWHQWO\ 1RZ WKDW ZH KDYH VHHQ DQ RYHUYLHZ RI WKH VWUXFWXUH OHW V H[DPLQH VRPH RI WKH GHWDLOV RI -DYD SURJUDPV LQ JUHDWHU GHWDLO 22 'LG <RX .QRZ +HOOR :RUOG 7KH KHOOR ZRUOG WUDGLWLRQ ZDV VWDUWHG E\ %ULDQ .HUQLJKDQ DQG 'HQQLV 5LWFKLH ZKR LQYHQWHG D SURJUDPPLQJ ODQJXDJH NQRZQ DV & LQ WKH V 7KH ILUVW FRPSOHWH SURJUDP LQ WKHLU ERRN GHVFULELQJ WKH & ODQJXDJH ZDV D KHOOR ZRUOG SURJUDP .HUQLJKDQ DQG 5LWFKLH DQG WKHLU ERRN 7KH & 3URJUDPPLQJ /DQJXDJH KDYH ERWK EHHQ DIIHFWLRQDWHO\ UHIHUUHG WR DV . 5 HYHU VLQFH 0DQ\ PDMRU SURJUDPPLQJ ODQJXDJHV KDYH ERUURZHG WKH EDVLF & V\QWD[ DV D ZD\ WR OHYHUDJH WKH SRSXODULW\ RI & DQG WR HQFRXUDJH SURJUDPPHUV WR VZLWFK 7KH ODQJXDJHV & DQG -DYD ERWK ERUURZ D JUHDW GHDO RI WKHLU FRUH V\QWD[ IURP & .HUQLJKDQ DQG 5LWFKLH DOVR KDG D GLVWLQFWLYH VW\OH IRU ZKHUH WR SODFH FXUO\ EUDFHV DQG KRZ WR LQGHQW WKHLU SURJUDPV WKDW KDV EHFRPH NQRZQ DV . 5 VW\OH 7KLV LV WKH VW\OH WKDW 6XQ UHFRPPHQGV DQG WKDW ZH XVH LQ WKLV ERRN 6WULQJ /LWHUDOV 6WULQJV 2IWHQ LQ ZULWLQJ -DYD SURJUDPV VXFK DV WKH SUHFHGLQJ KHOOR ZRUOG SURJUDP ZH ZDQW WR LQFOXGH VRPH OLWHUDO WH[W WKDW ZH ZDQW WR VHQG WR WKH FRQVROH ZLQGRZ DV RXWSXW 3URJUDPPHUV KDYH WUDGLWLRQDOO\ UHIHUUHG WR VXFK WH[W DV D VWULQJ EHFDXVH LW LV FRPSRVHG RI D VHTXHQFH RI FKDUDFWHUV WKDW ZH VWULQJ WRJHWKHU 7KH -DYD ODQJXDJH VSHFLILFDWLRQ UHIHUV WR WKHVH DV VWULQJ OLWHUDOV DOWKRXJK SURJUDPPHUV RIWHQ VLPSO\ UHIHU WR WKHP DV VWULQJV ,Q -DYD \RX VSHFLI\ D VWULQJ OLWHUDO E\ VXUURXQGLQJ WKH OLWHUDO WH[W LQ TXRWDWLRQ PDUNV DV LQ "This is a bunch of text surrounded by quotation marks." <RX PXVW XVH GRXEOH TXRWDWLRQ PDUNV QRW VLQJOH TXRWDWLRQ PDUNV 7KH IROORZLQJ LV QRW D YDOLG VWULQJ OLWHUDO 'Bad stuff here.' %XW WKH IROORZLQJ LV D YDOLG VWULQJ OLWHUDO "This is a quote even with 'these' quotes inside." 6WULQJ OLWHUDOV PXVW QRW VSDQ PRUH WKDQ RQH OLQH RI D SURJUDP 7KH IROORZLQJ LV QRW D YDOLG VWULQJ OLWHUDO "This is really bad stuff right here." 6\VWHP RXW SULQWOQ $V \RX KDYH VHHQ WKH PDLQ PHWKRG RI D -DYD SURJUDP FRQWDLQV D VHULHV RI VWDWHPHQWV IRU WKH FRPSXWHU WR FDUU\ RXW 7KH\ DUH H[HFXWHG VHTXHQWLDOO\ VWDUWLQJ ZLWK WKH ILUVW VWDWHPHQW WKHQ WKH VHFRQG WKHQ WKH WKLUG DQG VR RQ XQWLO WKH ILQDO VWDWHPHQW KDV EHHQ H[HFXWHG 2QH RI WKH VLPSOHVW DQG PRVW FRPPRQ VWDWHPHQWV LV WR XVH System.out.println WR SURGXFH D OLQH RI RXWSXW 7KLV LV DQRWKHU PDJLFDO LQFDQWDWLRQ WKDW SHRSOH WHQG WR MXVW PHPRUL]H DV LI LW ZHUH RQH ZRUG $V RI WKLV 23 ZULWLQJ *RRJOH OLVWV RYHU ZHE SDJHV WKDW PHQWLRQ System.out.println 7KH NH\ WKLQJ WR NQRZ DERXW System.out.println LV WKDW ZH XVH LW WR SURGXFH D OLQH RI RXWSXW WKDW LV VHQW WR WKH FRQVROH ZLQGRZ 7KH VLPSOHVW IRUP RI WKH SULQWOQ KDV QRWKLQJ LQVLGH SDUHQWKHVHV DQG SURGXFHV D EODQN OLQH RI RXWSXW System.out.println(); <RX QHHG WR LQFOXGH WKH SDUHQWKHVHV HYHQ LI \RX GRQ W KDYH DQ\WKLQJ WR SXW LQVLGH WKHP 1RWLFH WKH VHPLFRORQ DW WKH HQG RI WKH OLQH $OO VWDWHPHQWV LQ -DYD PXVW EH WHUPLQDWHG ZLWK D VHPLFRORQ 0RUH RIWHQ \RX XVH SULQWOQ WR RXWSXW D OLQH RI WH[W System.out.println("This line uses the println method."); 7KLV VWDWHPHQW FRPPDQGV WKH FRPSXWHU WR SURGXFH WKH IROORZLQJ OLQH RI RXWSXW This line uses the println method. (DFK SULQWOQ VWDWHPHQW SURGXFHV D GLIIHUHQW OLQH RI RXWSXW 7KHVH WKUHH VWDWHPHQWV HDFK SURGXFH D OLQH RI RXWSXW WKH VHFRQG LV EODQN System.out.println("This is the first line of output."); System.out.println(); System.out.println("And this is the third, below a blank line."); 7KH IROORZLQJ WKUHH OLQHV RI RXWSXW DUH SURGXFHG This is the first line of output. And this is the third, below a blank line. (VFDSH 6HTXHQFHV $Q\ V\VWHP WKDW LQYROYHV TXRWLQJ WH[W ZLOO OHDG \RX WR FHUWDLQ GLIILFXOW VLWXDWLRQV )RU H[DPSOH VWULQJ OLWHUDOV DUH FRQWDLQHG LQVLGH RI TXRWDWLRQ PDUNV VR KRZ FRXOG \RX LQFOXGH D TXRWDWLRQ PDUN LQVLGH D VWULQJ OLWHUDO" 6WULQJ OLWHUDOV DOVR DUHQ W DOORZHG WR EUHDN DFURVV OLQHV VR KRZ ZRXOG \RX LQFOXGH D OLQH EUHDN LQVLGH D VWULQJ OLWHUDO" 7KH VROXWLRQ LV WKDW \RX FDQ HPEHG ZKDW DUH NQRZQ DV HVFDSH VHTXHQFHV LQ D VWULQJ OLWHUDO 7KHVH DUH WZR FKDUDFWHU VHTXHQFHV WKDW DUH XVHG WR UHSUHVHQW VSHFLDO FKDUDFWHUV 7KH\ DOO EHJLQ ZLWK WKH EDFNVODVK FKDUDFWHU ? 7KH IROORZLQJ WDEOH OLVWV VRPH RI WKH PRUH FRPPRQ HVFDSH VHTXHQFHV 6HTXHQFH \t \n \" \\ 5HSUHVHQWV WDE FKDUDFWHU QHZOLQH FKDUDFWHU TXRWDWLRQ PDUN EDFNVODVK FKDUDFWHU 24 .HHS LQ PLQG WKDW HDFK RI WKHVH WZR FKDUDFWHU VHTXHQFHV DFWXDOO\ VWDQGV IRU MXVW D VLQJOH FKDUDFWHU )RU H[DPSOH LI \RX ZHUH WR H[HFXWH WKH IROORZLQJ VWDWHPHQW System.out.println("What \"characters\" does this \\ produce?"); <RX ZRXOG JHW WKH IROORZLQJ RXWSXW What "characters" does this \ produce? 7KH VWULQJ OLWHUDO LQ WKH SULQWOQ KDV WKUHH HVFDSH VHTXHQFHV WKDW DUH HDFK WZR FKDUDFWHUV ORQJ EXW WKH\ HDFK SURGXFH D VLQJOH FKDUDFWHU RI RXWSXW (YHQ WKRXJK \RX FDQ W KDYH VWULQJ OLWHUDOV WKDW VSDQ PXOWLSOH OLQHV \RX FDQ XVH WKH \n HVFDSH VHTXHQFH WR HPEHG QHZOLQH FKDUDFWHUV LQ D VWULQJ 7KLV OHDGV WR WKH RGG VLWXDWLRQ ZKHUH D VLQJOH SULQWOQ FDQ SURGXFH PRUH WKDQ RQH OLQH RI RXWSXW )RU H[DPSOH LI \RX H[HFXWH WKLV VWDWHPHQW System.out.println("This one line\nproduces 3 lines\nof output."); <RX ZLOO JHW WKH IROORZLQJ RXWSXW This one line produces 3 lines of output. 7KH SULQWOQ LWVHOI SURGXFHV RQH OLQH RI RXWSXW DQG WKH VWULQJ OLWHUDO FRQWDLQV WZR QHZOLQH FKDUDFWHUV LQ WKH PLGGOH WKDW FDXVH WKH OLQH RI RXWSXW WR EH EURNHQ XS LQWR D WRWDO RI WKUHH OLQHV RI RXWSXW 7KLV LV DQRWKHU SURJUDPPLQJ KDELW WKDW WHQGV WR YDU\ DFFRUGLQJ WR WDVWH 6RPH SHRSOH LQFOXGLQJ WKH DXWKRUV ILQG LW KDUG WR UHDG VWULQJ OLWHUDOV WKDW KDYH \n LQ WKHP EXW RWKHU SHRSOH SUHIHU WR ZULWH IHZHU OLQHV RI FRGH 2QFH DJDLQ \RX VKRXOG PDNH XS \RXU RZQ PLQG DERXW ZKHQ WR XVH WKH QHZOLQH HVFDSH VHTXHQFH ,GHQWLILHUV DQG .H\ZRUGV 7KH ZRUGV XVHG WR QDPH SDUWV RI D -DYD SURJUDP DUH FDOOHG LGHQWLILHUV $Q LGHQWLILHU VSHFLILHV WKH QDPH RI D FODVV PHWKRG RU RWKHU HQWLW\ LQ \RXU SURJUDP ,GHQWLILHU $ QDPH JLYHQ WR DQ HQWLW\ LQ D SURJUDP VXFK DV D FODVV RU PHWKRG ,GHQWLILHUV PXVW VWDUW ZLWK D OHWWHU DQG FDQ WKHQ EH IROORZHG E\ DQ\ QXPEHU RI OHWWHUV DQG GLJLWV 7KH IROORZLQJ DUH OHJDO LGHQWLILHUV first hiThere numStudents TwoBy4 7KH -DYD ODQJXDJH VSHFLILFDWLRQ GHILQHV WKH VHW RI OHWWHUV WR LQFOXGH WKH XQGHUVFRUH DQG GROODU VLJQ FKDUDFWHUV _ DQG $ ZKLFK PHDQV WKDW WKH IROORZLQJ DUH OHJDO LGHQWLILHUV DV ZHOO 25 two_plus_two _count $2donuts MAX_COUNT 7KH IROORZLQJ DUH LOOHJDO LGHQWLILHUV two+two hi there hi-There 2by4 -DYD KDV FRQYHQWLRQV IRU FDSLWDOL]DWLRQ WKDW DUH IROORZHG IDLUO\ FRQVLVWHQWO\ E\ SURJUDPPHUV $OO FODVV QDPHV VKRXOG EHJLQ ZLWK D FDSLWDO OHWWHU DV ZLWK WKH +HOOR +HOOR DQG +HOOR FODVVHV LQ WKH SUHYLRXV VHFWLRQ 7KH QDPHV RI PHWKRGV VKRXOG EHJLQ ZLWK ORZHUFDVH OHWWHUV DV LQ PHWKRG PDLQ :KHQ SXWWLQJ VHYHUDO ZRUGV WRJHWKHU ZH FDSLWDOL]H WKH ILUVW OHWWHU RI HDFK ZRUG ,Q WKH QH[W FKDSWHU ZH OO OHDUQ DERXW FRQVWDQWV ZKLFK KDYH DQ HYHQ PRUH GLVWLQFW FDSLWDOL]DWLRQ VFKHPH ZLWK DOO OHWWHUV LQ XSSHUFDVH DQG ZRUGV VHSDUDWHG E\ XQGHUVFRUHV 7KHVH GLIIHUHQW VFKHPHV PLJKW VHHP OLNH WHGLRXV FRQVWUDLQWV EXW XVLQJ FRQVLVWDQW FDSLWDOL]DWLRQ LQ RXU FRGH DOORZV XV WR TXLFNO\ LGHQWLI\ ZKDW NLQG RI FRGH HOHPHQW ZH DUH ORRNLQJ DW )RU H[DPSOH VXSSRVH WKDW \RX ZHUH JRLQJ WR SXW WRJHWKHU WKH ZRUGV DOO P\ FKLOGUHQ LQWR DQ LGHQWLILHU 'HSHQGLQJ XSRQ ZKDW WKH LGHQWLILHU LV XVHG IRU \RX G WXUQ WKLV LQWR IRU D FODVV QDPH VWDUWV ZLWK D FDSLWDO FDSLWDOL]HV UHPDLQLQJ ZRUGV allMyChildren IRU D PHWKRG QDPH VWDUWV ZLWK D ORZHUFDVH OHWWHU FDSLWDOL]HV UHPDLQLQJ ZRUGV ALL_MY_CHILDREN IRU D FRQVWDQW QDPH DOO XSSHUFDVH VHSDUDWHG E\ XQGHUVFRUHV GHVFULEHG LQ &KDSWHU AllMyChildren -DYD LV FDVH VHQVLWLYH VR WKH LGHQWLILHUV class Class CLASS DQG cLASs DUH DOO FRQVLGHUHG GLIIHUHQW ZRUGV .HHS WKLV LQ PLQG DV \RX UHDG HUURU PHVVDJHV IURP WKH FRPSLOHU 3HRSOH DUH JRRG DW XQGHUVWDQGLQJ ZKDW \RX DUH VD\LQJ HYHQ LI \RX PDNH OLWWOH PLVWDNHV OLNH FKDQJLQJ WKH FDSLWDOL]DWLRQ RI D ZRUG 7KH -DYD FRPSLOHU EHFRPHV KRSHOHVVO\ FRQIXVHG ZKHQ \RX PLVVSHOO HYHQ D VLQJOH ZRUG 'RQ W KHVLWDWH WR XVH ORQJ LGHQWLILHUV 7KH PRUH GHVFULSWLYH \RXU QDPHV WKH HDVLHU LW ZLOO EH IRU SHRSOH LQFOXGLQJ \RXUVHOI WR UHDG \RXU SURJUDPV 'HVFULSWLYH LGHQWLILHUV DUH ZRUWK WKH WLPH WKH\ WDNH WR W\SH -DYD V 6WULQJ FODVV IRU H[DPSOH KDV D PHWKRG FDOOHG FRPSDUH7R,JQRUH&DVH -DYD KDV D VHW RI SUHGHILQHG LGHQWLILHUV FDOOHG NH\ZRUGV WKDW DUH UHVHUYHG IRU D SDUWLFXODU XVH $V \RX UHDG WKLV ERRN \RX ZLOO OHDUQ PDQ\ RI WKHVH NH\ZRUGV DQG ZKDW WKH\ DUH XVHG IRU <RX FDQ RQO\ XVH NH\ZRUGV IRU WKHLU LQWHQGHG SXUSRVH <RX PXVW EH FDUHIXO WR DYRLG XVLQJ WKHVH ZRUGV IRU GHILQLWLRQV WKDW \RX PDNH )RU H[DPSOH LI \RX QDPH D PHWKRG short RU try WKLV ZLOO FDXVH D SUREOHP EHFDXVH VKRUW DQG WU\ DUH UHVHUYHG NH\ZRUGV +HUH LV WKH FRPSOHWH OLVW RI UHVHUYHG NH\ZRUGV /LVW RI -DYD .H\ZRUGV abstract boolean break byte case catch char class const continue default do double else extends final finally float for goto if implements import instanceof int interface long native new package private protected public return short static strictfp super switch synchronized this throw throws transient try void volatile while 26 $ &RPSOH[ ([DPSOH 'UDZ)LJXUHV 7KH SULQWOQ VWDWHPHQW FDQ EH XVHG WR GUDZ WH[W ILJXUHV DV RXWSXW &RQVLGHU WKLV PRUH FRPSOLFDWHG SURJUDP H[DPSOH 1RWLFH WKDW LW XVHV WZR HPSW\ SULQWOQ VWDWHPHQWV WR SURGXFH EODQN OLQHV 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 public class DrawFigures1 { public static void main(String args) { System.out.println(" /\\"); System.out.println(" / \\"); System.out.println(" / \\"); System.out.println(" \\ /"); System.out.println(" \\ /"); System.out.println(" \\/"); System.out.println(); System.out.println(" \\ /"); System.out.println(" \\ /"); System.out.println(" \\/"); System.out.println(" /\\"); System.out.println(" / \\"); System.out.println(" / \\"); System.out.println(); System.out.println(" /\\"); System.out.println(" / \\"); System.out.println(" / \\"); System.out.println("+------+"); System.out.println("| |"); System.out.println("| |"); System.out.println("+------+"); System.out.println("|United|"); System.out.println("|States|"); System.out.println("+------+"); System.out.println("| |"); System.out.println("| |"); System.out.println("+------+"); System.out.println(" /\\"); System.out.println(" / \\"); System.out.println(" / \\"); } } +HUH LV WKH RXWSXW LW JHQHUDWHV 1RWLFH WKDW WKH SURJUDP KDV GRXEOH EDFNVODVK FKDUDFWHUV \\ EXW WKH RXWSXW KDV VLQJOH EDFNVODVK FKDUDFWHUV 7KLV LV DQ H[DPSOH RI DQ HVFDSH VHTXHQFH DV GHVFULEHG SUHYLRXVO\ 27 /\ / / \ \ \/ \ \ \/ /\ / / /\ \ / \ +------+ | | | | +------+ |United| |States| +------+ | | | | +------+ /\ / \ / \ / \ \ / / \ \ / / &RPPHQWV DQG 5HDGDELOLW\ 7KH OD\RXW RI D SURJUDP FDQ HQKDQFH LWV UHDGDELOLW\ -DYD LV D IUHH IRUPDW ODQJXDJH 7KLV PHDQV \RX FDQ SXW LQ DV PDQ\ RU DV IHZ VSDFHV DQG EODQN OLQHV DV \RX OLNH DV ORQJ DV \RX SXW DW OHDVW RQH VSDFH RU RWKHU SXQFWXDWLRQ PDUN EHWZHHQ ZRUGV 7KH IROORZLQJ SURJUDP LV OHJDO EXW KDUG WR UHDG 1 2 public class Ugly{public static void main(String args) {System.out.println("How short I am!");}} +HUH DUH VRPH VLPSOH UXOHV WR IROORZ WKDW ZLOO PDNH \RXU SURJUDPV PRUH UHDGDEOH 3XW FODVV DQG PHWKRG KHDGHUV RQ OLQHV E\ WKHPVHOYHV 3XW QR PRUH WKDQ RQH VWDWHPHQW RQ HDFK OLQH ,QGHQW \RXU SURJUDP SURSHUO\ :KHQ D { EUDFH DSSHDUV LQFUHDVH WKH LQGHQWDWLRQ RI IROORZLQJ OLQHV :KHQ D } EUDFH DSSHDUV UHGXFH WKH LQGHQWDWLRQ ,QGHQW VWDWHPHQWV LQVLGH FXUO\ EUDFHV E\ D FRQVLVWHQW QXPEHU RI VSDFHV D FRPPRQ FKRLFH LV VSDFHV SHU OHYHO RI LQGHQWDWLRQ 8VH EODQN OLQHV WR VHSDUDWH SDUWV RI WKH SURJUDP H J PHWKRGV 8VLQJ WKHVH UXOHV WR UHZULWH WKH SURJUDP DERYH \LHOGV WKH IROORZLQJ 28 1 2 3 4 5 public class Ugly { public static void main(String args){ System.out.println("How short I am!"); } } :HOO ZULWWHQ -DYD SURJUDPV FDQ EH TXLWH UHDGDEOH EXW RIWHQ \RX ZLOO ZDQW WR LQFOXGH VRPH H[SODQDWLRQV WKDW DUH QRW SDUW RI WKH SURJUDP LWVHOI <RX FDQ DQQRWDWH SURJUDPV E\ SXWWLQJ QRWHV FDOOHG FRPPHQWV LQ WKHP &RPPHQW 7H[W LQFOXGHG E\ SURJUDPPHUV WR H[SODLQ WKHLU FRGH &RPPHQWV DUH LJQRUHG E\ WKH FRPSLOHU 7KHUH DUH WZR FRPPHQW IRUPV LQ -DYD ,Q WKH ILUVW IRUP \RX RSHQ WKH FRPPHQW ZLWK D VODVK IROORZHG E\ D VWDU DQG \RX FORVH LW E\ D VWDU IROORZHG E\ D VODVK /* like this */ <RX PXVW QRW SXW VSDFHV EHWZHHQ WKH VODVKHV DQG WKH DVWHULVNV / * this is bad * / <RX FDQ SXW DOPRVW DQ\ WH[W \RX OLNH LQFOXGLQJ PXOWLSOH OLQHV LQVLGH WKH FRPPHQW /* Thaddeus Martin Assignment #1 Instructor: Professor Walingford Grader: Bianca Montgomery */ 7KH RQO\ WKLQJ \RX DUHQ W DOORZHG WR SXW LQVLGH D FRPPHQW LV WKH FRPPHQW HQG FKDUDFWHU V 7KH IROORZLQJ LV QRW OHJDO /* This comment has an asterisk/slash /*/ in it which prematurely closes the comment. This is bad. */ -DYD SURYLGHV D VHFRQG FRPPHQW IRUP IRU VKRUWHU VLQJOH OLQH FRPPHQWV <RX FDQ XVH WZR VODVKHV LQ D URZ WR LQGLFDWH WKDW WKH UHVW RI WKH FXUUHQW OLQH HYHU\WKLQJ WR WKH ULJKW RI WKH WZR VODVKHV LV D FRPPHQW )RU H[DPSOH \RX FDQ SXW D FRPPHQW DIWHU D VWDWHPHQW System.out.println("You win!"); // Good job! 2U \RX FDQ FUHDWH D FRPPHQW RQ LWV RZQ OLQH // give an introduction to the user System.out.println("Welcome to the game of blackjack."); System.out.println(); System.out.println("Let me explain the rules."); <RX FDQ HYHQ FUHDWH EORFNV RI VLQJOH OLQH FRPPHQWV 29 // // // // Thaddeus Martin Assignment #1 Instructor: Professor Walingford Grader: Bianca Montgomery 6RPH SHRSOH SUHIHU WR XVH WKH RWKHU FRPPHQW IRUP LQ D FDVH OLNH WKLV ZKHQ \RX NQRZ \RX ZLOO KDYH PXOWLSOH OLQHV RI WH[W LQ WKH FRPPHQW EXW LW LV VDIHU WR XVH WKH VHFRQG IRUP EHFDXVH \RX GRQ W KDYH WR UHPHPEHU WR FORVH WKH FRPPHQW 3OXV LW PDNHV WKH FRPPHQW VWDQG RXW 7KLV LV DQRWKHU WLPH ZKHUH LI \RX DUH QRW WROG WR XVH D SDUWLFXODU FRPPHQW VW\OH E\ DQ LQVWUXFWRU RU FROOHDJXH \RX VKRXOG GHFLGH IRU \RXUVHOI ZKLFK VW\OH \RX SUHIHU DQG XVH LW FRQVLVWHQWO\ 'RQ W FRQIXVH FRPPHQWV ZLWK WKH WH[W RI WKH SULQWOQ VWDWHPHQWV 7KH WH[W RI \RXU FRPPHQWV ZLOO QRW EH GLVSOD\HG DV RXWSXW ZKHQ WKH SURJUDP H[HFXWHV 7KH FRPPHQWV DUH WR KHOS H[DPLQH DQG XQGHUVWDQG WKH SURJUDP ,W LV D JRRG LGHD WR LQFOXGH FRPPHQWV DW WKH EHJLQQLQJ RI HDFK FODVV ILOH WR LQGLFDWH ZKDW WKH FODVV GRHV <RX PLJKW DOVR ZDQW WR LQFOXGH LQIRUPDWLRQ DERXW ZKR \RX DUH ZKDW FRXUVH \RX DUH WDNLQJ \RXU LQVWUXFWRU DQG RU JUDGHU V QDPH WKH FXUUHQW GDWH HW FHWHUD <RX VKRXOG DOVR FRPPHQW HDFK PHWKRG WR LQGLFDWH ZKDW LW GRHV &RPPHQWLQJ EHFRPHV PRUH XVHIXO LQ ODUJHU DQG PRUH FRPSOLFDWHG SURJUDPV DV ZHOO DV SURJUDPV WKDW ZLOO EH YLHZHG RU PRGLILHG E\ PRUH WKDQ RQH SURJUDPPHU &OHDU FRPPHQWV DUH H[WUHPHO\ KHOSIXO WR H[SODLQ WR DQRWKHU SHUVRQ RU WR \RX \RXUVHOI DW D ODWHU WLPH ZKDW \RXU SURJUDP LV GRLQJ DQG ZK\ LW LV GRLQJ LW -DYD KDV D SDUWLFXODU VW\OH RI FRPPHQWV NQRZQ DV -DYDGRF FRPPHQWV 7KH\ KDYH D PRUH FRPSOH[ IRUPDW WR WKHP EXW WKH\ KDYH WKH DGYDQWDJH WKDW \RX FDQ XVH D SURJUDP WR H[WUDFW WKH FRPPHQWV WR PDNH +70/ ILOHV VXLWDEOH IRU UHDGLQJ ZLWK D ZHE EURZVHU -DYDGRF FRPPHQWV DUH XVHIXO LQ PRUH DGYDQFHG SURJUDPPLQJ DQG DUH QRW GLVFXVVHG KHUH LQ GHWDLO 3URJUDP (UURUV ,Q WRGD\ 0DXULFH :LONHV DQ HDUO\ SLRQHHU RI FRPSXWLQJ H[SUHVVHG D VHQWLPHQW WKDW VWLOO ULQJV WUXH $V VRRQ DV ZH VWDUWHG SURJUDPPLQJ ZH IRXQG RXW WR RXU VXUSULVH WKDW LW ZDVQ W DV HDV\ WR JHW SURJUDPV ULJKW DV ZH KDG WKRXJKW 'HEXJJLQJ KDG WR EH GLVFRYHUHG , FDQ UHPHPEHU WKH H[DFW LQVWDQW ZKHQ , UHDOL]HG WKDW D ODUJH SDUW RI P\ OLIH IURP WKHQ RQ ZDV JRLQJ WR EH VSHQW LQ ILQGLQJ PLVWDNHV LQ P\ RZQ SURJUDPV 0DXULFH :LONHV <RX ZLOO DOVR KDYH WR IDFH WKLV UHDOLW\ DV \RX OHDUQ WR SURJUDP <RX UH JRLQJ WR PDNH PLVWDNHV MXVW OLNH HYHU\ RWKHU SURJUDPPHU LQ KLVWRU\ DQG \RX UH JRLQJ WR QHHG VWUDWHJLHV IRU HOLPLQDWLQJ WKRVH PLVWDNHV )RUWXQDWHO\ WKH FRPSXWHU LWVHOI FDQ KHOS \RX ZLWK VRPH RI WKH ZRUN 7KHUH DUH WKUHH NLQGV RI HUURUV WKDW \RX OO HQFRXQWHU DV \RX ZULWH SURJUDPV 6\QWD[ HUURUV RFFXU ZKHQ \RX PLVXVH -DYD 7KH\ DUH WKH SURJUDPPLQJ HTXLYDOHQW RI EDG JUDPPDU DQG DUH FDXJKW E\ WKH -DYD FRPSLOHU 30 /RJLF HUURUV RFFXU ZKHQ \RX ZULWH FRGH WKDW GRHVQ W SHUIRUP WKH WDVN LW LV LQWHQGHG WR SHUIRUP 5XQWLPH HUURUV DUH ORJLF HUURUV WKDW DUH VR VHYHUH WKDW -DYD VWRSV \RXU SURJUDP IURP H[HFXWLQJ 6\QWD[ (UURUV +XPDQ EHLQJV WHQG WR EH IDLUO\ IRUJLYLQJ DERXW PLQRU PLVWDNHV LQ VSHHFK )RU H[DPSOH ZH PLJKW ILQG LW RGG EXW ZH JHQHUDOO\ XQGHUVWDQG 0DVWHU <RGD ZKHQ KH VD\V 8QIRUWXQDWH WKDW \RX UXVKHG WR IDFH KLP WKDW LQFRPSOHWH ZDV \RXU WUDLQLQJ 1RW UHDG\ IRU WKH EXUGHQ ZHUH \RX 7KH -DYD FRPSLOHU ZLOO EH IDU OHVV IRUJLYLQJ 7KH FRPSLOHU UHSRUWV V\QWD[ HUURUV DV LW DWWHPSWV WR WUDQVODWH \RXU SURJUDP IURP -DYD LQWR E\WHFRGHV LI \RXU SURJUDP EUHDNV DQ\ RI -DYD V JUDPPDU UXOHV )RU H[DPSOH LI \RX PLVSODFH D VLQJOH VHPLFRORQ LQ \RXU SURJUDP \RX FDQ VHQG WKH FRPSLOHU LQWR D WDLO VSLQ RI FRQIXVLRQ $QG RQFH FRQIXVHG WKH FRPSLOHU PLJKW JHW YHU\ FRQIXVHG 7KH FRPSLOHU PD\ UHSRUW VHYHUDO HUURU PHVVDJHV GHSHQGLQJ RQ ZKDW LW WKLQNV LV ZURQJ ZLWK \RXU SURJUDP $ SURJUDP WKDW JHQHUDWHV FRPSLODWLRQ HUURUV FDQQRW EH H[HFXWHG ,I \RX VXEPLW \RXU SURJUDP WR WKH FRPSLOHU DQG KDYH HUURUV UHSRUWHG \RX PXVW IL[ WKH HUURUV DQG UHVXEPLW WKH SURJUDP WR WKH FRPSLOHU <RX ZLOO QRW EH DEOH WR SURFHHG XQWLO \RXU SURJUDP LV IUHH RI FRPSLODWLRQ HUURUV $Q HUURU FDQ EH LQWURGXFHG EHIRUH \RX HYHQ VWDUW ZULWLQJ \RXU SURJUDP LI \RX FKRRVH WKH ZURQJ QDPH IRU LWV ILOH &RPPRQ 3URJUDPPLQJ (UURU )LOH QDPH GRHV QRW PDWFK FODVV QDPH $V PHQWLRQHG HDUOLHU -DYD UHTXLUHV WKDW D SURJUDP V FODVV QDPH DQG ILOH QDPH PDWFK )RU H[DPSOH D SURJUDP WKDW EHJLQV ZLWK public class Hello PXVW EH VWRUHG LQ D ILOH FDOOHG Hello.java ,I \RX XVH WKH ZURQJ ILOH QDPH VD\ VDYLQJ LW DV WrongFileName.java \RX OO JHW DQ HUURU PHVVDJH OLNH WKLV WrongFileName.java:1: class Hello is public, should be declared in a file named Hello.java public class Hello { ^ 1 error 2QFH \RXU ILOH QDPH LV FRUUHFW WKHUH DUH VWLOO D QXPEHU RI HUURUV WKDW PD\ H[LVW LQ \RXU -DYD SURJUDP 2QH RI WKH PRVW FRPPRQ V\QWD[ HUURUV LV WR PLVVSHOO D ZRUG &RPPRQ 3URJUDPPLQJ (UURU 0LVVSHOOHG ZRUGV -DYD OLNH PRVW SURJUDPPLQJ ODQJXDJHV LV YHU\ SLFN\ DERXW VSHOOLQJ <RX QHHG WR VSHOO HDFK ZRUG FRUUHFWO\ LQFOXGLQJ SURSHU FDSLWDOL]DWLRQ 6XSSRVH IRU H[DPSOH WKDW ZH UHSODFH WKH SULQWOQ VWDWHPHQW LQ WKH KHOOR ZRUOG SURJUDP ZLWK WKH IROORZLQJ System.out.pruntln("Hello, world!"); :KHQ ZH WU\ WR FRPSLOH WKLV SURJUDP LW JHQHUDWHV DQ HUURU PHVVDJH VLPLODU WR WKH IROORZLQJ 31 Hello.java:3: cannot find symbol symbol : method pruntln(java.lang.String) location: class java.io.PrintStream System.out.pruntln("Hello, world!"); ^ 1 error 7KH ILUVW OLQH RI WKLV RXWSXW LQGLFDWHV WKDW WKH HUURU RFFXUV LQ WKH ILOH +HOOR MDYD RQ OLQH DQG WKDW WKH HUURU LV WKDW WKH FRPSLOHU FDQQRW ILQG V\PERO 7KH VHFRQG OLQH LQGLFDWHV WKDW WKH V\PERO LW FDQ W ILQG LV D PHWKRG FDOOHG pruntln 7KDW V EHFDXVH WKHUH LV QR VXFK PHWKRG WKH PHWKRG LV FDOOHG println 7KH HUURU PHVVDJH FDQ WDNH VOLJKWO\ GLIIHUHQW IRUPV GHSHQGLQJ XSRQ ZKDW \RX KDYH PLVVSHOOHG )RU H[DPSOH LI \RX IRUJHW WR FDSLWDOL]H WKH ZRUG System system.out.println("Hello, world!"); <RX ZLOO JHW WKH IROORZLQJ HUURU PHVVDJH Hello.java:3: package system does not exist system.out.println("Hello, world!"); ^ 1 error $JDLQ WKH ILUVW OLQH LQGLFDWHV WKDW WKH HUURU RFFXUV LQ OLQH RI WKH ILOH +HOOR MDYD 7KH HUURU PHVVDJH LV VOLJKWO\ GLIIHUHQW KHUH LQGLFDWLQJ WKDW LW FDQ W ILQG DQ\WKLQJ FDOOHG system 7KH VHFRQG DQG WKLUG OLQHV RI WKLV HUURU PHVVDJH LQFOXGH WKH RULJLQDO OLQH RI FRGH ZLWK DQ DUURZ SRLQWLQJ WR ZKHUH WKH FRPSLOHU JRW FRQIXVHG 7KH FRPSLOHU HUURUV DUH QRW DOZD\V YHU\ FOHDU EXW LI \RX SD\ DWWHQWLRQ WR ZKHUH WKH HUURU LV SRLQWLQJ \RX KDYH D SUHWW\ JRRG VHQVH RI DERXW ZKHUH WKH HUURU RFFXUV 6RPH -DYD HGLWRUV VXFK DV (FOLSVH DOVR XQGHUOLQH \RXU V\QWD[ HUURUV IRU \RX DV \RX ZULWH \RXU SURJUDP 7KLV PDNHV LW HDVLHU WR VHH WKH OLQH DQG SRUWLRQ RI WKH SURJUDP WKDW FRQWDLQV WKH HUURU 2QFH \RXU VSHOOLQJ LV FRUUHFW \RX PD\ VWLOO KDYH HUURUV UHODWHG WR SXQFWXDWLRQ VXFK DV PLVVLQJ VHPLFRORQV &RPPRQ 3URJUDPPLQJ (UURU )RUJHWWLQJ D VHPLFRORQ $OO -DYD VWDWHPHQWV HQG ZLWK VHPLFRORQV ,W V HDV\ WR IRUJHW WR SXW D VHPLFRORQ DW WKH HQG RI D VWDWHPHQW DV LV GRQH LQ WKH IROORZLQJ SURJUDP 1 2 3 4 5 6 public class MissingSemicolon { public static void main(String args) { System.out.println("A rose by any other name") System.out.println("would smell as sweet"); } } 7KH FRPSLOHU SURGXFHV RXWSXW VLPLODU WR WKH IROORZLQJ MissingSemicolon.java:4: ';' expected System.out.println("would smell as sweet"); ^ 1 error 32 7KH RGG WKLQJ DERXW WKH FRPSLOHU V RXWSXW LV WKDW LW V OLVWHG OLQH DV WKH FDXVH RI WKH SUREOHP QRW OLQH ZKHUH WKH VHPLFRORQ ZDV DFWXDOO\ IRUJRWWHQ 7KLV LV EHFDXVH WKH FRPSLOHU LV ORRNLQJ IRUZDUG IRU D VHPLFRORQ DQG LVQ W XSVHW XQWLO LW ILQGV VRPHWKLQJ WKDW LVQ W D VHPLFRORQ ZKLFK LW GRHV ZKHQ LW UHDFKHV OLQH 8QIRUWXQDWHO\ LQ FDVHV OLNH WKLV FRPSLOHU HUURU PHVVDJHV GRQ W DOZD\V GLUHFW \RX WR WKH FRUUHFW OLQH WR EH IL[HG 6HPLFRORQV DUH QRW WKH RQO\ LWHP WKDW \RX FDQ IRUJHW ZKHQ ZULWLQJ \RXU SURJUDP ,W V DOVR HDV\ WR IRUJHW DQ HQWLUH ZRUG VXFK DV D UHTXLUHG NH\ZRUG &RPPRQ 3URJUDPPLQJ (UURU )RUJHWWLQJ D UHTXLUHG NH\ZRUG $QRWKHU FRPPRQ V\QWD[ HUURU LV WR IRUJHW D UHTXLUHG NH\ZRUG ZKHQ W\SLQJ \RXU SURJUDP VXFK DV static RU class 'RXEOH FKHFN \RXU SURJUDPV DJDLQVW WKH H[DPSOHV LQ WKH WH[WERRN WR PDNH VXUH \RX KDYHQ W RPLWWHG DQ LPSRUWDQW NH\ZRUG 7KH FRPSLOHU ZLOO JLYH GLIIHUHQW HUURU PHVVDJHV GHSHQGLQJ RQ ZKLFK NH\ZRUG LV PLVVLQJ EXW WKH PHVVDJHV FDQ EH KDUG WR XQGHUVWDQG )RU H[DPSOH \RX PLJKW ZULWH D SURJUDP QDPHG Bug4 DQG IRUJHW WKH NH\ZRUG class ZKHQ ZULWLQJ LWV FODVV KHDGHU 7KH FRPSLOHU SURYLGHV WKH IROORZLQJ HUURU PHVVDJH Bug4.java:1: 'class' or 'interface' expected public Bug4 { ^ 1 error +RZHYHU LI \RX IRUJHW WKH NH\ZRUG void ZKHQ GHFODULQJ WKH main PHWKRG WKH FRPSLOHU JHQHUDWHV D GLIIHUHQW HUURU PHVVDJH Bug5.java:2: invalid method declaration; return type required public static main(String args) { ^ 1 error 7KH HUURU PHVVDJH WKH FRPSLOHU JLYHV PD\ RU PD\ QRW EH KHOSIXO 0DQ\ WLPHV WKH PHVVDJH ZLOO FRQIXVH \RX EHFDXVH LW ZLOO XVH ZRUGV DQG WHUPV WKDW ZH KDYHQ W GLVFXVVHG \HW 1RWLFH WKDW LQ WKHVH FDVHV WKH HUURU FRQWDLQV D FDUHW PDUNHU ^ SRLQWLQJ DW WKH SRVLWLRQ LQ WKH OLQH ZKHUH WKH FRPSLOHU EHFDPH FRQIXVHG 7KLV FDQ KHOS \RX SLQSRLQW WKH SODFH ZKHUH D UHTXLUHG NH\ZRUG PLJKW EH PLVVLQJ ,I \RX FDQ W XQGHUVWDQG WKH HUURU PHVVDJH ORRNLQJ DW WKH HUURU V OLQH QXPEHU DQG FRPSDULQJ WKH FRQWHQWV RI WKDW OLQH ZLWK VLPLODU OLQHV LQ RWKHU SURJUDPV LV D JRRG SODFH WR VWDUW $OVR DVNLQJ VRPHRQH HOVH VXFK DV DQ LQVWUXFWRU RU ODE DVVLVWDQW WR H[DPLQH \RXU SURJUDP FDQ RIWHQ XQFRYHU VXFK PLVWDNHV $QRWKHU FRPPRQ V\QWD[ HUURU LV WR IRUJHW WR FORVH D VWULQJ OLWHUDO &RPPRQ 3URJUDPPLQJ (UURU 1RW FORVLQJ D VWULQJ OLWHUDO RU FRPPHQW (YHU\ VWULQJ OLWHUDO KDV WR KDYH DQ RSHQLQJ TXRWH DQG D FORVLQJ TXRWH EXW LW V HDV\ WR IRUJHW WKH FORVLQJ TXRWDWLRQ PDUN )RU H[DPSOH \RX PLJKW VD\ 33 System.out.println("Hello, world!); 7KLV SURGXFHV WZR GLIIHUHQW HUURU PHVVDJHV HYHQ WKRXJK WKHUH LV RQO\ RQH XQGHUO\LQJ HUURU Hello.java:3: unclosed string literal System.out.println("Hello, world!); ^ Hello.java:4: ')' expected } ^ 2 errors ,Q WKLV FDVH WKH ILUVW HUURU PHVVDJH LV TXLWH FOHDU LQFOXGLQJ DQ DUURZ SRLQWLQJ DW WKH EHJLQQLQJ RI WKH VWULQJ OLWHUDO WKDW ZDVQ W FORVHG 7KH VHFRQG HUURU PHVVDJH ZDV FDXVHG E\ WKH ILUVW %HFDXVH WKH VWULQJ OLWHUDO ZDV QRW FORVHG WKH FRPSLOHU GLGQ W QRWLFH WKH ULJKW SDUHQWKHVLV DQG VHPLFRORQ WKDW DSSHDU DW WKH HQG RI WKH OLQH 7KLV LV DQ H[DPSOH RI D VLQJOH V\QWD[ HUURU JHQHUDWLQJ VHYHUDO GLIIHUHQW HUURU PHVVDJHV $ VLPLODU SUREOHP RFFXUV ZKHQ \RX IRUJHW WR FORVH D PXOWL OLQH FRPPHQW E\ ZULWLQJ */ DV LV GRQH LQ WKH IROORZLQJ SURJUDP /* This is a bad program. public class Bad { public static void main(String args){ System.out.println("Hi there."); } } /* end of program */ 7KH SUHFHGLQJ ILOH LV QRW D SURJUDP LW LV RQH ORQJ FRPPHQW %HFDXVH WKH FRPPHQW RQ WKH ILUVW OLQH LV QRW FORVHG WKH HQWLUH SURJUDP LV VZDOORZHG XS /XFNLO\ PDQ\ -DYD HGLWRU SURJUDPV FRORU WKH SDUWV RI D SURJUDP WR KHOS \RX LGHQWLI\ WKHP YLVXDOO\ 8VXDOO\ LI \RX IRUJHW WR FORVH D VWULQJ OLWHUDO RU FRPPHQW WKH UHVW RI \RXU SURJUDP ZLOO WXUQ WKH ZURQJ FRORU ZKLFK FDQ KHOS \RX VSRW WKH PLVWDNH $ JRRG UXOH RI WKXPE WR IROORZ LV WKDW WKH ILUVW HUURU UHSRUWHG E\ WKH FRPSLOHU LV WKH PRVW LPSRUWDQW RQH WR SD\ DWWHQWLRQ WR 7KH UHVW PLJKW EH WKH UHVXOW RI WKDW ILUVW HUURU 0DQ\ SURJUDPPHUV GRQ W HYHQ ERWKHU WR ORRN DW HUURUV EH\RQG WKH ILUVW )L[LQJ WKH ILUVW HUURU DQG UHFRPSLOLQJ PD\ FDXVH WKH RWKHU HUURUV WR GLVDSSHDU /RJLF (UURUV EXJV /RJLF HUURUV DUH DOVR FDOOHG EXJV &RPSXWHU SURJUDPPHUV XVH ZRUGV OLNH EXJ ULGGHQ DQG EXJJ\ WR GHVFULEH SRRUO\ ZULWWHQ SURJUDPV 7KH ZRUG EXJ LV DQ ROG HQJLQHHULQJ WHUP WKDW SUHGDWHV FRPSXWHUV HDUO\ FRPSXWLQJ EXJV ZHUH VRPHWLPHV LQ KDUGZDUH DV ZHOO DV VRIWZDUH $GPLUDO *UDFH +RSSHU DQ HDUO\ SLRQHHU RI FRPSXWLQJ LV ODUJHO\ FUHGLWHG ZLWK SRSXODUL]LQJ WKH XVH RI WKH WHUP EXJ LQ FRPSXWHU SURJUDPPLQJ FRQWH[W 6KH RIWHQ WROG WKH WKH WUXH VWRU\ RI D JURXS RI SURJUDPPHUV DW +DUYDUG 8QLYHUVLW\ LQ WKH PLG V ZKR FRXOGQ W ILJXUH RXW ZKDW ZDV ZURQJ ZLWK WKHLU SURJUDPV XQWLO WKH\ RSHQHG XS WKH FRPSXWHU DQG IRXQG DQ DFWXDO PRWK WUDSSHG LQVLGH 7KH SURFHVV RI ILQGLQJ DQG HOLPLQDWLQJ EXJV IURP SURJUDPV LV FDOOHG GHEXJJLQJ 34 7KH IRUP WKDW D EXJ WDNHV ZLOO YDU\ 6RPHWLPHV \RXU SURJUDP ZLOO VLPSO\ EHKDYH LPSURSHUO\ )RU H[DPSOH LW PLJKW SURGXFH WKH ZURQJ RXWSXW 2WKHU WLPHV LW ZLOO DVN WKH FRPSXWHU WR SHUIRUP VRPH WDVN WKDW LV FOHDUO\ D PLVWDNH LQ ZKLFK FDVH \RXU SURJUDP ZLOO KDYH D UXQWLPH HUURU WKDW VWRSV LW IURP H[HFXWLQJ ,Q WKLV FKDSWHU VLQFH RXU NQRZOHGJH RI -DYD LV OLPLWHG JHQHUDOO\ WKH RQO\ W\SH RI ORJLF HUURU ZH ZLOO VHH LV D PLVWDNH LQ SURJUDP RXWSXW IURP DQ LQFRUUHFW SULQWOQ VWDWHPHQW RU PHWKRG FDOO 3URFHGXUDO 'HFRPSRVLWLRQ %ULDQ .HUQLJKDQ RQH RI WKH FUHDWRUV RI WKH & SURJUDPPLQJ ODQJXDJH KDV VDLG WKDW &RQWUROOLQJ FRPSOH[LW\ LV WKH HVVHQFH RI FRPSXWHU SURJUDPPLQJ 3HRSOH KDYH RQO\ D PRGHVW FDSDFLW\ IRU GHWDLO :H FDQ W VROYH FRPSOH[ SUREOHPV DOO DW RQFH ,QVWHDG ZH VWUXFWXUH RXU SUREOHP VROYLQJ E\ GLYLGLQJ WKH SUREOHP LQWR PDQDJHDEOH SLHFHV DQG FRQTXHULQJ HDFK SLHFH LQGLYLGXDOO\ :H RIWHQ XVH WKH WHUP GHFRPSRVLWLRQ WR GHVFULEH WKLV SULQFLSOH DV DSSOLHG WR SURJUDPPLQJ 'HFRPSRVLWLRQ $ VHSDUDWLRQ LQWR GLVFHUQLEOH SDUWV HDFK RI ZKLFK LV VLPSOHU WKDQ WKH ZKROH :LWK SURFHGXUDO SURJUDPPLQJ ODQJXDJHV OLNH .HUQLJKDQ V & GHFRPSRVLWLRQ LQYROYHV GLYLGLQJ D FRPSOH[ WDVN LQWR D VHW RI VXEWDVNV 7KLV LV YHU\ YHUE RU DFWLRQ RULHQWHG GLYLGLQJ XS WKH RYHUDOO DFWLRQ LQWR D VHULHV RI VPDOOHU DFWLRQV 7KLV WHFKQLTXH LV FDOOHG SURFHGXUDO GHFRPSRVLWLRQ -DYD ZDV GHVLJQHG IRU D GLIIHUHQW NLQG RI GHFRPSRVLWLRQ WKDW LV PRUH QRXQ RU REMHFW RULHQWHG ,QVWHDG RI WKLQNLQJ RI WKH SUREOHP DV D VHULHV RI DFWLRQV WR EH SHUIRUPHG ZH WKLQN RI LW DV D FROOHFWLRQ RI REMHFWV WKDW KDYH WR LQWHUDFW $V D FRPSXWHU VFLHQWLVW \RX VKRXOG EH IDPLOLDU ZLWK ERWK W\SHV RI SUREOHP VROYLQJ 7KLV ERRN EHJLQV ZLWK SURFHGXUDO GHFRPSRVLWLRQ DQG GHYRWHV PDQ\ FKDSWHUV WR PDVWHULQJ YDULRXV DVSHFWV RI WKH SURFHGXUDO DSSURDFK 2QO\ DIWHU \RX KDYH WKRURXJKO\ SUDFWLFHG SURFHGXUDO SURJUDPPLQJ ZLOO ZH WXUQ RXU DWWHQWLRQ EDFN WR REMHFW GHFRPSRVLWLRQ DQG REMHFW RULHQWHG SURJUDPPLQJ $V DQ H[DPSOH RI SURFHGXUDO GHFRPSRVLWLRQ FRQVLGHU WKH SUREOHP RI EDNLQJ D FDNH <RX FDQ GLYLGH WKLV SUREOHP LQWR WKH IROORZLQJ VXESUREOHPV 0DNH WKH EDWWHU %DNH WKH FDNH 0DNH WKH IURVWLQJ )URVW WKH FDNH (DFK RI WKHVH IRXU WDVNV KDV GHWDLOV DVVRFLDWHG ZLWK LW 7R PDNH WKH EDWWHU IRU H[DPSOH \RX 0L[ WKH GU\ LQJUHGLHQWV &UHDP WKH EXWWHU DQG WKH VXJDU %HDW LQ WKH HJJV 6WLU LQ WKH GU\ LQJUHGLHQWV 35 7KXV \RX GLYLGH WKH RYHUDOO WDVN LQWR VXEWDVNV DQG IXUWKHU GLYLGH WKHVH VXEWDVNV LQWR VPDOOHU VXEWDVNV (YHQWXDOO\ \RX UHDFK GHVFULSWLRQV WKDW DUH VR VLPSOH WKH\ UHTXLUH QR IXUWKHU H[SODQDWLRQ L H SULPLWLYHV $ GLDJUDP RI WKLV SDUWLDO VROXWLRQ ZRXOG ORRN OLNH WKLV Make Cake | +-----------+-----------------+----------------------+ | | | | Make Batter Bake Make Frosting Frost Cake | +--------+------+-------+ | | | | Mix Cream Beat Stir Dry Butter In In Dry Ingr. Sugar Eggs Ingredients 0DNH &DNH LV WKH KLJKHVW OHYHO RSHUDWLRQ RI DOO ,W LV GHILQHG LQ WHUPV RI IRXU ORZHU OHYHO RSHUDWLRQV FDOOHG 0DNH %DWWHU %DNH 0DNH )URVWLQJ DQG )URVW &DNH 7KH 0DNH %DWWHU RSHUDWLRQ LV GHILQHG LQ WHUPV RI HYHQ ORZHU OHYHO RSHUDWLRQV :H XVH GLDJUDPV OLNH WKLV WKURXJKRXW WKH ERRN 7KH\ DUH FDOOHG VWUXFWXUH GLDJUDPV DQG DUH LQWHQGHG WR VKRZ KRZ D SUREOHP LV EURNHQ GRZQ LQWR VXESUREOHPV ,Q WKLV GLDJUDP \RX FDQ DOVR WHOO LQ ZKDW RUGHU RSHUDWLRQV DUH SHUIRUPHG E\ UHDGLQJ OHIW WR ULJKW 7KDW ZLOO QRW EH WUXH RI PRVW VWUXFWXUH GLDJUDPV 7R GHWHUPLQH WKH DFWXDO RUGHU WKDW VXE SURJUDPV DUH SHUIRUPHG \RX ZLOO XVXDOO\ KDYH WR UHIHU WR WKH SURJUDP LWVHOI 7ZR VW\OHV RI DSSURDFKLQJ SUREOHPV DUH FDOOHG ERWWRP XS DQG WRS GRZQ 7R PDNH D FDNH \RX PLJKW VWDUW DW WKH WRS DQG UHDVRQ , FDQ PDNH D FDNH E\ ILUVW PDNLQJ EDWWHU WKHQ EDNLQJ LW WKHQ PDNLQJ IURVWLQJ DQG ILQDOO\ SXWWLQJ WKH IURVWLQJ RQ WKH FDNH , FRXOG PDNH WKH EDWWHU E\ ILUVW 7KLV LV D WRS GRZQ VROXWLRQ LW VWDUWV DW WKH KLJKHVW OHYHO DQG SURFHHGV GRZQZDUG $ ERWWRP XS VROXWLRQ LQYROYHV UHDVRQLQJ OLNH :KDW FDQ , GR QRZ" , FRXOG SXW WRJHWKHU GU\ LQJUHGLHQWV ,I , WKHQ FUHDPHG WKH EXWWHU DQG VXJDU DQG WKHQ EHDW LQ HJJV DQG WKHQ PL[HG LQ GU\ LQJUHGLHQWV , G KDYH VRPH EDWWHU ,I , WKHQ EDNHG WKDW EDWWHU , G KDYH DQ XQIURVWHG FDNH ,I , WKHQ 7KLV LV D ERWWRP XS VROXWLRQ EHFDXVH LW VWDUWV ZLWK ORZ OHYHO RSHUDWLRQV DQG SXWV WKHP WRJHWKHU WR IRUP KLJKHU OHYHO RSHUDWLRQV 2QH ILQDO SUREOHP VROYLQJ WHUP KDV WR GR ZLWK WKH SURFHVV RI SURJUDPPLQJ 3URIHVVLRQDO SURJUDPPHUV GHYHORS SURJUDPV LQ VWDJHV ,QVWHDG RI WU\LQJ WR SURGXFH D FRPSOHWH ZRUNLQJ SURJUDP DOO DW RQFH WKH\ FKRRVH VRPH SLHFH RI WKH SUREOHP WR LPSOHPHQW ILUVW 7KHQ DQRWKHU SLHFH LV DGGHG DQG DQRWKHU DQG DQRWKHU 7KH RYHUDOO SURJUDP LV EXLOW XS VORZO\ SLHFH E\ SLHFH 7KLV SURFHVV LV NQRZQ DV LWHUDWLYH HQKDQFHPHQW RU VWHSZLVH UHILQHPHQW ,WHUDWLYH (QKDQFHPHQW 7KH SURFHVV RI SURGXFLQJ D SURJUDP LQ VWDJHV DGGLQJ QHZ IXQFWLRQDOLW\ DW HDFK VWDJH $ NH\ IHDWXUH RI HDFK LWHUDWLYH VWHS LV WKDW \RX FDQ WHVW LW WR PDNH VXUH WKDW SLHFH ZRUNV EHIRUH PRYLQJ RQ 36 :H OO QRZ GLVFXVV D FRQVWUXFW WKDW ZLOO DOORZ XV WR LWHUDWLYHO\ HQKDQFH RXU -DYD SURJUDPV WR LPSURYH WKHLU VWUXFWXUH DQG UHGXFH WKHLU UHGXQGDQF\ VWDWLF PHWKRGV 6WDWLF 0HWKRGV -DYD LV GHVLJQHG IRU REMHFWV DQG SURJUDPPLQJ LQ -DYD XVXDOO\ LQYROYHV GHFRPSRVLQJ D SUREOHP LQWR YDULRXV REMHFWV HDFK ZLWK PHWKRGV WKDW SHUIRUP SDUWLFXODU WDVNV :H ZLOO VHH KRZ WKLV ZRUNV HYHQWXDOO\ EXW IRU QRZ ZH DUH JRLQJ WR H[SORUH SURFHGXUDO GHFRPSRVLWLRQ 7KLV ZLOO DOORZ XV WR SRVWSRQH VRPH RI -DYD V GHWDLOV ZKLOH ZH OHDUQ DERXW SURJUDPPLQJ LQ JHQHUDO 7KH ILUVW GHFRPSRVLWLRQ FRQVWUXFW ZH ZLOO VWXG\ LV NQRZQ DV D VWDWLF PHWKRG 6WDWLF 0HWKRG $ EORFN RI -DYD VWDWHPHQWV ZKLFK LV JLYHQ D QDPH 6WDWLF PHWKRGV DUH XQLWV RI SURFHGXUDO GHFRPSRVLWLRQ :H W\SLFDOO\ EUHDN D FODVV LQWR VHYHUDO VWDWLF PHWKRGV HDFK RI ZKLFK VROYHV VRPH SLHFH RI WKH RYHUDOO SUREOHP &RQVLGHU WKH IROORZLQJ SURJUDP ZKLFK GUDZV WZR WH[W ER[HV RQ WKH FRQVROH 1 2 3 4 5 6 7 8 9 10 11 12 13 public class DrawBoxes { public static void main(String args) { System.out.println("+------+"); System.out.println("| |"); System.out.println("| |"); System.out.println("+------+"); System.out.println(); System.out.println("+------+"); System.out.println("| |"); System.out.println("| |"); System.out.println("+------+"); } } 7KH SURJUDP ZRUNV FRUUHFWO\ EXW WKH IRXU OLQHV XVHG WR GUDZ WKH ER[ DUH UHSHDWHG WZLFH 7KLV UHGXQGDQF\ LV XQGHVLUDEOH IRU VHYHUDO UHDVRQV :H PLJKW ZLVK WR FKDQJH WKH DSSHDUDQFH RI WKH ER[HV LQ ZKLFK FDVH ZH G KDYH WR FKDQJH ERWK SODFHV LQ WKH SURJUDP $OVR ZH PLJKW ZLVK WR GUDZ DGGLWLRQDO ER[HV ZKLFK ZRXOG UHTXLUH XV WR W\SH DGGLWLRQDO FRSLHV RI RU FRS\ DQG SDVWH WKH UHGXQGDQW OLQHV $ SUHIHUDEOH VROXWLRQ ZRXOG EH WR FUHDWH D -DYD FRPPDQG WKDW VSHFLILHV KRZ WR GUDZ D ER[ DQG WKHQ WR H[HFXWH WKDW FRPPDQG WZLFH -DYD GRHVQ W KDYH D GUDZ D ER[ FRPPDQG EXW ZH FDQ FUHDWH RQH 7R FUHDWH VXFK D FRPPDQG ZH ZULWH D OLQH WKDW JLYHV D QDPH WR WKH FRPPDQG DQG WKHQ EHWZHHQ D SDLU RI { DQG } EUDFHV ZH SODFH WKH VWDWHPHQWV WR EH H[HFXWHG E\ WKDW FRPPDQG 6XFK D QDPHG FRPPDQG LV FDOOHG D VWDWLF PHWKRG )RU H[DPSOH KHUH LV D VWDWLF PHWKRG WR GUDZ D WH[W ER[ public static void drawBox() { System.out.println("+------+"); System.out.println("| |"); System.out.println("| |"); System.out.println("+------+"); } 37 <RX KDYH DOUHDG\ VHHQ D VWDWLF PHWKRG LQ WKH SURJUDPV ZH KDYH ZULWWHQ FDOOHG PDLQ 5HFDOO WKDW WKH PDLQ PHWKRG KDV WKH IROORZLQJ IRUP public static void main(String args) { <statement>; <statement>; ... <statement>; } 7KH VWDWLF PHWKRGV ZH OO ZULWH KDYH D VLPLODU VWUXFWXUH public static void <name>() { <statement>; <statement>; ... <statement>; } 7KH ILUVW OLQH LV NQRZQ DV WKH PHWKRG KHDGHU )RU QRZ \RX ZLOO QRW IXOO\ XQGHUVWDQG ZKDW HDFK ZRUG RI WKLV KHDGHU PHDQV LQ -DYD WKH PRVW LPSRUWDQW WKLQJ WR UHPHPEHU LV WKDW \RX OO ZULWH public static void IROORZHG E\ WKH QDPH \RX ZLVK WR JLYH WKH PHWKRG IROORZHG E\ WZR SDUHQWKHVHV () )RU WKH VDNH RI FRPSOHWHQHVV ZH OO EULHIO\ GLVFXVV ZKDW WKH ZRUGV LQ WKH KHDGHU PHDQ 7KH NH\ZRUG public LQGLFDWHV WKDW WKLV PHWKRG LV DYDLODEOH WR EH XVHG E\ DOO SDUWV RI \RXU SURJUDP $OO PHWKRGV ZH ZULWH ZLOO EH public 7KH NH\ZRUG static LQGLFDWHV WKDW WKLV LV D VWDWLF SURFHGXUDO VW\OH QRW REMHFW RULHQWHG PHWKRG )RU QRZ DOO PHWKRGV ZH ZULWH ZLOO EH static XQWLO ZH OHDUQ DERXW ZULWLQJ REMHFWV LQ &KDSWHU 7KH ZRUG void LQGLFDWHV WKDW WKLV PHWKRG H[HFXWHV VWDWHPHQWV EXW GRHV QRW FRPSXWH DQG HPLW DQ\ YDOXH 2WKHU PHWKRGV ZH OO VHH ODWHU FRPSXWH DQG UHWXUQ YDOXHV RXWZDUG 7KH ZRUG drawBox LV WKH QDPH RI WKH PHWKRG 7KH HPSW\ SDUHQWKHVHV VSHFLI\ D OLVW LQ WKLV FDVH DQ HPSW\ OLVW RI YDOXHV WKDW DUH VHQW LQZDUG WR \RXU PHWKRG DV LQSXW VXFK YDOXHV DUH FDOOHG SDUDPHWHUV DQG ZLOO QRW EH XVHG E\ RXU PHWKRGV XQWLO &KDSWHU ,W FDQ EH FXPEHUVRPH RU FRQIXVLQJ WR KDYH WR LQFOXGH WKH NH\ZRUG static IRU HDFK RI WKHVH PHWKRGV 2WKHU -DYD WH[WERRNV RIWHQ GR QRW GLVFXVV VWDWLF PHWKRGV DV HDUO\ DV ZH ZLOO KHUH LQVWHDG RWKHU WHFKQLTXHV DUH VKRZQ IRU GHFRPSRVLQJ SUREOHPV %XW HYHQ WKRXJK VWDWLF PHWKRGV UHTXLUH D ELW RI ZRUN WR FUHDWH WKH\ DUH SRZHUIXO DQG XVHIXO WRROV IRU LPSURYLQJ EDVLF -DYD SURJUDPV $IWHU WKH KHDGHU \RX VHH D VHULHV RI SULQWOQ VWDWHPHQWV WKDW PDNH XS WKH ERG\ RI WKLV VWDWLF PHWKRG $V LQ PHWKRG PDLQ WKH VWDWHPHQWV RI WKLV PHWKRG DUH H[HFXWHG LQ RUGHU IURP ILUVW WR ODVW ZKHQ WKH PHWKRG H[HFXWHV %\ GHILQLQJ WKH PHWKRG GUDZ%R[ ZH KDYH JLYHQ D VLPSOH QDPH WR WKLV VHTXHQFH RI SULQWOQ VWDWHPHQWV ,W V DV LI ZH VDLG WR WKH -DYD FRPSLOHU :KHQHYHU , WHOO \RX WR GUDZ%R[ , UHDOO\ PHDQ WKDW \RX VKRXOG H[HFXWH WKRVH SULQWOQ VWDWHPHQWV LQ WKH GUDZ%R[ PHWKRG %XW WKH FRPPDQG ZRQ W DFWXDOO\ EH H[HFXWHG XQOHVV RXU PDLQ PHWKRG H[SOLFLWO\ VD\V WKDW LW ZDQWV WR GR VR 7KH DFW RI H[HFXWLQJ D VWDWLF PHWKRG LV FDOOHG D PHWKRG FDOO 38 0HWKRG &DOO $ FRPPDQG WR H[HFXWH DQRWKHU PHWKRG ZKLFK FDXVHV DOO RI WKH VWDWHPHQWV LQVLGH WKDW PHWKRG WR EH H[HFXWHG )RU WKH GUDZ%R[ PHWKRG D SURJUDP ZRXOG FDOO LW E\ VD\LQJ drawBox(); 6LQFH ZH ZDQW WR H[HFXWH WKH GUDZ%R[ FRPPDQG WZLFH ZH ZDQW WR GUDZ WZR ER[HV WKH PDLQ PHWKRG VKRXOG FRQWDLQ WZR FDOOV WR WKH GUDZ%R[ PHWKRG 7KH IROORZLQJ SURJUDP XVHV WKH GUDZ%R[ PHWKRG WR SURGXFH WKH VDPH RXWSXW DV WKH RULJLQDO 'UDZ%R[HV SURJUDP 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class DrawBoxes2 { public static void main(String args) { drawBox(); System.out.println(); drawBox(); } public static void drawBox() { System.out.println("+------+"); System.out.println("| |"); System.out.println("| |"); System.out.println("+------+"); } } )ORZ RI &RQWURO 7KH PRVW FRQIXVLQJ WKLQJ DERXW VWDWLF PHWKRGV LV WKDW SURJUDPV ZLWK VWDWLF PHWKRGV GR QRW H[HFXWH VHTXHQWLDOO\ IURP WKH WRS WR WKH ERWWRP 5DWKHU HDFK WLPH WKH SURJUDP HQFRXQWHUV D VWDWLF PHWKRG FDOO WKH H[HFXWLRQ RI WKH SURJUDP MXPSV WR WKDW VWDWLF PHWKRG DQG H[HFXWHV HDFK VWDWHPHQW LQ WKDW PHWKRG LQ RUGHU WKHQ MXPSV EDFN WR WKH SRLQW ZKHUH WKH FDOO EHJDQ DQG UHVXPHV H[HFXWLQJ 7KH RUGHU LQ ZKLFK WKH VWDWHPHQWV RI D SURJUDP DUH H[HFXWHG LV FDOOHG WKH SURJUDP V IORZ RI FRQWURO )ORZ RI &RQWURO 7KH RUGHU LQ ZKLFK WKH VWDWHPHQWV RI D -DYD SURJUDP DUH H[HFXWHG /HW V ORRN DW WKH FRQWURO IORZ RI WKH 'UDZ%R[HV SURJUDP VKRZQ SUHYLRXVO\ ,W KDV WZR PHWKRGV 7KH ILUVW PHWKRG LV WKH IDPLOLDU PHWKRG PDLQ DQG WKH VHFRQG LV PHWKRG GUDZ%R[ $V ZLWK DQ\ -DYD SURJUDP H[HFXWLRQ VWDUWV ZLWK WKH PDLQ PHWKRG public static void main(String args) { drawBox(); System.out.println(); drawBox(); } :H H[HFXWH HDFK RI WKH VWDWHPHQWV OLVWHG LQ WKH PDLQ PHWKRG IURP ILUVW WR ODVW 7KHQ ZH UH GRQH ZLWK H[HFXWLRQ 6R LQ D VHQVH WKH H[HFXWLRQ RI WKLV SURJUDP LV VHTXHQWLDO DV DOZD\V 39 %XW WKLV PDLQ PHWKRG LQFOXGHV WZR GLIIHUHQW FDOOV RQ WKH GUDZ%R[ PHWKRG 7KLV SURJUDP LV JRLQJ WR GR WKUHH GLIIHUHQW WKLQJV H[HFXWH GUDZ%R[ H[HFXWH D SULQWOQ H[HFXWH GUDZ%R[ DJDLQ ,W LV RIWHQ XVHIXO WR WUDFH WKH IORZ RI FRQWURO 7KH IROORZLQJ GLDJUDP FDSWXUHV WKH LGHD WKDW ZH DUH H[HFXWLQJ WKH PDLQ PHWKRG ZKLFK LQYROYHV WKUHH GLIIHUHQW VWHSV WZR RI ZKLFK LQYROYH H[HFXWLQJ WKH GUDZ%R[ PHWKRG execute main method: execute drawBox method System.out.println(); execute drawBox method %XW ZKDW KDSSHQV ZKHQ ZH DVN -DYD WR H[HFXWH WKH GUDZ%R[ PHWKRG" :H WUDQVIHU FRQWURO WR WKH PHWKRG DQG H[HFXWH HDFK RI LWV VWDWHPHQWV DQG WKHQ FRPH EDFN WR ZKHUH ZH OHIW RII 6R ZH FDQ H[SDQG WKLV GLDJUDP WR VKRZ ZKDW KDSSHQV ZKHQ ZH PDNH WKH WZR FDOOV RQ WKH GUDZ%R[ PHWKRG execute main method: execute drawBox method: System.out.println("+------+"); System.out.println("| |"); System.out.println("| |"); System.out.println("+------+"); System.out.println(); execute drawBox method: System.out.println("+------+"); System.out.println("| |"); System.out.println("| |"); System.out.println("+------+"); 7KH WUDQVIHU RI FRQWURO WR WKH GUDZ%R[ PHWKRG FDXVHV LWV IRXU SULQWOQ PHWKRGV WR EH H[HFXWHG ILUVW 7KHQ ZH UHWXUQ WR WKH PDLQ PHWKRG DQG H[HFXWH LWV SULQWOQ PHWKRG 7KHQ ZH FDOO GUDZ%R[ DJDLQ DQG H[HFXWH LWV IRXU SULQWOQ VWDWHPHQWV D VHFRQG WLPH 7KH QHW HIIHFW LV WKDW ZH FRXOG LQVWHDG KDYH H[HFXWHG WKH IROORZLQJ PDLQ PHWKRG public static void main(String args) { System.out.println("+------+"); System.out.println("| |"); System.out.println("| |"); System.out.println("+------+"); System.out.println(); System.out.println("+------+"); System.out.println("| |"); System.out.println("| |"); System.out.println("+------+"); } 7KLV YHUVLRQ LV VLPSOHU LQ WHUPV RI LWV IORZ RI FRQWURO EXW WKH RWKHU YHUVLRQ DYRLGV WKH UHGXQGDQF\ RI KDYLQJ WKH VDPH SULQWOQ VWDWHPHQWV DSSHDU PXOWLSOH WLPHV ,W DOVR JLYHV D EHWWHU VHQVH RI WKH VWUXFWXUH RI WKH VROXWLRQ ,Q WKH RULJLQDO YHUVLRQ LW LV FOHDU WKDW WKHUH LV D VXEWDVN WKDW ZH KDYH FDOOHG GUDZ%R[ WKDW LV EHLQJ SHUIRUPHG WZLFH ,Q DGGLWLRQ LW ZRXOG EH HDVLHU WR DGG D WKLUG ER[ WR WKH SURJUDP WKDW XVHV WKH GUDZ%R[ PHWKRG VLQFH WKLV ZRXOG VLPSO\ EH D WKLUG PHWKRG FDOO LQ PDLQ UDWKHU WKDQ KDYLQJ WR DGG IRXU QHZ SULQWOQ VWDWHPHQWV 40 -DYD DOORZV \RX WR GHILQH PHWKRGV LQ DQ\ RUGHU \RX OLNH ,W LV D FRPPRQ FRQYHQWLRQ WR SXW WKH PDLQ PHWKRG HLWKHU DV WKH ILUVW RU ODVW PHWKRG LQ WKH FODVV ,Q WKLV WH[WERRN ZH ZLOO JHQHUDOO\ SXW PDLQ ILUVW EXW WKH SURJUDP ZRXOG EHKDYH WKH VDPH LI ZH VZLWFK WKH RUGHU 0HWKRG PDLQ LV DOZD\V WKH VWDUWLQJ SRLQW IRU SURJUDP H[HFXWLRQ DQG IURP WKDW VWDUWLQJ SRLQW ZH FDQ GHWHUPLQH WKH RUGHU LQ ZKLFK RWKHU PHWKRGV DUH FDOOHG )RU H[DPSOH WKH IROORZLQJ PRGLILHG SURJUDP EHKDYHV LGHQWLFDOO\ WR WKH SUHYLRXV 'UDZ%R[HV SURJUDP 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class DrawBoxes3 { public static void drawBox() { System.out.println("+------+"); System.out.println("| |"); System.out.println("| |"); System.out.println("+------+"); } public static void main(String args) { drawBox(); System.out.println(); drawBox(); } } 0HWKRGV 7KDW &DOO 2WKHU 0HWKRGV 7KH PDLQ PHWKRG LV QRW WKH RQO\ SODFH ZKHUH \RX FDQ FDOO DQRWKHU PHWKRG ,Q IDFW DQ\ PHWKRG PD\ FDOO DQ\ RWKHU PHWKRG $V D UHVXOW WKH IORZ RI FRQWURO FDQ JHW TXLWH FRPSOLFDWHG &RQVLGHU IRU H[DPSOH WKH IROORZLQJ UDWKHU VWUDQJH SURJUDP :H XVH QRQVHQVH ZRUGV IRR EDU ED] DQG PXPEOH RQ SXUSRVH EHFDXVH WKH SURJUDP LV QRW LQWHQGHG WR PDNH VHQVH 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 public class FooBarBazMumble { public static void main(String args) { foo(); bar(); } public static void foo() { System.out.println("foo"); mumble(); System.out.println(); } public static void bar() { System.out.println("bar"); baz(); } public static void baz() { System.out.println("baz"); mumble(); } public static void mumble() { System.out.println("mumble"); } } 41 <RX FDQ W WHOO HDVLO\ ZKDW RXWSXW WKLV SURJUDP SURGXFHV /HW V DJDLQ WUDFH LQ GHWDLO ZKDW WKH SURJUDP LV GRLQJ 5HPHPEHU WKDW -DYD DOZD\V EHJLQV ZLWK WKH PHWKRG FDOOHG main ,Q WKLV SURJUDP WKH main PHWKRG GRHV WZR WKLQJV LW FDOOV WKH foo PHWKRG DQG LW FDOOV WKH bar PHWKRG execute main method: execute foo method execute bar method (DFK RI WKHVH WZR PHWKRG FDOOV LV JRLQJ WR H[SDQG LQWR PRUH VWDWHPHQWV /HW V ILUVW H[SDQG WKH FDOO RQ WKH foo PHWKRG +RZ GR ZH H[HFXWH WKH foo PHWKRG" :H WUDQVIHU FRQWURO WR WKH PHWKRG DQG H[HFXWH HDFK RI LWV VWDWHPHQWV ,W KDV WKUHH GLIIHUHQW VWDWHPHQWV execute main method: execute foo method: System.out.println("foo"); execute mumble method System.out.println(); execute bar method 7KLV KHOSV WR PDNH RXU WUDFH PRUH FRPSOHWH EXW QRWLFH WKDW foo LV FDOOLQJ WKH mumble PHWKRG VR ZH KDYH WR H[SDQG WKDW DV ZHOO :KDW KDSSHQV ZKHQ ZH H[HFXWH WKH mumble PHWKRG WKDW foo FDOOV" :H WUDQVIHU FRQWURO WR LW DQG H[HFXWH LWV VWDWHPHQWV 7KH mumble PHWKRG KDV D VLQJOH SULQWOQ VWDWHPHQW execute main method: execute foo method: System.out.println("foo"); execute mumble method: System.out.println("mumble"); System.out.println(); execute bar method $W WKLV SRLQW ZH KDYH ILQLVKHG H[SDQGLQJ WKH GHWDLOV RI WKH FDOO RQ WKH foo PHWKRG IURP main EXW ZH KDYHQ W \HW ORRNHG DW ZKDW LV LQYROYHG LQ H[HFXWLQJ WKH bar PHWKRG &RQWURO ZRXOG WUDQVIHU WR WKH PHWKRG DQG ZH H[HFXWH HDFK RI LWV VWDWHPHQWV 7KH bar PHWKRG KDV WZR VWDWHPHQWV execute main method: execute foo method: System.out.println("foo"); execute mumble method: System.out.println("mumble"); System.out.println(); execute bar method: System.out.println("bar"); execute baz method :H UH JHWWLQJ FORVHU EXW ZH KDYH WR ILJXUH RXW ZKDW KDSSHQV ZKHQ ZH H[HFXWH WKH baz PHWKRG ,W KDV WZR VWHSV DV ZHOO 42 execute main method: execute foo method: System.out.println("foo"); execute mumble method: System.out.println("mumble"); System.out.println(); execute bar method: System.out.println("bar"); execute baz method: System.out.println("baz"); execute mumble method 7KLV OHDGV WR \HW DQRWKHU PHWKRG FDOO WKDW ZH QHHG WR H[SDQG 7KH FDOO RQ mumble FDXVHV LWV VLQJOH SULQWOQ WR EH H[HFXWHG execute main method: execute foo method: System.out.println("foo"); execute mumble method: System.out.println("mumble"); System.out.println(); execute bar method: System.out.println("bar"); execute baz method: System.out.println("baz"); execute mumble method: System.out.println("mumble"); )LQDOO\ ZH KDYH ILQLVKHG WUDFLQJ WKH H[HFXWLQJ RI WKLV SURJUDP ,W VKRXOG PDNH VHQVH WKHQ WKDW WKH SURJUDP SURGXFHV WKH IROORZLQJ RXWSXW foo mumble bar baz mumble :H ZLOO VHH D PXFK PRUH XVHIXO H[DPSOH RI PHWKRGV FDOOLQJ PHWKRGV ZKHQ ZH JR WKURXJK WKH FDVH VWXG\ DW WKH HQG RI WKH FKDSWHU 'LG <RX .QRZ +DFNHU V 'LFWLRQDU\ &RPSXWHU VFLHQWLVWV DQG FRPSXWHU SURJUDPPHUV XVH D ORW RI MDUJRQ WKDW FDQ EH FRQIXVLQJ WR QRYLFHV $ JURXS RI VRIWZDUH SURIHVVLRQDOV VSHDUKHDGHG E\ (ULF 5D\PRQG FROOHFWHG WRJHWKHU PDQ\ RI WKH MDUJRQ WHUPV LQ D ERRN FDOOHG 7KH 1HZ +DFNHU V 'LFWLRQDU\ <RX FDQ EX\ WKH ERRN EXW \RX FDQ DOVR EURZVH LW RQOLQH DW (ULF V ZHEVLWH http://catb.org/~esr/jargon/html/frames.html )RU H[DPSOH LI \RX ORRN XS IRR \RX OO ILQG D GHILQLWLRQ WKDW VD\V LW LV 8VHG YHU\ JHQHUDOO\ DV D VDPSOH QDPH IRU DEVROXWHO\ DQ\WKLQJ HVS SURJUDPV DQG ILOHV ,Q RWKHU ZRUGV ZKHQ ZH ILQG RXUVHOYHV ORRNLQJ IRU D QRQVHQVH ZRUG ZH XVH IRR 43 7KH +DFNHU V 'LFWLRQDU\ FRQWDLQV D JUHDW GHDO RI KLVWRULFDO LQIRUPDWLRQ DERXW WKH RULJLQV RI MDUJRQ WHUPV 7KH HQWU\ IRU IRR LQFOXGHV D OHQJWK\ GLVFXVVLRQ RI WKH FRPELQHG WHUP IRREDU DQG KRZ LW FDPH LQWR FRPPRQ XVDJH DPRQJ HQJLQHHUV ,I \RX ZDQW WR JHW D IODYRU RI ZKDW LV WKHUH FKHFN RXW WKH HQWULHV IRU EXJ KDFNHU DQG ERJRVLW\ DQG ERJR VRUW $Q ([DPSOH 5XQWLPH (UURU 5XQWLPH HUURUV RFFXU ZKHQ D EXJ FDXVHV \RXU SURJUDP WR EH XQDEOH WR FRQWLQXH H[HFXWLQJ :KDW FRXOG FDXVH VXFK D WKLQJ WR KDSSHQ" 2QH H[DPSOH LV LI \RX DVNHG WKH FRPSXWHU WR FDOFXODWH DQ LQYDOLG YDOXH VXFK DV $QRWKHU H[DPSOH ZRXOG EH LI \RXU SURJUDP WULHV WR UHDG GDWD IURP D ILOH WKDW GRHV QRW H[LVW :H GRQ W NQRZ KRZ WR FRPSXWH YDOXHV RU UHDG ILOHV \HW EXW WKHUH LV D ZD\ ZH FDQ DFFLGHQWDOO\ FDXVH D UXQWLPH HUURU 7KH ZD\ WR GR WKLV LV WR ZULWH D VWDWLF PHWKRG WKDW FDOOV LWVHOI ,I \RX GR WKLV \RXU SURJUDP ZLOO QRW VWRS UXQQLQJ EHFDXVH WKH PHWKRG ZLOO NHHS FDOOLQJ LWVHOI LQGHILQLWHO\ XQWLO WKH FRPSXWHU UXQV RXW RI PHPRU\ :KHQ WKLV KDSSHQV WKH SURJUDP SULQWV D ODUJH QXPEHU RI OLQHV RI RXWSXW EXW HYHQWXDOO\ VWRSV H[HFXWLQJ ZLWK DQ HUURU PHVVDJH FDOOHG D 6WDFN2YHUIORZ(UURU 1 2 3 4 5 6 7 8 9 10 public class Infinite { public static void main(String args) { oops(); } public static void oops() { System.out.println("Make it stop!"); oops(); } } 7KLV LOO IDWHG SURJUDP SURGXFHV WKH IROORZLQJ RXWSXW ZLWK ODUJH JURXSV RI LGHQWLFDO OLQHV UHSUHVHQWHG E\ 44 Make Make Make Make Make Make Make Make Make ... it it it it it it it it it stop! stop! stop! stop! stop! stop! stop! stop! stop! Make it stop! Exception in thread "main" java.lang.StackOverflowError at sun.nio.cs.SingleByteEncoder.encodeArrayLoop(Unknown Source) at sun.nio.cs.SingleByteEncoder.encodeLoop(Unknown Source) at java.nio.charset.CharsetEncoder.encode(Unknown Source) at sun.nio.cs.StreamEncoder$CharsetSE.implWrite(Unknown Source) at sun.nio.cs.StreamEncoder.write(Unknown Source) at java.io.OutputStreamWriter.write(Unknown Source) at java.io.BufferedWriter.flushBuffer(Unknown Source) at java.io.PrintStream.newLine(Unknown Source) at java.io.PrintStream.println(Unknown Source) at Infinite.oops(Infinite.java:7) at Infinite.oops(Infinite.java:8) at Infinite.oops(Infinite.java:8) at Infinite.oops(Infinite.java:8) at ... 5XQWLPH HUURUV DUH VRPHWKLQJ ZH OO XQIRUWXQDWHO\ KDYH WR OLYH ZLWK DV ZH OHDUQ WR SURJUDP :H ZLOO KDYH WR FDUHIXOO\ HQVXUH WKDW RXU SURJUDPV QRW RQO\ FRPSLOH VXFFHVVIXOO\ EXW WKDW WKH\ GR QRW FRQWDLQ DQ\ EXJV WKDW ZLOO FDXVH D UXQWLPH HUURU 7KH PRVW FRPPRQ ZD\ WR FDWFK DQG IL[ UXQWLPH HUURUV LV WR UXQ WKH SURJUDP VHYHUDO WLPHV WR WHVW LWV EHKDYLRU &DVH 6WXG\ 'UDZ)LJXUHV (DUOLHU LQ WKH FKDSWHU ZH VDZ D SURJUDP FDOOHG 'UDZ)LJXUHV WKDW SURGXFHG WKH IROORZLQJ RXWSXW 45 /\ / / \ \ \/ \ \ \/ /\ / / /\ \ / \ +------+ | | | | +------+ |United| |States| +------+ | | | | +------+ /\ / \ / \ / \ \ / / \ \ / / ,W GLG VR ZLWK D ORQJ VHTXHQFH RI SULQWOQ VWDWHPHQWV LQ WKH PDLQ PHWKRG ,Q WKLV VHFWLRQ ZH OO LPSURYH WKH SURJUDP XVLQJ VWDWLF PHWKRGV IRU SURFHGXUDO GHFRPSRVLWLRQ WR FDSWXUH VWUXFWXUH DQG HOLPLQDWH UHGXQGDQF\ 6WUXFWXUHG 9HUVLRQ ,I ZH ORRN FORVHO\ DW WKLV RXWSXW ZH FDQ VHH WKDW WKHUH LV D VWUXFWXUH WR LW WKDW ZRXOG EH GHVLUDEOH WR FDSWXUH LQ RXU SURJUDP VWUXFWXUH 7KH RXWSXW LV GLYLGHG LQWR WKUHH VXEILJXUHV :H FDQ EHWWHU LQGLFDWH WKH VWUXFWXUH RI WKH SURJUDP E\ GLYLGLQJ LW LQWR VWDWLF PHWKRGV 6LQFH WKHUH DUH WKUHH VXEILJXUHV ZH OO FUHDWH WKUHH PHWKRGV RQH IRU HDFK VXEILJXUH 7KH IROORZLQJ SURJUDP SURGXFHV WKH VDPH RXWSXW DV 'UDZ)LJXUHV 46 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 46 public class DrawFigures2 { public static void main(String args) { drawDiamond(); drawX(); drawRocket(); } public static void drawDiamond() { System.out.println(" /\\"); System.out.println(" / \\"); System.out.println(" / \\"); System.out.println(" \\ /"); System.out.println(" \\ /"); System.out.println(" \\/"); System.out.println(); } public static void drawX() { System.out.println(" \\ /"); System.out.println(" \\ /"); System.out.println(" \\/"); System.out.println(" /\\"); System.out.println(" / \\"); System.out.println(" / \\"); System.out.println(); } public static void drawRocket() { System.out.println(" /\\"); System.out.println(" / \\"); System.out.println(" / \\"); System.out.println("+------+"); System.out.println("| |"); System.out.println("| |"); System.out.println("+------+"); System.out.println("|United|"); System.out.println("|States|"); System.out.println("+------+"); System.out.println("| |"); System.out.println("| |"); System.out.println("+------+"); System.out.println(" /\\"); System.out.println(" / \\"); System.out.println(" / \\"); } } 7KH SURJUDP DSSHDUV LQ D FODVV FDOOHG 'UDZ)LJXUHV DQG KDV IRXU VWDWLF PHWKRGV LQFOXGLQJ PDLQ GHILQHG ZLWKLQ LW 7KH ILUVW VWDWLF PHWKRG LV WKH XVXDO PHWKRG PDLQ ZKLFK FDOOV WKUHH PHWKRGV 7KHVH WKUHH PHWKRGV FDOOHG E\ PDLQ DSSHDU QH[W )LQDO 9HUVLRQ ZLWKRXW 5HGXQGDQF\ 2XU SURJUDP FDQ EH IXUWKHU LPSURYHG (DFK RI WKH WKUHH VXEILJXUHV KDV LQGLYLGXDO HOHPHQWV DQG VRPH RI WKRVH HOHPHQWV DSSHDU LQ PRUH WKDQ RQH RI WKH WKUHH VXEILJXUHV 7KH IROORZLQJ LV RQH UHGXQGDQW JURXS RI OLQHV WKDW LV SULQWHG VHYHUDO WLPHV E\ WKH SURJUDP 47 /\ / / \ \ $ EHWWHU YHUVLRQ RI WKH SUHFHGLQJ SURJUDP DGGV DQ DGGLWLRQDO PHWKRG IRU HDFK UHGXQGDQW VHFWLRQ RI RXWSXW 7KH UHGXQGDQW VHFWLRQV DUH WKH XSZDUG GLDPRQG VKDSH WKH GRZQZDUG 9 VKDSH DQG WKH ER[ XVHG LQ WKH URFNHW +HUH LV WKH LPSURYHG 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 46 47 48 public class DrawFigures3 { public static void main(String args) { drawDiamond(); drawX(); drawRocket(); } public static void drawDiamond() { drawCone(); drawV(); System.out.println(); } public static void drawX() { drawV(); drawCone(); System.out.println(); } public static void drawRocket() { drawCone(); drawBox(); System.out.println("|United|"); System.out.println("|States|"); drawBox(); drawCone(); System.out.println(); } public static void drawBox() { System.out.println("+------+"); System.out.println("| |"); System.out.println("| |"); System.out.println("+------+"); } public static void drawCone() { System.out.println(" /\\"); System.out.println(" / \\"); System.out.println(" / \\"); } public static void drawV() { System.out.println(" \\ /"); System.out.println(" \\ /"); System.out.println(" \\/"); } } 48 7KLV SURJUDP QRZ FDOOHG 'UDZ)LJXUHV KDV VHYHQ VWDWLF PHWKRGV LQFOXGLQJ PDLQ GHILQHG ZLWKLQ LW 7KH ILUVW VWDWLF PHWKRG LV WKH XVXDO PHWKRG PDLQ ZKLFK FDOOV WKUHH PHWKRGV 7KHVH WKUHH PHWKRGV LQ WXUQ FDOO WKUHH RWKHU PHWKRGV WKDW DSSHDU QH[W $QDO\VLV RI )ORZ RI ([HFXWLRQ +HUH LV D VWUXFWXUH GLDJUDP WKDW VKRZV ZKLFK VWDWLF PHWKRGV PDLQ FDOOV DQG ZKLFK VWDWLF PHWKRGV DUH FDOOHG E\ HDFK RI WKHP main | +-----------------------------+--------------------------+ | | | | | | +-drawDiamond-+ +----drawX----+ +-drawRocket--+ | | | | | | | | | | | | drawCone drawV drawV drawCone drawCone drawBox $V \RX FDQ VHH WKLV SURJUDP KDV WKUHH OHYHOV RI VWUXFWXUH WZR OHYHOV RI GHFRPSRVLWLRQ 7KH RYHUDOO WDVN LV VSOLW LQWR WKUHH VXEWDVNV $QG HDFK RI WKRVH VXEWDVNV KDV WZR VXEWDVNV $ SURJUDP ZLWK PHWKRGV KDV D PRUH FRPSOH[ IORZ RI FRQWURO WKDQ RQH ZLWKRXW WKHP EXW WKH UXOHV DUH VWLOO IDLUO\ VLPSOH 5HPHPEHU WKDW ZKHQ D PHWKRG LV FDOOHG WKH FRPSXWHU H[HFXWHV WKH VWDWHPHQWV LQ WKH ERG\ RI WKDW PHWKRG 7KHQ FRQWURO SURFHHGV WR WKH VWDWHPHQW DIWHU WKH PHWKRG FDOO $OVR UHPHPEHU WKDW ZH DOZD\V VWDUW ZLWK WKH PDLQ PHWKRG H[HFXWLQJ LWV VWDWHPHQWV IURP ILUVW WR ODVW 6R LQ H[HFXWLQJ SURJUDP 'UDZ)LJXUHV ZH ILUVW H[HFXWH LWV PDLQ PHWKRG 7KDW LQ WXUQ ILUVW H[HFXWHV WKH ERG\ RI PHWKRG GUDZ'LDPRQG 7KHQ GUDZ'LDPRQG H[HFXWHV PHWKRGV GUDZ&RQH DQG GUDZ9 LQ WKDW RUGHU :KHQ GUDZ'LDPRQG ILQLVKHV H[HFXWLQJ FRQWURO VKLIWV WR WKH QH[W VWDWHPHQW LQ WKH ERG\ RI WKH PDLQ SURJUDP WKH FDOO RQ PHWKRG GUDZ; $ FRPSOHWH EUHDNGRZQ RI WKH IORZ RI FRQWURO IURP VWDWLF PHWKRG WR VWDWLF PHWKRG LQ 'UDZ)LJXUHV IROORZV 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th main drawDiamond drawCone drawV drawX drawV drawCone drawRocket drawCone drawBox drawBox drawCone 5HFDOO WKDW WKH RUGHU LQ ZKLFK \RX GHILQH PHWKRGV GRHV QRW KDYH WR SDUDOOHO WKH RUGHU LQ ZKLFK WKH\ DUH H[HFXWHG 7KH RUGHU RI H[HFXWLRQ LV GHWHUPLQHG E\ WKH ERG\ RI PHWKRG PDLQ DQG E\ WKH ERGLHV RI PHWKRGV FDOOHG IURP PDLQ $ VWDWLF PHWKRG GHFODUDWLRQ LV OLNH D GLFWLRQDU\ HQWU\ LW GHILQHV D 49 ZRUG EXW LW GRHV QRW VSHFLI\ KRZ WKH ZRUG ZLOO EH XVHG 7KH ERG\ RI WKLV PDLQ VD\V WR ILUVW H[HFXWH GUDZ'LDPRQG WKHQ GUDZ; WKHQ GUDZ5RFNHW 7KLV LV WKH RUGHU RI H[HFXWLRQ UHJDUGOHVV RI WKH RUGHU WKH\ ZHUH GHILQHG LQ -DYD DOORZV \RX WR GHILQH PHWKRGV LQ DQ\ RUGHU \RX OLNH :H VWDUWHG ZLWK PDLQ DW WKH WRS DQG ZRUNHG RXU ZD\ GRZQ WR ORZHU DQG ORZHU OHYHO PHWKRGV 7KLV LV D SRSXODU DSSURDFK WR WDNH EXW PDQ\ SHRSOH SUHIHU WKH RSSRVLWH KDYLQJ WKH ORZ OHYHO PHWKRGV ILUVW DQG KDYLQJ PDLQ DW WKH HQG -DYD GRHVQ W FDUH ZKDW RUGHU \RX XVH VR \RX FDQ GHFLGH IRU \RXUVHOI DQG GR ZKDW \RX WKLQN LV EHVW &RQVLVWHQF\ LV LPSRUWDQW VR WKDW \RX FDQ ILQG D PHWKRG HDVLO\ ODWHU LQ D ODUJHU SURJUDP ,W LV LPSRUWDQW WR QRWH WKDW WKH SURJUDPV 'UDZ)LJXUH 'UDZ)LJXUH DQG 'UDZ)LJXUH SURGXFH H[DFWO\ WKH VDPH RXWSXW WR WKH FRQVROH :KLOH 'UDZ)LJXUH PD\ EH WKH HDVLHVW SURJUDP IRU D QRYLFH WR UHDG 'UDZ)LJXUH DQG SDUWLFXODUO\ 'UDZ)LJXUH KDYH PDQ\ RWKHU DGYDQWDJHV RYHU LW )RU RQH D ZHOO VWUXFWXUHG VROXWLRQ LV HDVLHU WR FRPSUHKHQG DQG WKH PHWKRGV WKHPVHOYHV EHFRPH D PHDQV RI H[SODLQLQJ D SURJUDP $OVR SURJUDPV ZLWK PHWKRGV DUH PRUH IOH[LEOH PRUH HDVLO\ DGDSWHG WR D VLPLODU EXW GLIIHUHQW WDVN <RX FDQ WDNH WKH VHYHQ PHWKRGV GHILQHG LQ 'UDZ)LJXUHV DQG ZULWH WKH IROORZLQJ QHZ SURJUDP WR SURGXFH D ODUJHU DQG PRUH FRPSOH[ RXWSXW %XLOGLQJ VWDWLF PHWKRGV WR FUHDWH QHZ FRPPDQGV LQFUHDVHV \RXU IOH[LELOLW\ ZLWKRXW DGGLQJ XQQHFHVVDU\ FRPSOLFDWLRQ )RU H[DPSOH ZH FRXOG UHSODFH RXU PDLQ PHWKRG ZLWK D YHUVLRQ WKDW FDOOV RXU PHWKRGV LQ WKH IROORZLQJ QHZ RUGHU :KDW RXWSXW ZRXOG LW SURGXFH" public static void main(String args) { drawCone(); drawCone(); drawRocket(); drawX(); drawRocket(); drawDiamond(); drawBox(); drawDiamond(); drawX(); drawRocket(); } &KDSWHU 6XPPDU\ &RPSXWHUV H[HFXWH VHWV RI LQVWUXFWLRQV FDOOHG SURJUDPV &RPSXWHUV VWRUH LQIRUPDWLRQ LQWHUQDOO\ DV VHTXHQFHV RI V DQG V ELQDU\ QXPEHUV 3URJUDPPLQJ DQG FRPSXWHU VFLHQFH GHDO ZLWK DOJRULWKPV ZKLFK DUH VWHS E\ VWHS GHVFULSWLRQV IRU VROYLQJ SUREOHPV 3URJUDPV DUH WUDQVODWHG IURP WH[W LQWR FRPSXWHU LQVWUXFWLRQV E\ DQRWKHU SURJUDP FDOOHG D FRPSLOHU -DYD V FRPSLOHU WXUQV -DYD SURJUDPV LQWR D VSHFLDO IRUPDW FDOOHG E\WHFRGH ZKLFK LV H[HFXWHG XVLQJ D VSHFLDO SURJUDP FDOOHG WKH -DYD UXQWLPH V\VWHP -DYD LV D PRGHUQ REMHFW RULHQWHG SURJUDPPLQJ ODQJXDJH GHYHORSHG E\ 6XQ 0LFURV\VWHPV WKDW KDV D ODUJH VHW RI OLEUDULHV \RX FDQ XVH WR EXLOG FRPSOH[ SURJUDPV 50 -DYD SURJUDPPHUV W\SLFDOO\ FRPSOHWH WKHLU ZRUN XVLQJ DQ HGLWRU FDOOHG DQ ,QWHJUDWHG 'HYHORSPHQW (QYLURQPHQW ,'( 7KH FRPPDQGV PD\ YDU\ IURP HQYLURQPHQW WR HQYLURQPHQW EXW DOZD\V LQYROYH WKH VDPH WKUHH VWHS SURFHVV W\SH LQ D SURJUDP DV D -DYD FODVV FRPSLOH WKH SURJUDP ILOH UXQ WKH FRPSLOHG YHUVLRQ RI WKH SURJUDP -DYD XVHV D FRPPDQG FDOOHG System.out.println WR GLVSOD\ WH[W RQ WKH FRQVROH VFUHHQ :ULWWHQ ZRUGV LQ D SURJUDP FDQ WDNH GLIIHUHQW PHDQLQJV .H\ZRUGV DUH VSHFLDO ZRUGV WKDW DUH SDUW RI WKH ODQJXDJH ,GHQWLILHUV DUH ZRUGV GHILQHG E\ WKH SURJUDPPHU WR JLYH QDPHV WR HQWLWLHV LQ WKH SURJUDP :RUGV FDQ DOVR EH SXW LQWR VWULQJV ZKLFK DUH SLHFHV RI WH[W WKDW FDQ EH SULQWHG WR WKH FRQVROH -DYD SURJUDPV WKDW XVH SURSHU VSDFLQJ DQG OD\RXW DUH PRUH UHDGDEOH WR SURJUDPPHUV 5HDGDELOLW\ LV DOVR LPSURYHG E\ ZULWLQJ QRWHV FDOOHG FRPPHQWV LQVLGH WKH SURJUDP 7KH -DYD ODQJXDJH KDV D V\QWD[ RU D OHJDO VHW RI FRPPDQGV WKDW FDQ EH XVHG $ -DYD SURJUDP WKDW GRHV QRW IROORZ WKH SURSHU V\QWD[ ZLOO QRW FRPSLOH $ SURJUDP WKDW GRHV FRPSLOH EXW LV ZULWWHQ LQFRUUHFWO\ PD\ VWLOO FRQWDLQ HUURUV FDOOHG H[FHSWLRQV WKDW RFFXU ZKHQ WKH SURJUDP UXQV $ WKLUG NLQG RI HUURUV DUH ORJLF RU LQWHQW HUURUV ZKHQ WKH SURJUDP UXQV EXW GRHV QRW GR ZKDW WKH SURJUDPPHU LQWHQGHG &RPPDQGV LQ SURJUDPV DUH FDOOHG VWDWHPHQWV $ FODVV FDQ JURXS VWDWHPHQWV LQWR ODUJHU FRPPDQGV FDOOHG VWDWLF PHWKRGV 6WDWLF PHWKRGV KHOS WKH SURJUDPPHU JURXS FRGH LQWR UHXVDEOH SLHFHV $Q LPSRUWDQW VWDWLF PHWKRG WKDW PXVW EH SDUW RI HYHU\ SURJUDP LV FDOOHG main &RPSOH[ SURJUDPPLQJ WDVNV VKRXOG EH EURNHQ GRZQ LQWR WKH PDMRU WDVNV WKH FRPSXWHU PXVW SHUIRUP 7KLV SURFHVV LV FDOOHG SURFHGXUDO GHFRPSRVLWLRQ &RUUHFW XVH RI VWDWLF PHWKRGV DLGV SURFHGXUDO GHFRPSRVLWLRQ 6HOI &KHFN 3UREOHPV 6HFWLRQ %DVLF &RPSXWLQJ &RQFHSWV :K\ GR FRPSXWHUV XVH ELQDU\ QXPEHUV" &RQYHUW HDFK RI WKH IROORZLQJ GHFLPDO QXPEHUV LQWR LWV HTXLYDOHQW ELQDU\ QXPEHU :KDW LV WKH GHFLPDO HTXLYDOHQW RI HDFK RI WKH IROORZLQJ ELQDU\ QXPEHUV" 51 ,Q \RXU RZQ ZRUGV GHVFULEH DQ DOJRULWKP IRU EDNLQJ FRRNLHV $VVXPH WKDW \RX KDYH D ODUJH QXPEHU RI KXQJU\ IULHQGV VR \RX OO ZDQW WR SURGXFH VHYHUDO EDWFKHV RI WKHP :KDW LV WKH GLIIHUHQFH EHWZHHQ WKH ILOH MyProgram.java DQG WKH ILOH MyProgram.class" 6HFWLRQ $QG 1RZ -DYD :KLFK RI WKH IROORZLQJ FDQ EH XVHG LQ D -DYD SURJUDP DV LGHQWLILHUV" println 42isTheAnswer first-name for AnnualSalary sum_of_data "hello" _average ABC B4 :KDW LV WKH RXWSXW SURGXFHG IURP WKH IROORZLQJ VWDWHPHQWV" System.out.println("\"Quotes\""); System.out.println("Slashes \\//"); System.out.println("How '\"confounding' \"\\\" it is!"); :KDW LV WKH RXWSXW SURGXFHG IURP WKH IROORZLQJ VWDWHPHQWV" System.out.println("Shaq is 7\'1\""); System.out.println("The string \"\" is an empty message."); System.out.println("\\'\"\""); :KDW LV WKH RXWSXW SURGXFHG IURP WKH IROORZLQJ VWDWHPHQWV" System.out.println("Dear \"DoubleSlash\" magazine,"); System.out.println("\tYour publication confuses me. Is it a"); System.out.println("\\\\ slash or a //// slash that I should use?"); System.out.println("\nSincerely,"); System.out.println("Susan \"Suzy\" Smith"); :KDW VHULHV RI println VWDWHPHQWV ZRXOG SURGXFH WKH IROORZLQJ RXWSXW" "Several slashes are sometimes seen," said Sally. "I've said so myself." See? \ / \\ // \\\ /// :KDW VHULHV RI println VWDWHPHQWV ZRXOG SURGXFH WKH IROORZLQJ RXWSXW" This is a test of your knowledge of "quotes" used in 'string literals.' You're bound to "get it right" if you read the section on ''quotes.'' 6HFWLRQ 3URJUDP (UURUV 1DPH WKH WKUHH HUURUV LQ WKH IROORZLQJ SURJUDP 52 1 2 3 4 5 6 public MyProgram { public static void main(String args) { System.out.println("This is a test") System.out.Println("of the emergency broadcast system."); } } 1DPH WKH IRXU HUURUV LQ WKH IROORZLQJ SURJUDP 1 2 3 4 5 6 public class SecretMessage { public static main(string args) { System.out.println("Speak friend"); System.out.println("and enter); } 1DPH WKH WKUHH HUURUV LQ WKH IROORZLQJ SURJUDP 1 2 3 4 5 6 7 8 9 10 11 public class FamousSpeech public static void main(String) { System.out.println("Four score and seven years ago,"); System.out.println("our fathers brought forth on this continent"); System.out.println("a new nation"); System.out.println("conceived in liberty,"); System.out.println("and dedicated to the"); /* this part should System.out.println("proposition that all"; really say, System.out.println("men are created equal"); "all PEOPLE"! */ } } 6HFWLRQ 3URFHGXUDO 'HFRPSRVLWLRQ :KDW LV WKH RXWSXW RI WKH IROORZLQJ SURJUDP" <RX PD\ ZLVK WR GUDZ D VWUXFWXUH GLDJUDP ILUVW 53 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class Strange { public static void first() { System.out.println("Inside first method"); } public static void second() { System.out.println("Inside second method"); first(); } public static void third() { System.out.println("Inside third method"); first(); second(); } public static void main(String args) { first(); third(); second(); third(); } } :KDW ZRXOG KDYH EHHQ WKH RXWSXW RI WKH SUHFHGLQJ SURJUDP LI WKH third PHWKRG KDG FRQWDLQHG WKH IROORZLQJ VWDWHPHQWV" public static void third() { first(); second(); System.out.println("Inside third method"); } :KDW ZRXOG KDYH EHHQ WKH RXWSXW RI WKH Strange SURJUDP LI WKH main PHWKRG KDG FRQWDLQHG WKH IROORZLQJ VWDWHPHQWV" 8VH WKH RULJLQDO YHUVLRQ RI third QRW WKH PRGLILHG YHUVLRQ IURP WKH PRVW UHFHQW H[HUFLVH public static void main(String args) { second(); first(); second(); third(); } :KDW LV WKH RXWSXW RI WKH IROORZLQJ SURJUDP" <RX PD\ ZLVK WR GUDZ D VWUXFWXUH GLDJUDP ILUVW 54 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class Confusing { public static void method2() { method1(); System.out.println("I am method 2."); } public static void method3() { method2(); System.out.println("I am method 3."); method1(); } public static void method1() { System.out.println("I am method 1."); } public static void main(String args) { method1(); method3(); method2(); method3(); } } :KDW ZRXOG KDYH EHHQ WKH RXWSXW RI WKH SUHFHGLQJ SURJUDP LI WKH method3 PHWKRG KDG FRQWDLQHG WKH IROORZLQJ VWDWHPHQWV" public static void method3() { method1(); method2(); System.out.println("I am method 3."); } :KDW ZRXOG KDYH EHHQ WKH RXWSXW RI WKH Confusing SURJUDP LI WKH main PHWKRG KDG FRQWDLQHG WKH IROORZLQJ VWDWHPHQWV" 8VH WKH RULJLQDO YHUVLRQ RI method3 QRW WKH PRGLILHG YHUVLRQ IURP WKH PRVW UHFHQW H[HUFLVH public static void main(String args) { method2(); method1(); method3(); method2(); } 7KH IROORZLQJ SURJUDP FRQWDLQV DW OHDVW 1 2 3 4 5 6 7 8 9 10 V\QWD[ HUURUV :KDW DUH WKH\" public class LotsOf Errors { public static main(String args) { System.println("Hello, world!); message() } public static void message { System.out println("This program surely cannot"; System.out.println("have any so-called "errors" in it"); } &RQVLGHU WKH IROORZLQJ SURJUDP VDYHG LQWR D ILOH QDPHG ([DPSOH MDYD 55 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class Example { public static void displayRule() { System.out println("The first rule "); System.out.println("of Java Club is,"); System.out.println(); System.out.println("you do not talk about Java Club."); } public static void main(String args) { System.out.println("The rules of Java Club."); displayRule(); displayRule(); } } :KDW ZRXOG KDSSHQ LI HDFK RI WKH IROORZLQJ FKDQJHV ZDV PDGH WR WKH ([DPSOH SURJUDP" 7UHDW HDFK FKDQJH LQGHSHQGHQWO\ RI WKH RWKHUV )RU H[DPSOH QR HIIHFW V\QWD[ HUURU RU GLIIHUHQW SURJUDP RXWSXW FKDQJH OLQH WR public class Demonstration FKDQJH OLQH WR public static void MAIN(String args) { LQVHUW D QHZ OLQH DIWHU OLQH WKDW UHDGV System.out.println(); FKDQJH OLQH WR public static void printMessage() { FKDQJH OLQH WR public static void showMessage() { DQG FKDQJH OLQHV DQG WR showMessage(); UHSODFH OLQHV ZLWK System.out println("The first rule of Java Club is,"); 7KH IROORZLQJ SURJUDP LV OHJDO XQGHU -DYD V V\QWD[ UXOHV EXW LW LV GLIILFXOW WR UHDG EHFDXVH RI LWV OD\RXW DQG ODFN RI FRPPHQWV 5HIRUPDW LW XVLQJ WKH UXOHV JLYHQ LQ WKLV FKDSWHU DQG DGG D FRPPHQW KHDGHU DW WKH WRS RI WKH SURJUDP 1 2 3 4 5 6 7 8 9 public class GiveAdvice{ public static void main(String[ ]args){ System.out.println ( "Programs can be easy or difficult"); System.out.println( "to read, depending upon their format." ); System.out.println() ; System.out.println("Everyone, including yourself, will be");System.out.println ( "happier if you choose to format your");System.out.println("Programs."); } } 7KH IROORZLQJ SURJUDP LV OHJDO XQGHU -DYD V V\QWD[ UXOHV EXW LW LV GLIILFXOW WR UHDG EHFDXVH RI LWV OD\RXW DQG ODFN RI FRPPHQWV 5HIRUPDW LW XVLQJ WKH UXOHV JLYHQ LQ WKLV FKDSWHU DQG DGG D FRPPHQW KHDGHU DW WKH WRS RI WKH SURJUDP 1 2 3 4 5 6 7 8 9 public class Messy{public static void main(Stringargs){message ( ;System.out.println() ; message ( );} message() { System.out.println( "I really wish that" );System.out.println ("I had formatted my source") ;System.out.println("code correctly!");}} ) public static void 56 ([HUFLVHV :ULWH D FRPSOHWH -DYD SURJUDP WKDW SULQWV WKH IROORZLQJ RXWSXW ////////////////////// || Victory is mine! || \\\\\\\\\\\\\\\\\\\\\\ :ULWH D FRPSOHWH -DYD SURJUDP WKDW SULQWV WKH IROORZLQJ RXWSXW \/ \\// \\\/// ///\\\ //\\ /\ :ULWH D FRPSOHWH -DYD SURJUDP WKDW SULQWV WKH IROORZLQJ RXWSXW A well-formed Java program has a main method with { and } braces. A System.out.println statement has ( and ) and usually a String that starts and ends with a " character. (But we type \" instead!) :ULWH D FRPSOHWH -DYD SURJUDP WKDW SULQWV WKH IROORZLQJ RXWSXW What is the difference between a ' and a "? Or between a " and a \"? One is what we see when we're typing our program. The other is what appears on the "console." :ULWH D FRPSOHWH -DYD SURJUDP WKDW SULQWV WKH IROORZLQJ RXWSXW 8VH DW OHDVW RQH VWDWLF PHWKRG EHVLGHV main WR KHOS \RX ////////////////////// || Victory is mine! || \\\\\\\\\\\\\\\\\\\\\\ || Victory is mine! || \\\\\\\\\\\\\\\\\\\\\\ || Victory is mine! || \\\\\\\\\\\\\\\\\\\\\\ || Victory is mine! || \\\\\\\\\\\\\\\\\\\\\\ || Victory is mine! || \\\\\\\\\\\\\\\\\\\\\\ :ULWH D SURJUDP WKDW GLVSOD\V WKH IROORZLQJ RXWSXW _______ \ / \ -"-'-"-'-"\ / \_______/ / 57 0RGLI\ WKH SURJUDP IURP WKH SUHYLRXV H[HUFLVH VR WKDW LW GLVSOD\V WKH IROORZLQJ RXWSXW 8VH VWDWLF PHWKRGV DV DSSURSULDWH _______ / / \ \ \ / \_______/ -"-'-"-'-"_______ / \ / \ \ / \_______/ -"-'-"-'-"\ / \_______/ _______ / \ / \ -"-'-"-'-"\ / \_______/ :ULWH D -DYD SURJUDP WKDW JHQHUDWHV WKH IROORZLQJ RXWSXW 8VH VWDWLF PHWKRGV WR VKRZ VWUXFWXUH DQG HOLPLQDWH UHGXQGDQF\ LQ \RXU VROXWLRQ 1RWH WKDW WKHUH DUH WZR URFNHW VKLSV QH[W WR HDFK RWKHU :KDW UHGXQGDQF\ FDQ \RX HOLPLQDWH XVLQJ VWDWLF PHWKRGV" :KDW UHGXQGDQF\ FDQQRW EH HOLPLQDWHG" /\ \ / \ +------+ | | | | +------+ |United| |States| +------+ | | | | +------+ /\ / \ / \ / / /\ \ / \ +------+ | | | | +------+ |United| |States| +------+ | | | | +------+ /\ / \ / \ :ULWH D -DYD SURJUDP WKDW JHQHUDWHV WKH IROORZLQJ RXWSXW 8VH VWDWLF PHWKRGV WR VKRZ VWUXFWXUH DQG HOLPLQDWH UHGXQGDQF\ LQ \RXU VROXWLRQ 58 ***** ***** * * * * * ***** ***** * * * * * ***** ***** * * * ***** ***** * * * * * :ULWH D SURJUDP WKDW SULQWV WKH IROORZLQJ OLQH RI RXWSXW All work and no play makes Jack a dull boy. WLPHV <RX VKRXOG QRW ZULWH D SURJUDP ZKRVH VRXUFH FRGH LV OLQHV ORQJ XVH PHWKRGV WR VKRUWHQ WKH SURJUDP :KDW LV WKH VKRUWHVW SURJUDP \RX FDQ ZULWH WKDW ZLOO SURGXFH WKH OLQHV RI RXWSXW XVLQJ RQO\ WKH PDWHULDO IURP WKLV FKDSWHU" 3URJUDPPLQJ 3URMHFWV :ULWH D SURJUDP WR VSHOO RXW 0,66,66,33, XVLQJ EORFN OHWWHUV OLNH WKH IROORZLQJ RQH SHU OLQH M M MM MM M M M M M M M M M M M M M IIIII I I I I I IIIII S S SSSSS S PPPPPP P P P P PPPPPP P P P SSSSS S S S SSSSS 6RPHWLPHV ZH ZDQW WR ZULWH VLPLODU OHWWHUV WR GLIIHUHQW SHRSOH )RU H[DPSOH \RX PLJKW ZULWH WR \RXU SDUHQWV WR WHOO WKHP DERXW \RXU FODVVHV \RXU IULHQGV DQG WR DVN IRU PRQH\ \RX PLJKW ZULWH WR D IULHQG DERXW \RXU ORYH OLIH \RXU FODVVHV DQG \RXU KREELHV DQG \RX PLJKW ZULWH WR \RXU EURWKHU DERXW \RXU KREELHV WR DVN IRU PRQH\ DQG \RXU IULHQGV :ULWH D SURJUDP WKDW SULQWV VLPLODU OHWWHUV VXFK DV WKHVH WR WKUHH SHRSOH RI \RXU FKRLFH <RXU OHWWHUV VKRXOG KDYH DW OHDVW RQH FRPPRQ SDUDJUDSK <RXU PDLQ SURJUDP VKRXOG KDYH WKUHH PHWKRG FDOOV RQH IRU HDFK RI WKH SHRSOH WR ZKRP \RX DUH ZULWLQJ 7U\ WR LVRODWH UHSHDWHG WDVNV LQWR PHWKRGV :ULWH D SURJUDP WKDW SURGXFHV DV RXWSXW WKH O\ULFV RI WKH VRQJ 7KHUH :DV $Q 2OG /DG\ 8VH PHWKRGV IRU HDFK YHUVH DQG WKH UHIUDLQ +HUH DUH WKH VRQJ V FRPSOHWH O\ULFV 59 There was an old lady who swallowed a fly. I don't know why she swallowed that fly, Perhaps she'll die. There was an old lady who swallowed a spider, That wriggled and iggled and jiggled inside her. She swallowed the spider to catch the fly, I don't know why she swallowed that fly, Perhaps she'll die. There was an old lady who swallowed a bird, How absurd to swallow a bird. She swallowed the bird to catch the spider, She swallowed the spider to catch the fly, I don't know why she swallowed that fly, Perhaps she'll die. There was an old lady who swallowed a cat, Imagine that to swallow a cat. She swallowed the cat to catch the bird, She swallowed the bird to catch the spider, She swallowed the spider to catch the fly, I don't know why she swallowed that fly, Perhaps she'll die. There was an old lady who swallowed a dog, What a hog to swallow a dog. She swallowed the dog to catch the cat, She swallowed the cat to catch the bird, She swallowed the bird to catch the spider, She swallowed the spider to catch the fly, I don't know why she swallowed that fly, Perhaps she'll die. There was an old lady who swallowed a horse, She died of course. :ULWH D SURJUDP WKDW SURGXFHV DV RXWSXW WKH ZRUGV RI 7KH 7ZHOYH 'D\V RI &KULVWPDV 6WDWLF PHWKRGV VLPSOLI\ WKLV WDVN +HUH DUH WKH ILUVW WZR YHUVHV DQG WKH ODVW YHUVH RI WKH VRQJ 60 On the first day of Christmas, my true love sent to me a partridge in a pear tree. On the second day of Christmas, my true love sent to me two turtle doves, and a partridge in a pear tree. ... On the twelfth day of Christmas, my true love sent to me Twelve drummers drumming, eleven pipers piping, ten lords a-leaping, nine ladies dancing, eight maids a-milking, seven swans a-swimming, six geese a-laying, five gold rings; four calling birds, three French hens, two turtle doves, and a partridge in a pear tree. 6WXDUW 5HJHV 0DUW\ 6WHSS 61 62 ...
View Full Document

Ask a homework question - tutors are online