Chapter 04 - Conditional Execution

Chapter 04- - &KDSWHU&RQGLWLRQDO[HFXWLRQ&RS\ULJKW E 6WXDUW 5HJHV DQG 0DUW 6WHSS 7H[W 3URFHVVLQJ 7KH FKDU 7\SH 6\VWHP RXW SULQWI 0HWKRGV

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 &RQGLWLRQDO ([HFXWLRQ &RS\ULJKW E\ 6WXDUW 5HJHV DQG 0DUW\ 6WHSS 7H[W 3URFHVVLQJ 7KH FKDU 7\SH 6\VWHP RXW SULQWI 0HWKRGV ZLWK &RQGLWLRQDO ([HFXWLRQ 3UHFRQGLWLRQV DQG 3RVWFRQGLWLRQV 7KURZLQJ ([FHSWLRQV 5HYLVLWLQJ 5HWXUQ 9DOXHV &DVH 6WXG\ %RG\ 0DVV ,QGH[ %0, 2QH SHUVRQ 8QVWUXFWXUHG 6ROXWLRQ 7ZR SHUVRQ 8QVWUXFWXUHG 6ROXWLRQ 7ZR SHUVRQ 6WUXFWXUHG 6ROXWLRQ /RRS 7HFKQLTXHV &XPXODWLYH 6XP )HQFHSRVW /RRSV DND ORRS DQG D KDOI LI HOVH 6WDWHPHQWV 5HODWLRQDO 2SHUDWRUV &XPXODWLYH 6XP ZLWK LI )HQFHSRVW ZLWK LI 1HVWHG LI HOVH 6XEWOHWLHV RI &RQGLWLRQDO ([HFXWLRQ 2EMHFW (TXDOLW\ 5RXQGRII (UURUV )DFWRULQJ LI HOVH 6WDWHPHQWV 0LQ 0D[ /RRSV ,QWURGXFWLRQ ,Q WKH ODVW IHZ FKDSWHUV ZH KDYH VHHQ KRZ WR VROYH FRPSOH[ SURJUDPPLQJ WDVNV XVLQJ IRU ORRSV WR UHSHDW FHUWDLQ WDVNV PDQ\ WLPHV :H KDYH EHHQ DEOH WR LQWURGXFH VRPH IOH[LELOLW\ LQWR RXU SURJUDPV WKURXJK WKH XVH RI FODVV FRQVWDQWV DQG WKH DELOLW\ WR UHDG YDOXHV IURP WKH XVHU ZLWK D 6FDQQHU REMHFW 1RZ ZH DUH JRLQJ WR H[SORUH D PXFK PRUH SRZHUIXO WHFKQLTXH IRU ZULWLQJ FRGH WKDW FDQ DGDSW WR GLIIHUHQW VLWXDWLRQV :H DUH JRLQJ WR VWXG\ FRQGLWLRQDO H[HFXWLRQ LQ WKH IRUP RI D FRQWURO VWUXFWXUH NQRZQ DV WKH LI HOVH :LWK LI HOVH VWDWHPHQWV ZH ZLOO EH DEOH WR LQVWUXFW WKH FRPSXWHU WR H[HFXWH GLIIHUHQW OLQHV RI FRGH GHSHQGLQJ XSRQ ZKHWKHU FHUWDLQ FRQGLWLRQV DUH WUXH 7KH LI HOVH VWDWHPHQW OLNH WKH IRU ORRS LV VR SRZHUIXO WKDW \RX ZLOO ZRQGHU KRZ \RX PDQDJHG WR ZULWH SURJUDPV ZLWKRXW LW :H ZLOO DOVR EH H[SDQGLQJ RXU XQGHUVWDQGLQJ RI FRPPRQ SURJUDPPLQJ VLWXDWLRQV 7KH FKDSWHU EHJLQV ZLWK DQ H[SORUDWLRQ RI ORRS WHFKQLTXHV ZH KDYHQ W \HW H[SORUHG DQG LQFOXGHV DQ H[SORUDWLRQ RI WH[W SURFHVVLQJ LVVXHV :H ZLOO DOVR ILQG WKDW DGGLQJ FRQGLWLRQDO H[HFXWLRQ WR RXU UHSHUWRLUH ZLOO UHTXLUH XV WR UHYLVLW WKH LVVXH RI PHWKRGV SDUDPHWHUV DQG UHWXUQ YDOXHV VR WKDW ZH FDQ EHWWHU XQGHUVWDQG VRPH RI WKH ILQH SRLQWV 213 /RRS 7HFKQLTXHV 7KH PRUH \RX SURJUDP WKH PRUH \RX ZLOO ILQG WKDW FHUWDLQ SDWWHUQV HPHUJH %HIRUH ZH GHOYH LQWR FRQGLWLRQDO H[HFXWLRQ ZH DUH JRLQJ WR ORRN DW WZR FRPPRQ ORRS SDWWHUQV WKDW FRPH XS RIWHQ LQ SURJUDPPLQJ FXPXODWLYH VXP DQG IHQFHSRVW ORRSV &XPXODWLYH 6XP :H RIWHQ ZDQW WR ILQG WKH VXP RI D VHULHV RI QXPEHUV <RX FRXOG LPDJLQH GHFODULQJ D GLIIHUHQW YDULDEOH IRU HDFK YDOXH ZH ZDQW WR LQFOXGH EXW WKDW ZRXOG QRW EH SUDFWLFDO ,I \RX KDYH WR DGG RQH KXQGUHG QXPEHUV WRJHWKHU \RX GRQ W ZDQW WR KDYH WR GHFODUH RQH KXQGUHG GLIIHUHQW YDULDEOHV )RUWXQDWHO\ WKHUH LV D VLPSOHU ZD\ 7KH WULFN LV WR NHHS D UXQQLQJ WDOO\ DQG WR SURFHVV RQH QXPEHU DW D WLPH ,I \RX KDYH D YDULDEOH FDOOHG VXP IRU H[DPSOH \RX ZRXOG DGG LQ WKH QH[W QXPEHU E\ VD\LQJ sum = sum + next; RU XVLQJ WKH VKRUWKDQG DVVLJQPHQW RSHUDWRU sum += next; 7KLV VWDWHPHQW VD\V WR WDNH WKH ROG YDOXH RI VXP DGG WKH YDOXH RI D YDULDEOH FDOOHG QH[W DQG VWRUH WKLV DV WKH QHZ YDOXH RI VXP 7KLV RSHUDWLRQ LV SHUIRUPHG IRU HDFK QXPEHU WR EH VXPPHG 7KHUH LV D VOLJKW SUREOHP ZKHQ H[HFXWLQJ WKLV VWDWHPHQW IRU WKH ILUVW QXPEHU EHFDXVH WKHUH LV QR ROG YDOXH RI VXP WKH ILUVW WLPH DURXQG 7R JHW DURXQG WKLV \RX LQLWLDOL]H VXP WR D YDOXH WKDW ZLOO QRW DIIHFW WKH DQVZHU ]HUR +HUH LV D SVHXGRFRGH GHVFULSWLRQ RI WKH FXPXODWLYH VXP DOJRULWKP sum = 0. for (all numbers to sum) { obtain "next". sum += next. } 7R LPSOHPHQW WKLV DOJRULWKP \RX PXVW GHFLGH KRZ PDQ\ WLPHV WR JR WKURXJK WKH ORRS DQG KRZ WR REWDLQ D QH[W YDOXH +HUH LV DQ LQWHUDFWLYH SURJUDP WKDW SURPSWV WKH XVHU IRU KRZ PDQ\ QXPEHUV WR VXP WRJHWKHU DQG IRU WKH QXPEHUV WKHPVHOYHV 214 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 // Finds the sum of a sequence of numbers. import java.util.*; public class ExamineNumbers1 { public static void main(String args) { System.out.println("This program adds a sequence of numbers."); System.out.println(); Scanner console = new Scanner(System.in); System.out.print("How many numbers do you want me to examine? "); int totalNumber = console.nextInt(); double sum = 0.0; for (int i = 1; i <= totalNumber; i++) { System.out.print(" #" + i + "? "); double next = console.nextDouble(); sum += next; } System.out.println(); System.out.println("sum = " + sum); } } 7KH SURJUDP ZLOO H[HFXWH VRPHWKLQJ OLNH WKLV This program adds a sequence of numbers. How many numbers do you want me to examine? 6 #1? 3.2 #2? 4.7 #3? 5.1 #4? 9.0 #5? 2.4 #6? 3.1 sum = 27.5 /HW V WUDFH WKH H[HFXWLRQ LQ GHWDLO %HIRUH ZH HQWHU WKH IRU ORRS ZH LQLWLDOL]H WKH YDULDEOH VXP WR EH +------+ sum | 0.0 | +------+ 2Q WKH ILUVW H[HFXWLRQ RI WKH IRU ORRS ZH UHDG LQ D YDOXH RI WKH VXP +------+ sum | 3.2 | +------+ +-----+ next | 3.2 | +-----+ IURP WKH XVHU DQG DGG WKLV YDOXH WR 7KH VHFRQG WLPH WKURXJK WKH ORRS ZH UHDG LQ D YDOXH RI DQG DGG WKLV LQWR RXU VXP 215 +------+ sum | 7.9 | +------+ +-----+ next | 4.7 | +-----+ 1RWLFH WKDW WKH VXP QRW LQFOXGHV ERWK RI WKH QXPEHUV HQWHUHG E\ WKH XVHU EHFDXVH ZH KDYH DGGHG WKH QHZ YDOXH WR WKH ROG YDOXH RI 7KH WKLUG WLPH WKURXJK WKH ORRS ZH DGG LQ WKH YDOXH +------+ sum | 13.0 | +------+ +-----+ next | 5.1 | +-----+ 1RWLFH WKDW WKH YDULDEOH VXP QRZ FRQWDLQV WKH VXP RI WKH ILUVW WKUHH QXPEHUV ZH UHDG LQ DQG DGG LW WR WKH VXP +------+ sum | 22.0 | +------+ +-----+ next | 9.0 | +-----+ 7KHQ 7KHQ ZH DGG LQ WKH ILIWK YDOXH RI +------+ sum | 24.4 | +------+ +-----+ next | 2.4 | +-----+ $QG ILQDOO\ DGG LQ WKH VL[WK YDOXH RI +------+ sum | 27.5 | +------+ +-----+ next | 3.1 | +-----+ :H WKHQ H[LW WKH IRU ORRS DQG SULQW WKH YDOXH RI VXP 7KHUH LV DQ LQWHUHVWLQJ VFRSH LVVXH LQ WKLV SDUWLFXODU SURJUDP 1RWLFH WKDW WKH YDULDEOH VXP LV GHFODUHG RXWVLGH WKH ORRS ZKLOH WKH YDULDEOH QH[W LV GHFODUHG LQVLGH WKH ORRS :H KDYH QR FKRLFH EXW WR GHFODUH VXP RXWVLGH WKH ORRS EHFDXVH LW QHHGV WR EH LQLWLDOL]HG DQG LW LV XVHG DIWHU WKH ORRS %XW WKH YDULDEOH QH[W LV XVHG RQO\ LQVLGH WKH ORRS VR LW FDQ EH GHFODUHG LQ WKDW LQQHU VFRSH ,W LV EHVW WR GHFODUH YDULDEOHV LQ WKH LQQHUPRVW VFRSH SRVVLEOH 7KH FXPXODWLYH VXP DOJRULWKP DQG YDULDWLRQV RQ LW ZLOO EH XVHIXO LQ PDQ\ RI WKH SURJUDPPLQJ WDVNV \RX VROYH +RZ ZRXOG \RX GR D FXPXODWLYH SURGXFW IRU H[DPSOH" +HUH LV WKH SVHXGRFRGH product = 1. for (all numbers to multiply) { obtain "next". product *= next. } )HQFHSRVW /RRSV DND ORRS DQG D KDOI $QRWKHU FRPPRQ SURJUDPPLQJ SUREOHP LQYROYHV D SDUWLFXODU NLQG RI ORRS NQRZQ DV D IHQFHSRVW ORRS &RQVLGHU WKH IROORZLQJ SUREOHP <RX ZDQW WR SXW XS D IHQFH WKDW LV \DUGV ORQJ DQG \RX ZDQW WR KDYH D SRVW HYHU\ \DUGV +RZ PDQ\ SRVWV GR \RX QHHG" ,I \RX GR D TXLFN GLYLVLRQ LQ \RXU KHDG \RX PLJKW VD\ WKDW \RX QHHG SRVWV EXW DFWXDOO\ \RX QHHG SRVWV 7KDW V EHFDXVH IHQFHV EHJLQ DQG HQG ZLWK SRVWV ,Q RWKHU ZRUGV WKH IHQFH ORRNV OLNH WKLV 216 %HFDXVH \RX ZDQW SRVWV RQ ERWK WKH IDU OHIW DQG WKH IDU ULJKW \RX FDQ W XVH WKH IROORZLQJ VLPSOH ORRS EHFDXVH LW GRHVQ W SODQW WKH ILQDO SRVW for (the length of the fence) { plant a post. attach some wire. } ,I \RX XVH WKH SUHFHGLQJ ORRS \RX OO JHW D IHQFH WKDW ORRNV OLNH WKLV 6ZLWFKLQJ WKH RUGHU RI WKH WZR RSHUDWLRQV GRHVQ W KHOS EHFDXVH \RX PLVV WKH ILUVW SRVW 7KH SUREOHP ZLWK WKLV ORRS LV WKDW LW SURGXFHV WKH VDPH QXPEHU RI SRVWV DV VHFWLRQV RI ZLUH EXW ZH NQRZ ZH QHHG DQ H[WUD SRVW 7KDW V ZK\ WKLV SUREOHP LV DOVR VRPHWLPHV UHIHUUHG WR DV WKH ORRS DQG D KDOI SUREOHP EHFDXVH ZH ZDQW WR H[HFXWH RQH KDOI RI WKLV ORRS SODQWLQJ D SRVW RQH H[WUD WLPH 2QH VROXWLRQ LV WR SODQW RQH RI WKH SRVWV HLWKHU EHIRUH RU DIWHU WKH ORRS 7KH XVXDO VROXWLRQ LV WR GR LW EHIRUH plant a post. for (the length of the fence) { attach some wire. plant a post. } 1RWLFH WKDW WKH RUGHU RI WKH WZR RSHUDWLRQV LQ WKH ERG\ RI WKH ORRS LV QRZ UHYHUVHG EHFDXVH WKH LQLWLDO SRVW LV SODQWHG EHIRUH \RX HQWHU WKH ORRS $V D VLPSOH H[DPSOH FRQVLGHU WKH SUREOHP RI ZULWLQJ RXW WKH LQWHJHUV EHWZHHQ E\ FRPPDV ,Q RWKHU ZRUGV ZH ZDQW WR JHW WKLV RXWSXW 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 DQG VHSDUDWHG 7KLV LV D FODVVLF IHQFHSRVW SUREOHP EHFDXVH ZH ZDQW WR ZULWH RXW QXPEHUV EXW RQO\ FRPPDV ,Q RXU IHQFHSRVW WHUPLQRORJ\ ZULWLQJ D QXPEHU LV WKH SRVW SDUW RI WKH WDVN DQG ZULWLQJ D FRPPD LV WKH ZLUH SDUW 6R LPSOHPHQWLQJ WKH SVHXGRFRGH DERYH ZH SULQW WKH ILUVW QXPEHU EHIRUH WKH ORRS 217 System.out.print(1); for (int i = 2; i <= 10; i++) { System.out.print(", " + i); } System.out.println(); LI HOVH 6WDWHPHQWV <RX ZLOO RIWHQ ILQG \RXUVHOI ZULWLQJ FRGH WKDW \RX ZDQW WR H[HFXWH VRPH RI WKH WLPH EXW QRW DOO RI WKH WLPH )RU H[DPSOH LI \RX DUH ZULWLQJ D JDPH SOD\LQJ SURJUDP DQG D QHZ KLJK VFRUH KDV EHHQ UHDFKHG \RX PLJKW ZDQW WR SULQW D PHVVDJH DQG UHPHPEHU WKH QHZ KLJK VFRUH <RX FDQ DFFRPSOLVK WKLV E\ SXWWLQJ WKH WZR OLQHV RI FRGH LQVLGH DQ LI VWDWHPHQW if (currentScore > maxScore) { System.out.println("A new high score!"); maxScore = currentScore; } 7KH LGHD LV WKDW ZH VRPHWLPHV ZDQW WR H[HFXWH WKH WZR OLQHV RI FRGH LQVLGH WKH LI EXW QRW DOZD\V 7KH WHVW LQ SDUHQWKHVHV GHWHUPLQHV ZKHWKHU RU QRW WKH VWDWHPHQWV LQVLGH WKH LI DUH H[HFXWHG ,Q RWKHU ZRUGV WKH WHVW GHVFULEHV WKH FRQGLWLRQV XQGHU ZKLFK ZH ZDQW WR H[HFXWH WKH FRGH 7KH JHQHUDO IRUP RI WKH LI VWDWHPHQW LV DV IROORZV if (<test>) { <statement>; <statement>; ... <statement>; } 7KH LI VWDWHPHQW OLNH WKH IRU ORRS LV D FRQWURO VWUXFWXUH 1RWLFH WKDW ZH RQFH DJDLQ VHH D -DYD NH\ZRUG LI IROORZHG E\ SDUHQWKHVHV DQG D VHW RI FXUO\ EUDFHV HQFORVLQJ D VHULHV RI FRQWUROOHG VWDWHPHQWV 7KH GLDJUDP EHORZ LQGLFDWHV WKH IORZ RI FRQWURO IRU WKH VLPSOH LI VWDWHPHQW 7KH FRPSXWHU SHUIRUPV WKH WHVW DQG LI LW HYDOXDWHV WR WUXH WKH FRPSXWHU H[HFXWHV WKH FRQWUROOHG VWDWHPHQWV ,I WKH WHVW HYDOXDWHV WR IDOVH WKH FRPSXWHU VNLSV WKH FRQWUROOHG VWDWHPHQWV 218 :H XVH WKH VLPSOH LI VWDWHPHQW ZKHQ ZH KDYH FRGH WKDW ZH VRPHWLPHV ZDQW WR H[HFXWH DQG VRPHWLPHV ZDQW WR VNLS -DYD KDV D YDULDWLRQ NQRZQ DV WKH LI HOVH VWDWHPHQW WKDW DOORZV XV WR FKRRVH EHWZHHQ WZR DOWHUQDWLYHV 6XSSRVH IRU H[DPSOH WKDW ZH ZDQW WR VHW D YDULDEOH FDOOHG DQVZHU WR WKH VTXDUH URRW RI D QXPEHU answer = Math.sqrt(number); :H KDYH D SRWHQWLDO SUREOHP LQ WKDW ZH GRQ W ZDQW WR DVN IRU WKH VTXDUH URRW LI WKH QXPEHU LV QHJDWLYH :H FRXOG XVH D VLPSOH LI VWDWHPHQW WR DYRLG WKH SUREOHP if (number >= 0) { answer = Math.sqrt(number); } 7KLV ZLOO DYRLG DVNLQJ IRU WKH VTXDUH URRW RI D QHJDWLYH QXPEHU EXW WKHQ ZKDW YDOXH ZLOO DQVZHU KDYH LI QXPEHU LV QHJDWLYH" 7KLV LV D FDVH ZKHUH ZH ZRXOG SUREDEO\ ZDQW WR JLYH D YDOXH WR DQVZHU HLWKHU ZD\ 6XSSRVH ZH ZDQW DQVZHU WR EH ZKHQ QXPEHU LV QHJDWLYH :H FDQ H[SUHVV WKLV SDLU RI DOWHUQDWLYHV ZLWK DQ LI HOVH VWDWHPHQW if (number >= 0) { answer = Math.sqrt(number); } else { answer = -1; } 7KH LGHD EHKLQG WKH LI HOVH LV WKDW ZH FKRRVH EHWZHHQ WZR DOWHUQDWLYHV DQG H[HFXWH RQH RU WKH RWKHU 6R LQ WKH FRGH DERYH ZH NQRZ WKDW DQVZHU ZLOO EH DVVLJQHG D YDOXH RQH ZD\ RU WKH RWKHU 7KH JHQHUDO IRUP RI WKH LI HOVH VWDWHPHQW LV DV IROORZV 219 if (<test>) { <statement>; <statement>; ... <statement>; } else { <statement>; ... <statement>; <statement>; } 7KLV FRQWURO VWUXFWXUH LV XQXVXDO LQ WKDW LW KDV WZR VHWV RI FRQWUROOHG VWDWHPHQWV DQG WZR GLIIHUHQW NH\ZRUGV LI DQG HOVH 7KH GLDJUDP EHORZ LQGLFDWHV WKH IORZ RI FRQWURO 7KH FRPSXWHU SHUIRUPV WKH WHVW DQG GHSHQGLQJ XSRQ ZKHWKHU LW HYDOXDWHV WR WUXH RU IDOVH WKH FRPSXWHU H[HFXWHV RQH RU WKH RWKHU JURXSV RI VWDWHPHQWV 7KXV WKH LI HOVH FRQWUROV WZR GLIIHUHQW VHWV RI VWDWHPHQWV RQH WR EH H[HFXWHG ZKHQ WKH WHVW HYDOXDWHV WR WUXH DQG WKH RWKHU WR EH H[HFXWHG ZKHQ WKH WHVW HYDOXDWHV WR IDOVH $V ZLWK WKH IRU ORRS LI \RX KDYH D VLQJOH VWDWHPHQW WR H[HFXWH WKHQ \RX GRQ W QHHG WR LQFOXGH WKH FXUO\ EUDFHV %XW WKH 6XQ FRQYHQWLRQ LV WR LQFOXGH WKH FXUO\ EUDFHV HYHQ LI \RX GRQ W QHHG WKHP DQG ZH IROORZ WKDW FRQYHQWLRQ LQ WKLV ERRN 5HODWLRQDO 2SHUDWRUV 7KH LI HOVH VWDWHPHQWV DUH FRQWUROOHG E\ D WHVW 6LPSOH WHVWV FRPSDUH WZR H[SUHVVLRQV WR VHH LI WKH\ DUH UHODWHG LQ VRPH ZD\ 6XFK D WHVW LV LWVHOI DQ H[SUHVVLRQ WKDW UHWXUQV HLWKHU WUXH RU IDOVH DQG LV RI WKH IROORZLQJ IRUP <expression> <relational operator> <expression> 220 7R HYDOXDWH VXFK D WHVW \RX ILUVW HYDOXDWH WKH WZR H[SUHVVLRQV DQG WKHQ VHH LI WKH JLYHQ UHODWLRQ KROGV EHWZHHQ WKH YDOXH RQ WKH OHIW DQG WKH YDOXH RQ WKH ULJKW ,I WKH UHODWLRQ GRHV KROG WKH WHVW HYDOXDWHV WR WUXH ,I QRW WKH WHVW HYDOXDWHV WR IDOVH 7KH UHODWLRQDO RSHUDWRUV DUH 5HODWLRQDO 2SHUDWRUV 2SHUDWRU == != < > <= >= 0HDQLQJ ([DPSOH 9DOXH WUXH WUXH IDOVH WUXH IDOVH WUXH 7KLV LV WR GLVWLQJXLVK 2 + 2 == 4 HTXDOV 3.2 != 4.1 QRW HTXDOV 4 < 3 OHVV WKDQ 4 > 3 JUHDWHU WKDQ 2 <= 0 OHVV WKDQ RU HTXDOV JUHDWHU WKDQ RU HTXDOV 2.4 >= 1.6 1RWLFH WKDW WKH WHVW IRU HTXDOLW\ LQYROYHV WZR HTXDOV FKDUDFWHUV LQ D URZ LW IURP WKH DVVLJQPHQW RSHUDWRU %HFDXVH ZH XVH WKH UHODWLRQDO RSHUDWRUV DV D QHZ ZD\ RI IRUPLQJ H[SUHVVLRQV ZH PXVW UHFRQVLGHU SUHFHGHQFH %HORZ LV DQ XSGDWHG YHUVLRQ RI WKH SUHFHGHQFH WDEOH WKDW LQFOXGHV WKHVH QHZ RSHUDWRUV <RX ZLOO VHH WKDW WHFKQLFDOO\ WKH HTXDOLW\ FRPSDULVRQV DUH FRQVLGHUHG DW D VOLJKWO\ GLIIHUHQW OHYHO RI SUHFHGHQFH WKDQ WKH RWKHU UHODWLRQDO RSHUDWRUV EXW ERWK VHWV RI RSHUDWRUV KDYH D ORZHU SUHFHGHQFH WKDQ WKH DULWKPHWLF RSHUDWRUV -DYD 2SHUDWRU 3UHFHGHQFH 'HVFULSWLRQ 2SHUDWRUV ++, --, +, XQDU\ RSHUDWRUV *, /, % PXOWLSOLFDWLYH RSHUDWRUV +, DGGLWLYH RSHUDWRUV <, >, <=, >= UHODWLRQDO RSHUDWRUV ==, != HTXDOLW\ RSHUDWRUV DVVLJQPHQW RSHUDWRUV =, +=, -=, *=, /=, %= 7KH IROORZLQJ H[SUHVVLRQ LV PDGH XS RI WKH FRQVWDQWV HTXDOV 3 + 2 * 2 == 9 DQG WKH RSHUDWLRQV SOXV WLPHV DQG :KLFK RI WKH RSHUDWLRQV LV SHUIRUPHG ILUVW" %HFDXVH WKH UHODWLRQDO RSHUDWRUV KDYH D ORZHU OHYHO RI SUHFHGHQFH WKDQ WKH DULWKPHWLF RSHUDWRUV WKH DQVZHU LV WKDW WKH WLPHV LV SHUIRUPHG ILUVW WKHQ WKH SOXV WKHQ WKH HTXDOV WHVW ,Q RWKHU ZRUGV -DYD ZLOO SHUIRUP DOO RI WKH PDWK ILUVW EHIRUH LW WHVWV IRU RQH RI WKHVH UHODWLRQVKLSV 7KLV SUHFHGHQFH VFKHPH IUHHV \RX IURP SDUHQWKHVL]LQJ WKH OHIW DQG ULJKW VLGHV RI D WHVW XVLQJ D UHODWLRQDO RSHUDWRU 8VLQJ WKHVH SUHFHGHQFH UXOHV WKH H[SUHVVLRQ DERYH LV HYDOXDWHG DV IROORZV 221 3 + 2 * 2 == 9 \---/ 3 + 4 == 9 \-----/ 7 == 9 \---------/ false <RX FDQ SXW DUELWUDU\ H[SUHVVLRQV RQ HLWKHU VLGH RI WKH UHODWLRQDO RSHUDWRU DV ORQJ DV WKH\ DUH RI D FRPSDWLEOH W\SH +HUH LV D WHVW ZLWK FRPSOH[ H[SUHVVLRQV RQ HLWKHU VLGH (2 - 3 * 8) / (435 % (7 * 2)) <= 3.8 - 4.5 / (2.2 * 3.8) 2QH OLPLWDWLRQ RI WKHVH RSHUDWRUV LV WKDW WKH\ VKRXOG RQO\ EH XVHG ZLWK SULPLWLYH GDWD /DWHU LQ WKLV FKDSWHU ZH ZLOO WDON DERXW KRZ WR FRPSDUH REMHFWV IRU HTXDOLW\ DQG LQ D ODWHU FKDSWHU ZH OO GLVFXVV KRZ WR SHUIRUP OHVV WKDQ DQG JUHDWHU WKDQ FRPSDULVRQV RQ REMHFWV &XPXODWLYH 6XP ZLWK LI /HW V QRZ VHH KRZ WKH XVH RI LI HOVH VWDWHPHQWV FDQ DOORZ XV WR FUHDWH VRPH LQWHUHVWLQJ YDULDWLRQV RQ WKH FXPXODWLYH VXP DOJRULWKP 6XSSRVH WKDW ZH ZDQW WR UHDG D VHTXHQFH RI QXPEHUV DQG FRPSXWH WKH DYHUDJH 7KLV VHHPV OLNH D VWUDLJKWIRUZDUG YDULDWLRQ RI RXU FXPXODWLYH VXP FRGH :H FDQ FRPSXWH WKH DYHUDJH DV WKH VXP GLYLGHG E\ WKH QXPEHU RI QXPEHUV DV LQ System.out.println("average = " + sum / totalNumber); 7KHUH LV RQH PLQRU SUREOHP ZLWK WKLV FRGH 6XSSRVH WKDW ZKHQ ZH DVN WKH XVHU KRZ PDQ\ QXPEHUV WR SURFHVV WKH XVHU VD\V WR SURFHVV QXPEHUV 7KDW ZRXOG PHDQ WKDW ZH QHYHU HQWHU RXU FXPXODWLYH VXP ORRS DQG ZH WU\ WR FRPSXWH WKH YDOXH RI GLYLGHG E\ -DYD ZRXOG SULQW RXW WKDW WKH DYHUDJH LV 1D1 7KLV FU\SWLF PHVVDJH LV VKRUW IRU 1RW D 1XPEHU ,W ZRXOG EH EHWWHU WR SULQW RXW VRPH RWKHU NLQG RI PHVVDJH WKDW ZRXOG LQGLFDWH WKDW WKHUH ZHUHQ W DQ\ QXPEHUV WR DYHUDJH :H FDQ XVH DQ LI HOVH IRU WKLV SXUSRVH if (totalNumber <= 0) { System.out.println("No numbers to average"); } else { System.out.println("average = " + sum / totalNumber); } :H FDQ ILW LQ DQRWKHU XVH RI LI VWDWHPHQWV E\ FRXQWLQJ KRZ PDQ\ QHJDWLYH QXPEHUV DUH HQWHUHG E\ WKH XVHU <RX ZLOO RIWHQ ILQG \RXUVHOI ZDQWLQJ WR FRXQW KRZ PDQ\ WLPHV VRPHWKLQJ RFFXUV LQ D SURJUDP 7KLV JRDO LV HDV\ WR DFFRPSOLVK ZLWK DQ LI VWDWHPHQW DQG DQ LQWHJHU YDULDEOH FDOOHG D FRXQWHU <RX VWDUW E\ LQLWLDOL]LQJ WKH FRXQWHU WR int negatives = 0; <RX FDQ XVH DQ\ QDPH \RX ZDQW IRU WKH YDULDEOH +HUH ZH XVHG WKH QDPH QHJDWLYHV EHFDXVH WKDW LV ZKDW ZH UH FRXQWLQJ 7KH RWKHU HVVHQWLDO LQJUHGLHQW LV WR LQFUHPHQW WKH FRXQWHU LQVLGH WKH ORRS LI LW SDVVHV WKH WHVW ZH UH LQWHUHVWHG LQ 222 if (next < 0) { negatives++; } 3XWWLQJ WKLV DOO WRJHWKHU DQG PRGLI\LQJ WKH FRPPHQWV DQG LQWURGXFWLRQ ZH HQG XS ZLWK WKH IROORZLQJ YDULDWLRQ RI WKH FXPXODWLYH VXP 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 // Finds the average of a sequence of numbers as well as reporting // how many of the numbers were negative. import java.util.*; public class ExamineNumbers2 { public static void main(String args) { System.out.println("This program examines a sequence of numbers to"); System.out.println("find the average as well as counting how many"); System.out.println("are negative."); System.out.println(); Scanner console = new Scanner(System.in); System.out.print("How many numbers do you want me to examine? "); int totalNumber = console.nextInt(); int negatives = 0; double sum = 0.0; for (int i = 1; i <= totalNumber; i++) { System.out.print(" #" + i + "? "); double next = console.nextDouble(); sum += next; if (next < 0) { negatives++; } } System.out.println(); if (totalNumber <= 0) { System.out.println("No numbers to average"); } else { System.out.println("average = " + sum / totalNumber); } System.out.println("# of negatives = " + negatives); } } 7KH SURJUDP ZLOO H[HFXWH VRPHWKLQJ OLNH WKLV 223 This program examines a sequence of numbers to find the average as well as counting how many are negative. How many numbers do you want me to examine? 8 #1? 2.5 #2? 9.2 #3? -19.4 #4? 208.2 #5? 42.3 #6? 92.7 #7? -17.4 #8? 8 average = 40.7625 # of negatives = 2 )HQFHSRVW ZLWK LI 0DQ\ RI WKH IHQFHSRVW ORRSV WKDW \RX ZULWH ZLOO UHTXLUH FRQGLWLRQDO H[HFXWLRQ ,Q IDFW WKH IHQFHSRVW SUREOHP LWVHOI FDQ EH VROYHG ZLWK DQ LI VWDWHPHQW 5HPHPEHU WKDW WKH FODVVLF VROXWLRQ WR WKH IHQFHSRVW LV WR KDQGOH WKH ILUVW SRVW EHIRUH WKH ORRS EHJLQV plant a post. for (the length of the fence) { attach some wire. plant a post. } 7KLV VROXWLRQ VROYHV WKH SUREOHP EXW LW FDQ EH FRQIXVLQJ EHFDXVH LQVLGH WKH ORRS ZH GR WKLQJV LQ UHYHUVH RUGHU :LWK DQ LI VWDWHPHQW ZH FDQ NHHS WKH RULJLQDO RUGHU RI WKH VWHSV for (the length of the fence) { plant a post. if (this isn't the last post) { attach some wire. } } 7KLV YDULDWLRQ LVQ W XVHG DV RIWHQ DV WKH FODVVLF VROXWLRQ EHFDXVH LW LQYROYHV D GRXEOH WHVW :H KDYH D ORRS WHVW DQG ZH KDYH D WHVW LQVLGH WKH ORRS 2IWHQ WKHVH DUH QHDUO\ WKH VDPH WHVW VR LW LV LQHIILFLHQW WR WHVW WKH VDPH WKLQJ WZLFH HDFK WLPH WKURXJK WKH ORRS %XW WKHUH ZLOO EH VLWXDWLRQV ZKHUH \RX PLJKW XVH WKLV DSSURDFK )RU H[DPSOH LQ WKH FODVVLF DSSURDFK ZH UHSHDW ZKDWHYHU OLQHV RI FRGH FRUUHVSRQG WR SODQWLQJ D SRVW ,I WKHUH LV D ORW RI FRGH LQYROYHG ZH PLJKW GHFLGH WKDW WKH LI LQVLGH WKH ORRS LV D EHWWHU DSSURDFK HYHQ LI LW OHDGV WR VRPH H[WUD WHVWLQJ $V DQ H[DPSOH OHW V FRQVLGHU ZULWLQJ D PHWKRG FDOOHG PXOWLSULQW WKDW ZLOO SULQW D VWULQJ D SDUWLFXODU QXPEHU RI WLPHV 6XSSRVH WKDW ZH ZDQW WKH RXWSXW RQ D OLQH E\ LWVHOI LQVLGH VTXDUH EUDFNHWV DQG VHSDUDWHG E\ FRPPDV %HORZ DUH WZR H[DPSOH FDOOV multiprint("please", 4); multiprint("beetlejuice", 3); 224 :H H[SHFW WKHVH FDOOV WR SURGXFH WKH IROORZLQJ RXWSXW [please, please, please, please] [beetlejuice, beetlejuice, beetlejuice] ,I ZH GRQ W WKLQN DERXW WKH IHQFHSRVW ZH FDQ ZULWH D VLPSOH ORRS WKDW SULQWV VTXDUH EUDFNHWV RXWVLGH WKH ORRS DQG WKDW SULQWV WKH VWULQJ ZLWK D FRPPD LQVLGH WKH ORRS public static void multiprint(String s, int times) { System.out.print("["); for (int i = 1; i <= times; i++) { System.out.print(s + ", "); } System.out.println("]"); } 8QIRUWXQDWHO\ WKLV FRGH SURGXFHV DQ H[WUDQHRXV FRPPD DIWHU WKH ODVW YDOXH [please, please, please, please, ] [beetlejuice, beetlejuice, beetlejuice, ] %HFDXVH WKH FRPPDV DUH VHSDUDWRUV ZH ZDQW WR SULQW RQH PRUH VWULQJ WKDQ FRPPD H J WZR FRPPDV WR VHSDUDWH WKH WKUHH RFFXUUHQFHV RI EHHWOHMXLFH :H FDQ XVH WKH FODVVLF VROXWLRQ WR WKH IHQFHSRVW SUREOHP WR JHW WKLV EHKDYLRU E\ SULQWLQJ RQH VWULQJ RXWVLGH WKH ORRS DQG UHYHUVLQJ WKH RUGHU RI WKH SULQWLQJ LQVLGH WKH ORRS public static void multiprint(String s, int times) { System.out.print("[" + s); for (int i = 2; i <= times; i++) { System.out.print(", " + s); } System.out.println("]"); } 1RWLFH WKDW EHFDXVH ZH DUH SULQWLQJ RQH RI WKH VWULQJV EHIRUH WKH ORRS EHJLQV ZH KDYH WR PRGLI\ WKH ORRS VR WKDW LW ZRQ W SULQW DV PDQ\ VWULQJV DV LW GLG EHIRUH :H KDYH DGMXVWHG WKH ORRS YDULDEOH L WR VWDUW DW WR DFFRXQW IRU WKH ILUVW YDOXH WKDW LV SULQWHG EHIRUH WKH ORRS 8QIRUWXQDWHO\ WKLV VROXWLRQ GRHV QRW TXLWH ZRUN SURSHUO\ 7KLQN RI ZKDW KDSSHQV ZKHQ ZH DVN WKH PHWKRG WR SULQW D VWULQJ ]HUR WLPHV DV LQ multiprint("please don't", 0); 7KLV FDOO SURGXFHV WKH IROORZLQJ LQFRUUHFW RXWSXW [please don't] ,W VKRXOG EH SRVVLEOH WR UHTXHVW ]HUR RFFXUUHQFHV RI D VWULQJ VR WKH PHWKRG VKRXOGQ W GR WKLV 7KH SUREOHP LV WKDW WKH FODVVLF VROXWLRQ WR WKH IHQFHSRVW LQYROYHV SULQWLQJ RQH YDOXH EHIRUH WKH ORRS EHJLQV 7R JHW LW WR EHKDYH FRUUHFWO\ IRU WKH ]HUR FDVH ZH ZRXOG KDYH WR LQFOXGH DQ LI HOVH 225 public static void multiprint(String s, int times) { if (times == 0) { System.out.println(""); } else { System.out.print("[" + s); for (int i = 2; i <= times; i++) { System.out.print(", " + s); } System.out.println("]"); } } $V DQ DOWHUQDWLYH ZH FDQ XVH WKH DSSURDFK RI LQFOXGLQJ DQ LI LQVLGH WKH ORRS WKH GRXEOH WHVW DSSURDFK public static void multiprint(String s, int times) { System.out.print("["); for (int i = 1; i <= times; i++) { System.out.print(s); if (i < times) { System.out.print(", "); } } System.out.println("]"); } $OWKRXJK WKLV FRGH SHUIRUPV D VLPLODU WHVW WZLFH RQ HDFK LWHUDWLRQ LW LV VLPSOHU WKDQ WKH FODVVLF VROXWLRQ DQG LWV VSHFLDO FDVH 1HLWKHU VROXWLRQ LV EHWWHU WKDQ WKH RWKHU 7KHUH LV D WUDGHRII LQYROYHG ,I ZH WKLQN WKH FRGH ZLOO EH H[HFXWHG RIWHQ ZLWK WKH ORRS LWHUDWLQJ PDQ\ WLPHV ZH ZRXOG EH PRUH LQFOLQHG WR XVH WKH HIILFLHQW VROXWLRQ 2WKHUZLVH ZH PLJKW FKRRVH WKH VLPSOHU FRGH LQVWHDG 1HVWHG LI HOVH 0DQ\ EHJLQQHUV ZULWH FRGH WKDW ORRNV OLNH WKLV if (<test1>) { <statement1>; } if (<test2>) { <statement2>; } if (<test3>) { <statement3>; } 7KLV VHTXHQWLDO VWUXFWXUH LV DSSURSULDWH LI \RX ZDQW WR H[HFXWH DQ\ FRPELQDWLRQ RI WKH WKUHH VWDWHPHQWV <RX PLJKW ZULWH WKLV FRGH LQ D SURJUDP IRU D TXHVWLRQQDLUH ZLWK WKUHH RSWLRQDO SDUWV DQ\ FRPELQDWLRQ RI ZKLFK PLJKW EH DSSOLFDEOH IRU D JLYHQ SHUVRQ 7KH IROORZLQJ GLDJUDP VKRZV WKH IORZ RI WKH VHTXHQWLDO LI FRGH 1RWLFH WKDW LW V SRVVLEOH WR H[HFXWH QRQH RI WKH FRQWUROOHG VWDWHPHQWV LI DOO WHVWV DUH IDOVH RU MXVW RQH RI WKHP LI RQO\ WKDW WHVW KDSSHQV WR EH WUXH RU PDQ\ RI WKHP LI PXOWLSOH WHVWV DUH WUXH 226 2IWHQ KRZHYHU \RX RQO\ ZDQW WR H[HFXWH RQH RI D VHULHV RI VWDWHPHQWV ,Q VXFK FDVHV LW LV EHWWHU WR QHVW WKH LI VWDWHPHQWV if (<test1>) { <statement1>; } else { if (<test2>) { <statement2>; } else { if (<test3>) { <statement3>; } } } 227 :LWK WKLV FRQVWUXFW \RX FDQ EH VXUH WKDW RQH VWDWHPHQW DW PRVW LV H[HFXWHG 7KH ILUVW WHVW WR UHWXUQ WUXH KDV LWV FRUUHVSRQGLQJ VWDWHPHQW H[HFXWHG ,I QR WHVWV UHWXUQ WUXH QR VWDWHPHQW LV H[HFXWHG ,I H[HFXWLQJ DW PRVW RQH VWDWHPHQW LV \RXU REMHFWLYH WKHQ WKLV FRQVWUXFW LV PRUH DSSURSULDWH WKDQ WKH VHTXHQWLDO LI VWDWHPHQWV ,W UHGXFHV WKH OLNHOLKRRG RI HUURUV DQG VLPSOLILHV WKH WHVWLQJ SURFHVV $V \RX FDQ VHH QHVWLQJ LI VWDWHPHQWV OLNH WKLV OHDGV WR D ORW RI LQGHQWDWLRQ 7KH LQGHQWDWLRQ DOVR LVQ W DOO WKDW KHOSIXO EHFDXVH ZH UHDOO\ WKLQN RI WKLV DV FKRRVLQJ RQH RI D QXPEHU RI DOWHUQDWLYHV . 5 VW\OH KDV D VROXWLRQ IRU WKLV DV ZHOO ,I DQ HOVH LV IROORZHG E\ DQ LI ZH SXW WKHP RQ WKH VDPH OLQH if (<test1>) { <statement1>; } else if (<test2>) { <statement2>; } else if (<test3>) { <statement3>; } 7KLV ZD\ WKH YDULRXV VWDWHPHQWV WKDW ZH DUH FKRRVLQJ IURP DOO DSSHDU DW WKH VDPH OHYHO RI LQGHQWDWLRQ 6XQ UHFRPPHQGV WKDW QHVWHG LI HOVH VWDWHPHQWV EH LQGHQWHG LQ WKLV ZD\ 7KH IROORZLQJ GLDJUDP VKRZV WKH IORZ RI WKH QHVWHG LI HOVH FRGH 1RWLFH WKDW LW LV SRVVLEOH WR H[HFXWH RQH RI WKH FRQWUROOHG VWDWHPHQWV WKH ILUVW RQH ZKRVH WHVW LV WUXH RU QRQH LI QR WHVWV DUH WUXH 7KHUH LV D YDULDWLRQ RI WKLV VWUXFWXUH LQ ZKLFK WKH ILQDO VWDWHPHQW LV FRQWUROOHG E\ DQ HOVH LQVWHDG RI D WHVW 228 if (<test1>) { <statement1>; } else if (<test2>) { <statement2>; } else { <statement3>; } ,Q WKLV FRQVWUXFW WKH ILQDO EUDQFK ZLOO DOZD\V EH WDNHQ ZKHQ DOO WKH WHVWV IDLO DQG WKXV WKH FRQVWUXFW ZLOO DOZD\V H[HFXWH H[DFWO\ RQH RI WKH WKUHH VWDWHPHQWV 7KH IROORZLQJ GLDJUDP VKRZV WKH IORZ RI WKLV PRGLILHG QHVWHG LI HOVH FRGH 7R H[SORUH WKHVH YDULDWLRQV FRQVLGHU WKH WDVN RI ZULWLQJ RXW ZKHWKHU D QXPEHU LV SRVLWLYH QHJDWLYH RU ]HUR <RX FRXOG VWUXFWXUH WKLV DV WKUHH VLPSOH LI VWDWHPHQWV DV IROORZV if (number > 0) { System.out.println("Number is positive."); } if (number == 0) { System.out.println("Number is zero."); } if (number < 0) { System.out.println("Number is negative."); } 7R GHWHUPLQH KRZ PDQ\ RI WKH SULQWOQV DUH SRWHQWLDOO\ H[HFXWHG \RX KDYH WR VWRS DQG WKLQN DERXW WKH WHVWV EHLQJ SHUIRUPHG <RX VKRXOGQ W KDYH WR SXW WKDW PXFK HIIRUW LQWR XQGHUVWDQGLQJ WKLV FRGH 7KH FRGH LV FOHDUHU LI ZH QHVW WKH LI VWDWHPHQWV 229 if (number > 0) { System.out.println("Number is positive."); } else if (number == 0) { System.out.println("Number is zero."); } else if (number < 0) { System.out.println("Number is negative."); } 7KLV VROXWLRQ KRZHYHU LV QRW WKH EHVW <RX NQRZ WKDW \RX ZDQW WR H[HFXWH RQH DQG RQO\ RQH SULQWOQ VWDWHPHQW 7KLV QHVWHG VWUXFWXUH GRHV QRW SUHFOXGH WKH SRVVLELOLW\ RI QR VWDWHPHQW EHLQJ H[HFXWHG ,I DOO WKUHH WHVWV IDLO QR VWDWHPHQW ZRXOG EH H[HFXWHG :LWK WKHVH SDUWLFXODU WHVWV WKDW ZLOO QHYHU KDSSHQ ,I D QXPEHU LV QHLWKHU SRVLWLYH QRU ]HUR LW PXVW EH QHJDWLYH 7KXV WKH ILQDO WHVW KHUH LV XQQHFHVVDU\ DQG PLVOHDGLQJ <RX PXVW WKLQN DERXW WKH WHVWV LQ RUGHU WR NQRZ ZKHWKHU RU QRW LW LV SRVVLEOH IRU DOO WKUHH RI WKHVH EUDQFKHV WR EH VNLSSHG ,Q WKLV FDVH WKH EHVW VROXWLRQ LV WKH QHVWHG LI HOVH ZLWK D ILQDO EUDQFK WKDW LV DOZD\V WDNHQ LI WKH ILUVW WZR WHVWV IDLO if (number > 0) { System.out.println("Number is positive."); } else if (number == 0) { System.out.println("Number is zero."); } else { System.out.println("Number is negative."); } <RX FDQ JODQFH DW WKLV FRQVWUXFW DQG VHH WKDW H[DFWO\ RQH SULQWOQ ZLOO EH H[HFXWHG <RX GRQ W KDYH WR ORRN DW WKH WHVWV EHLQJ SHUIRUPHG LQ RUGHU WR UHDOL]H WKLV LW LV D SURSHUW\ RI WKLV NLQG RI QHVWHG LI HOVH 7KLV LV D JRRG SODFH WR LQFOXGH D FRPPHQW WR PDNH LW FOHDU ZKDW LV JRLQJ RQ if (number > 0) { System.out.println("Number is positive."); } else if (number == 0) { System.out.println("Number is zero."); } else { // number must be negative System.out.println("Number is negative."); } 2QH ILQDO EHQHILW RI WKLV DSSURDFK LV HIILFLHQF\ :LWK WKUHH VLPSOH LI VWDWHPHQWV ZH DOZD\V SHUIRUP DOO WKUHH WHVWV :LWK WKH QHVWHG LI HOVH DSSURDFK ZH WHVW RQO\ XQWLO ZH ILQG D PDWFK )RU H[DPSOH LQ WKH FRGH DERYH ZH RQO\ QHHG WR SHUIRUP RQH WHVW IRU SRVLWLYH QXPEHUV DQG DW PRVW WZR WHVWV RYHUDOO :KHQ \RX ILQG \RXUVHOI ZULWLQJ FRGH WR SLFN DPRQJ DOWHUQDWLYHV OLNH WKHVH \RX KDYH WR DQDO\]H WKH SDUWLFXODU SUREOHP WR ILJXUH RXW KRZ PDQ\ RI WKH EUDQFKHV \RX SRWHQWLDOO\ ZDQW WR H[HFXWH ,I DQ\ FRPELQDWLRQ FDQ EH WDNHQ XVH VHTXHQWLDO LI VWDWHPHQWV ,I \RX ZDQW RQH RU QRQH RI WKH EUDQFKHV WR EH WDNHQ XVH WKH QHVWHG LI HOVH ZLWK D WHVW IRU HDFK VWDWHPHQW ,I \RX ZDQW H[DFWO\ RQH EUDQFK WR EH WDNHQ XVH WKH QHVWHG LI HOVH ZLWK D ILQDO EUDQFK FRQWUROOHG E\ DQ HOVH UDWKHU WKDQ E\ D WHVW 7KH WDEOH EHORZ VXPPDUL]HV WKHVH FKRLFHV 230 ,I (OVH 2SWLRQV 6LWXDWLRQ <RX ZDQW WR H[HFXWH DQ\ FRPELQDWLRQ RI FRQWUROOHG VWDWHPHQWV &RQVWUXFW VHTXHQWLDO LIV %DVLF )RUP if (<test1>) { <statement1>; } if (<test2>) { <statement2>; } if (<test3>) { <statement3>; } if (<test1>) { <statement1>; } else if (<test2>) { <statement2>; } else if (<test3>) { <statement3>; } if (<test1>) { <statement1>; } else if (<test2>) { <statement2>; } else { <statement3>; } <RX ZDQW WR H[HFXWH RU RI WKH FRQWUROOHG VWDWHPHQWV QHVWHG LIV HQGLQJ LQ WHVW <RX ZDQW WR H[HFXWH H[DFWO\ RI WKH FRQWUROOHG VWDWHPHQWV QHVWHG LIV HQGLQJ LQ HOVH &RPPRQ 3URJUDPPLQJ (UURU &KRRVLQJ WKH :URQJ LI HOVH &RQVWUXFW 6XSSRVH WKDW \RXU LQVWUXFWRU KDV WROG \RX WKDW JUDGHV ZLOO EH GHWHUPLQHG DV IROORZV $ IRU VFRUHV % IRU VFRUHV & IRU VFRUHV ' IRU VFRUHV ) IRU VFRUHV :H FDQ WUDQVODWH WKLV LQWR FRGH DV IROORZV String grade; if (score >= 90) { grade = "A"; } if (score >= 80) { grade = "B"; } if (score >= 70) { grade = "C"; } if (score >= 60) { grade = "D"; } if (score < 60) { grade = "F"; } 7KH ILUVW SUREOHP ZH HQFRXQWHU LV WKDW LI ZH WU\ WR XVH WKH YDULDEOH JUDGH DIWHU WKLV FRGH ZH JHW WKLV HUURU IURP WKH FRPSLOHU 231 variable grade might not have been initialized 7KLV VKRXOG DOUHDG\ EH D FOXH WKDW ZH KDYH D SUREOHP 7KH -DYD FRPSLOHU LV VD\LQJ WKDW WKHUH DUH SDWKV WKURXJK WKLV FRGH WKDW ZRXOG OHDYH WKH YDULDEOH JUDGH XQLQLWLDOL]HG :H FDQ IL[ WKLV E\ JLYLQJ DQ LQLWLDO YDOXH WR JUDGH String grade = "no grade"; 7KLV FKDQJH DOORZV WKH FRGH WR FRPSLOH %XW LI \RX FRPSLOH DQG UXQ LW \RX ZLOO ILQG WKDW LW JLYHV RXW RQO\ WZR JUDGHV ' DQG ) $Q\RQH ZKR KDV D VFRUH RI DW OHDVW HQGV XS ZLWK D ' DQG DQ\RQH ZLWK D JUDGH EHORZ HQGV XS ZLWK DQ ) $QG HYHQ WKRXJK WKH FRPSLOHU FRPSODLQHG WKDW WKHUH ZDV D SDWK WKDW ZRXOG DOORZ JUDGH QRW WR EH LQLWLDOL]HG ZH QHYHU HQG XS ZLWK VRPHRQH JHWWLQJ D JUDGH RI QR JUDGH 7KH SUREOHP KHUH LV WKDW ZH ZDQW WR H[HFXWH H[DFWO\ RQH RI WKHVH DVVLJQPHQW VWDWHPHQWV %XW ZLWK WKH VHTXHQWLDO LI VWDWHPHQWV ZH FDQ H[HFXWH VHYHUDO RI WKHP )RU H[DPSOH LI VRPHRQH KDV D VFRUH RI ZH VHW JUDGH WR $ WKHQ UHVHW LW WR % WKHQ UHVHW LW WR & DQG ILQDOO\ UHVHW LW WR ' :H FDQ IL[ WKLV E\ XVLQJ D QHVWHG LI HOVH FRQVWUXFW String grade; if (score >= 90) { grade = "A"; } else if (score >= 80) { grade = "B"; } else if (score >= 70) { grade = "C"; } else if (score >= 60) { grade = "D"; } else { // score < 60 grade = "F"; } :H GRQ W HYHQ QHHG WR VHW JUDGH WR QR JUDGH EHFDXVH WKH FRPSLOHU FDQ VHH WKDW QR PDWWHU ZKDW SDWK ZH IROORZ WKH YDULDEOH JUDGH ZLOO EH DVVLJQHG D YDOXH H[DFWO\ RQH RI WKH EUDQFKHV ZLOO EH H[HFXWHG 6XEWOHWLHV RI &RQGLWLRQDO ([HFXWLRQ 7KH ODVW VHFWLRQ SUHVHQWHG WKH EDVLF LGHD EHKLQG LI VWDWHPHQW DQG LI HOVH FRQVWUXFWV <RX OO ILQG DV ZH JHW GHHSHU LQWR SURJUDPPLQJ WKDW PDQ\ RI WKH QHZ FRQVWUXFWV ZH VWXG\ DUHQ W RYHUO\ FRPSOH[ EXW WKH\ WDNH WLPH WR PDVWHU 7KLV LV WUXH RI DQ\ FUDIW )RU H[DPSOH LW ZRXOG SUREDEO\ WDNH RQO\ D GD\ WR OHDUQ HDFK RI WKH PDMRU WRROV XVHG E\ D FDUSHQWHU EXW OHDUQLQJ ZKHQ WR XVH ZKLFK WRRO KRZ WR XVH WKHP WRJHWKHU DQG KRZ WR XVH WKHP ZHOO ZRXOG UHTXLUH PXFK PRUH WKDQ D GD\ RI WUDLQLQJ ,Q WKLV VHFWLRQ ZH ZLOO H[SORUH VRPH RI WKH LVVXHV WKDW DULVH ZKHQ \RX VWDUW XVLQJ FRQGLWLRQDO H[HFXWLRQ 232 2EMHFW (TXDOLW\ :H VDZ HDUOLHU LQ WKH FKDSWHU WKDW \RX FDQ XVH WKH DQG RSHUDWRUV WR WHVW IRU HTXDOLW\ DQG QRQ HTXDOLW\ UHVSHFWLYHO\ 8QIRUWXQDWHO\ WKHVH RSHUDWRUV GR QRW ZRUN DV \RX PLJKW H[SHFW ZKHQ WHVWLQJ IRU HTXDOLW\ DQG QRQ HTXDOLW\ RI REMHFWV OLNH 6WULQJV DQG 3RLQWV VR ZH ZLOO KDYH WR OHDUQ D QHZ ZD\ WR WHVW REMHFWV IRU HTXDOLW\ 6XSSRVH WKDW ZH ZHUH WR H[HFXWH WKH IROORZLQJ OLQHV RI FRGH Point p1 = new Point(3, 4); Point p2 = new Point(3, 4); Point p3 = p2; ,W ZLOO OHDG WR WKUHH YDULDEOHV DQG WZR REMHFWV +--------------------+ +---+ | +----+ +----+ | p1 | +-+--> | x | 3 | y | 4 | | +---+ | +----+ +----+ | +--------------------+ +--------------------+ +---+ | +----+ +----+ | p2 | +-+--> | x | 3 | y | 4 | | +---+ | +----+ +----+ | +--------------------+ ^ +---+ | p3 | --+--------------+ +---+ (YHQ WKRXJK ZH KDYH WZR 3RLQW REMHFWV WKH\ ERWK KDYH FRRUGLQDWHV 6R ZH ZRXOG QRUPDOO\ WKLQN RI WKHVH SRLQWV DV EHLQJ HTXDO %XW LI ZH ZHUH WR WHVW ZKHWKHU S S WKH DQVZHU ZRXOG EH IDOVH ,Q SHUIRUPLQJ WKLV WHVW -DYD VHHV ZKHWKHU WKH YDULDEOHV S DQG S VWRUH H[DFWO\ WKH VDPH YDOXH 7KHVH YDULDEOHV VWRUH UHIHUHQFHV WR 3RLQW REMHFWV EXW WKH\ DUHQ W WKH 3RLQW REMHFWV WKHPVHOYHV 6R WKLV WHVW LV DVNLQJ ZKHWKHU S DQG S UHIHU WR WKH VDPH REMHFW 6LQFH WKH\ UHIHU WR GLIIHUHQW REMHFWV WKH WHVW HYDOXDWHV WR IDOVH ,I ZH ZHUH LQVWHDG WR DVN ZKHWKHU S S WKH DQVZHU ZRXOG EH WUXH 7KDW V EHFDXVH S DQG S VWRUH WKH VDPH YDOXH ,Q RWKHU ZRUGV WKH\ ERWK UHIHU WR WKH VDPH REMHFW -DYD SURYLGHV D VHFRQG ZD\ RI WHVWLQJ IRU HTXDOLW\ WKDW LV LQWHQGHG IRU WHVWLQJ REMHFW HTXDOLW\ (YHU\ -DYD REMHFW KDV D PHWKRG FDOOHG HTXDOV WKDW WDNHV DQRWKHU REMHFW DV DQ DUJXPHQW 6R \RX DVN DQ REMHFW ZKHWKHU LW HTXDOV DQRWKHU REMHFW )RU H[DPSOH JLYHQ WKH YDULDEOHV DERYH ZH FRXOG WHVW ZKHWKHU if (p1.equals(p2)) { System.out.println("points are equal"); } 233 ,Q WKLV FDVH ZH DUH QRW FRPSDULQJ WKH YDULDEOHV ZH DUH FDOOLQJ D PHWKRG RI WKH 3RLQW REMHFW UHIHUUHG WR E\ S )RU 3RLQW REMHFWV WKH HTXDOV PHWKRG FRPSDUHV WKH [ DQG \ FRRUGLQDWHV VWRUHG LQ HDFK SRLQW WR VHH LI WKH\ DUH WKH VDPH ,Q HIIHFW ZH DUH VD\LQJ +H\ S GR \RX VWRUH WKH VDPH [ DQG \ FRRUGLQDWHV DV S " ,Q WKLV FDVH WKH WHVW HYDOXDWHV WR WUXH <RX ZLOO ZDQW WR XVH WKH HTXDOV PHWKRG WR WHVW IRU HTXDOLW\ RI 6WULQJV )RU H[DPSOH \RX PLJKW ZULWH FRGH OLNH WKH IROORZLQJ WR UHDG D WRNHQ IURP WKH FRQVROH DQG WR FDOO RQH RI WZR GLIIHUHQW PHWKRGV GHSHQGLQJ XSRQ ZKHWKHU WKH XVHU UHVSRQGHG ZLWK \HV RU QR ,I WKH XVHU W\SHV QHLWKHU ZRUG LW SULQWV DQ HUURU PHVVDJH System.out.print("yes or no? "); String s = console.next(); if (s.equals("yes")) { processYes(); } else if (s.equals("no")) { processNo(); } else { System.out.println("You didn't type yes or no"); } 7KLV FRGH ZLOO QRW ZRUN DV LQWHQGHG ZLWK WHVWV -DYD KDV D VSHFLDO YDULDWLRQ RI WKH HTXDOV PHWKRG IRU WKH 6WULQJ FODVV WKDW LJQRUHV FDVH GLIIHUHQFHV FDSLWDO YHUVXV VPDOO OHWWHUV 7KH PHWKRG LV FDOOHG HTXDOV,JQRUH&DVH )RU H[DPSOH \RX FRXOG UHZULWH WKH FRGH DERYH WR UHFRJQL]H UHVSRQVHV OLNH <HV <(6 1R 12 \(6 HWF System.out.print("yes or no? "); String s = console.next(); if (s.equalsIgnoreCase("yes")) { processYes(); } else if (s.equalsIgnoreCase("no")) { processNo(); } else { System.out.println("You didn't type yes or no"); } 5RXQGRII (UURUV (DUOLHU LQ WKH FKDSWHU ZH ORRNHG DW D SURJUDP FDOOHG ([DPLQH1XPEHUV WKDW ZLOO UHDG D VHULHV RI QXPEHUV DQG FRPSXWH WKH DYHUDJH RI WKH QXPEHUV DQG WKDW ZLOO FRXQW KRZ PDQ\ QHJDWLYHV DUH LQ WKH VHTXHQFH %HORZ LV D VDPSOH H[HFXWLRQ WKDW SURGXFHV DQ XQXVXDO ILQDO UHVXOW This program examines a sequence of numbers to find the average as well as counting how many are negative. How many numbers do you want me to examine? 4 #1? 2.1 #2? -3.8 #3? 5.4 #4? 7.4 average = 2.7750000000000004 # of negatives = 1 234 ,I \RX XVH D FDOFXODWRU \RX ZLOO ILQG WKDW WKH IRXU QXPEHUV DGG XS WR ,I \RX GLYLGH E\ LQ \RXU FDOFXODWRU \RX JHW <HW -DYD LV UHSRUWLQJ WKLV DV :KHUH GR DOO RI WKRVH ]HURV FRPH IURP DQG ZK\ GRHV LW HQG LQ " 7KH DQVZHU LV WKDW IORDWLQJ SRLQW QXPEHUV FDQ OHDG WR URXQG RII HUURUV 5RXQG 2II (UURU $ QXPHULFDO HUURU WKDW RFFXUV EHFDXVH IORDWLQJ SRLQW QXPEHUV DUH VWRUHG DV DSSUR[LPDWLRQV UDWKHU WKDQ DV H[DFW YDOXHV 5RXQG RII HUURUV DUH JHQHUDOO\ VPDOO DQG FDQ RFFXU LQ HLWKHU GLUHFWLRQ VOLJKWO\ KLJK RU VOLJKWO\ ORZ ,Q WKH FDVH DERYH ZH JRW D URXQGRII HUURU WKDW ZDV VOLJKWO\ KLJK ,W VKRXOGQ W EH WRR GLIILFXOW WR XQGHUVWDQG ZK\ WKLV RFFXUV 5HPHPEHU WKDW IORDWLQJ SRLQW QXPEHUV DUH VWRUHG LQ D IRUPDW VLPLODU WR VFLHQWLILF QRWDWLRQ ZLWK D VHW RI GLJLWV DQG DQ H[SRQHQW 7KLQN RI KRZ \RX ZRXOG VWRUH RQH WKLUG LQ VFLHQWLILF QRWDWLRQ XVLQJ EDVH :H GHVFULEH LW DV UHSHDWLQJ WLPHV WR WKH SRZHU :H FDQ W VWRUH DQ LQILQLWH QXPEHU RI GLJLWV RQ D FRPSXWHU VR ZH ZRXOG KDYH WR VWRS UHSHDWLQJ WKH V DW VRPH SRLQW 6XSSRVH ZH FRXOG VWRUH GLJLWV 7KHQ ZH G VWRUH LW DV WLPHV WR WKH ,I ZH PXOWLSO\ WKDW E\ ZH GRQ W JHW EDFN ,QVWHDG ZH JHW WLPHV WR WKH ZKLFK LV HTXDO WR <RX PLJKW ZRQGHU ZK\ WKH QXPEHUV ZH XVHG ZRXOG FDXVH D SUREOHP ZKHQ WKH\ GLGQ W KDYH DQ\ UHSHDWLQJ GLJLWV <RX KDYH WR UHPHPEHU WKDW WKH FRPSXWHU VWRUHV QXPEHUV LQ EDVH 1XPEHUV OLNH DQG PLJKW ORRN OLNH VLPSOH QXPEHUV LQ EDVH EXW WKH\ KDYH UHSHDWLQJ GLJLWV ZKHQ VWRUHG LQ EDVH 5RXQGRII HUURUV FDQ OHDG WR UDWKHU VXUSULVLQJ RXWFRPHV )RU H[DPSOH FRQVLGHU WKH IROORZLQJ VKRUW SURJUDP 1 2 3 4 5 6 7 8 9 public class Roundoff { public static void main(String args) { double n = 1.0; for (int i = 1; i <= 10; i++) { n += 0.1; System.out.println(n); } } } 7KLV LV D FODVVLF FXPXODWLYH VXP ZKHUH ZH DGG WR WKH QXPEHU HDFK WLPH WKURXJK WKH ORRS :H VWDUW ZLWK Q HTXDO WR DQG WKH ORRS LWHUDWHV WHQ WLPHV VR WKLV VKRXOG SULQW WKH QXPEHUV DQG VR RQ WKURXJK +HUH LV WKH RXWSXW LW SURGXFHV 235 1.1 1.2000000000000002 1.3000000000000003 1.4000000000000004 1.5000000000000004 1.6000000000000005 1.7000000000000006 1.8000000000000007 1.9000000000000008 2.000000000000001 :H JHW XQXVXDO RXWSXW EHFDXVH FDQQRW EH VWRUHG H[DFWO\ LQ EDVH LW SURGXFHV D UHSHDWLQJ VHW RI GLJLWV MXVW DV RQH WKLUG GRHV LQ EDVH (DFK WLPH WKURXJK WKH ORRS ZH FRPSRXQG WKH HUURU ZKLFK LV ZK\ WKH URXQGRII HUURU JHWV ZRUVH HDFK WLPH $V DQRWKHU H[DPSOH FRQVLGHU WKH WDVN RI DGGLQJ XS D SHQQ\ D QLFNHO D GLPH DQG D TXDUWHU ,I ZH XVH YDULDEOHV RI W\SH LQW ZH ZLOO JHW DQ H[DFW DQVZHU QR PDWWHU ZKDW RUGHU ZH DGG WKH QXPEHUV int cents1 = 1 + 5 + 10 + 25; int cents2 = 25 + 10 + 5 + 1; System.out.println(cents1); System.out.println(cents2); 7KH RXWSXW RI WKLV FRGH LV DV IROORZV 41 41 1R PDWWHU ZKDW RUGHU ZH XVH WR DGG WKH QXPEHUV ZH OO ILQG WKDW LW DGGV XS WR FHQWV %XW VXSSRVH WKDW LQVWHDG RI WKLQNLQJ RI WKHVH DV FHQWV ZH WKLQN RI WKHP DV IUDFWLRQV RI D GROODU WKDW ZH ZRXOG VWRUH ZLWK GRXEOHV double dollars1 = 0.01 + 0.05 + 0.10 + 0.25; double dollars2 = 0.25 + 0.10 + 0.05 + 0.01; System.out.println(dollars1); System.out.println(dollars2); 7KLV FRGH KDV D VXUSULVLQJ RXWSXW 0.41000000000000003 0.41 (YHQ WKRXJK ZH DUH DGGLQJ XS H[DFWO\ WKH VDPH QXPEHUV WKH IDFW WKDW ZH DGG WKHP LQ D GLIIHUHQW RUGHU PDNHV D GLIIHUHQFH 7KH UHDVRQ LV URXQGRII HUURUV 7KHUH DUH VHYHUDO OHVVRQV WR GUDZ IURP WKLV 5HDOL]H WKDW ZKHQ \RX VWRUH YDOXHV ZLWK D IORDWLQJ SRLQW W\SH OLNH GRXEOH \RX DUH VWRULQJ DSSUR[LPDWLRQV DQG QRW H[DFW YDOXHV ,I \RX QHHG VRPHWKLQJ VWRUHG H[DFWO\ VWRUH LW XVLQJ W\SH LQW ZKLFK KDV DQ H[DFW UHSUHVHQWDWLRQ 'RQ W EH VXUSULVHG ZKHQ \RX VHH QXPEHUV WKDW DUH VOLJKWO\ RII IURP WKH H[SHFWHG YDOXH 'RQ W H[SHFW WR EH DEOH WR FRPSDUH YDULDEOHV RI W\SH GRXEOH IRU HTXDOLW\ 236 7R IROORZ XS RQ WKH WKLUG SRLQW FRQVLGHU ZKDW ZRXOG KDSSHQ ZLWK WKH FRGH DERYH LI ZH ZHUH WR SHUIRUP WKH IROORZLQJ WHVW if (dollars1 == dollars2) { ... } 7KH WHVW ZRXOG HYDOXDWH WR IDOVH EHFDXVH WKH\ DUH YHU\ FORVH EXW QRW FORVH HQRXJK IRU -DYD WR FRQVLGHU WKHP HTXDO :H UDUHO\ XVH D WHVW IRU H[DFW HTXDOLW\ ZKHQ ZH ZRUN ZLWK GRXEOHV ,QVWHDG ZH FDQ WHVW WR VHH LI QXPEHUV DUH FORVH WR HDFK RWKHU XVLQJ D WHVW OLNH WKLV if (Math.abs(dollars1 - dollars2) < 0.001) { ... } :H XVH WKH DEVROXWH YDOXH PHWKRG IURP WKH 0DWK FODVV WR ILQG WKH PDJQLWXGH RI WKH GLIIHUHQFH DQG VHH ZKHWKHU LW LV OHVV WKDQ VRPH VPDOO HSVLORQ LQ WKLV FDVH /DWHU LQ WKH FKDSWHU ZH ZLOO VHH D YDULDWLRQ RQ SULQW SULQWOQ FDOOHG SULQWI WKDW ZLOO PDNH LW HDVLHU WR SULQW QXPEHUV OLNH WKHVH ZLWKRXW DOO RI WKH H[WUD GLJLWV )DFWRULQJ LI HOVH 6WDWHPHQWV 6XSSRVH \RX DUH ZULWLQJ D SURJUDP WKDW SOD\V D EHWWLQJ JDPH ZLWK D XVHU DQG \RX ZDQW WR JLYH GLIIHUHQW ZDUQLQJV DERXW KRZ PXFK FDVK WKH XVHU KDV 7KH IROORZLQJ QHVWHG LI HOVH GLVWLQJXLVKHV WKUHH GLIIHUHQW FDVHV PRQH\ OHVV WKDQ ZKLFK LV FRQVLGHUHG ORZ PRQH\ EHWZHHQ DQG ZKLFK LV FRQVLGHUHG RND\ DQG PRQH\ RYHU ZKLFK LV FRQVLGHUHG JRRG 1RWLFH WKDW WKH XVHU LV JLYHQ GLIIHUHQW DGYLFH LQ HDFK EUDQFK if (money < 500) { System.out.println("You have, $" + money + " left."); System.out.print("Your cash is dangerously low. Bet carefully."); System.out.print("How much do you want to bet? "); bet = console.nextInt(); } else if (money < 1000) { System.out.println("You have, $" + money + " left."); System.out.print("Your cash is somewhat low. Bet moderately."); System.out.print("How much do you want to bet? "); bet = console.nextInt(); } else { System.out.println("You have, $" + money + " left."); System.out.print("Your cash is in good shape. Bet liberally."); System.out.print("How much do you want to bet? "); bet = console.nextInt(); } 7KLV FRQVWUXFW LV UHSHWLWLRXV DQG FDQ EH UHGXFHG XVLQJ D WHFKQLTXH FDOOHG IDFWRULQJ :LWK LW \RX IDFWRU RXW FRPPRQ SLHFHV RI FRGH IURP WKH GLIIHUHQW EUDQFKHV RI WKH LI HOVH 7KH WHFKQLTXH LV VLPSOH 7KH DERYH FRQVWUXFW FUHDWHV WKUHH GLIIHUHQW EUDQFKHV GHSHQGLQJ XSRQ WKH YDOXH RI WKH YDULDEOH PRQH\ <RX VWDUW E\ ZULWLQJ GRZQ WKH VHULHV RI DFWLRQV EHLQJ SHUIRUPHG LQ HDFK EUDQFK DQG FRPSDULQJ WKHP 237 Depending upon money: | +-----------------------+-----------------------+ | | | money < 500 500 <= money < 1000 Money >= 1000 | | | Report money Report money Report money Warn about low money Report okay money Report good money Prompt for bet Prompt for bet Prompt for bet Read bet Read bet Read bet <RX FDQ IDFWRU DW ERWK WKH WRS DQG WKH ERWWRP RI VXFK D FRQVWUXFW ,I \RX QRWLFH WKDW WKH WRS VWDWHPHQW LQ HDFK EUDQFK LV WKH VDPH \RX IDFWRU LW RXW RI WKH EUDQFKLQJ SDUW DQG SXW LW EHIRUH WKH EUDQFK 6LPLODUO\ LI WKH ERWWRP VWDWHPHQW LQ HDFK EUDQFK LV WKH VDPH \RX IDFWRU LW RXW RI WKH EUDQFKLQJ SDUW DQG SXW LW DIWHU WKH ORRS <RX FDQ IDFWRU WKH WRS VWDWHPHQW LQ HDFK RI WKHVH EUDQFKHV DQG WKH ERWWRP WZR VWDWHPHQWV Report money Depending upon money: | +-----------------------+-----------------------+ | | | money < 500 500 <= money < 1000 Money >= 1000 | | | Warn about low money Report okay money Report good money Prompt for bet Read bet 7KXV WKH FRGH DERYH FDQ EH UHGXFHG WR WKH IROORZLQJ ZKLFK LV PRUH VXFFLQFW System.out.println("You have, $" + money + " left."); if (money < 500) { System.out.print("Your cash is dangerously low. Bet carefully."); } else if (money < 1000) { System.out.print("Your cash is somewhat low. Bet moderately."); } else { System.out.print("Your cash is in good shape. Bet liberally."); } System.out.print("How much do you want to bet? "); bet = console.nextInt(); 0LQ 0D[ /RRSV :H KDYH VHHQ KRZ WR FRPSXWH WKH DYHUDJH RI D VHTXHQFH RI QXPEHUV $QRWKHU FRPPRQ SURJUDPPLQJ WDVN LV WR NHHS WUDFN RI WKH PD[LPXP DQG RU PLQLPXP YDOXH LQ D VHTXHQFH )RU H[DPSOH ZLWK DQ DYHUDJH WHPSHUDWXUH RI DURXQG GHJUHHV EHORZ ]HUR )DKUHQKHLW WKH VXUIDFH RI WKH 0RRQ LV FOHDUO\ LQKRVSLWDEOH %XW WKH PXFK JUHDWHU FKDOOHQJH RI OLYLQJ RQ WKH 0RRQ LV WKDW WKH UDQJH RI WHPSHUDWXUHV LV IURP DURXQG GHJUHHV EHORZ ]HUR WR DURXQG GHJUHHV DERYH ]HUR 7R FRPSXWH WKH PD[LPXP RI D VHTXHQFH RI YDOXHV ZH FDQ NHHS WUDFN RI WKH ODUJHVW YDOXH ZH YH VHHQ VR IDU DQG XVH DQ LI VWDWHPHQW WR XSGDWH WKH PD[LPXP LI ZH FRPH DFURVV D QHZ YDOXH WKDW LV ODUJHU WKDQ WKH FXUUHQW PD[LPXP :H FDQ GHVFULEH WKLV DSSURDFK LQ SVHXGRFRGH DV IROORZV 238 initialize max. for (all numbers to examine) { obtain "next". if (next > max) { max = next; } } ,QLWLDOL]LQJ WKH PD[LPXP LVQ W TXLWH DV VLPSOH DV LW VRXQGV )RU H[DPSOH QRYLFHV RIWHQ LQLWLDOL]H PD[ WR %XW ZKDW LI WKH VHTXHQFH RI QXPEHUV \RX DUH H[DPLQLQJ LV FRPSRVHG HQWLUHO\ RI QHJDWLYH QXPEHUV" )RU H[DPSOH \RX PLJKW EH DVNHG WR ILQG WKH PD[LPXP RI WKLV VHTXHQFH -84, -7, -14, -39, -410, -17, -41, -9 7KH FRUUHFW DQVZHU LV WKDW LV WKH PD[LPXP YDOXH LQ WKLV VHTXHQFH %XW \RX ZRXOG QHYHU VHW PD[ WR EHFDXVH LV ODUJHU 6R \RX ZRXOG LQFRUUHFWO\ UHSRUW WKH PD[LPXP DV 7KHUH DUH WZR FODVVLF VROXWLRQV WR WKLV SUREOHP )LUVW LI \RX NQRZ WKH UDQJH RI WKH QXPEHUV \RX DUH H[DPLQLQJ WKHQ \RX KDYH DQ DSSURSULDWH FKRLFH IRU PD[ ,Q WKDW FDVH \RX FDQ VHW PD[ WR WKH ORZHVW YDOXH LQ WKH UDQJH 7KDW VHHPV FRXQWHULQWXLWLYH EHFDXVH QRUPDOO\ ZH WKLQN RI WKH PD[ DV EHLQJ ODUJH EXW WKH LGHD LV WR VHW WKH PD[ WR WKH VPDOOHVW SRVVLEOH YDOXH LW FRXOG HYHU EH VR WKDW DQ\WKLQJ ODUJHU ZLOO FDXVH XV WR UHVHW WKH PD[ WR WKDW YDOXH )RU H[DPSOH LI WKH QXPEHUV DERYH DUH WHPSHUDWXUHV LQ GHJUHHV )DKUHQKHLW ZH NQRZ WKDW WKH\ ZLOO QHYHU EH VPDOOHU WKDQ DEVROXWH ]HUR DURXQG GHJUHHV )DKUHQKHLW 6R ZH FRXOG LQLWLDOL]H PD[ WR WKDW YDOXH 7KH VHFRQG SRVVLELOLW\ LV WR LQLWLDOL]H PD[ WR WKH ILUVW YDOXH LQ WKH VHTXHQFH 7KDW ZRQ W DOZD\V EH FRQYHQLHQW EHFDXVH LW PHDQV REWDLQLQJ RQH RI WKH QXPEHUV RXWVLGH WKH ORRS &RPELQLQJ WKHVH WZR SRVVLELOLWLHV RXU SVHXGRFRGH EHFRPHV initialize max either to lowest possible value or to first value. for (all numbers to examine) { obtain "next". if (next > max) { max = next; } } 7KH SVHXGRFRGH IRU FRPSXWLQJ WKH PLQLPXP LV D VOLJKW YDULDWLRQ RI WKLV initialize min either to highest possible value or to first value. for (all numbers to examine) { obtain "next". if (next < min) { min = next; } } 7R XQGHUVWDQG WKLV EHWWHU OHW V SXW WKH SVHXGRFRGH LQWR DFWLRQ ZLWK D UHDO SUREOHP ,Q 0DWKHPDWLFV WKHUH LV DQ RSHQ SUREOHP WKDW LQYROYHV ZKDW DUH NQRZQ DV KDLOVWRQH VHTXHQFHV 7KH\ KDYH WKDW QDPH EHFDXVH WKH\ KDYH WKH SURSHUW\ WKDW WKH\ RIWHQ ULVH DQG IDOO LQ XQSUHGLFWDEOH SDWWHUQV ZKLFK LV VRPHZKDW DQDORJRXV WR WKH SURFHVV WKDW IRUPV KDLOVWRQHV 239 $ KDLOVWRQH VHTXHQFH LV D VHTXHQFH RI QXPEHUV LQ ZKLFK HDFK YDOXH [ LV IROORZHG HLWKHU E\ [ [ LI [ LV RGG LI [ LV HYHQ ZH JHW )RU H[DPSOH LI ZH VWDUW ZLWK DQG ZH FRQVWUXFW D VHTXHQFH RI OHQJWK ,Q WKLV VHTXHQFH WKH PD[LPXP DQG PLQLPXP YDOXHV DUH D VHTXHQFH RI OHQJWK ZH JHW DQG UHVSHFWLYHO\ ,I ZH H[WHQG WKLV WR ,Q WKLV FDVH WKH PD[LPXP DQG PLQLPXP YDOXHV DUH DQG UHVSHFWLYHO\ <RX ZLOO QRWLFH WKDW RQFH \RX JHW WR DQ\ RI WKH QXPEHUV RU WKH VHTXHQFH UHSHDWV LWVHOI ,W LV FRQMHFWXUHG WKDW DOO LQWHJHUV HYHQWXDOO\ UHDFK OLNH KDLOVWRQHV WKDW IDOO WR WKH JURXQG 7KLV LV DQ XQVROYHG SUREOHP LQ 0DWKHPDWLFV 1RERG\ KDV EHHQ DEOH WR GLVSURYH LW EXW QRERG\ KDV SURYHQ LW HLWKHU /HW V ZULWH D PHWKRG WKDW WDNHV D VWDUWLQJ YDOXH DQG D VHTXHQFH OHQJWK DQG WKDW SULQWV WKH PD[LPXP DQG PLQLPXP YDOXHV REWDLQHG LQ D KDLOVWRQH VHTXHQFH IRUPHG ZLWK WKDW VWDUWLQJ YDOXH DQG RI WKDW OHQJWK 6R RXU PHWKRG ZLOO ORRN OLNH WKLV public static void printHailstoneMaxMin(int value, int length) { ... } :H FDQ XVH WKH VWDUWLQJ YDOXH WR LQLWLDOL]H PD[ DQG PLQ int min = value; int max = value; :H WKHQ QHHG D ORRS WKDW ZLOO JHQHUDWH WKH RWKHU YDOXHV :H DUH SDVVHG D SDUDPHWHU WHOOLQJ XV KRZ PDQ\ WLPHV WR JR WKURXJK WKH ORRS EXW ZH GRQ W ZDQW WR H[HFXWH OHQJWK WLPHV 5HPHPEHU WKDW WKH VWDUWLQJ YDOXH LV SDUW RI WKH VHTXHQFH 6R LI ZH ZDQW WR XVH D VHTXHQFH RI WKH JLYHQ OHQJWK ZH KDYH WR PDNH VXUH WKDW WKH QXPEHU RI LWHUDWLRQV LV RQH OHVV WKDQ OHQJWK &RPELQLQJ WKLV LGHD ZLWK WKH PD[ PLQ SVHXGRFRGH ZH NQRZ WKH ORRS ZLOO ORRN OLNH WKLV for (int i = 1; i <= length - 1; i++) { compute next number. if (value > max) { max = value; } if (value < min) { min = value; } } print max and min. 240 7R ILOO RXW WKH SVHXGRFRGH IRU FRPSXWH QH[W QXPEHU ZH QHHG WR WUDQVODWH WKH KDLOVWRQH IRUPXOD LQWR FRGH 7KH IRUPXOD LV GLIIHUHQW GHSHQGLQJ XSRQ ZKHWKHU WKH FXUUHQW YDOXH LV RGG RU HYHQ :H FDQ XVH DQ LI HOVH WR VROYH WKLV WDVN )RU WKH WHVW ZH FDQ XVH D PRG WHVW WR VHH ZKDW UHPDLQGHU ZH JHW ZKHQ ZH GLYLGH E\ (YHQ QXPEHUV KDYH D UHPDLQGHU RI DQG RGG QXPEHUV KDYH D UHPDLQGHU RI 6R WKH WHVW VKRXOG ORRN OLNH WKLV if (value % 2 == 0) { do even computation. } else { do odd computation. } 7UDQVODWLQJ WKH KDLOVWRQH PDWKHPDWLFDO IRUPXODV LQWR -DYD H[SUHVVLRQV ZH JHW if (value % 2 == 0) { value = value / 2; } else { value = 3 * value + 1; } 7KH RQO\ SDUW RI RXU SVHXGRFRGH WKDW ZH KDYHQ W ILOOHG LQ \HW LV WKH SULQWLQJ SDUW DIWHU WKH ORRS 7KDW LV IDLUO\ HDV\ WR FRPSOHWH ZKLFK JLYHV XV WKH IROORZLQJ FRPSOHWH PHWKRG public static void printHailstoneMaxMin(int value, int length) { int min = value; int max = value; for (int i = 1; i <= length - 1; i++) { if (value % 2 == 0) { value = value / 2; } else { value = 3 * value + 1; } if (value > max) { max = value; } if (value < min) { min = value; } } System.out.println("max = " + max); System.out.println("min = " + min); } 7H[W 3URFHVVLQJ 3URJUDPPHUV FRPPRQO\ IDFH SUREOHPV WKDW UHTXLUH WKHP WR FUHDWH HGLW H[DPLQH DQG IRUPDW WH[W &ROOHFWLYHO\ ZH FDOO WKHVH W\SHV RI WDVNV WH[W SURFHVVLQJ 7H[W SURFHVVLQJ (GLWLQJ DQG IRUPDWWLQJ VWULQJV RI WH[W 241 ,Q WKLV VHFWLRQ ZH OO ORRN DW D QHZ SULPLWLYH W\SH QDPHG char DQG D QHZ FRPPDQG QDPHG 6\VWHP RXW SULQWI ERWK RI ZKLFK DUH YHU\ XVHIXO IRU WH[W SURFHVVLQJ WDVNV 7KH FKDU 7\SH ,Q WKH SUHYLRXV FKDSWHU ZH OHDUQHG DERXW 6WULQJ REMHFWV WKDW FRQVLVW RI ]HUR WR PDQ\ FKDUDFWHUV 7KHUH LV D SULPLWLYH W\SH QDPHG char WKDW UHSUHVHQWV D VLQJOH FKDUDFWHU RI WH[W ,W V OHJDO WR KDYH YDULDEOHV SDUDPHWHUV DQG UHWXUQ YDOXHV RI W\SH char LI \RX VR GHVLUH /LWHUDO YDOXHV RI W\SH char DUH H[SUHVVHG E\ SODFLQJ WKH FKDUDFWHU ZLWKLQ VLQJOH TXRWHV VXFK DV char ch = 'A'; ,W LV DOVR OHJDO WR FUHDWH D FKDU YDOXH WKDW UHSUHVHQWV DQ HVFDSH VHTXHQFH VXFK DV char newline = '\n'; 7KH GLVWLQFWLRQ EHWZHHQ FKDU DQG 6WULQJ LV D VXEWOH RQH WKDW FRQIXVHV PDQ\ QHZ -DYD SURJUDPPHUV :K\ KDYH WZR W\SHV IRU VXFK VLPLODU GDWD" 7\SH FKDU H[LVWV SULPDULO\ IRU KLVWRULFDO UHDVRQV GDWLQJ EDFN WR ROGHU ODQJXDJHV VXFK DV & WKDW LQIOXHQFHG WKH GHVLJQ RI -DYD 6R ZK\ ZRXOG D SHUVRQ HYHU XVH W\SH FKDU ZKHQ 6WULQJ LV DYDLODEOH" ,Q PDQ\ FDVHV ZH OO XVH FKDU RXW RI QHFHVVLW\ EHFDXVH VRPH PHWKRGV LQ -DYD V $3, XVH W\SH FKDU DV D SDUDPHWHU RU UHWXUQ W\SH %XW WKHUH DUH D IHZ FDVHV ZKHUH FKDU FDQ EH PRUH XVHIXO RU VLPSOHU WKDQ 6WULQJ 9DOXHV RI W\SH FKDU FDQ EH XVHG LQ VLPSOH DULWKPHWLF H[SUHVVLRQV DQG LQ ORRSV WR FRYHU UDQJHV RI OHWWHUV 7KH FKDU YDOXHV FDQ DOVR EH FRPSDUHG XVLQJ UHODWLRQDO RSHUDWRUV VXFK DV RU 6WULQJV GRQ W KDYH HLWKHU RI WKHVH DELOLWLHV )RU H[DPSOH WKH IROORZLQJ FRGH SULQWV HYHU\ OHWWHU RI WKH DOSKDEHW // prints abcdefghijklmnopqrstuvwxyz for (char letter = 'a'; letter <= 'z'; letter++) { System.out.print(letter); } 7KH PDLQ EULGJH EHWZHHQ W\SHV 6WULQJ DQG FKDU LV WKH 6WULQJ REMHFW V charAt PHWKRG ZKLFK DFFHSWV DQ LQWHJHU LQGH[ DV D SDUDPHWHU DQG UHWXUQV WKH 6WULQJ V FKDUDFWHU DW WKDW LQGH[ :H RIWHQ EUHDN DSDUW 6WULQJV EDVHG RQ FKDUDFWHUV RU GR ORRSV RYHU 6WULQJV WKDW H[DPLQH RU FKDQJH HDFK FKDUDFWHU )RU H[DPSOH WKH IROORZLQJ ORRS UHPRYHV DQ\ RFFXUUHQFHV RI WKH VSDFH FKDUDFWHU ' ' IURP D 6WULQJ String phrase = "the rain in Spain"; String noSpaces = ""; for (int i = 0; i < phrase.length(); i++) { char ch = phrase.charAt(i); if (ch != ' ') { noSpaces += ch; } } // noSpaces stores "theraininSpain" here 242 7KLV H[DPSOH XVHV DQ LQWHUHVWLQJ WHFKQLTXH LQ WKDW LW EXLOGV D 6WULQJ XVLQJ D ORRS VWDUWLQJ ZLWK DQ HPSW\ 6WULQJ DQG FRQFDWHQDWLQJ LQGLYLGXDO FKDUDFWHUV RQWR LW 7KLV LV VRPHWLPHV FDOOHG D FXPXODWLYH FRQFDWHQDWLRQ ZKLFK LV D YDULDWLRQ RI WKH FXPXODWLYH VXP WHFKQLTXH VKRZQ HDUOLHU LQ WKH FKDSWHU $W WKH HQG RI WKH SUHFHGLQJ FRGH WKH YDULDEOH QR6SDFHV VWRUHV WKH 6WULQJ "theraininSpain" 7KH FRGH DOVR LOOXVWUDWHV WKDW FKDU YDOXHV FDQ EH FRQFDWHQDWHG RQWR D 6WULQJ OLNH RWKHU SULPLWLYH YDOXHV FDQ 7KHUH DUH VHYHUDO XVHIXO PHWKRGV WKDW FDQ EH FDOOHG WR FKHFN LQIRUPDWLRQ DERXW D FKDUDFWHU RU FRQYHUW RQH FKDUDFWHU LQWR DQRWKHU 5HPHPEHU WKDW FKDU LV D SULPLWLYH W\SH ZKLFK PHDQV WKDW \RX FDQ W XVH WKH objectName.methodName() V\QWD[ OLNH ZH GLG ZLWK 6WULQJV ,QVWHDG WKH PHWKRGV DUH VWDWLF PHWKRGV LQ WKH &KDUDFWHU FODVV ZKLFK DFFHSW FKDU SDUDPHWHUV DQG UHWXUQ DSSURSULDWH YDOXHV +HUH DUH VRPH RI WKH PRVW XVHIXO &KDUDFWHU PHWKRGV 8VHIXO 0HWKRGV RI WKH &KDUDFWHU &ODVV 0HWKRG getNumericValue(char) 'HVFULSWLRQ ([DPSOH FRQYHUWV D FKDUDFWHU WKDW Character.getNumericValue('6') UHWXUQV 6 ORRNV OLNH D QXPEHU LQWR WKDW QXPEHU ZKHWKHU RU QRW FKDUDFWHU LV WKURXJK ZKHWKHU RU QRW FKDUDFWHU LV LQ UDQJH D WR ] RU $ WR = Character.isDigit('X') isDigit(char) UHWXUQV false UHWXUQV true isLetter(char) Character.isLetter('f') isLowerCase(char) ZKHWKHU RU QRW FKDUDFWHU LV D Character.isLowerCase('Q') UHWXUQV false ORZHUFDVH OHWWHU ZKHWKHU RU QRW FKDUDFWHU LV DQ XSSHUFDVH OHWWHU WKH ORZHUFDVH YHUVLRQ RI WKH JLYHQ OHWWHU WKH XSSHUFDVH YHUVLRQ RI WKH JLYHQ OHWWHU Character.isUpperCase('Q') isUpperCase(char) UHWXUQV true UHWXUQV 'q' UHWXUQV 'X' toLowerCase(char) Character.toLowerCase('Q') toUpperCase(char) Character.toUpperCase('x') )RU H[DPSOH WKH IROORZLQJ SURJUDP FRXQWV WKH QXPEHU RI OHWWHUV LQ D 6WULQJ s LJQRULQJ DOO QRQ OHWWHU FKDUDFWHUV 243 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // Counts letter characters in a String. import java.util.*; // for Scanner public class CountLetters { public static void main(String args) { Scanner console = new Scanner(System.in); System.out.print("Type a word: "); String s = console.next(); int count = 0; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (Character.isLetter(ch)) { count++; } } System.out.println("Letters: " + count); } } 7KH SURJUDP SURGXFHV WKH IROORZLQJ RXWSXW IRU DQ LQSXW RI WHVW Type a word: test123TEST Letters: 8 7(67 (YHU\ FKDUDFWHU DOVR KDV D FRUUHVSRQGLQJ LQWHJHU YDOXH -DYD LV ZLOOLQJ WR DXWRPDWLFDOO\ FRQYHUW D YDOXH RI W\SH FKDU LQWR DQ LQW ZKHQHYHU LW LV H[SHFWLQJ DQ LQW )RU H[DPSOH \RX FDQ IRUP WKH IROORZLQJ UDWKHU VWUDQJH H[SUHVVLRQ 2 * 'a' -DYD VHHV D YDOXH RI W\SH FKDU ZKHUH LW ZDV H[SHFWLQJ DQ LQW EXW LW V KDSS\ WR FRQYHUW WKLV IRU \RX ,W WXUQV RXW WKDW WKH LQWHJHU YDOXH IRU D LV VR WKH H[SUHVVLRQ V UHVXOW LV -DYD ZLOO QRW KRZHYHU DXWRPDWLFDOO\ FRQYHUW LQ WKH RWKHU GLUHFWLRQ 7KDW LV EHFDXVH WKH W\SH FKDU LV LQ D VHQVH D VXEVHW RI WKH W\SH LQW (YHU\ FKDU YDOXH KDV D FRUUHVSRQGLQJ LQW EXW QRW HYHU\ LQW KDV D FRUUHVSRQGLQJ FKDU 7KDW PHDQV WKDW -DYD ZRXOG DOZD\V EH DEOH WR WXUQ D FKDU LQWR DQ LQW EXW PLJKW QRW DOZD\V EH DEOH WR WXUQ DQ LQW LQWR D FKDU 6R LQ WKLV FDVH -DYD LQVLVWV RQ D FDVW 7KLV DOORZV \RX WR IRUP H[SUHVVLRQV OLNH WKH IROORZLQJ (char) ('a' + 3) ,QVLGH WKH SDUHQWKHVHV ZH KDYH D FRPELQDWLRQ RI D FKDU YDOXH DQG DQ LQW YDOXH VR -DYD FRQYHUWV WKH FKDU WR DQ LQW DQG WKHQ SHUIRUPV WKH DGGLWLRQ RSHUDWLRQ \LHOGLQJ WKH YDOXH 7KLV LQWHJHU LV WKHQ FRQYHUWHG WR D FKDU YDOXH EHFDXVH RI WKH FDVW ZKLFK \LHOGV WKH FKDUDFWHU G WKH FKDUDFWHU WKDW DSSHDUV ODWHU LQ WKH VHTXHQFH WKDQ WKH FKDUDFWHU D 'LG \RX .QRZ $6&,, DQG 8QLFRGH :H VWRUH GDWD RQ D FRPSXWHU DV ELQDU\ QXPEHUV VHTXHQFHV RI V DQG V 7R VWRUH WH[WXDO GDWD ZH QHHG DQ HQFRGLQJ VFKHPH WKDW ZLOO WHOO XV ZKDW VHTXHQFH RI V DQG V WR XVH IRU DQ\ JLYHQ FKDUDFWHU 7KLQN RI LW DV EHLQJ OLNH D JLDQW VHFUHW GHFRGHU ULQJ WKDW VD\V WKLQJV OLNH ,I \RX ZDQW WR VWRUH D OLWWOH D WKHQ XVH WKH VHTXHQFH 244 ,Q WKH HDUO\ V ,%0 GHYHORSHG DQ HQFRGLQJ VFKHPH FDOOHG (%&',& WKDW ZRUNHG ZHOO ZLWK WKHLU SXQFKHG FDUGV 3XQFKHG FDUGV ZHUH LQ XVH IRU GHFDGHV EHIRUH FRPSXWHUV ZHUH HYHQ LQYHQWHG %XW LW VRRQ EHFDPH FOHDU WKDW (%&',& ZDVQ W D FRQYHQLHQW HQFRGLQJ VFKHPH IRU FRPSXWHU SURJUDPPHUV WR ZRUN ZLWK )RU H[DPSOH WKHUH ZHUH JDSV LQ WKH VHTXHQFH ZKHUH FKDUDFWHUV OLNH L DQG M ZHUH IDU DSDUW HYHQ WKRXJK ZH WKLQN RI WKHP DV FRPLQJ RQH DIWHU WKH RWKHU ,Q WKH $PHULFDQ 6WDQGDUGV $VVRFLDWLRQ SXEOLVKHG D VFKHPH NQRZQ DV $6&,, SURQRXQFHG $6 NHH WKDW KDV EHHQ LQ FRPPRQ XVH HYHU VLQFH 7KH DFURQ\P LV VKRUW IRU $PHULFDQ 6WDQGDUG &RGH IRU ,QIRUPDWLRQ ,QWHUFKDQJH ,Q LWV RULJLQDO IRUP $6&,, GHILQHG FKDUDFWHUV WKDW FRXOG EH VWRUHG ZLWK ELWV RI GDWD 7KH ELJJHVW SUREOHP ZLWK $6&,, LV WKDW LW LV DQ $PHULFDQ FRGH 7KHUH DUH PDQ\ FKDUDFWHUV LQ FRPPRQ XVH LQ RWKHU FRXQWULHV WKDW ZHUH QRW LQFOXGHG LQ $6&,, )RU H[DPSOH WKH %ULWLVK SRXQG DQG WKH 6SDQLVK YDULDQW RI WKH OHWWHU Q x DUH QRW LQFOXGHG LQ WKH VWDQGDUG $6&,, FKDUDFWHUV 9DULRXV DWWHPSWV KDYH EHHQ PDGH WR DGG H[WUD FKDUDFWHUV WR $6&,, WR GRXEOH LW WR FKDUDFWHUV WKDW LQFOXGH PDQ\ RI WKHVH VSHFLDO FKDUDFWHUV EXW LW WXUQV RXW WKDW FKDUDFWHUV LV VLPSO\ QRW HQRXJK WR FDSWXUH WKH LQFUHGLEOH GLYHUVLW\ RI KXPDQ FRPPXQLFDWLRQ $URXQG WKH WLPH WKDW -DYD ZDV FUHDWHG D FRQVRUWLXP RI VRIWZDUH SURIHVVLRQDOV LQWURGXFHG D QHZ VWDQGDUG IRU HQFRGLQJ FKDUDFWHUV NQRZQ DV 8QLFRGH 7KH\ GHFLGHG WKDW WKH ELWV RI VWDQGDUG $6&,, DQG WKH ELWV RI H[WHQGHG $6&,, ZHUH VLPSO\ QRW ELJ HQRXJK 7KH\ KDYH QRW VHW D OLPLW RQ KRZ PDQ\ ELWV WKH\ PLJKW XVH IRU HQFRGLQJ FKDUDFWHUV $V RI WKH WLPH RI ZULWLQJ WKH FRQVRUWLXP KDV LGHQWLILHG RYHU WKRXVDQG FKDUDFWHUV ZKLFK UHTXLUHV D OLWWOH RYHU ELWV WR VWRUH ,W LQFOXGHV WKH FKDUDFWHUV XVHG LQ PRVW PRGHUQ ODQJXDJHV DQG HYHQ VRPH DQFLHQW ODQJXDJHV ,W GRHV QRW \HW LQFOXGH (J\SWLDQ RU 0D\DQ KLHURJO\SKV DQG D SURSRVDO WR LQFOXGH .OLQJRQ FKDUDFWHUV ZDV UHMHFWHG E\ WKH FRQVRUWLXP 7KH GHVLJQHUV RI -DYD GHFLGHG WR XVH 8QLFRGH DV WKH VWDQGDUG IRU WKH W\SH FKDU ZKLFK PHDQV WKDW -DYD SURJUDPV DUH FDSDEOH RI PDQLSXODWLQJ D IXOO UDQJH RI FKDUDFWHUV )RUWXQDWHO\ WKH 8QLFRGH &RQVRUWLXP GHFLGHG WR LQFRUSRUDWH WKH $6&,, HQFRGLQJV VR $6&,, FDQ EH VHHQ DV D VXEVHW RI 8QLFRGH ,I \RX DUH FXULRXV DERXW WKH DFWXDO RUGHULQJ RI FKDUDFWHUV LQ $6&,, W\SH $6&,, WDEOH LQWR \RXU IDYRULWH VHDUFK HQJLQH DQG \RX ZLOO ILQG PLOOLRQV RI KLWV WR H[SORUH 6\VWHP RXW SULQWI 6R IDU ZH YH VHHQ 6\VWHP RXW SULQWOQ DQG 6\VWHP RXW SULQW IRU FRQVROH RXWSXW 7KHUH V D WKLUG PHWKRG QDPHG 6\VWHP RXW SULQWI WKDW LV D ELW PRUH FRPSOLFDWHG WKDQ WKH RWKHUV EXW JLYHV XV VRPH XVHIXO QHZ DELOLWLHV 7KH I LQ SULQWI VWDQGV IRU IRUPDWWHG LPSO\LQJ WKDW 6\VWHP RXW SULQWI JLYHV \RX PRUH FRQWURO RYHU WKH IRUPDW LQ ZKLFK \RXU RXWSXW LV SULQWHG ,PDJLQH WKDW \RX G OLNH WR SULQW D PXOWLSOLFDWLRQ WDEOH IURP SULQWV WKH FRUUHFW QXPEHUV EXW LW GRHVQ W ORRN YHU\ QLFH XS WR 7KH IROORZLQJ FRGH 245 for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) { System.out.print(i * j + " "); } System.out.println(); } 7KH RXWSXW LV WKH IROORZLQJ 1RWLFH WKDW WKH QXPEHUV GRQ W OLQH XS KRUL]RQWDOO\ 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100 6HSDUDWLQJ WKH QXPEHUV E\ WDEV LV EHWWHU EXW WKH QXPEHUV DUH OHIW DOLJQHG OLNH LQ WKH VKRUWHQHG WDEOH EHORZ :KDW LI ZH ZDQW WKHP ULJKW DOLJQHG" 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25 ,W ZRXOG EH D SDLQ WR WU\ WR ULJKW DOLJQ WKH QXPEHUV EHFDXVH ZH G KDYH WR XVH LI HOVH VWDWHPHQWV WR FKHFN ZKHWKHU D JLYHQ QXPEHU ZDV LQ D FHUWDLQ UDQJH DQG SDG LW ZLWK D JLYHQ QXPEHU RI VSDFHV $ PXFK HDVLHU ZD\ WR SULQW YDOXHV DOLJQHG LQ IL[HG ZLGWK ILHOGV LV WKH 6\VWHP RXW SULQWI FRPPDQG 7KH SULQWI PHWKRG DFFHSWV D VSHFLDOO\ ZULWWHQ 6WULQJ FDOOHG D IRUPDW 6WULQJ VSHFLI\LQJ WKH JHQHUDO DSSHDUDQFH RI WKH RXWSXW IROORZHG E\ DQ\ SDUDPHWHUV WR EH SULQWHG System.out.printf(<format String>, <parameters>); $ IRUPDW 6WULQJ LV OLNH D QRUPDO 6WULQJ H[FHSW WKDW LW FDQ FRQWDLQ SODFHKROGHUV FDOOHG IRUPDW VSHFLILHUV ZKHUH \RX VSHFLI\ D ORFDWLRQ IRU LQVHUWLRQ RI D YDULDEOH V YDOXH DORQJ ZLWK WKH IRUPDW \RX G OLNH WR JLYH WKDW YDOXH )RUPDW VSHFLILHUV EHJLQ ZLWK D VLJQ DQG HQG ZLWK D OHWWHU VSHFLI\LQJ WKH NLQG RI YDOXH VXFK DV d IRU GHFLPDO LQWHJHUV LQW RU f IRU IORDWLQJ SRLQW UHDO QXPEHUV GRXEOH )RU H[DPSOH WKH IROORZLQJ FRGH SULQWV DQ [ DQG \ FRRUGLQDWH VXUURXQGHG E\ SDUHQWKHVHV int x = 38, y = -152; System.out.printf("%d %d", x, y); System.out.printf("location: (%d, %d)", x, y); // 38 -152 // location: (38, -152) 7KH %d LV QRW DFWXDOO\ SULQWHG RXW EXW LV LQVWHDG UHSODFHG ZLWK WKH FRUUHVSRQGLQJ SDUDPHWHU ZULWWHQ DIWHU WKH IRUPDW 6WULQJ 7KH QXPEHU RI IRUPDW VSHFLILHUV LQ WKH IRUPDW 6WULQJ PXVW PDWFK WKH QXPEHU RI SDUDPHWHUV WKDW IROORZ LW 7KH ILUVW VSHFLILHU ZLOO EH UHSODFHG E\ WKH ILUVW SDUDPHWHU WKH VHFRQG VSHFLILHU E\ WKH VHFRQG SDUDPHWHU DQG VR RQ 6\VWHP RXW SULQWI LV D EL]DUUH PHWKRG LQ WKLV ZD\ EHFDXVH LW FDQ DFFHSW D YDU\LQJ QXPEHU RI SDUDPHWHUV 246 7KH SULQWI FRPPDQG LV OLNH 6\VWHP RXW SULQW LQ WKDW LW GRHVQ W PRYH WR D QHZ OLQH XQOHVV \RX H[SOLFLWO\ WHOO LW WR GR VR $ EHWWHU IRUPDW 6WULQJ DERYH PLJKW KDYH EHHQ ORFDWLRQ G G ?Q EHFDXVH LW ZRXOG FRPSOHWH WKH OLQH )RUPDW VSHFLILHUV FDQ FRQWDLQ LQIRUPDWLRQ EHWZHHQ WKH DQG WKH OHWWHU IRU WKH W\SH WR VSHFLI\ WKH ZLGWK SUHFLVLRQ DQG DOLJQPHQW RI WKH YDOXH )RU H[DPSOH %8d VSHFLILHV DQ LQWHJHU ULJKW DOLJQHG LQ DQ VSDFH ZLGH DUHD DQG %12.4f VSHFLILHV D GRXEOH YDOXH ULJKW DOLJQHG LQ D VSDFH ZLGH DUHD URXQGHG WR GLJLWV SDVW WKH GHFLPDO SRLQW 7KH IROORZLQJ WDEOH OLVWV VRPH FRPPRQ IRUPDW VSHFLILHUV \RX PD\ ZLVK WR XVH LQ \RXU SURJUDPV 6SHFLILHU %d %8d %-6d %f %12f %.2f %16.3f %s %8s %-9s 5HVXOW LQWHJHU LQWHJHU ULJKW DOLJQHG LQWHJHU OHIW DOLJQHG UHDO QXPEHU UHDO QXPEHU ULJKW DOLJQHG VSDFH ZLGH ILHOG VSDFH ZLGH ILHOG UHDO QXPEHU URXQGHG WR QHDUHVW KXQGUHGWK UHDO QXPEHU URXQGHG WR QHDUHVW WKRXVDQGWK VWULQJ VWULQJ ULJKW DOLJQHG VWULQJ OHIW DOLJQHG VSDFH ZLGH ILHOG VSDFH ZLGH ILHOG VSDFH ZLGH ILHOG VSDFH ZLGH ILHOG $V D FRPSUHKHQVLYH H[DPSOH FRQVLGHU WKDW WKH IROORZLQJ YDULDEOHV KDYH EHHQ GHFODUHG int x = 38, y = -152; int grade = 86; double angle = 87.4163; String veggie = "carrot"; 7KH IROORZLQJ FRGH VDPSOH SULQWV WKH SUHFHGLQJ YDULDEOHV ZLWK VHYHUDO IRUPDW VSHFLILHUV System.out.printf("hello there\n"); System.out.printf("x=%d and y=%d\n", x, y); System.out.printf("%d%%\n", (grade + 5)); System.out.printf("!%d!%6d%6d\n", grade, x, y); System.out.printf("%.2f %10.1f\n", angle); System.out.printf("%s%12s!%-8s!\n", veggie, veggie, veggie); 7KH FRGH SURGXFHV WKH IROORZLQJ RXWSXW hello there x=38 and y=-152 91% !86! 38 -152 87.42 87.4 carrot carrot!carrot ! 247 /HW V UHWXUQ WR RXU PXOWLSOLFDWLRQ WDEOH H[DPSOH 1RZ WKDW ZH NQRZ DERXW SULQWI ZH FDQ SULQW WKH WDEOH ZLWK ULJKW DOLJQHG QXPEHUV UHODWLYHO\ HDVLO\ :H OO ULJKW DOLJQ WKH QXPEHUV LQWR ILHOGV RI ZLGWK for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) { System.out.printf("%5d", i * j); } System.out.println(); } 7KH FRGH SURGXFHV WKH IROORZLQJ RXWSXW 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100 ,Q IDFW LW V DOVR UHODWLYHO\ VLPSOH WR GR D GLYLVLRQ WDEOH VKRXOG \RX HYHU QHHG RQH :H OO XVH GRXEOH YDOXHV DQG VSHFLI\ D OLPLWHG QXPEHU RI GLJLWV DIWHU WKH GHFLPDO SRLQW for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) { System.out.printf("%8.3f", (double) i / j); } System.out.println(); } 7KH FRGH SURGXFHV WKH IROORZLQJ RXWSXW 1.000 2.000 3.000 4.000 5.000 0.500 1.000 1.500 2.000 2.500 0.333 0.667 1.000 1.333 1.667 0.250 0.500 0.750 1.000 1.250 0.200 0.400 0.600 0.800 1.000 $V D ILQDO H[DPSOH SULQWI FDQ HOHJDQWO\ VROYH WKH SUREOHP ZLWK WKH 5RXQGRII SURJUDP LQWURGXFHG LQ D SUHYLRXV VHFWLRQ %\ IL[LQJ WKH SUHFLVLRQ RI WKH GRXEOH YDOXH LW ZLOO EH URXQGHG WR DYRLG WKH WLQ\ URXQGRII PLVWDNHV WKDW UHVXOW IURP GRXEOH DULWKPHWLF +HUH LV WKH IL[HG SURJUDP 1 2 3 4 5 6 7 8 9 public class Roundoff2 { public static void main(String args) { double n = 1.0; for (int i = 1; i <= 10; i++) { n += 0.1; System.out.printf("%3.1f\n", n); } } } 248 7KH SURJUDP SURGXFHV WKH IROORZLQJ RXWSXW 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 0HWKRGV ZLWK &RQGLWLRQDO ([HFXWLRQ :H OHDUQHG D JUHDW GHDO DERXW PHWKRGV LQ &KDSWHU :H OHDUQHG DERXW KRZ WR XVH SDUDPHWHUV WR SDVV YDOXHV LQWR D PHWKRG DQG KRZ WR XVH D UHWXUQ VWDWHPHQW WR KDYH D PHWKRG UHWXUQ D YDOXH 1RZ WKDW ZH KDYH VHHQ FRQGLWLRQDO H[HFXWLRQ ZH QHHG WR UHYLVLW WKHVH LVVXHV VR WKDW ZH FDQ JDLQ D GHHSHU XQGHUVWDQGLQJ RI WKHP 3UHFRQGLWLRQV DQG 3RVWFRQGLWLRQV (YHU\ WLPH \RX ZULWH D PHWKRG \RX VKRXOG WKLQN DERXW H[DFWO\ ZKDW WKDW PHWKRG LV VXSSRVHG WR DFFRPSOLVK <RX FDQ GHVFULEH KRZ D PHWKRG ZRUNV E\ GHVFULELQJ WKH SUHFRQGLWLRQV WKDW PXVW EH WUXH EHIRUH LW H[HFXWHV DQG WKH SRVWFRQGLWLRQV WKDW ZLOO EH WUXH DIWHU LW LV GRQH H[HFXWLQJ 3UHFRQGLWLRQ $ FRQGLWLRQ WKDW PXVW EH WUXH EHIRUH D PHWKRG H[HFXWHV WR JXDUDQWHH WKDW LW FDQ SHUIRUP LWV WDVN 3RVWFRQGLWLRQ $ FRQGLWLRQ WKDW WKH PHWKRG JXDUDQWHHV ZLOO EH WUXH DIWHU LW ILQLVKHV H[HFXWLQJ DV ORQJ DV WKH SUHFRQGLWLRQV ZHUH WUXH EHIRUH LW ZDV FDOOHG )RU H[DPSOH WR GHVFULEH WKH MRE RI D SHUVRQ RQ DQ DXWR DVVHPEO\ OLQH \RX PLJKW XVH D SRVWFRQGLWLRQ OLNH 7KH EROWV WKDW VHFXUH WKH OHIW IURQW WLUH DUH RQ WKH FDU DQG WLJKW %XW SRVWFRQGLWLRQV DUH QRW WKH ZKROH VWRU\ (PSOR\HHV RQ DQ DVVHPEO\ OLQH GHSHQG RQ HDFK RWKHU $ OLQH ZRUNHU FDQ W DGG EROWV DQG WLJKWHQ WKHP LI WKH OHIW WLUH LVQ W WKHUH RU LI WKHUH DUH QR EROWV 6R WKH DVVHPEO\ OLQH ZRUNHU PLJKW KDYH SUHFRQGLWLRQV OLNH WKH OHIW WLUH LV PRXQWHG SURSHUO\ RQ WKH FDU WKHUH DUH DW OHDVW EROWV LQ WKH VXSSO\ ER[ DQG D ZRUNLQJ ZUHQFK LV DYDLODEOH <RX GHVFULEH WKH WDVN IXOO\ WKHQ E\ VD\LQJ WKDW WKH ZRUNHU FDQ PDNH WKH SRVWFRQGLWLRQV WUXH LI WKH SUHFRQGLWLRQV DUH WUXH EHIRUH VWDUWLQJ 0HWKRGV OLNH ZRUNHUV RQ DQ DVVHPEO\ OLQH QHHG WR ZRUN WRJHWKHU HDFK VROYLQJ LWV SRUWLRQ RI WKH WDVN LQ RUGHU WR VROYH WKH RYHUDOO WDVN 7KH SUHFRQGLWLRQV DQG SRVWFRQGLWLRQV GHVFULEH WKH GHSHQGHQFLHV EHWZHHQ PHWKRGV 7R XQGHUVWDQG WKLV EHWWHU FRQVLGHU WKLV QRQSURJUDPPLQJ H[DPSOH ZLWK WKH IROORZLQJ PHWKRGV DQG WKHLU FRQGLWLRQV 249 makeBatter pre : post: bakeCake pre : post: washDishes pre : post: bowl is clean. bowl has batter in it; bowl is dirty. bowl has batter in it; pan is clean. cake is baked; pan is dirty. none. bowl and pan are clean. :KHQ \RX FDOO WKHVH PHWKRGV \RX PXVW ILW WKHP WRJHWKHU VR WKH SUHFRQGLWLRQV DUH DOZD\V VDWLVILHG EHIRUH D PHWKRG H[HFXWHV )RU H[DPSOH LI \RX VXSSRVH WKDW WKH ERZO DQG SDQ DUH LQLWLDOO\ FOHDQ \RX FDQ PDNH D FDNH DV IROORZV makeBatter. bakeCake. +HUH LV D WUDFH RI LWV H[HFXWLRQ --> bowl and pan are clean. makeBatter. --> bowl has batter in it; bowl is dirty; pan is clean. bakeCake. --> cake is baked; bowl and pan are dirty. 7KH SUHFRQGLWLRQV DUH VDWLVILHG EHIRUH HDFK PHWKRG LV H[HFXWHG +RZHYHU LI \RX ZDQW D ORRS WR PDNH FDNHV \RX FDQ W GR LW WKLV ZD\ for (many cakes) { makeBatter. bakeCake. } 7KH ILUVW FDNH LV PDGH SURSHUO\ EXW QRW WKH VHFRQG 7KH HUURU RFFXUV EHFDXVH WKH SUHFRQGLWLRQV IRU PDNH%DWWHU DUH QRW VDWLVILHG RQ WKH VHFRQG H[HFXWLRQ RI WKH ORRS <RX QHHG D FOHDQ ERZO DQG SDQ WR H[HFXWH PDNH%DWWHU --> bowl and pan are clean. makeBatter. --> bowl has batter in it; bowl is dirty; pan is clean. bakeCake. --> cake is baked; bowl and pan are dirty. makeBatter. <RX QHHG WR FKDQJH \RXU VROXWLRQ for (many cakes) { makeBatter. bakeCake. washDishes. } 250 7KH H[HFXWLRQ RI ZDVK'LVKHV OHDYHV \RX ZLWK D FOHDQ ERZO DQG SDQ DQG JXDUDQWHHV WKDW \RX VDWLVI\ WKH SUHFRQGLWLRQV RI PDNH%DWWHU RQ WKH QH[W LWHUDWLRQ 7KURZLQJ ([FHSWLRQV :H KDYH VHHQ VHYHUDO FDVHV ZKHUH -DYD PLJKW WKURZ DQ H[FHSWLRQ )RU H[DPSOH LI ZH KDYH D FRQVROH 6FDQQHU DQG ZH FDOO QH[W,QW DQ H[FHSWLRQ ZLOO EH WKURZQ LI WKH XVHU W\SHV VRPHWKLQJ WKDW LVQ W DQ LQW ,Q &KDSWHU ZH ZLOO VHH KRZ \RX FDQ KDQGOH H[FHSWLRQV )RU QRZ ZH MXVW ZDQW WR H[SORUH VRPH RI WKH ZD\V LQ ZKLFK H[FHSWLRQV FDQ RFFXU DQG KRZ ZH PLJKW ZDQW WR JHQHUDWH WKHP LQ RXU RZQ FRGH ,GHDOO\ SURJUDPV H[HFXWH ZLWKRXW JHQHUDWLQJ DQ\ HUURUV EXW LQ SUDFWLFH YDULRXV SUREOHPV DULVH <RX PLJKW DVN WKH XVHU IRU DQ LQWHJHU DQG WKH XVHU ZLOO DFFLGHQWDOO\ RU SHUKDSV HYHQ PDOLFLRXVO\ W\SH VRPHWKLQJ WKDW LV QRW DQ LQWHJHU 2U \RX PLJKW H[HFXWH FRGH WKDW KDV D EXJ LQ LW %HORZ LV D SURJUDP WKDW DOZD\V WKURZV DQ H[FHSWLRQ EHFDXVH LW WULHV WR FRPSXWH WKH YDOXH RI GLYLGHG E\ ZKLFK LV PDWKHPDWLFDOO\ XQGHILQHG 1 2 3 4 5 6 public class CauseException { public static void main(String args) { int x = 1 / 0; System.out.println(x); } } :KHQ \RX UXQ WKH SURJUDP \RX JHW WKH IROORZLQJ HUURU PHVVDJH Exception in thread "main" java.lang.ArithmeticException: / by zero at CauseException.main(CauseException.java:3) 7KH SUREOHP LV LQ OLQH ZKHQ \RX DVN -DYD WR FRPSXWH D YDOXH WKDW FDQ W EH VWRUHG DV DQ LQW :KDW LV -DYD VXSSRVHG WR GR ZLWK WKDW YDOXH" ,W FRXOG LJQRUH WKH HUURU DQG FRQWLQXH H[HFXWLQJ EXW WKDW PD\ QRW EH WKH ULJKW WKLQJ WR GR ,QVWHDG -DYD WKURZV DQ H[FHSWLRQ WKDW VWRSV WKH SURJUDP IURP H[HFXWLQJ DQG JLYHV XV D ZDUQLQJ WKDW DQ DULWKPHWLF H[FHSWLRQ RFFXUUHG ZKLOH H[HFXWLQJ WKLV OLQH RI FRGH ,W LV ZRUWK QRWLQJ WKDW GLYLVLRQ E\ GRHV QRW DOZD\V SURGXFH DQ H[FHSWLRQ :H ZRQ W JHW DQ H[FHSWLRQ LI ZH H[HFXWH WKLV OLQH RI FRGH LQVWHDG double x = 1.0 / 0.0; 7KH SURJUDP H[HFXWHV QRUPDOO\ DQG SURGXFHV WKH RXWSXW ,QILQLW\ 7KLV LV EHFDXVH IORDWLQJ SRLQW QXPEHUV IROORZ D VWDQGDUG IURP WKH ,((( WKH ,QVWLWXWH RI (OHFWULFDO DQG (OHFWURQLFV (QJLQHHUV WKDW GHILQHV H[DFWO\ ZKDW VKRXOG KDSSHQ LQ WKHVH FDVHV DQG WKHUH DUH VSHFLDO YDOXHV UHSUHVHQWLQJ LQILQLW\ DQG VRPHWKLQJ FDOOHG 1D1 1RW D 1XPEHU :H PD\ ZDQW WR WKURZ H[FHSWLRQV RXUVHOYHV LQ WKH FRGH ZH ZULWH ,Q SDUWLFXODU LW LV D JRRG LGHD WR WKURZ DQ H[FHSWLRQ LI D SUHFRQGLWLRQ IDLOV )RU H[DPSOH VXSSRVH WKDW ZH ZDQW WR ZULWH D PHWKRG IRU FRPSXWLQJ WKH IDFWRULDO RI DQ LQWHJHU 7KH IDFWRULDO LV GHILQHG DV IROORZV n! (which is read as "n factorial") = 1 * 2 * 3 * ... * n 251 :H FDQ ZULWH D -DYD PHWKRG WKDW XVHV D FXPXODWLYH SURGXFW WR FRPSXWH WKLV UHVXOW public static int factorial(int n) { int product = 1; for (int i = 2; i <= n; i++) { product *= i; } return product; } :H FRXOG WHVW WKH PHWKRG IRU YDULRXV YDOXHV ZLWK D ORRS for (int i = 0; i <= 10; i++) { System.out.println(i + "! = " + factorial(i)); } ZKLFK SURGXFHV WKH IROORZLQJ RXWSXW 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880 10! = 3628800 ,W VHHPV RGG WKDW WKH IDFWRULDO PHWKRG VKRXOG UHWXUQ ZKHQ \RX DVN IRU EXW WKDW LV DFWXDOO\ SDUW RI WKH PDWKHPDWLFDO GHILQLWLRQ RI WKH IDFWRULDO IXQFWLRQ ,W LV UHWXUQLQJ EHFDXVH WKH ORFDO YDULDEOH SURGXFW LQ WKH IDFWRULDO PHWKRG LV LQLWLDOL]HG WR DQG ZH QHYHU HQWHU WKH ORRS ZKHQ WKH SDUDPHWHU Q KDV WKH YDOXH 6R WKLV LV DFWXDOO\ GHVLUDEOH EHKDYLRU IRU %XW ZKDW LI ZH DUH DVNHG WR FRPSXWH WKH IDFWRULDO RI D QHJDWLYH QXPEHU" 7KH PHWKRG UHWXUQV WKH VDPH YDOXH RI 7KH PDWKHPDWLFDO GHILQLWLRQ RI IDFWRULDO VD\V WKDW WKH IXQFWLRQ LV XQGHILQHG IRU QHJDWLYH YDOXHV RI Q 6R ZH VKRXOGQ W HYHQ FRPSXWH DQ DQVZHU ZKHQ Q LV QHJDWLYH 7KLV LV D SUHFRQGLWLRQ RI WKH PHWKRG WKDW ZH FDQ GHVFULEH LQ GRFXPHQWDWLRQ // pre : n >= 0 // post: returns n factorial (n!) $GGLQJ WKH FRPPHQW DERXW WKLV UHVWULFWLRQ LV KHOSIXO EXW ZKDW LI VRPHRQH FDOOV RXU IDFWRULDO PHWKRG ZLWK D QHJDWLYH YDOXH DQ\ZD\" 7KH EHVW VROXWLRQ LV WR WKURZ DQ H[FHSWLRQ 7KH JHQHUDO V\QWD[ RI WKH H[FHSWLRQ VWDWHPHQW LV DV IROORZV throw <exception>; ,Q -DYD H[FHSWLRQV DUH REMHFWV %HIRUH ZH FDQ WKURZ DQ H[FHSWLRQ ZH KDYH WR FRQVWUXFW DQ H[FHSWLRQ REMHFW XVLQJ QHZ :H QRUPDOO\ FRQVWUXFW WKH REMHFW DV ZH DUH WKURZLQJ LW EHFDXVH WKH H[FHSWLRQ REMHFW LQFOXGHV LQIRUPDWLRQ DERXW ZKDW ZDV JRLQJ RQ ZKHQ WKLV HUURU RFFXUUHG -DYD KDV D FODVV 252 FDOOHG ,OOHJDO$UJXPHQW([FHSWLRQ WKDW LV PHDQW WR FRYHU D FDVH OLNH WKLV ZKHUH VRPHRQH KDV SDVVHG DQ LQDSSURSULDWH YDOXH DV DQ DUJXPHQW 6R ZH FDQ FRQVWUXFW WKH H[FHSWLRQ REMHFW DQG LQFOXGH LW LQ D WKURZ VWDWHPHQW DV IROORZV throw new IllegalArgumentException(); 2I FRXUVH ZH RQO\ ZDQW WR GR WKLV LQ WKH FDVH ZKHUH WKH SUHFRQGLWLRQ IDLOV VR ZH QHHG WR LQFOXGH WKLV LQVLGH RI DQ LI VWDWHPHQW if (n < 0) { throw new IllegalArgumentException(); } <RX FDQ DOVR LQFOXGH VRPH WH[W ZKHQ \RX FRQVWUXFW WKH H[FHSWLRQ WKDW ZLOO EH GLVSOD\HG ZKHQ WKH H[FHSWLRQ LV WKURZQ if (n < 0) { throw new IllegalArgumentException("negative value in factorial"); } ,QFRUSRUDWLQJ WKH SUH SRVW FRPPHQWV DQG WKH H[FHSWLRQ FRGH LQWR RXU GHILQLWLRQ RI WKH PHWKRG ZH REWDLQ WKH IROORZLQJ // pre : n >= 0 // post: returns n factorial (n!) public static int factorial(int n) { if (n < 0) { throw new IllegalArgumentException("negative value in factorial"); } int product = 1; for (int i = 2; i <= n; i++) { product *= i; } return product; } :H GRQ W QHHG DQ HOVH DIWHU WKH LI WKDW WKURZV WKH H[FHSWLRQ EHFDXVH ZKHQ DQ H[FHSWLRQ LV WKURZQ LW KDOWV WKH H[HFXWLRQ RI WKH PHWKRG 6R LI VRPHRQH FDOOV WKH IDFWRULDO PHWKRG ZLWK D QHJDWLYH YDOXH RI Q -DYD ZLOO QHYHU H[HFXWH WKH FRGH WKDW FRPHV DIWHU WKH WKURZ VWDWHPHQW :H FDQ WHVW WKLV ZLWK WKH IROORZLQJ PDLQ PHWKRG public static void main(String args) { System.out.println(factorial(-1)); } :KHQ \RX H[HFXWH WKLV SURJUDP LW VWRSV H[HFXWLQJ ZLWK WKH IROORZLQJ PHVVDJH Exception in thread "main" java.lang.IllegalArgumentException: negative value in factorial at Factorial2.factorial(Factorial2.java:8) at Factorial2.main(Factorial2.java:3) 253 7KH PHVVDJH LQGLFDWHV WKDW WKH SURJUDP )DFWRULDO VWRSSHG UXQQLQJ EHFDXVH DQ ,OOHJDO$UJXPHQW([FHSWLRQ ZDV WKURZQ 7KH V\VWHP WKHQ VKRZV \RX D EDFNZDUG WUDFH RI KRZ LW JRW WKHUH ,W ZDV LQ OLQH RI WKH IDFWRULDO PHWKRG RI WKH )DFWRULDO FODVV ,W JRW WKHUH EHFDXVH RI D FDOO LQ OLQH RI PHWKRG PDLQ RI WKH )DFWRULDO FODVV 7KLV NLQG RI LQIRUPDWLRQ LV YHU\ KHOSIXO LQ ILJXULQJ RXW ZKHUH WKH EXJV DUH LQ \RXU SURJUDPV 7KURZLQJ H[FHSWLRQV LV DQ H[DPSOH RI GHIHQVLYH SURJUDPPLQJ :H GRQ W LQWHQG WR KDYH EXJV LQ WKH SURJUDPV ZH ZULWH EXW ZH UH RQO\ KXPDQ VR ZH ZDQW WR EXLOG LQ PHFKDQLVPV WKDW ZLOO JLYH XV IHHGEDFN ZKHQ ZH PDNH PLVWDNHV 7HVWLQJ WKH YDOXHV SDVVHG WR PHWKRGV DQG WKURZLQJ ,OOHJDO$UJXPHQW([FHSWLRQ ZKHQ D YDOXH LV QRW DSSURSULDWH LV D JUHDW ZD\ WR SURYLGH WKDW IHHGEDFN 5HYLVLWLQJ 5HWXUQ 9DOXHV ,Q FKDSWHU ZH VDZ H[DPSOHV RI VLPSOH FDOFXODWLQJ PHWKRGV WKDW ZRXOG UHWXUQ D YDOXH DV LQ WKLV PHWKRG IRU FRQYHUWLQJ IHHW WR PLOHV public static double miles(double feet) { return feet / 5280.0; } 1RZ WKDW ZH NQRZ KRZ WR ZULWH LI HOVH VWDWHPHQWV ZH FDQ KDYH PXFK PRUH LQWHUHVWLQJ H[DPSOHV LQYROYLQJ UHWXUQ YDOXHV )RU H[DPSOH ZH VDZ WKDW WKH 0DWK FODVV KDV D PHWKRG FDOOHG PD[ WKDW UHWXUQV WKH ODUJHU RI WZR YDOXHV 7KHUH DUH DFWXDOO\ WZR GLIIHUHQW YHUVLRQV RI WKH PHWKRG RQH WKDW ILQGV WKH PD[ RI WZR LQWV DQG RQH WKDW ILQGV WKH PD[ RI WZR GRXEOHV 5HFDOO WKDW WKLV LV FDOOHG RYHUORDGLQJ /HW V ZULWH RXU RZQ YHUVLRQ RI WKH PD[ PHWKRG WKDW UHWXUQV WKH ODUJHU RI WZR LQWV ,WV KHDGHU ZLOO ORRN OLNH WKLV public static int max(int x, int y) { ... } :H HLWKHU ZDQW WR UHWXUQ [ RU UHWXUQ \ GHSHQGLQJ XSRQ ZKLFK RQH LV ODUJHU 7KLV LV D SHUIHFW SODFH WR XVH DQ LI HOVH public static int max(int x, int y) { if (x > y) { return x; } else { return y; } } 7KLV EHJLQV E\ WHVWLQJ ZKHWKHU [ LV JUHDWHU WKDQ \ ,I LW LV LW H[HFXWHV WKH ILUVW EUDQFK E\ UHWXUQLQJ [ ,I QRW LW H[HFXWHV WKH HOVH EUDQFK E\ UHWXUQLQJ \ 7KHUH DUH WKUHH GLIIHUHQW FDVHV WR FRQVLGHU [ PLJKW EH ODUJHU \ PLJKW EH ODUJHU RU WKH\ PLJKW EH HTXDO 7KH FRGH DERYH H[HFXWHV WKH HOVH EUDQFK ZKHQ WKH YDOXHV DUH HTXDO EXW LW GRHVQ W PDWWHU ZKLFK UHWXUQ VWDWHPHQW LV H[HFXWHG ZKHQ WKH\ DUH HTXDO 254 5HPHPEHU WKDW ZKHQ -DYD H[HFXWHV D UHWXUQ VWDWHPHQW WKH PHWKRG VWRSV H[HFXWLQJ ,W V OLNH D FRPPDQG WR -DYD WR JHW RXW RI WKLV PHWKRG ULJKW QRZ 7KDW PHDQV WKDW WKLV PHWKRG FDQ DOVR EH ZULWWHQ DV IROORZV public static int max(int x, int y) { if (x > y) { return x; } return y; } 7KLV YHUVLRQ LV HTXLYDOHQW LQ EHKDYLRU EHFDXVH WKH VWDWHPHQW UHWXUQ [ LQVLGH WKH LI VWDWHPHQW ZLOO FDXVH -DYD WR H[LW WKH PHWKRG LPPHGLDWHO\ DQG QRW WR H[HFXWH WKH UHWXUQ \ VWDWHPHQW WKDW FRPHV DIWHU WKH LI ,I RQ WKH RWKHU KDQG ZH GRQ W HQWHU WKH LI WKHQ ZH SURFHHG WR WKH VWDWHPHQW WKDW IROORZV LW UHWXUQ \ :KHWKHU \RX XVH WKH ILUVW IRUP RU WKH VHFRQG GHSHQGV VRPHZKDW RQ SHUVRQDO WDVWH 7KH LI HOVH PDNHV LW PRUH FOHDU WKDW WKH PHWKRG LV FKRRVLQJ EHWZHHQ WZR DOWHUQDWLYHV %XW VRPH SHRSOH SUHIHU WKH VHFRQG EHFDXVH LW LV VKRUWHU 8QIRUWXQDWHO\ ZH FDQ W UHO\ RQ 6XQ WR EUHDN WKH WLH EHFDXVH WKH -DYD VRXUFH FRGH H[SUHVVHV WKLV XVLQJ VRPHWKLQJ NQRZQ DV WKH WHUQDU\ RSHUDWRU D PLQRU GHWDLO RI -DYD WKDW \RX FDQ UHDG DERXW E\ JRRJOLQJ -DYD WHUQDU\ RSHUDWRU $V DQRWKHU H[DPSOH FRQVLGHU WKH LQGH[2I PHWKRG LQ WKH 6WULQJ FODVV ,I ZH GHILQH D YDULDEOH V WKDW VWRUHV D 6WULQJ String s = "four score and seven years ago"; :H FDQ ZULWH H[SUHVVLRQV OLNH WKH IROORZLQJ WR GHWHUPLQH ZKHUH D SDUWLFXODU FKDUDFWHU DSSHDUV LQ WKH 6WULQJ int r = s.indexOf('r'); int v = s.indexOf('v'); 7KLV FRGH VHWV U WR EHFDXVH WKDW LV WKH LQGH[ RI WKH ILUVW RFFXUUHQFH RI WKH OHWWHU U LQ WKH 6WULQJ ,W VHWV Y WR EHFDXVH WKDW LV WKH LQGH[ RI WKH ILUVW RFFXUUHQFH RI WKH OHWWHU Y LQ WKH 6WULQJ 7KH LQGH[2I PHWKRG LV SDUW RI WKH 6WULQJ FODVV EXW OHW V VHH KRZ ZH FRXOG ZULWH D PHWKRG WKDW SHUIRUPV WKH VDPH WDVN 2EYLRXVO\ RXU PHWKRG ZRXOG EH FDOOHG GLIIHUHQWO\ :H ZRXOG KDYH WR SDVV LW ERWK WKH 6WULQJ DQG WKH OHWWHU DV LQ int r = indexOf('r', s); int v = indexOf('v', s); 6R WKH KHDGHU IRU RXU PHWKRG ZRXOG ORRN OLNH WKLV public static int indexOf(char ch, String s) { ... } 255 5HPHPEHU WKDW ZKHQ D PHWKRG UHWXUQV D YDOXH \RX KDYH WR LQFOXGH WKH UHWXUQ W\SH DIWHU WKH ZRUGV SXEOLF VWDWLF ,Q WKLV FDVH ZH KDYH LQGLFDWHG WKDW WKH PHWKRG UHWXUQV DQ LQW EHFDXVH WKH LQGH[ ZLOO EH DQ LQWHJHU 7KLV SUREOHP FDQ EH VROYHG UDWKHU QLFHO\ ZLWK D IRU ORRS WKDW JRHV WKURXJK HDFK SRVVLEOH LQGH[ IURP ILUVW WR ODVW :H FDQ GHVFULEH WKLV LQ SVHXGRFRGH DV IROORZV for (int i = 0; i < s.length(); i++) { if char is at position i, then we've found it. } 7R IOHVK WKLV RXW ZH KDYH WR WKLQN DERXW KRZ WR WHVW ZKHWKHU WKH FKDUDFWHU DW SRVLWLRQ L LV WKH RQH ZH DUH ORRNLQJ IRU 5HPHPEHU WKDW 6WULQJ REMHFWV KDYH D PHWKRG FDOOHG FKDU$W WKDW DOORZV XV WR SXOO RXW DQ LQGLYLGXDO FKDUDFWHU IURP WKH 6WULQJ 6R ZH FDQ UHILQH RXU SVHXGRFRGH DV IROORZV for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == ch) { then we've found it. } } 7R FRPSOHWH WKLV ZH KDYH WR UHILQH ZKDW WR GR ZKHQ ZH YH IRXQG LW ,I ZH ILQG WKH FKDUDFWHU WKHQ ZH KDYH RXU DQVZHU 7KH DQVZHU ZLOO EH WKH FXUUHQW YDOXH RI WKH YDULDEOH L $QG LI WKDW LV WKH DQVZHU ZH ZDQW WR UHWXUQ WKHQ ZH FDQ SXW D UHWXUQ VWDWHPHQW WKHUH for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == ch) { return i; } } 7R XQGHUVWDQG WKLV FRGH \RX KDYH WR XQGHUVWDQG KRZ WKH UHWXUQ VWDWHPHQW ZRUNV )RU H[DPSOH LI WKH 6WULQJ V LV WKH RQH IURP RXU H[DPSOH IRXU VFRUH DQG ZH DUH VHDUFKLQJ IRU WKH FKDUDFWHU U ZH NQRZ WKDW ZKHQ L LV HTXDO WR ZH ZLOO ILQG WKDW V FKDU$W LV HTXDO WR WKH FKDUDFWHU U 7KDW FDXVHV XV WR H[HFXWH WKH UHWXUQ VWDWHPHQW HIIHFWLYHO\ VD\LQJ return 3; :KHQ D UHWXUQ VWDWHPHQW LV H[HFXWHG ZH LPPHGLDWHO\ H[LW WKH PHWKRG 7KDW PHDQV ZH ZRXOG EUHDN RXW RI WKH ORRS DQG UHWXUQ DV RXU DQVZHU (YHQ WKRXJK WKH ORRS ZRXOG QRUPDOO\ LQFUHPHQW L WR DQG NHHS JRLQJ ZH GRQ W GR WKDW EHFDXVH ZH KLW WKH UHWXUQ VWDWHPHQW 7KHUH LV RQO\ RQH WKLQJ PLVVLQJ IURP RXU FRGH ,I ZH WU\ WR FRPSLOH LW DV LV ZH JHW WKLV HUURU PHVVDJH IURP WKH -DYD FRPSLOHU missing return statement 7KLV HUURU PHVVDJH RFFXUV EHFDXVH ZH KDYHQ W WROG -DYD ZKDW WR GR LI ZH QHYHU ILQG WKH FKDUDFWHU ZH DUH VHDUFKLQJ IRU ,Q WKDW FDVH ZH ZLOO H[HFXWH WKH IRU ORRS LQ LWV HQWLUHW\ DQG UHDFK WKH HQG RI WKH PHWKRG ZLWKRXW KDYLQJ UHWXUQHG D YDOXH 7KDW LV QRW DFFHSWDEOH ,I ZH VD\ WKDW WKH PHWKRG UHWXUQV DQ LQW WKHQ ZH KDYH WR JXDUDQWHH WKDW HYHU\ SDWK WKURXJK WKH PHWKRG ZLOO UHWXUQ DQ LQW 256 ,I ZH GRQ W ILQG WKH FKDUDFWHU ZH ZDQW WR UHWXUQ VRPH NLQG RI VSHFLDO YDOXH WR LQGLFDWH WKDW WKH FKDUDFWHU ZDV QRW IRXQG <RX PLJKW LPDJLQH XVLQJ WKH YDOXH EXW LV D OHJDO LQGH[ IRU D 6WULQJ WKH LQGH[ RI WKH ILUVW FKDUDFWHU 6R WKH FRQYHQWLRQ LQ -DYD LV WR UHWXUQ LI WKH FKDUDFWHU LV QRW IRXQG 7KLV LV HDV\ WR DGG DIWHU WKH IRU ORRS public static int indexOf(char ch, String s) { for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == ch) { return i; } } return -1; } ,W PD\ VHHP VWUDQJH WKDW ZH GRQ W KDYH D WHVW IRU WKH ILQDO UHWXUQ VWDWHPHQW WKDW UHWXUQV EXW UHPHPEHU WKDW WKH IRU ORRS WULHV HYHU\ SRVVLEOH LQGH[ RI WKH 6WULQJ VHDUFKLQJ IRU WKH FKDUDFWHU ,I WKH FKDUDFWHU DSSHDUV DQ\ZKHUH LQ WKH 6WULQJ WKHQ ZH ZLOO H[HFXWH WKH UHWXUQ VWDWHPHQW LQVLGH WKH ORRS DQG QHYHU JHW WR WKH UHWXUQ VWDWHPHQW DIWHU WKH ORRS 7KH RQO\ ZD\ WR JHW WR WKH UHWXUQ VWDWHPHQW DIWHU WKH ORRS LV WR ILQG WKDW WKH FKDUDFWHU DSSHDUV QRZKHUH LQ WKH JLYHQ 6WULQJ &RPPRQ 3URJUDPPLQJ (UURU 6WULQJ ,QGH[ 2XW RI %RXQGV ,W V YHU\ HDV\ WR IRUJHW WKDW WKH ODVW LQGH[ RI D 6WULQJ RI OHQJWK Q LV DFWXDOO\ Q FDQ FDXVH \RX WR ZULWH LQFRUUHFW WH[W SURFHVVLQJ ORRSV OLNH WKLV RQH // This version of the code has a mistake! // The test should be i < s.length() public static int indexOf(char ch, String s) { for (int i = 0; i <= s.length(); i++) { if (s.charAt(i) == ch) { return i; } } return -1; } )RUJHWWLQJ WKLV IDFW 7KH SURJUDP ZLOO FUDVK LI WKH ORRS UXQV SDVW WKH HQG RI WKH 6WULQJ 2Q WKH ODVW SDVV WKURXJK WKH ORRS WKH YDOXH RI YDULDEOH i ZLOO EH HTXDO WR s.length() PHDQLQJ WKDW ZKHQ LW H[HFXWHV WKH LI VWDWHPHQW WHVW WKH FRGH ZLOO FUDVK 7KH HUURU PHVVDJH UHVHPEOHV WKH IROORZLQJ Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 11 at java.lang.String.charAt(Unknown Source) at OutOfBoundsExample.indexOf(OutOfBoundsExample.java:9) at OutOfBoundsExample.main(OutOfBoundsExample.java:4) $Q LQWHUHVWLQJ WKLQJ DERXW WKH EXJ LQ WKLV H[DPSOH LV WKDW LW RQO\ FUDVKHV LI WKH 6WULQJ GRHV QRW FRQWDLQ WKH FKDUDFWHU ch ,I FK LV FRQWDLQHG LQ WKH 6WULQJ WKH LI WHVW ZLOO EH WUXH IRU RQH RI WKH OHJDO LQGH[HV LQ V VR WKH FRGH ZLOO UHWXUQ WKDW LQGH[ 2QO\ LI DOO FKDUDFWHUV IURP V KDYH EHHQ H[DPLQHG ZLWKRXW ILQGLQJ FK ZLOO WKH ORRS DWWHPSW LWV ODVW IDWDO SDVV 257 &DVH 6WXG\ %RG\ 0DVV ,QGH[ %0, ,Q UHFHQW \HDUV LW KDV EHFRPH FRPPRQ WR FRPSXWH WKH %RG\ 0DVV ,QGH[ RU %0, IRU DQ LQGLYLGXDO 7KH &HQWHUV IRU 'LVHDVH &RQWURO KDV SXW WRJHWKHU D ZHEVLWH DERXW %RG\ 0DVV ,QGH[ DW KWWS ZZZ FGF JRY QFFGSKS GQSD EPL $V WKDW VLWH H[SODLQV %RG\ 0DVV ,QGH[ %0, LV D QXPEHU FDOFXODWHG IURP D SHUVRQ V ZHLJKW DQG KHLJKW %0, SURYLGHV D UHOLDEOH LQGLFDWRU RI ERG\ IDWQHVV IRU PRVW SHRSOH DQG LV XVHG WR VFUHHQ IRU ZHLJKW FDWHJRULHV WKDW PD\ OHDG WR KHDOWK SUREOHPV ,W KDV DOVR EHFRPH SRSXODU WR FRPSDUH WKH VWDWLVWLFV IRU WZR RU PRUH LQGLYLGXDOV WR KDYH D ILWQHVV FKDOOHQJH RU LI WKH QXPEHUV DUH IRU WKH VDPH SHUVRQ WR JHW D VHQVH RI KRZ %0, ZLOO YDU\ LI D SHUVRQ ORVHV ZHLJKW ,Q WKLV VHFWLRQ ZH ZLOO ZULWH D SURJUDP WKDW SURPSWV WKH XVHU IRU WKH KHLJKW DQG ZHLJKW RI WZR LQGLYLGXDOV DQG WKDW UHSRUWV WKH RYHUDOO UHVXOWV IRU WKH WZR SHRSOH %HORZ LV D VDPSOH H[HFXWLRQ IRU WKH SURJUDP ZH ZDQW WR ZULWH This program reads in data for two people and computes their body mass index (BMI) and weight status. Enter next person's information: height (in inches)? 73.5 weight (in pounds)? 230 Enter next person's information: height (in inches)? 71 weight (in pounds)? 220.5 Person #1 body mass index = 29.930121708547368 overweight Person #2 body mass index = 30.75014878000397 obese ,Q &KDSWHU ZH LQWURGXFHG WKH LGHD RI LWHUDWLYH HQKDQFHPHQW LQ ZKLFK \RX GHYHORS D FRPSOH[ SURJUDP LQ VWDJHV (YHU\ SURIHVVLRQDO SURJUDPPHU XVHV WKLV WHFKQLTXH VR LW LV LPSRUWDQW WR OHDUQ WR DSSO\ WKH LGHD \RXUVHOI LQ WKH SURJUDPV \RX ZULWH )RU WKLV SDUWLFXODU SURJUDP ZH HYHQWXDOO\ ZDQW WR KDYH D SURJUDP WKDW H[SODLQV WKH SURJUDP WR WKH XVHU DQG WKDW FRPSXWHV UHVXOWV IRU WZR GLIIHUHQW SHRSOH :H ZLOO DOVR ZDQW WKH SURJUDP WR EH ZHOO VWUXFWXUHG %XW ZH GRQ W KDYH WR GR HYHU\WKLQJ DOO DW RQFH ,Q IDFW LI ZH WU\ WR GR HYHU\WKLQJ DOO DW RQFH ZH DUH OLNHO\ WR EH RYHUZKHOPHG E\ WKH GHWDLOV ,Q ZULWLQJ WKLV SURJUDP ZH ZLOO JR WKURXJK WKUHH GLIIHUHQW VWDJHV D SURJUDP WKDW FRPSXWHV UHVXOWV IRU MXVW RQH SHUVRQ ZLWKRXW DQ LQWURGXFWLRQ DQG QRW ZRUU\LQJ DERXW SURJUDP VWUXFWXUH D FRPSOHWH SURJUDP WKDW FRPSXWHV UHVXOWV IRU WZR SHRSOH LQFOXGLQJ DQ LQWURGXFWLRQ EXW QRW ZRUU\LQJ DERXW SURJUDP VWUXFWXUH D ZHOO VWUXFWXUHG DQG FRPSOHWH SURJUDP 258 2QH SHUVRQ 8QVWUXFWXUHG 6ROXWLRQ (YHQ WKH ILUVW YHUVLRQ RI WKH SURJUDP SURPSWV IRU XVHU LQSXW VR ZH ZLOO QHHG WR FRQVWUXFW D 6FDQQHU REMHFW WR UHDG IURP WKH FRQVROH Scanner console = new Scanner(System.in); 7R FRPSXWH WKH %0, IRU DQ LQGLYLGXDO ZH ZLOO QHHG WR NQRZ WKH KHLJKW DQG ZHLJKW RI WKDW SHUVRQ 7KLV LV D IDLUO\ VWUDLJKWIRUZDUG SURPSW DQG UHDG WDVN 7KH RQO\ UHDO GHFLVLRQ KHUH LV ZKDW W\SH RI YDULDEOH WR XVH IRU VWRULQJ WKH KHLJKW DQG ZHLJKW 3HRSOH RIWHQ WDON DERXW KHLJKW DQG ZHLJKW ZLWK ZKROH QXPEHUV EXW WKH TXHVWLRQ WR DVN LV ZKHWKHU RU QRW LW PDNHV VHQVH IRU SHRSOH WR XVH IUDFWLRQV 'RHV DQ\RQH GHVFULEH WKHLU KHLJKW XVLQJ KDOI DQ LQFK" 7KH DQVZHU LV \HV 'RHV DQ\RQH GHVFULEH WKHLU ZHLJKW XVLQJ KDOI D SRXQG" $JDLQ WKH DQVZHU LV \HV 6R LW PDNHV PRUH VHQVH WR VWRUH WKH YDOXHV DV GRXEOHV WR DOORZ SHRSOH WR HQWHU HLWKHU LQWHJHU YDOXHV RU IUDFWLRQV System.out.println("Enter next person's information:"); System.out.print("height (in inches)? "); double height1 = console.nextDouble(); System.out.print("weight (in pounds)? "); double weight1 = console.nextDouble(); 2QFH ZH KDYH WKH KHLJKW DQG ZLGWK ZH FDQ FRPSXWH WKH EPL 7KH &'& ZHEVLWH VD\V WKDW WKH IRUPXOD IRU DGXOWV IRU %0, LV DV IROORZV ZHLJKW OE >KHLJKW LQ @ [ 7KLV LV IDLUO\ HDV\ WR WUDQVODWH LQWR D -DYD H[SUHVVLRQ double bmi1 = weight1 / (height1 * height1) * 703; ,I \RX ORRN FORVHO\ DW WKH VDPSOH H[HFXWLRQ \RX ZLOO VHH WKDW ZH ZDQW WR KDYH EODQN OLQHV WR VHSDUDWH GLIIHUHQW SDUWV RI WKH XVHU LQWHUDFWLRQ 7KH LQWURGXFWLRQ HQGV ZLWK D EODQN OLQH WKHQ WKHUH LV D EODQN OLQH DIWHU WKH SURPSW DQG UHDG SRUWLRQ RI WKH LQWHUDFWLRQ 6R DGGLQJ DQ HPSW\ SULQWOQ DQG SXWWLQJ DOO RI WKHVH SLHFHV WRJHWKHU RXU PHWKRG PDLQ ORRNV OLNH WKLV VR IDU public static void main(String args) { Scanner console = new Scanner(System.in); System.out.println("Enter next person's information:"); System.out.print("height (in inches)? "); double height1 = console.nextDouble(); System.out.print("weight (in pounds)? "); double weight1 = console.nextDouble(); double bmi1 = weight1 / (height1 * height1) * 703; System.out.println(); ... } 7KLV SURJUDP SURPSWV IRU YDOXHV DQG FRPSXWHV WKH %0, 1RZ ZH QHHG WR LQFOXGH FRGH WR UHSRUW WKH UHVXOWV :H FRXOG XVH D SULQWOQ IRU WKH %0, System.out.println("Person #1 body mass index = " + bmi1); 259 7KLV ZRXOG ZRUN EXW LW SURGXFHV RXWSXW OLNH WKH IROORZLQJ Person #1 body mass index = 29.930121708547368 7KH ORQJ VHTXHQFH RI GLJLWV DIWHU WKH GHFLPDO SRLQW LV GLVWUDFWLQJ DQG LW LPSOLHV D OHYHO RI SUHFLVLRQ WKDW ZH VLPSO\ GRQ W KDYH ,W ZRXOG EH PRUH DSSURSULDWH DQG PRUH DSSHDOLQJ WR WKH XVHU WR OLVW MXVW D IHZ GLJLWV DIWHU WKH GHFLPDO SRLQW 6R WKLV ZRXOG DOVR KDYH EHHQ D JRRG SODFH WR XVH D SULQWI System.out.printf("Person #1 body mass index = %4.2f\n", bmi1); ,Q WKH VDPSOH RI H[HFXWLRQ ZH DOVR VHH D UHSRUW RI WKH SHUVRQ V ZHLJKW VWDWXV 7KH &'& ZHEVLWH LQFOXGHV WKH IROORZLQJ WDEOH :HLJKW 6WDWXV E\ %0, %0, %HORZ :HLJKW 6WDWXV 8QGHUZHLJKW 1RUPDO 2YHUZHLJKW DQG $ERYH 2EHVH 7KHUH DUH IRXU HQWULHV LQ WKLV WDEOH VR ZH QHHG IRXU GLIIHUHQW SULQWOQ VWDWHPHQWV IRU WKH IRXU SRVVLELOLWLHV :H ZLOO ZDQW VRPH NLQG RI LI RU LI HOVH VWDWHPHQWV WR FRQWURO WKH IRXU SULQWOQ VWDWHPHQWV ,Q WKLV FDVH ZH NQRZ WKDW ZH ZDQW WR SULQW H[DFWO\ RQH RI WKH IRXU SRVVLELOLWLHV 7KHIRUH LW PDNHV PRVW VHQVH WR XVH D QHVWHG LI HOVH WKDW HQGV ZLWK DQ HOVH %XW ZKDW WHVWV GR ZH XVH IRU WKH QHVWHG LI HOVH" ,I \RX ORRN FORVHO\ DW WKLV WDEOH \RX ZLOO VHH WKDW WKHUH DUH VRPH JDSV )RU H[DPSOH ZKDW LI \RXU %0, LV " 7KDW LVQ W EHWZHHQ DQG DQG LW LVQ W EHWZHHQ DQG ,W VHHPV FOHDU WKDW WKH &'& LQWHQGHG WKH WDEOH WR EH LQWHUSUHWHG VOLJKWO\ GLIIHUHQWO\ 7KH\ SUREDEO\ PHDQ UHSHDWLQJ %XW WKDW ZRXOG ORRN UDWKHU RGG LQ D WDEOH ,Q IDFW WKLV LV D FDVH ZKHUH D QHVWHG LI HOVH H[SUHVVHV WKLV PRUH FOHDUO\ WKDQ WKH WDEOH GRHV LI \RX XQGHUVWDQG QHVWHG LI HOVH VWDWHPHQWV if (bmi1 < 18.5) { System.out.println("underweight"); } else if (bmi1 < 25) { System.out.println("normal"); } else if (bmi1 < 30) { System.out.println("overweight"); } else { // bmi1 >= 30 System.out.println("obese"); } 6R SXWWLQJ WKLV DOO WRJHWKHU ZH JHW D FRPSOHWH YHUVLRQ RI WKH ILUVW SURJUDP 260 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 import java.util.*; public class BMI1 { public static void main(String args) { Scanner console = new Scanner(System.in); System.out.println("Enter next person's information:"); System.out.print("height (in inches)? "); double height1 = console.nextDouble(); System.out.print("weight (in pounds)? "); double weight1 = console.nextDouble(); double bmi1 = weight1 / (height1 * height1) * 703; System.out.println(); System.out.println("Person #1 body mass index = " + bmi1); if (bmi1 < 18.5) { System.out.println("underweight"); } else if (bmi1 < 25) { System.out.println("normal"); } else if (bmi1 < 30) { System.out.println("overweight"); } else { // bmi1 >= 30 System.out.println("obese"); } } } %HORZ LV D VDPSOH H[HFXWLRQ RI WKH SURJUDP Enter next person's information: height (in inches)? 73.5 weight (in pounds)? 230 Person #1 body mass index = 29.930121708547368 overweight 7ZR SHUVRQ 8QVWUXFWXUHG 6ROXWLRQ 1RZ WKDW ZH KDYH D SURJUDP WKDW FRPSXWHV RQH SHUVRQ V %0, DQG ZHLJKW VWDWXV OHW V H[SDQG LW WR KDQGOH WZR GLIIHUHQW SHRSOH ([SHULHQFHG SURJUDPPHUV ZRXOG SUREDEO\ EHJLQ E\ DGGLQJ VWUXFWXUH WR WKH SURJUDP EHIRUH WU\LQJ WR PDNH LW KDQGOH WZR GLIIHUHQW SHRSOH %XW IRU QRYLFH SURJUDPPHUV LW LV RIWHQ KHOSIXO WR FRQVLGHU WKH XQVWUXFWXUHG VROXWLRQ ILUVW ,Q WXUQLQJ WKLV LQWR D SURJUDP WKDW KDQGOHV WZR SHRSOH ZH QRWLFH D ORW RI RYHUODS :H FDQ FRS\ DQG SDVWH D JUHDW GHDO RI FRGH DQG PDNH VOLJKWO\ PRGLILFDWLRQV )RU H[DPSOH LQVWHDG RI XVLQJ YDULDEOHV FDOOHG KHLJKW ZHLJKW DQG EPL ZH FKDQJH WKH FRGH WR XVH YDULDEOHV KHLJKW ZHLJKW DQG EPL :H DOVR KDYH WR EH FDUHIXO WR GR HDFK VWHS LQ WKH ULJKW RUGHU /RRNLQJ DW WKH VDPSOH H[HFXWLRQ \RX OO VHH WKDW WKH SURJUDP SURPSWV IRU GDWD IRU ERWK LQGLYLGXDOV ILUVW DQG WKHQ UHSRUWV UHVXOWV IRU ERWK 6R ZH FDQ W FRS\ WKH HQWLUH SURJUDP DQG VLPSO\ SDVWH D VHFRQG FRS\ :H KDYH WR UHDUUDQJH WKH RUGHU VR WKDW DOO RI WKH SURPSWLQJ KDSSHQV ILUVW DQG DOO RI WKH UHSRUWLQJ KDSSHQV ODWHU 261 :H DOVR GHFLGHG WKDW LQ PRYLQJ WR WKLV VHFRQG VWDJH ZH ZLOO DGG FRGH IRU WKH LQWURGXFWLRQ 7KLV FRGH VKRXOG DSSHDU DW WKH EHJLQQLQJ RI WKH SURJUDP DQG VKRXOG LQFOXGH DQ HPSW\ SULQWOQ WR SURGXFH D EODQN OLQH WR VHSDUDWH WKH LQWURGXFWLRQ IURP WKH UHVW RI WKH XVHU LQWHUDFWLRQ &RPELQLQJ WKHVH HOHPHQWV LQWR D FRPSOHWH SURJUDP ZH JHW WKH IROORZLQJ 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 49 50 51 52 // This program finds the body mass index (BMI) for two individuals. import java.util.*; public class BMI2 { public static void main(String args) { System.out.println("This program reads in data for two people"); System.out.println("and computes their body mass index (BMI)"); System.out.println("and weight status."); System.out.println(); Scanner console = new Scanner(System.in); System.out.println("Enter next person's information:"); System.out.print("height (in inches)? "); double height1 = console.nextDouble(); System.out.print("weight (in pounds)? "); double weight1 = console.nextDouble(); double bmi1 = weight1 / (height1 * height1) * 703; System.out.println(); System.out.println("Enter next person's information:"); System.out.print("height (in inches)? "); double height2 = console.nextDouble(); System.out.print("weight (in pounds)? "); double weight2 = console.nextDouble(); double bmi2 = weight2 / (height2 * height2) * 703; System.out.println(); System.out.println("Person #1 body mass index = " + bmi1); if (bmi1 < 18.5) { System.out.println("underweight"); } else if (bmi1 < 25) { System.out.println("normal"); } else if (bmi1 < 30) { System.out.println("overweight"); } else { // bmi1 >= 30 System.out.println("obese"); } System.out.println("Person #2 body mass index = " + bmi2); if (bmi2 < 18.5) { System.out.println("underweight"); } else if (bmi2 < 25) { System.out.println("normal"); } else if (bmi2 < 30) { System.out.println("overweight"); } else { // bmi2 >= 30 System.out.println("obese"); } } } 262 7KLV SURJUDP FRPSLOHV DQG ZRUNV :KHQ ZH H[HFXWH LW ZH JHW H[DFWO\ WKH LQWHUDFWLRQ ZH ZHUH ORRNLQJ IRU %XW WKH SURJUDP ODFNV VWUXFWXUH $OO RI WKH FRGH DSSHDUV LQ PDLQ DQG WKHUH LV VLJQLILFDQW UHGXQGDQF\ 7KDW VKRXOGQ W EH D VXUSULVH EHFDXVH ZH FUHDWHG WKLV YHUVLRQ E\ XVLQJ FRS\ DQG SDVWH LQ WKH HGLWRU :KHQHYHU \RX ILQG \RXUVHOI XVLQJ FRS\ DQG SDVWH \RX VKRXOG ZRQGHU ZKHWKHU WKHUH LVQ W D EHWWHU ZD\ WR VROYH WKH SUREOHP 0RVW RIWHQ WKHUH LV 7ZR SHUVRQ 6WUXFWXUHG 6ROXWLRQ /HW V H[SORUH KRZ VWDWLF PHWKRGV FDQ LPSURYH WKH VWUXFWXUH RI WKH SURJUDP /RRNLQJ DW WKH FRGH \RX ZLOO QRWLFH D JUHDW GHDO RI UHGXQGDQF\ )RU H[DPSOH ZH KDYH WZR FRGH VHJPHQWV WKDW ORRN OLNH WKLV System.out.println("Enter next person's information:"); System.out.print("height (in inches)? "); double height1 = console.nextDouble(); System.out.print("weight (in pounds)? "); System.out.println(); 7KH RQO\ GLIIHUHQFH EHWZHHQ WKHVH WZR FRGH VHJPHQWV LV WKDW WKH ILUVW XVHV YDULDEOHV KHLJKW ZHLJKW EPL DQG WKH VHFRQG XVHV YDULDEOHV KHLJKW ZHLJKW EPL :H HOLPLQDWH UHGXQGDQF\ E\ PRYLQJ FRGH OLNH WKLV LQWR D PHWKRG WKDW ZH FDQ FDOO WZLFH 6R DV D ILUVW DSSUR[LPDWLRQ ZH FDQ WXUQ WKLV LQWR D PRUH JHQHULF IRUP DV D PHWKRG public static void getBMI(Scanner console) { System.out.println("Enter next person's information:"); System.out.print("height (in inches)? "); double height = console.nextDouble(); System.out.print("weight (in pounds)? "); double weight = console.nextDouble(); double bmi = weight / (height * height) * 703; System.out.println(); } :H KDYH WR SDVV LQ WKH 6FDQQHU IURP PDLQ 2WKHUZLVH ZH KDYH PDGH DOO RI WKH YDULDEOHV ORFDO WR WKLV PHWKRG )URP PDLQ ZH FDQ FDOO WKLV PHWKRG WZLFH getBMI(console); getBMI(console); 8QIRUWXQDWHO\ LQWURGXFLQJ WKLV FKDQJH EUHDNV WKH UHVW RI WKH FRGH ,I ZH WU\ WR FRPSLOH DQG UXQ WKH SURJUDP ZH ILQG WKDW ZH JHW HUURU PHVVDJHV LQ PDLQ ZKHQHYHU ZH UHIHU WR YDULDEOHV EPL DQG EPL 7KH SUREOHP LV WKDW WKH PHWKRG FRPSXWHV D EPL YDOXH WKDW ZH QHHG ODWHU LQ WKH SURJUDP :H FDQ IL[ WKLV E\ KDYLQJ WKH PHWKRG UHWXUQ WKH EPL YDOXH WKDW LW FRPSXWHV 263 public double void getBMI(Scanner console) { System.out.println("Enter next person's information:"); System.out.print("height (in inches)? "); double height = console.nextDouble(); System.out.print("weight (in pounds)? "); double weight = console.nextDouble(); double bmi = weight / (height * height) * 703; System.out.println(); return bmi; } 1RWLFH WKDW WKH PHWKRG KHDGHU QRZ OLVWV WKH UHWXUQ W\SH DV GRXEOH :H DOVR KDYH WR FKDQJH PDLQ :H FDQ W MXVW FDOO WKH PHWKRG WZLFH WKH ZD\ ZH ZRXOG FDOO D YRLG PHWKRG (DFK FDOO LV UHWXUQLQJ D %0, UHVXOW WKDW QHHGV WR EH UHPHPEHUHG 6R IRU HDFK FDOO ZH KDYH WR VWRUH WKH UHVXOW FRPLQJ EDFN IURP WKH PHWKRG LQ D YDULDEOH double bmi1 = getBMI(console); double bmi2 = getBMI(console); 6WXG\ WKLV FKDQJH FDUHIXOO\ EHFDXVH WKLV WHFKQLTXH FDQ EH RQH RI WKH PRVW FKDOOHQJLQJ IRU QRYLFHV WR PDVWHU ,Q ZULWLQJ WKH PHWKRG ZH KDYH WR PDNH VXUH WKDW LW UHWXUQV WKH %0, UHVXOW ,Q ZULWLQJ WKH FDOO ZH KDYH WR PDNH VXUH WKDW ZH VWRUH WKH UHVXOW LQ D YDULDEOH VR WKDW ZH FDQ DFFHVV LW ODWHU :LWK WKLV PRGLILFDWLRQ WKH SURJUDP ZRXOG DJDLQ FRPSLOH DQG UXQ SURSHUO\ 7KHUH LV DQRWKHU REYLRXV UHGXQGDQF\ LQ WKH PDLQ SURJUDP LQ WKDW ZH KDYH WKH VDPH QHVWHG LI HOVH WZLFH 7KH RQO\ GLIIHUHQFH LV WKDW LQ RQH FDVH ZH XVH WKH YDULDEOH EPL DQG LQ WKH RWKHU FDVH ZH XVH WKH YDULDEOH EPL 7KLV LV HDVLO\ JHQHUDOL]HG ZLWK D SDUDPHWHU public static void reportStatus(double bmi) { if (bmi < 18.5) { System.out.println("underweight"); } else if (bmi < 25) { System.out.println("normal"); } else if (bmi < 30) { System.out.println("overweight"); } else { // bmi >= 30 System.out.println("obese"); } } :LWK WKLV PHWKRG ZH FDQ UHSODFH WKH FRGH LQ PDLQ ZLWK WZR FDOOV System.out.println("Person #1 body mass index = " + bmi1); reportStatus(bmi1); System.out.println("Person #2 body mass index = " + bmi2); reportStatus(bmi2); 7KDW WDNHV FDUH RI WKH UHGXQGDQF\ LQ WKH SURJUDP EXW ZH FDQ VWLOO XVH VWDWLF PHWKRGV WR LPSURYH WKH SURJUDP WR LQGLFDWH VWUXFWXUH ,W LV EHVW WR NHHS WKH PDLQ PHWKRG VKRUW LI SRVVLEOH WR UHIOHFW WKH RYHUDOO VWUXFWXUH RI WKH SURJUDP 7KH SUREOHP EUHDNV GRZQ LQWR WKUHH PDMRU SKDVHV LQWURGXFWLRQ FRPSXWH EPL UHSRUW UHVXOWV :H DOUHDG\ KDYH D PHWKRG IRU FRPSXWLQJ WKH EPL EXW ZH KDYHQ W \HW LQWURGXFHG PHWKRGV IRU WKH LQWURGXFWLRQ DQG WKH UHSRUWLQJ RI UHVXOWV 7KHVH DUH IDLUO\ VLPSOH WR DGG 264 7KHUH LV RQH RWKHU PHWKRG WKDW PDNHV VHQVH WR DGG WR WKH SURJUDP :H DUH XVLQJ D IRUPXOD IURP WKH &'& ZHEVLWH IRU FDOFXODWLQJ WKH %0, RI DQ LQGLYLGXDO JLYHQ WKH SHUVRQ V KHLJKW DQG ZHLJKW :KHQHYHU \RX ILQG \RXUVHOI SURJUDPPLQJ D IRUPXOD LW LV D JRRG LGHD WR LQWURGXFH D PHWKRG IRU WKDW IRUPXOD VR WKDW LW LV HDV\ WR VSRW DQG VR WKDW LW KDV D QDPH $SSO\LQJ WKHVH GLIIHUHQW LGHDV ZH HQG XS ZLWK WKH IROORZLQJ YHUVLRQ RI WKH SURJUDP 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 // This program finds the body mass index (BMI) for two individuals. // variation includes several methods other than method main. import java.util.*; public class BMI3 { public static void main(String args) { giveIntro(); Scanner console = new Scanner(System.in); double bmi1 = getBMI(console); double bmi2 = getBMI(console); reportResults(bmi1, bmi2); } // introduces the program to the user public static void giveIntro() { System.out.println("This program reads in data for two people"); System.out.println("and computes their body mass index (BMI)"); System.out.println("and weight status."); System.out.println(); } // prompts for one person's statistics, returning the BMI public static double getBMI(Scanner console) { System.out.println("Enter next person's information:"); System.out.print("height (in inches)? "); double height = console.nextDouble(); System.out.print("weight (in pounds)? "); double weight = console.nextDouble(); double bmi = BMIFor(height, weight); System.out.println(); return bmi; } // this method contains the body mass index formula for converting the // given height (in inches) and weight (in pounds) into a BMI public static double BMIFor(double height, double weight) { return weight / (height * height) * 703; } // reports the overall bmi values and weight status to the user public static void reportResults(double bmi1, double bmi2) { System.out.println("Person #1 body mass index = " + bmi1); reportStatus(bmi1); System.out.println("Person #2 body mass index = " + bmi2); reportStatus(bmi2); } // reports the weight status for the given bmi value public static void reportStatus(double bmi) { if (bmi < 18.5) { This 265 52 53 54 55 56 57 58 59 60 61 System.out.println("underweight"); } else if (bmi < 25) { System.out.println("normal"); } else if (bmi < 30) { System.out.println("overweight"); } else { // bmi >= 30 System.out.println("obese"); } } } ,W KDV WKH VDPH EHKDYLRU DV WKH XQVWUXFWXUHG VROXWLRQ EXW LW KDV D PXFK QLFHU VWUXFWXUH 7KH XQVWUXFWXUHG SURJUDP LV LQ VRPH VHQVH VLPSOHU EXW WKH VWUXFWXUHG VROXWLRQ LV HDVLHU WR PDLQWDLQ LI ZH ZDQW WR H[SDQG WKH SURJUDP RU PDNH RWKHU PRGLILFDWLRQV 7KHVH VWUXFWXUDO EHQHILWV DUHQ W VR LPSRUWDQW LQ VKRUW SURJUDPV EXW WKH\ EHFRPH HVVHQWLDO DV SURJUDPV EHFRPH ORQJHU DQG PRUH FRPSOH[ &KDSWHU 6XPPDU\ $ IHQFHSRVW ORRS H[HFXWHV D ORRS DQG D KDOI E\ SODFLQJ SDUW RI D ORRS V ERG\ RQFH EHIRUH WKH ORRS EHJLQV $ FXPXODWLYH VXP ORRS GHFODUHV D VXP YDULDEOH DQG LQFUHPHQWDOO\ DGGV WR WKDW YDULDEOH V YDOXH LQVLGH WKH ORRS $Q LI VWDWHPHQW OHWV \RX ZULWH FRGH WKDW ZLOO RQO\ H[HFXWH LI D FHUWDLQ FRQGLWLRQ LV PHW $Q LI HOVH VWDWHPHQW OHWV \RX H[HFXWH RQH SLHFH RI FRGH LI D FRQGLWLRQ LV PHW DQG DQRWKHU LI QRW &RQGLWLRQV DUH ERROHDQ H[SUHVVLRQV DQG FDQ EH ZULWWHQ XVLQJ UHODWLRQDO RSHUDWRUV VXFK DV ! DQG ,I HOVH VWDWHPHQWV FDQ EH QHVWHG WR WHVW D VHULHV RI FRQGLWLRQV DQG H[HFXWH WKH DSSURSULDWH EORFN RI FRGH EDVHG RQ ZKLFK FRQGLWLRQ LV WUXH &RPPRQ FRGH WKDW DSSHDUV LQ HYHU\ EUDQFK RI DQ LI HOVH VWDWHPHQW VKRXOG EH IDFWRUHG RXW VR WKDW LW LV QRW UHSOLFDWHG PXOWLSOH WLPHV LQ WKH FRGH ([FHSWLRQV ZLOO RFFXU LI \RX DVN D 6FDQQHU WR UHDG WRNHQV RI LQSXW WKDW GR QRW PDWFK ZKDW WKH XVHU W\SHV 7KH RSHUDWRU WKDW WHVWV IRU HTXDOLW\ RQ SULPLWLYH GDWD GRHVQ W EHKDYH WKH ZD\ ZH ZRXOG H[SHFW RQ REMHFWV VR ZH WHVW REMHFWV IRU HTXDOLW\ E\ FDOOLQJ WKHLU equals PHWKRG LQVWHDG <RX FDQ WKURZ JHQHUDWH H[FHSWLRQV LQ \RXU RZQ FRGH 7KLV FDQ EH XVHIXO LQ FDVHV ZKHUH \RXU FRGH UHDFKHV DQ XQUHFRYHUDEOH HUURU FRQGLWLRQ VXFK DV DQ LQYDOLG DUJXPHQW YDOXH EHLQJ SDVVHG WR D PHWKRG 266 6HOI &KHFN 3UREOHPV 6HFWLRQ /RRS 7HFKQLTXHV :KDW LV ZURQJ ZLWK WKH IROORZLQJ FRGH WKDW DWWHPSWV WR DGG DOO QXPEHUV IURP PD[LPXP" 'HVFULEH KRZ WR IL[ WKH FRGH VR WKDW LW ZLOO EHKDYH SURSHUO\ public static int sumTo(int n) { for (int i = 1; i <= n; i++) { int sum = 0; sum += i; } return sum; } WR D JLYHQ :KDW LV ZURQJ ZLWK WKH IROORZLQJ FRGH WKDW DWWHPSWV WR SULQW DOO QXPEHUV IURP WR D JLYHQ PD[LPXP VHSDUDWHG E\ FRPPDV" 'HVFULEH KRZ WR IL[ WKH FRGH VR WKDW LW ZLOO EHKDYH SURSHUO\ for (int i = 1; i <= n; i++) { System.out.print(i + ", "); } System.out.println(); :ULWH FRGH WR SURGXFH D FXPXODWLYH SURGXFW E\ PXOWLSO\LQJ WRJHWKHU PDQ\ QXPEHUV UHDG IURP WKH FRQVROH 6HFWLRQ LI HOVH 6WDWHPHQWV 7UDQVODWH HDFK RI WKH IROORZLQJ (QJOLVK VWDWHPHQWV LQWR ORJLFDO WHVWV WKDW FRXOG EH XVHG LQ DQ LI HOVH VWDWHPHQW :ULWH WKH DSSURSULDWH LI VWDWHPHQW ZLWK \RXU ORJLFDO WHVW $VVXPH WKDW WKUHH LQW YDULDEOHV [ \ DQG ] KDYH EHHQ GHFODUHG ] LV RGG ] LV QRW JUHDWHU WKDQ \ V VTXDUH URRW \ LV SRVLWLYH RQH RI [ DQG \ LV HYHQ DQG WKH RWKHU LV RGG \ LV D PXOWLSOH RI ] ] LV QRW ]HUR \ LV JUHDWHU LQ PDJQLWXGH WKDQ ] [ DQG ] DUH RI RSSRVLWH VLJQ \ LV D RQH GLJLW QXPEHU ] LV QRQQHJDWLYH [ LV HYHQ [ LV FORVHU LQ YDOXH WR \ WKDQ ] LV *LYHQ WKH IROORZLQJ YDULDEOH GHFODUDWLRQV int x = 4; int y = -3; int z = 4; 267 :KDW DUH WKH UHVXOWV RI WKH IROORZLQJ UHODWLRQDO H[SUHVVLRQV" x == 4 x == y x == z y == z x + y > 0 x - z != 0 y * y <= z y / y == 1 x * (y + 2) > y - (y + z) * 2 &RQVLGHU WKH IROORZLQJ -DYD PHWKRG ZKLFK LV ZULWWHQ LQFRUUHFWO\ // This method should return how many of its three // arguments are odd numbers. public static void printNumOdd(int n1, int n2, int n3) { int count = 0; if (n1 % 2 == 1) { count++; } else if (n2 % 2 == 1) { count++; } else if (n3 % 2 == 1) { count++; } System.out.println(count + " of the 3 numbers are odd."); } 8QGHU ZKDW FDVHV ZLOO WKH PHWKRG SULQW WKH FRUUHFW DQVZHU DQG ZKHQ ZLOO LW SULQW DQ LQFRUUHFW DQVZHU" :KDW VKRXOG EH FKDQJHG WR IL[ WKH FRGH" &DQ \RX WKLQN RI D ZD\ WR ZULWH WKH FRGH FRUUHFWO\ ZLWKRXW DQ\ LI HOVH VWDWHPHQWV" :ULWH -DYD FRGH WR UHDG DQ LQWHJHU IURP WKH XVHU WKHQ SULQWV HYHQ LI WKDW QXPEHU LV DQ HYHQ QXPEHU RU RGG RWKHUZLVH <RX PD\ DVVXPH WKDW WKH XVHU W\SHV D YDOLG LQWHJHU 7KH IROORZLQJ FRGH FRQWDLQV D ORJLF HUURU Scanner console = new Scanner(System.in); System.out.print("Type a number: "); int number = console.nextInt(); if (number % 2 == 0) { if (number % 3 == 0) { System.out.println("Divisible by 6."); } else { System.out.println("Odd."); } } ([DPLQH WKH SUHFHGLQJ FRGH DQG GHVFULEH D FDVH ZKHUH WKH FRGH ZRXOG SULQW VRPHWKLQJ WKDW LV XQWUXH DERXW WKH QXPEHU WKDW ZDV HQWHUHG DQG H[SODLQ ZK\ 7KHQ FRUUHFW WKH FRGH VR WKDW WKH LQWHQW HUURU LV IL[HG 268 :KDW LV ZURQJ ZLWK WKH IROORZLQJ FRGH WKDW DWWHPSWV WR UHWXUQ WKH QXPEHU RI IDFWRUV RI D JLYHQ LQWHJHU Q" 'HVFULEH KRZ WR IL[ WKH FRGH VR WKDW LW ZLOO EHKDYH SURSHUO\ public static int countFactors(int n) { for (int i = 1; i <= n; i++) { if (n % i == 0) { // factor return i; } } } 6HFWLRQ 6XEWOHWLHV RI &RQGLWLRQDO ([HFXWLRQ 7KH IROORZLQJ FRGH LV SRRUO\ VWUXFWXUHG int sum = 1000; Scanner console = new Scanner(System.in); System.out.print("Is your money multiplied 1 or 2 times? "); int times = console.nextInt(); if (times == 1) { System.out.print("And how much are you contributing? "); int donation = console.nextInt(); sum = sum + donation; count1++; total = total + donation; } if (times == 2) { System.out.print("And how much are you contributing? "); int donation = console.nextInt(); sum = sum + 2 * donation; count2++; total = total + donation; } 5HZULWH LW VR WKDW LW KDV D EHWWHU VWUXFWXUH DQG DYRLGV UHGXQGDQF\ 7R VLPSOLI\ WKLQJV \RX PD\ DVVXPH WKDW WKH XVHU DOZD\V W\SHV D RU +RZ ZRXOG WKH FRGH QHHG WR EH PRGLILHG LI WKH XVHU PLJKW W\SH DQ\ QXPEHU" +RZ ZRXOG LW EH PRGLILHG LI WKH XVHU PLJKW W\SH DQ\WKLQJ HYHQ VRPHWKLQJ WKDW LV QRW D QXPEHU" 7KH IROORZLQJ FRGH LV SRRUO\ VWUXFWXUHG 269 Scanner console = new Scanner(System.in); System.out.print("How much will John be spending? "); double amount = console.nextDouble(); System.out.println(); int numBills1 = (int) (amount / 20.0); if (numBills1 * 20.0 < amount) { numBills1++; } System.out.print("How much will Jane be spending? "); amount = console.nextDouble(); System.out.println(); int numBills2 = (int) (amount / 20.0); if (numBills2 * 20.0 < amount) { numBills2++; } System.out.println("John needs " + numBills1 + " bills"); System.out.println("Jane needs " + numBills2 + " bills"); 5HZULWH LW VR WKDW LW KDV D EHWWHU VWUXFWXUH DQG DYRLGV UHGXQGDQF\ <RX PD\ ZLVK WR LQWURGXFH D PHWKRG WR KHOS FDSWXUH UHGXQGDQW FRGH 'HVFULEH D SRWHQWLDO SUREOHP ZLWK WKH IROORZLQJ FRGH Scanner console = new Scanner(System.in); System.out.print("What is your favorite color?"); String name = console.next(); if (name == "blue") { System.out.println("Mine, too!"); } :KDW LV WKH RXWSXW RI WKH IROORZLQJ FRGH" Point p1 = new Point(3, -2); Point p2 = new Point(3, -2); if (p1 == p2) { System.out.println("equal"); } else { System.out.println("non-equal"); } :KDW LV WKH RXWSXW RI WKH IROORZLQJ FRGH" 270 Point p1 = new Point(3, -2); Point p2 = new Point(4, 0); if (p1.equals(p2)) { System.out.println("first"); } p2.translate(-1, -2); if (p1.equals(p2)) { System.out.println("second"); } p2 = p1; p2.translate(2, 5); if (p1.equals(p2)) { System.out.println("third"); } p2 = new Point(5, -3); p1.translate(1, 1); if (p1.equals(p2)) { System.out.println("fourth"); } :ULWH D SLHFH RI FRGH WKDW UHDGV D VKRUWKDQG WH[W GHVFULSWLRQ RI D FRORU DQG SULQWV WKH ORQJHU HTXLYDOHQW $FFHSWDEOH FRORU QDPHV DUH % IRU %OXH * IRU *UHHQ DQG 5 IRU 5HG ,I WKH XVHU W\SHV VRPHWKLQJ RWKHU WKDQ % * RU 5 SULQW DQ HUURU PHVVDJH 0DNH \RXU SURJUDP FDVH LQVHQVLWLYH VR WKDW WKH XVHU FDQ W\SH DQ XSSHUFDVH RU ORZHUFDVH OHWWHU +HUH DUH VRPH H[DPSOH GLDORJXHV What color do you want? B You have chosen Blue. What color do you want? g You have chosen Green. What color do you want? Bork Unknown color: Bork :ULWH D SLHFH RI FRGH WKDW UHDGV D VKRUWKDQG WH[W GHVFULSWLRQ RI D SOD\LQJ FDUG DQG SULQWV WKH ORQJKDQG HTXLYDOHQW 7KH VKRUWKDQG GHVFULSWLRQ RI WKH FDUG LV WKH FDUG V UDQN WKURXJK 4 . RU $ IROORZHG E\ LWV VXLW & ' + RU 6 <RX VKRXOG H[SDQG WKH VKRUWKDQG LQWR WKH IRUP 5DQN RI 6XLW <RX PD\ DVVXPH WKDW WKH XVHU W\SHV YDOLG LQSXW +HUH DUH WZR H[DPSOH GLDORJXHV Enter a card: 9 S Nine of Spades Enter a card: K C King of Clubs 7KH IROORZLQJ H[SUHVVLRQ LQ -DYD VKRXOG HTXDO 0.2 + 1.2 + 2.2 + 3.2 EXW LW GRHV QRW :K\ GRHV WKLV RFFXU" 271 7KH IROORZLQJ FRGH ZDV LQWHQGHG WR SULQW D PHVVDJH EXW LW DFWXDOO\ SURGXFHV QR RXWSXW 'HVFULEH D ZD\ WR IL[ WKH FRGH VR WKDW WKH H[SHFWHG PHVVDJH ZLOO EH SULQWHG double gpa = 3.2; if (gpa * 3 == 9.6) { System.out.println("You earned enough credits."); } 6HFWLRQ 7H[W 3URFHVVLQJ :ULWH DQ LI VWDWHPHQW WKDW WHVWV WR VHH ZKHWKHU D 6WULQJ EHJLQV ZLWK D FDSLWDO OHWWHU :KDW LV ZURQJ ZLWK WKH IROORZLQJ FRGH ZKLFK DWWHPSWV WR FRXQW WKH QXPEHU RFFXUUHQFHV RI WKH OHWWHU H LQ D 6WULQJ FDVH LQVHQVLWLYHO\" int count = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i).toLowerCase() == 'e') { count++; } } &RQVLGHU D 6WULQJ VWRUHG LQ D YDULDEOH QDPHG name WKDW VWRUHV D SHUVRQ V ILUVW DQG ODVW QDPH VXFK DV "Marla Singer" :ULWH WKH H[SUHVVLRQ WKDW ZRXOG SURGXFH WKH ODVW QDPH IROORZHG E\ WKH ILUVW LQLWLDO VXFK DV "Singer, M." :ULWH FRGH WR H[DPLQH D 6WULQJ DQG GHWHUPLQH KRZ PDQ\ RI LWV OHWWHUV FRPH IURP WKH VHFRQG KDOI RI WKH DOSKDEHW WKDW LV KDYH YDOXHV RI Q RU ODWHU &RPSDUH FDVH LQVHQVLWLYHO\ YDOXHV RI 1 WKURXJK = DOVR FRXQW $VVXPH WKDW HYHU\ OHWWHU LQ WKH 6WULQJ LV D OHWWHU 6HFWLRQ 0HWKRGV ZLWK &RQGLWLRQDO ([HFXWLRQ &RQVLGHU D PHWKRG printTriangleType WKDW DFFHSWV WKUHH LQWHJHU DUJXPHQWV WKDW UHSUHVHQW WKH OHQJWKV RI WKH VLGHV RI D WULDQJOH DQG SULQWV ZKDW W\SH RI WULDQJOH LW LV 7KH WKUHH W\SHV DUH HTXLODWHUDO LVRVFHOHV DQG VFDOHQH $Q HTXLODWHUDO WULDQJOH KDV DOO VLGHV WKH VDPH OHQJWK DQ LVRVFHOHV KDV VLGHV WKH VDPH OHQJWK DQG D VFDOHQH KDV VLGHV RI GLIIHUHQW OHQJWKV +RZHYHU WKHUH DUH FHUWDLQ LQWHJHU YDOXHV RU FRPELQDWLRQV RI YDOXHV WKDW ZRXOG EH LOOHJDO DQG FRXOG QRW UHSUHVHQW WKH VLGHV RI D YDOLG WULDQJOH :KDW DUH WKHVH YDOXHV" +RZ ZRXOG \RX GHVFULEH WKH SUHFRQGLWLRQ V RI WKH printTriangleType PHWKRG" &RQVLGHU D PHWKRG getGrade WKDW DFFHSWV DQ LQWHJHU UHSUHVHQWLQJ D VWXGHQW V JUDGH SHUFHQWDJH LQ D FRXUVH DQG UHWXUQV WKDW VWXGHQWV QXPHULFDO FRXUVH JUDGH EHWZHHQ IDLOLQJ DQG SHUIHFW :KDW DUH WKH SUHFRQGLWLRQV RI VXFK D PHWKRG" 7KH IROORZLQJ PHWKRG DWWHPSWV WR UHWXUQ WKH PHGLDQ PLGGOH RI WKUHH LQWHJHU YDOXHV EXW LW FRQWDLQV ORJLF HUURUV ,Q ZKDW FDVHV GRHV WKH PHWKRG UHWXUQ DQ LQFRUUHFW UHVXOW" +RZ FDQ WKH FRGH EH IL[HG" 272 public static int medianOf3(int n1, int n2, int n3) { if (n1 < n2) { if (n2 < n3) { return n2; } else { return n3; } } else { if (n1 < n3) { return n1; } else { return n3; } } } 7KH ODVW FKDSWHU KDG DQ H[HUFLVH DVNLQJ WR ZULWH D PHWKRG quadratic WKDW ZRXOG ILQG WKH URRWV E[ F 7KH quadratic PHWKRG ZDV SDVVHG D E RI D TXDGUDWLF HTXDWLRQ RI WKH IRUP D[ DQG F DQG WKHQ DSSOLHG WKH IROORZLQJ TXDGUDWLF IRUPXOD 8QGHU ZKDW FRQGLWLRQV ZRXOG WKLV IRUPXOD IDLO" 0RGLI\ WKH quadratic PHWKRG VR WKDW LW ZLOO UHMHFW LQYDOLG YDOXHV RI D E RU F E\ WKURZLQJ DQ H[FHSWLRQ ,I \RX GLG QRW FRPSOHWH WKH H[HUFLVH LQ WKH SUHYLRXV FKDSWHU MXVW ZULWH WKH PHWKRG V KHDGHU DQG WKH H[FHSWLRQ WKURZLQJ FRGH ([HUFLVHV :ULWH D PHWKRG QDPHG fractionSum WKDW DFFHSWV DQ LQWHJHU SDUDPHWHU 1 DQG UHWXUQV DV D GRXEOH WKH VXP RI WKH ILUVW 1 WHUPV RI WKH VHTXHQFH 2U LQ RWKHU ZRUGV WKH VHTXHQFH 1 + (1/2) + (1/3) + (1/4) + (1/5) + ... <RX PD\ DVVXPH WKDW WKH SDUDPHWHU 1 LV QRQ QHJDWLYH :ULWH D PHWKRG QDPHG printFractionSum WKDW DFFHSWV DQ LQWHJHU SDUDPHWHU 1 DQG SULQWV WR WKH FRQVROH WKH ILUVW 1 WHUPV RI WKH VHTXHQFH 7KH FDOO printFractionSum(1) VKRXOG SULQW 273 1 7KH FDOO printFractionSum(5) VKRXOG SULQW 1 + (1/2) + (1/4) + (1/8) + (1/16) <RX PD\ DVVXPH WKDW WKH SDUDPHWHU 1 LV QRQ QHJDWLYH 3ULQW DQ HQWLUH FRPSOHWHG OLQH RI RXWSXW :ULWH D PHWKRG QDPHG repl WKDW DFFHSWV D 6WULQJ DQG D QXPEHU RI UHSHWLWLRQV DV SDUDPHWHUV DQG UHWXUQV WKH 6WULQJ FRQFDWHQDWHG WKDW PDQ\ WLPHV )RU H[DPSOH WKH FDOO repl("hello", 3) UHWXUQV "hellohellohello" ,I WKH QXPEHU RI UHSHWLWLRQV LV RU OHVV DQ HPSW\ VWULQJ LV UHWXUQHG 7KH IROORZLQJ FRGH LV DQ LQFRUUHFW ORRS WR UHDG QXPEHUV XQWLO WKH XVHU W\SHV D YDOLG =,3 FRGH D QXPEHU EHWZHHQ DQG LQFOXVLYH :KDW LV ZURQJ ZLWK WKH FRGH" ([SODLQ WKH PLVWDNH DQG GHVFULEH D ZD\ WR IL[ LW Scanner console = new Scanner(System.in); System.out.print("Enter a ZIP code: "); int number = 0; while (number < 1 || number > 99999) { System.out.print("Invalid ZIP code. Try again: "); number = console.nextInt(); } :ULWH D PHWKRG QDPHG printFactors WKDW DFFHSWV DQ LQWHJHU DV LWV SDUDPHWHU DQG XVHV D IHQFHSRVW ORRS WR SULQW WKH IDFWRUV RI WKDW QXPEHU VHSDUDWHG E\ WKH ZRUG DQG )RU H[DPSOH WKH QXPEHU V IDFWRUV VKRXOG SULQW DV 1 and 2 and 3 and 4 and 6 and 12 and 24 <RX PD\ DVVXPH WKDW WKH QXPEHU SDUDPHWHU V YDOXH LV JUHDWHU WKDQ \RX PD\ WKURZ DQ H[FHSWLRQ LI LW LV RU QHJDWLYH RU IRU H[WUD FKDOOHQJH :ULWH D PHWKRG QDPHG printLetters WKDW DFFHSWV D 6WULQJ DV LWV SDUDPHWHU DQG XVHV D IHQFHSRVW ORRS WR SULQW WKH OHWWHUV RI WKH 6WULQJ VHSDUDWHG E\ FRPPDV )RU H[DPSOH LI WKH DFWXDO SDUDPHWHU V YDOXH LV 5DEELW \RXU FRGH VKRXOG SULQW WKH IROORZLQJ R, a, b, b, i, t <RXU PHWKRG VKRXOG SULQW QRWKLQJ LI WKH HPSW\ VWULQJ LV SDVVHG :ULWH D PHWKRG QDPHG pow WKDW DFFHSWV D EDVH DQG DQ H[SRQHQW DV SDUDPHWHUV DQG UHWXUQV WKH EDVH UDLVHG WR WKH JLYHQ SRZHU )RU H[DPSOH WKH FDOO pow(3, 4) UHWXUQV RU $VVXPH WKDW WKH EDVH DQG H[SRQHQW DUH QRQ QHJDWLYH :ULWH FRGH WKDW DVNV WKH XVHU WR HQWHU QXPEHUV WKHQ SULQWV WKH VPDOOHVW DQG ODUJHVW RI DOO WKH QXPEHUV W\SHG LQ E\ WKH XVHU <RX PD\ DVVXPH WKH XVHU HQWHUV D YDOLG QXPEHU JUHDWHU WKDQ IRU WKH QXPEHU RI QXPEHUV WR UHDG +HUH LV DQ H[DPSOH GLDORJXH 274 How many numbers do you want to enter? 4 Number 1: 5 Number 2: 11 Number 3: -2 Number 4: 3 Smallest = -2 Largest = 11 :ULWH D PHWKRG QDPHG printRange WKDW DFFHSWV WZR LQWHJHUV DV DUJXPHQWV DQG SULQWV WKH VHTXHQFH RI QXPEHUV EHWZHHQ WKH WZR DUJXPHQWV 3ULQW DQ LQFUHDVLQJ VHTXHQFH LI WKH ILUVW DUJXPHQW LV VPDOOHU WKDQ WKH VHFRQG DQG RWKHUZLVH SULQW D GHFUHDVLQJ VHTXHQFH ,I WKH WZR QXPEHUV DUH WKH VDPH WKDW QXPEHU VKRXOG EH SULQWHG EHWZHHQ > DQG @ +HUH DUH WZR VDPSOH FDOOV WR printRange printRange(2, 7); printRange(19, 11); printRange(5, 5); 7KH RXWSXW SURGXFHG VKRXOG EH WKH IROORZLQJ [2, 3, 4, 5, 6, 7] [19, 18, 17, 16, 15, 14, 13, 12, 11] [5] :ULWH D SLHFH RI FRGH WKDW FDOFXODWHV D VWXGHQW V JUDGH DYHUDJH 7KH XVHU ZLOO W\SH D OLQH RI LQSXW FRQWDLQLQJ WKH VWXGHQW V QDPH WKHQ D QXPEHU RI VFRUHV IROORZHG E\ WKDW PDQ\ LQWHJHU VFRUHV +HUH DUH WZR H[DPSOH GLDORJXHV Enter a student record: Maria 5 72 91 84 89 78 Maria's grade is 82.8 Enter a student record: Jordan 4 86 71 62 90 Jordan's grade is 77.25 )RU H[DPSOH 0DULD V JUDGH LV EHFDXVH KHU DYHUDJH RI HTXDOV :ULWH D PHWKRG QDPHG printTriangleType WKDW DFFHSWV WKUHH LQWHJHU DUJXPHQWV WKDW UHSUHVHQW WKH OHQJWKV RI WKH VLGHV RI D WULDQJOH DQG SULQWV ZKDW W\SH RI WULDQJOH LW LV 7KH WKUHH W\SHV DUH HTXLODWHUDO LVRVFHOHV DQG VFDOHQH $Q HTXLODWHUDO WULDQJOH KDV DOO VLGHV WKH VDPH OHQJWK DQ LVRVFHOHV KDV VLGHV WKH VDPH OHQJWK DQG D VFDOHQH KDV VLGHV RI GLIIHUHQW OHQJWKV +HUH DUH VRPH H[DPSOH FDOOV WR printTriangleType printTriangleType(5, printTriangleType(6, printTriangleType(5, printTriangleType(2, 7, 7); 6, 6); 7, 8); 18, 2); 7KH RXWSXW SURGXFHG VKRXOG EH WKH IROORZLQJ isosceles equilateral scalene isosceles 275 :ULWH D PHWKRG QDPHG numUnique WKDW WDNHV WKUHH LQWHJHUV DV SDUDPHWHUV DQG WKDW UHWXUQV WKH QXPEHU RI XQLTXH LQWHJHUV DPRQJ WKH WKUHH )RU H[DPSOH WKH FDOO numUnique(7, 31, 7) VKRXOG UHWXUQ EHFDXVH WKH SDUDPHWHUV UHSUHVHQW GLIIHUHQW QXPEHUV WKH YDOXHV DQG %\ FRQWUDVW WKH FDOO RI numUnique(6, 6, 6) ZRXOG UHWXUQ EHFDXVH WKHUH LV RQO\ XQLTXH QXPEHU DPRQJ WKH WKUHH SDUDPHWHUV WKH YDOXH :ULWH D PHWKRG QDPHG average WKDW WDNHV WZR LQWHJHUV DV SDUDPHWHUV DQG WKDW UHWXUQV WKH DYHUDJH RI WKH WZR LQWHJHUV 0RGLI\ \RXU pow PHWKRG IURP D SUHYLRXV H[HUFLVH WR XVH W\SH GRXEOH DQG WR ZRUN FRUUHFWO\ IRU QHJDWLYH QXPEHUV )RU H[DPSOH WKH FDOO pow(-4.0, 3.0) UHWXUQV RU DQG WKH FDOO pow(4.0, -2.0) UHWXUQV RU :ULWH D PHWKRG QDPHG getGrade WKDW DFFHSWV DQ LQWHJHU UHSUHVHQWLQJ D VWXGHQW V JUDGH SHUFHQWDJH LQ D FRXUVH DQG UHWXUQV WKDW VWXGHQWV QXPHULFDO FRXUVH JUDGH EHWZHHQ IDLOLQJ DQG SHUIHFW $VVXPH WKDW VFRUHV DUH LQ WKH UDQJH RI WR DQG WKDW JUDGHV DUH EDVHG RQ WKH IROORZLQJ VFDOH Score Grade ------------------------< 60 0.0 60-62 0.7 63 0.8 64 0.9 65 1.0 ... 92 3.7 93 3.8 94 3.9 >= 95 4.0 )RU DGGHG FKDOOHQJH PDNH \RXU PHWKRG WKURZ DQ IllegalArgumentException LI WKH XVHU SDVVHV D JUDGH ORZHU WKDQ RU KLJKHU WKDQ :ULWH -DYD FRGH WKDW SURPSWV WKH XVHU WR HQWHU RQH RU PRUH ZRUGV DQG SULQWV ZKHWKHU WKDW 6WULQJ LV D SDOLQGURPH $ SDOLQGURPH LV D VWULQJ WKDW UHDGV WKH VDPH IRUZDUGV DV LW GRHV EDFNZDUGV VXFK DV DEED RU UDFHFDU +LQW 7KH SUHYLRXV FKDSWHU KDG D VHOI FKHFN H[HUFLVH DVNLQJ \RX WR ZULWH D PHWKRG FDOOHG reverse WKDW ZRXOG UHWXUQ D 6WULQJ LQ UHYHUVHG RUGHU ,I \RX FRPSOHWHG WKLV H[HUFLVH \RX PD\ ILQG LW XVHIXO KHUH )RU DGGHG FKDOOHQJH PDNH WKH FRGH FDVH LQVHQVLWLYH VR WKDW ZRUGV OLNH $EED RU 0DGDP ZRXOG EH FRQVLGHUHG SDOLQGURPHV :ULWH D PHWKRG QDPHG swapPairs WKDW DFFHSWV D 6WULQJ DV D SDUDPHWHU DQG UHWXUQV WKDW 6WULQJ ZLWK HDFK SDLU RI DGMDFHQW OHWWHUV UHYHUVHG ,I WKH 6WULQJ KDV DQ RGG QXPEHU RI OHWWHUV WKH ODVW OHWWHU LV XQFKDQJHG )RU H[DPSOH WKH FDOO swapPairs("example") ZRXOG UHWXUQ "xemalpe" DQG WKH FDOO swapPairs("hello there") ZRXOG UHWXUQ "ehll ohtree" 276 :ULWH D PHWKRG QDPHG wordCount WKDW DFFHSWV D 6WULQJ DV LWV SDUDPHWHU DQG UHWXUQV WKH QXPEHU RI ZRUGV LQ WKH 6WULQJ $ ZRUG LV D VHTXHQFH RI RQH RU PRUH QRQ VSDFH FKDUDFWHUV DQ\ FKDUDFWHU RWKHU WKDQ )RU H[DPSOH WKH FDOO wordCount("hello") UHWXUQV WKH FDOO wordCount("how are you?") UHWXUQV WKH FDOO wordCount(" this string has wide spaces ") UHWXUQV DQG WKH FDOO wordCount(" ") UHWXUQV 3URJUDPPLQJ 3URMHFWV :ULWH D SURJUDP WKDW SURPSWV IRU DQ LQWHJHU DQG UHSRUWV WKH IDFWRUV RI WKH LQWHJHU L H WKH QXPEHUV WKDW JR HYHQO\ LQWR LW :ULWH D SURJUDP WKDW SURPSWV IRU WKH OHQJWKV RI WKH VLGHV RI D WULDQJOH DQG UHSRUWV WKH WKUHH DQJOHV :ULWH D SURJUDP WKDW SURPSWV IRU D QXPEHU DQG GLVSOD\V LW LQ 5RPDQ QXPHUDOV :ULWH D SURJUDP WKDW SURPSWV IRU D GDWH PRQWK GD\ \HDU DQG UHSRUWV WKH GD\ RI WKH ZHHN IRU WKDW GDWH ,W PLJKW EH KHOSIXO WR NQRZ WKDW -DQXDU\ VW ZDV D 0RQGD\ $ QHZ WD[ ODZ KDV MXVW EHHQ SDVVHG E\ WKH JRYHUQPHQW WKH ILUVW RI LQFRPH LV IUHH RI WD[ WKH QH[W LV WD[HG DW WKH QH[W LV WD[HG DW DQG WKH UHVW LV WD[HG DW :ULWH DQ LQWHUDFWLYH SURJUDP WKDW SURPSWV IRU D XVHU V LQFRPH DQG UHSRUWV WKH FRUUHVSRQGLQJ WD[ :ULWH D SURJUDP WKDW GLVSOD\V 3DVFDO V WULDQJOH 1 1 1 1 1 1 5 1 6 4 10 3 6 10 2 3 4 5 1 1 1 1 1 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 1 10 45 120 210 252 210 120 45 10 1 8VH 6\VWHP RXW SULQWI WR IRUPDW WKH RXWSXW LQWR ILHOGV RI ZLGWK $ XVHIXO WHFKQLTXH IRU FDWFKLQJ W\SLQJ HUURUV LV WR XVH D FKHFN GLJLW )RU H[DPSOH VXSSRVH WKDW D VFKRRO DVVLJQV D VL[ GLJLW QXPEHU WR HDFK VWXGHQW $ VHYHQWK GLJLW FDQ EH GHWHUPLQHG IURP WKH RWKHU GLJLWV DV LQ (1 * (1st digit) + 2 * (2nd digit) + ... + 6 * (6th digit)) % 10 :KHQ VRPHRQH W\SHV LQ D VWXGHQW QXPEHU WKH\ W\SH DOO VHYHQ GLJLWV ,I WKH QXPEHU LV W\SHG LQFRUUHFWO\ WKH FKHFN GLJLW ZLOO IDLO WR PDWFK LQ RI WKH FDVHV :ULWH DQ LQWHUDFWLYH SURJUDP WKDW SURPSWV IRU D VL[ GLJLW VWXGHQW QXPEHU DQG UHSRUWV WKH FKHFN GLJLW IRU WKDW QXPEHU XVLQJ WKH VFKHPH GHVFULEHG DERYH 6WXDUW 5HJHV 277 0DUW\ 6WHSS 278 ...
View Full Document

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

Ask a homework question - tutors are online