Chapter 08 - Classes

Chapter 08 - Classes - &KDSWHU &ODVVHV...

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 &ODVVHV &RS\ULJKW E\ 6WXDUW 5HJHV DQG 0DUW\ 6WHSS 0RUH ,QVWDQFH 0HWKRGV 7KH WR6WULQJ 0HWKRG 7KH HTXDOV 0HWKRG 7KH WKLV .H\ZRUG 0XOWLSOH &RQVWUXFWRUV &DVH 6WXG\ 'HVLJQLQJ D 6WRFN &ODVV 6WRFN %HKDYLRU 6WRFN )LHOGV 6WRFN &RQVWUXFWRU 6WRFN 0HWKRG ,PSOHPHQWDWLRQ 7KH &RPSOHWH 6WRFN &ODVV 2EMHFW 2ULHQWHG 3URJUDPPLQJ &RQFHSWV &ODVVHV DQG 2EMHFWV 2EMHFW 6WDWH )LHOGV 2EMHFW %HKDYLRU 0HWKRGV $ 'HWDLOHG ([DPSOH 0XWDWRUV DQG $FFHVVRUV 2EMHFW ,QLWLDOL]DWLRQ &RQVWUXFWRUV (QFDSVXODWLRQ 3ULYDWH 'DWD )LHOGV &ODVV ,QYDULDQWV ,QWURGXFWLRQ 1RZ WKDW ZH KDYH VSHQW WLPH PDVWHULQJ WKH EDVLFV RI SURFHGXUDO VW\OH SURJUDPPLQJ LQ -DYD ZH DUH ILQDOO\ UHDG\ WR H[SORUH ZKDW -DYD ZDV GHVLJQHG IRU REMHFW RULHQWHG SURJUDPPLQJ 7KLV FKDSWHU LQWURGXFHV WKH EDVLF WHUPLQRORJ\ ZH XVH ZKHQ WDONLQJ DERXW REMHFWV DQG VKRZV \RX KRZ WR GHFODUH \RXU RZQ FODVVHV WR FUHDWH \RXU RZQ REMHFWV :H OO VHH WKDW REMHFWV DUH HQWLWLHV WKDW FRQWDLQ VWDWH DQG EHKDYLRU DQG FDQ EH XVHG DV SDUWV RI ODUJHU SURJUDPV :H OO GLVFXVV WKH FRQFHSWV RI DEVWUDFWLRQ DQG HQFDSVXODWLRQ ZKLFK DOORZ XV WR XVH REMHFWV DW D KLJK OHYHO ZLWKRXW XQGHUVWDQGLQJ WKHLU LQQHU GHWDLOV :H OO DOVR GLVFXVV LGHDV IRU GHVLJQLQJ QHZ FODVVHV RI REMHFWV DQG LPSOHPHQWLQJ WKH SURJUDPV WKDW XWLOL]H WKHP 2EMHFW 2ULHQWHG 3URJUDPPLQJ &RQFHSWV 0RVW RI RXU IRFXV VR IDU KDV EHHQ RQ SURFHGXUDO GHFRPSRVLWLRQ EUHDNLQJ D FRPSOH[ WDVN LQWR VPDOOHU VXEWDVNV 7KLV LV WKH ROGHVW VW\OH RI SURJUDPPLQJ DQG HYHQ LQ D ODQJXDJH OLNH -DYD ZH VWLOO XVH SURFHGXUDO WHFKQLTXHV %XW -DYD SURYLGHV D GLIIHUHQW DSSURDFK WR SURJUDPPLQJ WKDW ZH FDOO REMHFW RULHQWHG SURJUDPPLQJ 2EMHFW 2ULHQWHG 3URJUDPPLQJ 223 5HDVRQLQJ DERXW D SURJUDP DV D VHW RI REMHFWV UDWKHU WKDQ DV D VHW RI DFWLRQV 2EMHFW RULHQWHG SURJUDPPLQJ LQYROYHV D SDUWLFXODU YLHZ RI SURJUDPPLQJ WKDW KDV LWV RZQ WHUPLQRORJ\ /HW V H[SORUH WKDW WHUPLQRORJ\ ZLWK QRQ SURJUDPPLQJ H[DPSOHV ILUVW :H YH EHHQ XVLQJ WKH ZRUG REMHFW IRU D ZKLOH ZLWKRXW JLYLQJ D YHU\ JRRG GHILQLWLRQ RI WKH ZRUG %HORZ LV D GHILQLWLRQ WKDW LV RIWHQ XVHG E\ REMHFW RULHQWHG SURJUDPPHUV 2EMHFW $ SURJUDPPLQJ HQWLW\ WKDW FRQWDLQV VWDWH DQG EHKDYLRU 427 7R XQGHUVWDQG WKLV GHILQLWLRQ ZH KDYH WR XQGHUVWDQG WKH WHUPV VWDWH DQG EHKDYLRU VRPH RI WKH PRVW IXQGDPHQWDO FRQFHSWV LQ REMHFW RULHQWHG SURJUDPPLQJ 7KHVH DUH /HW V FRQVLGHU WKH FODVV RI REMHFWV ZH FDOO UDGLRV :KDW DUH WKH GLIIHUHQW VWDWHV D UDGLR FDQ EH LQ" ,W FDQ EH WXUQHG RQ RU WXUQHG RII ,W FDQ EH WXQHG WR RQH RI PDQ\ GLIIHUHQW VWDWLRQV DQG LW FDQ EH VHW WR GLIIHUHQW YROXPHV $Q\ JLYHQ UDGLR KDV WR NQRZ ZKDW VWDWH LW LV LQ ZKLFK PHDQV WKDW LW KDV WR NHHS WUDFN RI WKLV LQIRUPDWLRQ LQWHUQDOO\ :H FDOO WKH FROOHFWLRQ RI VXFK LQWHUQDO YDOXHV WKH VWDWH RI DQ REMHFW 6WDWH $ VHW RI YDOXHV LQWHUQDO GDWD VWRUHG LQ DQ REMHFW :KDW DUH WKH EHKDYLRUV RI D UDGLR" 7KH PRVW REYLRXV RQH LV WKDW LW SURGXFHV VRXQG ZKHQ LW LV WXUQHG RQ DQG WKH YROXPH LV WXUQHG XS %XW LW KDV RWKHU EHKDYLRUV WKDW LQYROYH WKH PDQLSXODWLRQ RI LWV LQWHUQDO VWDWH :H FDQ WXUQ D UDGLR RQ RU RII :H FDQ FKDQJH WKH VWDWLRQ RU YROXPH :H FDQ VHH ZKDW VWDWLRQ WKH UDGLR LV VHW WR ULJKW QRZ :H FDOO WKH FROOHFWLRQ RI WKHVH RSHUDWLRQV WKH EHKDYLRU RI DQ REMHFW %HKDYLRU $ VHW RI DFWLRQV DQ REMHFW FDQ SHUIRUP RIWHQ UHSRUWLQJ RU PRGLI\LQJ LWV LQWHUQDO VWDWH 2EMHFWV WKHPVHOYHV DUH QRW FRPSOHWH SURJUDPV ,QVWHDG WKH\ DUH FRPSRQHQWV WKDW DUH JLYHQ GLVWLQFW UROHV DQG UHVSRQVLELOLWLHV 2EMHFWV FDQ EH XVHG DV SDUW RI ODUJHU SURJUDPV WR VROYH SUREOHPV 7KH SLHFHV RI FRGH WKDW FUHDWH DQG XVH REMHFWV DUH NQRZQ DV FOLHQWV &OLHQW RU FOLHQW FRGH &RGH WKDW XVHV DQ REMHFW &OLHQW SURJUDPV LQWHUDFW ZLWK REMHFWV E\ VHQGLQJ PHVVDJHV WR WKHP DVNLQJ WKHP WR SHUIRUP EHKDYLRU $ PDMRU EHQHILW RI REMHFWV LV WKDW WKH\ SURYLGH UHXVDEOH SLHFHV RI FRGH WKDW FDQ EH XWLOL]HG LQ PDQ\ FOLHQW SURJUDPV <RX YH XVHG VHYHUDO LQWHUHVWLQJ REMHFWV VWDUWLQJ LQ &KDSWHU VXFK DV WKRVH RI W\SH 6WULQJ 3RLQW 6FDQQHU 5DQGRP DQG )LOH -DYD V FODVV OLEUDULHV FRQWDLQ RYHU H[LVWLQJ FODVVHV RI REMHFWV $V \RX ZULWH ODUJHU SURJUDPV \RX OO ILQG FDVHV ZKHUH -DYD GRHVQ W KDYH D SUH H[LVWLQJ REMHFW IRU WKH SUREOHP \RX UH VROYLQJ )RU H[DPSOH LI \RX ZHUH FUHDWLQJ D FDOHQGDU DSSOLFDWLRQ \RX PLJKW ZDQW REMHFWV WR UHSUHVHQW GDWHV FRQWDFWV DQG DSSRLQWPHQWV ,I \RX ZHUH FUHDWLQJ D GLPHQVLRQDO JUDSKLFDO VLPXODWLRQ \RX PLJKW ZDQW REMHFWV WR UHSUHVHQW ' SRLQWV YHFWRUV DQG PDWULFHV ,I \RX ZHUH ZULWLQJ D ILQDQFLDO SURJUDP \RX PLJKW ZDQW FODVVHV WR UHSUHVHQW \RXU YDULRXV DVVHWV WUDQVDFWLRQV DQG H[SHQVHV ,Q WKLV FKDSWHU ZH OO OHDUQ KRZ WR FUHDWH RXU RZQ FODVVHV RI REMHFWV WKDW FDQ EH XVHG E\ FOLHQW SURJUDPV OLNH WKHVH 428 'LG <RX .QRZ 2SHUDWLQJ 6\VWHPV +LVWRU\ DQG 2EMHFWV 7R XQGHUVWDQG WKH EDVLF LGHD RI 223 FRQVLGHU WKH KLVWRU\ RI KRZ XVHUV LQWHUDFW ZLWK D FRPSXWHU ,I \RX ZHQW EDFN WR \RX G ILQG WKDW WKH ,%0 3& DQG LWV FORQHV ZHUH WKH GRPLQDQW PDFKLQHV DQG PRVW SHRSOH ZHUH UXQQLQJ DQ RSHUDWLQJ V\VWHP FDOOHG '26 '26 XVHV ZKDW ZH FDOO D FRPPDQG OLQH LQWHUIDFH LQ ZKLFK WKH XVHU W\SHV FRPPDQGV DW D SURPSW 7KH FRQVROH ZLQGRZ ZH KDYH EHHQ XVLQJ LV D VLPLODU LQWHUIDFH 7R GHOHWH D ILOH LQ '26 IRU H[DPSOH \RX ZRXOG JLYH WKH FRPPDQG GHO VKRUW IRU GHOHWH IROORZHG E\ WKH ILOH QDPH DV LQ del data.txt 7KLV LQWHUIDFH FDQ EH GHVFULEHG LQ VLPSOH WHUPV DV YHUE QRXQ ,Q IDFW LI \RX ORRN DW D '26 PDQXDO \RX ZLOO ILQG WKDW LW LV IXOO RI YHUEV 7KLV FORVHO\ SDUDOOHOV WKH SURFHGXUDO DSSURDFK WR SURJUDPPLQJ :KHQ ZH ZDQW WR DFFRPSOLVK VRPH WDVN ZH LVVXH D FRPPDQG WKH YHUE DQG WKHQ PHQWLRQ WKH REMHFW RI WKH DFWLRQ WKH QRXQ WKH WKLQJ ZH ZDQW WR DIIHFW ,Q $SSOH &RPSXWHU UHOHDVHG D QHZ FRPSXWHU FDOOHG D 0DFLQWRVK WKDW KDG D GLIIHUHQW RSHUDWLQJ V\VWHP WKDW XVHG ZKDW ZH FDOO D *UDSKLFDO 8VHU ,QWHUIDFH RU *8, 7KH *8, LQWHUIDFH XVHV D JUDSKLFDO GHVNWRS PHWDSKRU WKDW KDV EHFRPH VR ZHOO NQRZQ WKDW SHRSOH DOPRVW IRUJHW WKDW LW LV D PHWDSKRU 7KH 0DFLQWRVK ZDV QRW WKH ILUVW FRPSXWHU WR XVH WKH GHVNWRS PHWDSKRU DQG D *8, EXW LW ZDV WKH ILUVW VXFK FRPSXWHU WKDW EHFDPH D PDMRU FRPPHUFLDO VXFFHVV /DWHU 0LFURVRIW EURXJKW WKLV IXQFWLRQDOLW\ WR ,%0 3&V ZLWK LWV :LQGRZV RSHUDWLQJ V\VWHP 6R KRZ GR \RX GHOHWH D ILOH RQ D 0DFLQWRVK RU RQ D :LQGRZV PDFKLQH" <RX ORFDWH WKH LFRQ IRU WKH ILOH DQG FOLFN RQ LW 7KHQ \RX KDYH VHYHUDO RSWLRQV <RX FDQ GUDJ LW WR WKH JDUEDJH UHF\FOLQJ ELQ 2U \RX FDQ JLYH D GHOHWH FRPPDQG (LWKHU ZD\ \RX VWDUW ZLWK WKH WKLQJ \RX ZDQW WR GHOHWH DQG WKHQ JLYH WKH FRPPDQG \RX ZDQW WR SHUIRUP 7KLV LV D UHYHUVDO RI WKH IXQGDPHQWDO SDUDGLJP ,Q '26 LW ZDV YHUE QRXQ EXW ZLWK D *8, LW V QRXQ YHUE 7KLV GLIIHUHQW ZD\ RI YLHZLQJ WKH ZRUOG LV WKH FRUH RI REMHFW RULHQWHG SURJUDPPLQJ 0RVW PRGHUQ SURJUDPV XVH D JUDSKLFDO XVHU LQWHUIDFH EHFDXVH ZH KDYH OHDUQHG WKDW SHRSOH ILQG LW PRUH QDWXUDO WR ZRUN WKLV ZD\ :H DUH XVHG WR SRLQWLQJ DW WKLQJV SLFNLQJ XS WKLQJV JUDEELQJ WKLQJV 6WDUWLQJ ZLWK WKH REMHFW LV YHU\ QDWXUDO IRU XV 7KLV DSSURDFK KDV DOVR SURYHG WR EH D KHOSIXO ZD\ WR VWUXFWXUH RXU SURJUDPV E\ GLYLGLQJ RXU SURJUDPV XS LQWR GLIIHUHQW REMHFWV WKDW HDFK FDQ GR FHUWDLQ WDVNV UDWKHU WKDQ GLYLGLQJ XS D WDVN LQWR VXEWDVNV &ODVVHV DQG 2EMHFWV ,Q WKH SUHYLRXV FKDSWHUV ZH YH FRQVLGHUHG WKH ZRUGV FODVV DQG SURJUDP WR EH URXJKO\ V\QRQ\PRXV :H ZURWH SURJUDPV E\ FUHDWLQJ D QHZ FODVV DQG SODFLQJ D PDLQ VWDWLF PHWKRG LQWR WKDW FODVV %XW FODVVHV KDYH DQRWKHU XVH LQ -DYD WR VHUYH DV D WHPSODWH IRU D QHZ W\SH RI REMHFWV 7R FUHDWH D QHZ W\SH RI REMHFWV LQ -DYD ZH PXVW FUHDWH D FODVV DQG DGG FRGH WR LW WKDW VSHFLILHV WKH IROORZLQJ WKLQJV 429 7KH VWDWH VWRUHG LQ HDFK REMHFW 7KH EHKDYLRU HDFK REMHFW FDQ SHUIRUP +RZ WR FRQVWUXFW REMHFWV RI WKH W\SH 2QFH ZH KDYH ZULWWHQ WKH DSSURSULDWH FRGH WKH FODVV FDQ EH XVHG WR FUHDWH REMHFWV RI LWV W\SH :H FDQ WKHQ XVH WKRVH REMHFWV LQ RXU FOLHQW SURJUDPV :H VD\ WKDW REMHFWV DUH LQVWDQFHV RI WKH FODVV EHFDXVH RQH FODVV FDQ EH XVHG WR FRQVWUXFW PDQ\ REMHFWV $QRWKHU ZD\ WR WKLQN RI D FODVV LV DV D EOXHSULQW $ EOXHSULQW FRQWDLQV WKH GLUHFWLRQV WR EXLOG D KRXVH 2QH EOXHSULQW FDQ EH XVHG WR FUHDWH PDQ\ VLPLODU KRXVHV HDFK ZLWK LWV RZQ VWDWH 6LPLODUO\ D FODVV FRQWDLQV WKH GLUHFWLRQV WR EXLOG REMHFWV ZLWK VWDWH DQG EHKDYLRU ,Q WKH QH[W VHYHUDO VHFWLRQV ZH OO H[SORUH WKH VWUXFWXUH RI D FODVV E\ ZULWLQJ D QHZ FODVV LQFUHPHQWDOO\ :H OO ZULWH RXU RZQ YHUVLRQ RI WKH 3RLQW W\SH IURP WKH MDYD DZW SDFNDJH $ 3RLQW REMHFW UHSUHVHQWV D GLPHQVLRQDO [ \ ORFDWLRQ 3RLQW REMHFWV DUH XVHIXO IRU DSSOLFDWLRQV WKDW VWRUH PDQ\ ' ORFDWLRQV VXFK DV PDSV RI FLWLHV JUDSKLFDO DQLPDWLRQV DQG JDPHV 7KH PDLQ FRPSRQHQWV RI D FODVV WKDW ZH OO VHH LQ WKHVH VHFWLRQV DUH ILHOGV WKH GDWD VWRUHG LQ HDFK REMHFW PHWKRGV WKH EHKDYLRU HDFK REMHFW FDQ H[HFXWH FRQVWUXFWRUV VSHFLDO PHWKRGV XVHG WR FRQVWUXFW REMHFWV ZLWK WKH new NH\ZRUG DQG HQFDSVXODWLRQ SURWHFWLQJ DQ REMHFW V GDWD IURP RXWVLGH DFFHVV :H OO IRFXV RQ WKHVH FRQFHSWV E\ FUHDWLQJ IRXU PDMRU YHUVLRQV RI WKH 3RLQW FODVV 2XU ILUVW YHUVLRQ ZLOO JLYH XV 3RLQW REMHFWV WKDW FRQWDLQ RQO\ GDWD 7KH VHFRQG YHUVLRQ ZLOO DGG EHKDYLRU WR WKH REMHFWV 7KH WKLUG YHUVLRQ ZLOO DOORZ XV WR FRQVWUXFW 3RLQWV DW DQ\ LQLWLDO SRVLWLRQ 7KH IRXUWK YHUVLRQ ZLOO HQFDSVXODWH HDFK 3RLQW V LQWHUQDO GDWD IURP XQZDQWHG RXWVLGH DFFHVV 2QO\ WKH IRXUWK YHUVLRQ RI WKH 3RLQW FODVV ZLOO EH ZULWWHQ LQ SURSHU REMHFW RULHQWHG VW\OH WKH RWKHUV ZLOO EH LQFRPSOHWH DQG XVHG WR LOOXVWUDWH HDFK IHDWXUH RI FODVVHV LQ LVRODWLRQ 2EMHFW 6WDWH )LHOGV 7KH ILUVW YHUVLRQ RI RXU 3RLQW FODVV ZLOO FRQWDLQ VWDWH RQO\ 7R VSHFLI\ HDFK REMHFW V VWDWH ZH GHFODUH VSHFLDO YDULDEOHV LQVLGH WKH FODVV FDOOHG ILHOGV 7KHUH DUH PDQ\ V\QRQ\PV IRU ILHOG WKDW FRPH IURP RWKHU SURJUDPPLQJ ODQJXDJHV DQG HQYLURQPHQWV VXFK DV LQVWDQFH YDULDEOH GDWD PHPEHU DQG DWWULEXWH )LHOG RU ,QVWDQFH 9DULDEOH $ YDULDEOH LQVLGH DQ REMHFW WKDW PDNHV XS SDUW RI LWV LQWHUQDO VWDWH :KHQ FUHDWLQJ D QHZ FODVV ZH FUHDWH D ILOH IRU WKH FODVV DQG WKHQ VSHFLI\ WKH VWDWH DQG EHKDYLRU HDFK REMHFW RI WKDW FODVV VKRXOG KDYH 'DWD ILHOGV DUH GHFODUHG E\ ZULWLQJ D W\SH DQG QDPH LQVLGH WKH EUDFHV IRU WKH FODVV :H OO VHH D PRGLILHG V\QWD[ IRU GDWD ILHOGV ODWHU LQ WKLV FKDSWHU ZKHQ ZH GLVFXVV HQFDSVXODWLRQ 430 +HUH V WKH ILUVW YHUVLRQ RI RXU 3RLQW FODVV 7KLV FRGH FUHDWHV D QHZ FODVV QDPHG 3RLQW DQG GHFODUHV WKDW HDFK 3RLQW REMHFW ZLOO FRQWDLQ WZR ILHOGV DQ LQWHJHU QDPHG [ DQG DQ LQWHJHU QDPHG \ 7KH FODVV PXVW EH VDYHG LQWR D ILOH QDPHG 3RLQW MDYD 1 2 3 4 5 6 7 // A Point object represents an ordered pair of (x, y) coordinates. // First version: state only. public class Point { int x; int y; } 7KRXJK WKH V\QWD[ IRU GDWD ILHOGV PDNHV WKHP ORRN OLNH QRUPDO YDULDEOHV WKH\ DUH YHU\ GLIIHUHQW 7KH SUHFHGLQJ FRGH LV QRW MXVW GHFODULQJ D SDLU RI LQW YDULDEOHV QDPHG [ DQG \ 7KH FRGH LV DFWXDOO\ GHFODULQJ WKDW HYHU\ 3RLQW REMHFW ZLOO FRQWDLQ LWV RZQ LQW ILHOGV QDPHG [ DQG \ ,I ZH FUHDWH RQH KXQGUHG 3RLQW REMHFWV ZH OO KDYH RQH KXQGUHG SDLUV RI [ DQG \ ILHOGV RQH KHOG LQVLGH HDFK REMHFW 2QH WKLQJ \RX OO HQFRXQWHU ZKHQ \RX UH ZULWLQJ FODVVHV RI REMHFWV DQG FOLHQW FRGH LV WKDW \RXU SURJUDP ZLOO QRZ RFFXS\ PRUH WKDQ RQH ILOH 7KH 3RLQW FODVV LV VWRUHG LQ 3RLQW MDYD DQG WKH FOLHQW FRGH WKDW XVHV 3RLQW H[LVWV LQ D VHSDUDWH ILOH 7KH 3RLQW MDYD ILOH LVQ W DQ H[HFXWDEOH -DYD SURJUDP LQ LWVHOI EXW VLPSO\ D GHILQLWLRQ RI D QHZ FODVV RI REMHFWV IRU FOLHQW SURJUDPV WR XVH 3RLQW MDYD DQG LWV FOLHQW SURJUDPV VKRXOG EH LQ WKH VDPH IROGHU RQ \RXU FRPSXWHU VR WKDW WKH FODVVHV FDQ ILQG HDFK RWKHU DQG FRPSLOH VXFFHVVIXOO\ )RU QRZ FOLHQW SURJUDPV FUHDWH 3RLQW REMHFWV ZLWK WKH new NH\ZRUG DQG HPSW\ SDUHQWKHVHV VXFK DV Point origin = new Point(); 5HFDOO WKDW D 3RLQW REMHFW LV DOVR FDOOHG DQ LQVWDQFH RI WKH 3RLQW FODVV 7KH 3RLQW FODVV VHUYHV DV WKH EOXHSULQW IRU WKH FRQWHQWV RI HDFK RI LWV LQVWDQFHV &OLHQW SURJUDPV FDQ FUHDWH DV PDQ\ 3RLQW LQVWDQFHV DV QHHGHG :KHQ D 3RLQW REMHFW LV LQLWLDOO\ FRQVWUXFWHG LWV GDWD ILHOGV DUH JLYHQ GHIDXOW LQLWLDO YDOXHV RI VR D QHZ 3RLQW REMHFW DOZD\V EHJLQV DW WKH RULJLQ RI XQWLO ZH FKDQJH LWV [ \ YDOXHV 7KLV DXWRPDWLF LQLWLDOL]DWLRQ LV VLPLODU WR WKH ZD\ WKDW DUUD\ HOHPHQWV DUH DXWRPDWLFDOO\ JLYHQ GHIDXOW YDOXHV +HUH V WKH ILUVW YHUVLRQ RI D FOLHQW SURJUDP WKDW XVHV RXU 3RLQW FODVV ,W ZRXOG EH VDYHG LQWR D VHSDUDWH ILOH QDPHG 3RLQW0DLQ MDYD 431 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 // A program that deals with 2D points. // First version, to accompany Point class with state only. public class PointMain { public static void main(String args) { // create two Point objects Point p1 = new Point(); p1.x = 7; p1.y = 2; Point p2 = new Point(); p2.x = 4; p2.y = 3; // print each point and its distance from origin System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); double dist1 = Math.sqrt(p1.x * p1.x + p1.y * p1.y); System.out.println("distance from origin = " + dist1); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); double dist2 = Math.sqrt(p2.x * p2.x + p2.y * p2.y); System.out.println("distance from origin = " + dist2); System.out.println(); // translate each point to a new location p1.x += 11; p1.y += 6; p2.x += 1; p2.y += 7; // print the points again System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); } } 7KH FRGH SURGXFHV WKH IROORZLQJ RXWSXW p1 is (7, 2) distance from origin = 7.280109889280518 p2 is (4, 3) distance from origin = 5.0 p1 is (18, 8) p2 is (5, 10) 7KH FOLHQW SURJUDP KDV VRPH UHGXQGDQF\ WKDW ZH OO HOLPLQDWH DV ZH LPSURYH RXU 3RLQW FODVV LQ WKH QH[W VHFWLRQV 2EMHFW %HKDYLRU 0HWKRGV 7KH VHFRQG YHUVLRQ RI RXU 3RLQW FODVV ZLOO FRQWDLQ ERWK VWDWH DQG EHKDYLRU %HKDYLRU RI REMHFWV LV VSHFLILHG E\ ZULWLQJ LQVWDQFH PHWKRGV 7KH LQVWDQFH PHWKRGV RI DQ REMHFW GHVFULEH ZKDW PHVVDJHV WKDW REMHFW FDQ UHFHLYH 432 ,QVWDQFH 0HWKRG $ PHWKRG WKDW H[LVWV LQVLGH DQ REMHFW DQG RSHUDWHV RQ WKDW REMHFW PDNLQJ XS SDUW RI LWV EHKDYLRU $Q LQVWDQFH PHWKRG H[LVWV LQVLGH RI DQ REMHFW DQG LV FDOOHG RQ WKDW SDUWLFXODU REMHFW E\ ZULWLQJ WKH REMHFW YDULDEOH V QDPH D GRW DQG WKH PHWKRG QDPH ,Q SUHYLRXV FKDSWHUV \RX KDYH DOUHDG\ FDOOHG LQVWDQFH PHWKRGV RQ VHYHUDO W\SHV RI -DYD REMHFWV VXFK DV WKH OHQJWK PHWKRG RI D 6WULQJ RU WKH QH[W,QW PHWKRG RI D 6FDQQHU 2XU FOLHQW SURJUDP IURP WKH SUHYLRXV VHFWLRQ WUDQVODWHV WKH SRVLWLRQ RI WZR 3RLQW REMHFWV 2XU 3RLQW W\SH GRHVQ W KDYH DQ\ EHKDYLRU \HW VR WKH FOLHQW SURJUDP LV IRUFHG WR WUDQVODWH SRLQWV PDQXDOO\ E\ DGMXVWLQJ WKHLU [ \ ILHOG YDOXHV p1.x += 11; p1.y += 6; // our Point doesn't have a translate method 7UDQVODWLQJ SRLQWV LV D FRPPRQ RSHUDWLRQ VR ZH VKRXOG UHSUHVHQW LW DV D PHWKRG <RX PLJKW EH WHPSWHG WR ZULWH D VWDWLF WUDQVODWH PHWKRG LQ \RXU FOLHQW FRGH WKDW DFFHSWV D 3RLQW D GHOWD [ DQG D GHOWD \ DV SDUDPHWHUV ,WV FRGH ZRXOG ORRN OLNH WKLV // A static method to translate a Point. // Not a good model to follow. public static void translate(Point p, int dx, int dy) { p.x += dx; p.y += dy; } $ FDOO WR WKH VWDWLF PHWKRG ZRXOG ORRN OLNH WKLV translate(p1, 11, 6); // calling a translate static method %XW D VWDWLF PHWKRG LVQ W WKH EHVW ZD\ WR LPSOHPHQW WKH WUDQVODWH EHKDYLRU 2QH RI WKH ELJJHVW EHQHILWV RI SURJUDPPLQJ ZLWK REMHFWV LV WKDW ZH FDQ SXW UHODWHG GDWD DQG EHKDYLRU WRJHWKHU 7KH DELOLW\ IRU D 3RLQW WR WUDQVODWH LV FORVHO\ UHODWHG WR WKDW 3RLQW V [ \ GDWD VR LW V EHWWHU WR VSHFLI\ WKDW HDFK 3RLQW REMHFW ZLOO NQRZ KRZ WR WUDQVODWH LWVHOI :H OO GR WKLV E\ ZULWLQJ DQ LQVWDQFH PHWKRG LQ WKH 3RLQW FODVV 7KH FOLHQW SURJUDP FRXOG FDOO D 3RLQW V WUDQVODWH PHWKRG OLNH WKH IROORZLQJ FDOO 1RWLFH WKDW RXU LQVWDQFH PHWKRG RQO\ QHHGV WZR SDUDPHWHUV G[ DQG G\ 7KH FOLHQW GRHVQ W SDVV WKH 3RLQW DV D SDUDPHWHU LQVWHDG LW VHQGV D WUDQVODWH PHVVDJH WR WKH 3RLQW REMHFW E\ ZULWLQJ WKH REMHFW UHIHUHQFH V QDPH p1 EHIRUH WKH GRW DQG PHWKRG QDPH p1.translate(11, 6); // calling a translate instance method ,QVWDQFH PHWKRG KHDGHUV GR QRW KDYH WKH static NH\ZRUG WKDW VWDWLF PHWKRGV KDYH :H VWLOO LQFOXGH WKH SXEOLF NH\ZRUG WKH PHWKRG V UHWXUQ W\SH LWV QDPH DQG DQ\ SDUDPHWHUV WKH PHWKRG DFFHSWV +HUH V WKH VWDUW RI D 3RLQW FODVV ZLWK D WUDQVODWH PHWKRG :H YH GHFODUHG WKH KHDGHU EXW OHIW WKH ERG\ EODQN IRU D PRPHQW 433 public class Point { int x; int y; public void translate(int dx, int dy) { <code to implement the method> } } 5HPHPEHU WKDW ZKHQ ZH GHFODUH D WUDQVODWH PHWKRG LQ WKH 3RLQW FODVV ZH DUH VD\LQJ WKDW HDFK 3RLQW REMHFW KDV LWV RZQ FRS\ RI WKDW PHWKRG 6R ZKHQ ZH UH ZULWLQJ WKH ERG\ RI WKH WUDQVODWH PHWKRG ZH KDYH WR WKLQN IURP WKH SHUVSHFWLYH RI D SDUWLFXODU 3RLQW REMHFW 7KH FOLHQW KDV JLYHQ PH D G[ DQG G\ DQG VKH ZDQWV PH WR FKDQJH P\ [ DQG \ YDOXHV E\ WKRVH DPRXQWV ,Q SUHYLRXV FKDSWHUV ZH YH WDONHG DERXW VFRSH WKH UDQJH LQ ZKLFK D YDULDEOH FDQ EH VHHQ DQG XVHG 7KH VFRSH RI D GDWD ILHOG VSDQV LWV HQWLUH FODVV 7KLV PHDQV WKDW D 3RLQW REMHFW V LQVWDQFH PHWKRGV FDQ UHIHU WR LWV GDWD ILHOGV VLPSO\ E\ ZULWLQJ WKHLU QDPH 6R LI IRU H[DPSOH WKH 3RLQW ZDQWV WR DGMXVW LWV [ YDOXH E\ G[ ZH FDQ VLPSO\ ZULWH WKH VWDWHPHQW x += dx; +HUH LV D ZRUNLQJ WUDQVODWH PHWKRG WKDW DGMXVWV WKH 3RLQW V SRVLWLRQ public void translate(int dx, int dy) { x += dx; y += dy; } ,W PLJKW VHHP VWUDQJH WKDW WKH WUDQVODWH PHWKRG FDQ UHIHU WR [ DQG \ GLUHFWO\ ZLWKRXW EHLQJ PRUH VSHFLILF DERXW ZKLFK REMHFW LW V DIIHFWLQJ $V D QRQ SURJUDPPLQJ DQDORJ\ LI \RX UH VWDQGLQJ RXWVLGH QHDU VHYHUDO FDUV \RX PLJKW SRLQW DW RQH DQG VD\ 7XUQ WKDW FDU V VWHHULQJ ZKHHO WR WKH OHIW %XW LI \RX ZHUH LQVLGH WKH FDU \RX G VLPSO\ VD\ 7XUQ OHIW <RX ZRXOGQ W IHHO D QHHG WR VSHFLI\ ZKLFK FDU EHFDXVH LW V LPSOLHG WKDW \RX ZDQW WR VWHHU WKH FDU \RX UH FXUUHQWO\ RFFXS\LQJ ,Q LQVWDQFH PHWKRGV ZH GRQ W QHHG WR VSHFLI\ ZKLFK REMHFW V [ RU \ ZH UH XVLQJ EHFDXVH LW V LPSOLHG WKDW ZH ZDQW WR XVH WKH RQH RI WKH REMHFW WKDW UHFHLYHG WKH PHVVDJH +HUH V WKH FRPSOHWH 3RLQW FODVV WKDW FRQWDLQV D WUDQVODWH PHWKRG -DYD VW\OH JXLGHOLQHV VXJJHVW WR GHFODUH ILHOGV DW WKH WRS RI WKH FODVV ZLWK PHWKRGV EHORZ %XW LQ JHQHUDO LW LV OHJDO IRU D FODVV V FRQWHQWV WR DSSHDU LQ DQ\ RUGHU // A Point object represents an ordered pair of (x, y) coordinates. // Second version: state and behavior. public class Point { int x; int y; // Shifts this Point's position by the given amount. public void translate(int dx, int dy) { x += dx; y += dy; } } 7KH JHQHUDO V\QWD[ IRU LQVWDQFH PHWKRGV LV WKH IROORZLQJ 434 public <type> <name>(<type> <name>, ..., <type> <name>) { <statement>; <statement>; ... <statement>; } :H VDLG D PRPHQW DJR WKDW DQ LQVWDQFH PHWKRG V ERG\ KDV DQ LPSOLFLW NQRZOHGJH RI ZKDW REMHFW LW V RSHUDWLQJ XSRQ 7KLV LPSOLFLW NQRZOHGJH LV VRPHWLPHV FDOOHG WKH LPSOLFLW SDUDPHWHU /LNH D SDUDPHWHU LW LV D SLHFH RI LQIRUPDWLRQ WKDW FKDQJHV KRZ D PHWKRG H[HFXWHV ,PSOLFLW 3DUDPHWHU 7KH REMHFW EHLQJ DFWHG XSRQ LQ DQ LQVWDQFH PHWKRG $ 'HWDLOHG ([DPSOH /HW V ZDON WKURXJK DQ H[DPSOH LQ GHWDLO WR XQGHUVWDQG KRZ LQVWDQFH PHWKRGV ZRUN +HUH ZH OO ZULWH VRPH FOLHQW FRGH WKDW FRQVWUXFWV WZR 3RLQW REMHFWV DQG VHWV LQLWLDO SRVLWLRQV IRU WKHP // Construct two Point objects Point p1 = new Point(); p1.x = 7; p1.y = 2; Point p2 = new Point(); p2.x = 4; p2.y = 3; $IWHU WKH SUHFHGLQJ FRGH D GLDJUDP RI WKH YDULDEOHV DQG REMHFWV LQ PHPRU\ ZRXOG ORRN OLNH WKLV 5HPHPEHU WKDW HDFK REMHFW KDV LWV RZQ FRS\ RI WKH WUDQVODWH LQVWDQFH PHWKRG +-----------------------------------------+ +---+ | +----+ +----+ | p1 | +-+-->| x | 7 | y | 2 | | +---+ | +----+ +----+ | | | | public void translate(int dx, int dy) { | | x += dx; | | y += dy; | | } | +-----------------------------------------+ +-----------------------------------------+ +---+ | +----+ +----+ | p2 | +-+-->| x | 4 | y | 3 | | +---+ | +----+ +----+ | | | | public void translate(int dx, int dy) { | | x += dx; | | y += dy; | | } | +-----------------------------------------+ 435 1RZ ZH OO FDOO WKH WUDQVODWH PHWKRG RQ HDFK REMHFW )LUVW S LV WUDQVODWHG p1.translate(11, 6); :KHQ DQ LQVWDQFH PHWKRG LV FDOOHG RQ S S V FRS\ RI WKH WUDQVODWH PHWKRG LV SDVVHG WKH SDUDPHWHUV DQG DQG LV H[HFXWHG 6R WKH VWDWHPHQWV x += dx; DQG y += dy; DIIHFW WKH [ DQG \ GDWD ILHOGV LQVLGH S V REMHFW +-----------------------------------------+ +---+ | +----+ +----+ | p1 | +-+-->| x | 18 | y | 8 | | +---+ | +----+ +----+ | | | | public void translate(int dx, int dy) { | | | x += dx; | | y += dy; | } | +-----------------------------------------+ +-----------------------------------------+ +---+ | +----+ +----+ | p2 | +-+-->| x | 4 | y | 3 | | +---+ | +----+ +----+ | | | | public void translate(int dx, int dy) { | | x += dx; | | y += dy; | | } | +-----------------------------------------+ 'XULQJ WKH VHFRQG PHWKRG FDOO S V FRS\ RI WKH PHWKRG LV H[HFXWHG 6R WKH OLQHV LQ WKH ERG\ RI WKH WUDQVODWH PHWKRG FKDQJH WKH [ DQG \ ILHOGV RI S V REMHFW p2.translate(1, 7); +-----------------------------------------+ +---+ | +----+ +----+ | p1 | +-+-->| x | 18 | y | 8 | | +---+ | +----+ +----+ | | | | public void translate(int dx, int dy) { | | x += dx; | | y += dy; | | } | +-----------------------------------------+ +-----------------------------------------+ +---+ | +----+ +----+ | p2 | +-+-->| x | 5 | y | 10 | | +---+ | +----+ +----+ | | | | public void translate(int dx, int dy) { | | | x += dx; | | y += dy; | } | +-----------------------------------------+ 436 0HWKRGV OLNH WUDQVODWH DUH XVHIXO EHFDXVH WKH\ JLYH RXU REMHFWV XVHIXO EHKDYLRU WKDW OHWV XV ZULWH PRUH H[SUHVVLYH DQG FRQFLVH FOLHQW SURJUDPV +DYLQJ WKH FOLHQW FRGH PDQXDOO\ DGMXVW WKH [ DQG \ YDOXHV RI 3RLQW REMHFWV WR PRYH WKHP LV WHGLRXV HVSHFLDOO\ LQ ODUJHU FOLHQW SURJUDPV WKDW WUDQVODWH PDQ\ 3RLQWV %\ DGGLQJ WKH WUDQVODWH PHWKRG ZH KDYH JLYHQ D FOHDQ ZD\ WR DGMXVW WKH SRVLWLRQ RI D 3RLQW LQ D VLQJOH VWDWHPHQW 0XWDWRUV DQG $FFHVVRUV 2XU WUDQVODWH PHWKRG LV DQ H[DPSOH RI D NLQG RI LQVWDQFH PHWKRGV LQIRUPDOO\ NQRZQ DV PXWDWRUV $ PXWDWRU LV DQ LQVWDQFH PHWKRG WKDW FKDQJHV WKH VWDWH RI WKDW REMHFW LQ VRPH ZD\ *HQHUDOO\ WKLV PHDQV WKDW D PXWDWRU DVVLJQV D QHZ YDOXH WR RQH RI WKH REMHFW V GDWD ILHOGV )RU D UDGLR WKH PXWDWRUV ZRXOG EH VZLWFKHV DQG NQREV WKDW WXUQHG WKH UDGLR RQ DQG RII RU WKDW FKDQJHG WKH VWDWLRQ RU WKDW FKDQJHG WKH YROXPH <RX KDYH DOUHDG\ XVHG VHYHUDO PXWDWRUV VXFK DV WKH VHW/RFDWLRQ RI -DYD V 3RLQW REMHFWV RU WKH QH[W,QW PHWKRG RQ 6FDQQHU REMHFWV 0XWDWRU $ PHWKRG RI DQ REMHFW WKDW PRGLILHV LWV LQWHUQDO VWDWH 7KHUH DUH VRPH FRQYHQWLRQV DERXW KRZ WR ZULWH PXWDWRUV 0DQ\ PXWDWRU PHWKRGV QDPHV EHJLQ ZLWK VHW VXFK DV VHW,' RU VHW7LWOH 8VXDOO\ PXWDWRU PHWKRGV KDYH D YRLG UHWXUQ W\SH 0XWDWRUV RIWHQ DFFHSW SDUDPHWHUV WKDW VSHFLI\ WKH QHZ VWDWH RI WKH REMHFW RU WKH DPRXQW E\ ZKLFK WR PRGLI\ LWV FXUUHQW VWDWH $ VHFRQG LPSRUWDQW FDWHJRU\ RI LQVWDQFH PHWKRGV LV NQRZQ DV DFFHVVRUV $Q DFFHVVRU LV DQ LQVWDQFH PHWKRG WKDW SURYLGHV LQIRUPDWLRQ DERXW WKH VWDWH RI WKDW REMHFW )RU H[DPSOH D UDGLR REMHFW PLJKW SURYLGH DFFHVV WR LWV FXUUHQW VWDWLRQ VHWWLQJ RU FXUUHQW YROXPH ([DPSOHV RI DFFHVVRU PHWKRGV \RX KDYH VHHQ LQFOXGH WKH OHQJWK DQG VXEVWULQJ PHWKRGV RI 6WULQJ REMHFWV RU WKH LV'LUHFWRU\ PHWKRG RQ )LOH REMHFWV $FFHVVRU $Q LQVWDQFH PHWKRG WKDW OHWV FOLHQW FRGH UHDG WKH REMHFW V LQWHUQDO VWDWH 2XU FOLHQW SURJUDP FRPSXWHV KRZ IDU WZR 3RLQWV DUH IURP WKH RULJLQ RI 6LQFH WKLV LV D FRPPRQ RSHUDWLRQ UHODWHG WR WKH GDWD LQ D 3RLQW OHW V JLYH HDFK 3RLQW DQ DFFHVVRU QDPHG GLVWDQFH)URP2ULJLQ WKDW FRPSXWHV DQG UHWXUQV WKDW 3RLQW V GLVWDQFH IURP WKH RULJLQ 7KH PHWKRG DFFHSWV QR SDUDPHWHUV DQG UHWXUQV WKH GLVWDQFH DV D GRXEOH 7KH GLVWDQFH IURP WKH RULJLQ LV FRPSXWHG XVLQJ WKH 3\WKDJRUHDQ 7KHRUHP WDNLQJ WKH VTXDUH URRW RI WKH VXP RI WKH VTXDUHV RI WKH [ DQG \ YDOXHV $V LQ WKH WUDQVODWH PHWKRG ZH OO UHIHU WR WKH 3RLQW V [ DQG \ ILHOGV GLUHFWO\ LQ RXU FRPSXWDWLRQ // Returns the direct distance between this Point and the origin (0, 0). public double distanceFromOrigin() { return Math.sqrt(x * x + y * y); } 437 1RWH WKDW WKH GLVWDQFH)URP2ULJLQ PHWKRG GRHVQ W FKDQJH WKH 3RLQW V [ RU \ YDOXHV $FFHVVRUV DUH QRW XVHG WR FKDQJH WKH VWDWH RI WKH REMHFW WKH\ RQO\ UHSRUW LQIRUPDWLRQ DERXW WKH REMHFW 7KHUH DUH VRPH FRQYHQWLRQV DERXW KRZ WR ZULWH DFFHVVRUV 7KH\ XVXDOO\ GRQ W QHHG WR DFFHSW DQ\ SDUDPHWHUV DQG WKH\ XVXDOO\ KDYH D QRQ YRLG UHWXUQ W\SH WR DOORZ WKHP WR UHWXUQ WKH DSSURSULDWH LQIRUPDWLRQ 0DQ\ DFFHVVRUV QDPHV EHJLQ ZLWK WKH SUHIL[ JHW RU LV VXFK DV JHW%DODQFH RU LV(PSW\ +HUH V WKH FRPSOHWH VHFRQG YHUVLRQ RI RXU 3RLQW FODVV WKDW QRZ FRQWDLQV ERWK VWDWH DQG EHKDYLRU 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // A Point object represents an ordered pair of (x, y) coordinates. // Second version: state and behavior. public class Point { int x; int y; // Returns the direct distance between this Point and the origin (0, 0). public double distanceFromOrigin() { return Math.sqrt(x * x + y * y); } // Shifts this Point's position by the given amount. public void translate(int dx, int dy) { x += dx; y += dy; } } 7KH FOLHQW SURJUDP FDQ QRZ XVH WKH QHZ EHKDYLRU RI WKH 3RLQW FODVV 7KH UHVXOWLQJ SURJUDP SURGXFHV WKH VDPH RXWSXW DV EHIRUH EXW LV VKRUWHU DQG PRUH UHDGDEOH WKDQ WKH RULJLQDO 438 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 // A program that deals with 2D points. // Second version, to accompany Point class with state and behavior. public class PointMain { public static void main(String args) { // create two Point objects Point p1 = new Point(); p1.x = 7; p1.y = 2; Point p2 = new Point(); p2.x = 4; p2.y = 3; // print each point and its distance from origin System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); System.out.println("distance from origin = " + p1.distanceFromOrigin()); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); System.out.println("distance from origin = " + p2.distanceFromOrigin()); // translate each point to a new location p1.translate(11, 6); p2.translate(1, 7); // print the points again System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); } } 2EMHFW ,QLWLDOL]DWLRQ &RQVWUXFWRUV 2XU WKLUG YHUVLRQ RI WKH 3RLQW FODVV ZLOO FRQWDLQ WKH DELOLW\ WR FUHDWH 3RLQW REMHFWV DW DQ\ LQLWLDO SRVLWLRQ 7KH LQLWLDO VWDWH RI REMHFWV LV VSHFLILHG E\ ZULWLQJ D VSHFLDO PHWKRG FDOOHG D FRQVWUXFWRU &RQVWUXFWRU $ PHWKRG WKDW FUHDWHV D QHZ REMHFW DQG LQLWLDOL]HV LWV VWDWH $Q DQQR\LQJ SUREOHP LQ RXU FOLHQW FRGH VR IDU LV WKDW LW WDNHV WKUHH OLQHV WR FUHDWH DQG LQLWLDOL]H WKH VWDWH RI RQH 3RLQW REMHFW // client needs 3 statements to initialize one Point object (bad) Point p1 = new Point(); p1.x = 7; p1.y = 2; <RX PD\ UHFDOO WKDW LQ &KDSWHU ZH ZHUH DEOH WR FUHDWH -DYD 3RLQW REMHFWV ZLWK LQLWLDO SRVLWLRQV LQ D VLQJOH VWDWHPHQW :H GLG WKLV E\ ZULWLQJ WKHLU LQLWLDO [ \ YDOXHV LQ SDUHQWKHVHV DV ZH FRQVWUXFWHG WKH new Point Point p = new Point(10, 27); // this is legal with Java's Point type 439 6XFK D VWDWHPHQW ZRXOGQ W EH OHJDO IRU RXU 3RLQW FODVV EHFDXVH ZH KDYHQ W ZULWWHQ DQ\ FRGH VSHFLI\LQJ KRZ WR FUHDWH D 3RLQW ZLWK DQ LQLWLDO [ \ SRVLWLRQ :H FDQ VSHFLI\ KRZ WR GR WKLV E\ ZULWLQJ D FRQVWUXFWRU LQ RXU 3RLQW FODVV $ FRQVWUXFWRU LV WKH SLHFH RI FRGH WKDW LV H[HFXWHG ZKHQ WKH FOLHQW XVHV WKH new NH\ZRUG WR FUHDWH D QHZ REMHFW 7KH FRGH IRU D FRQVWUXFWRU ORRNV OLNH D PHWKRG WKDW KDV WKH VDPH QDPH DV WKH FODVV DQG KDV QR UHWXUQ W\SH VSHFLILHG $ FRQVWUXFWRU PD\ DFFHSW SDUDPHWHUV WKDW KHOS WR LQLWLDOL]H WKH REMHFW V VWDWH 2XU FRQVWUXFWRU IRU 3RLQWV ZLOO DFFHSW LQLWLDO [ DQG \ YDOXHV DV SDUDPHWHUV DQG VWRUH WKHP LQWR WKH QHZ 3RLQW V [ DQG \ GDWD ILHOGV +HUH V WKH FRQVWUXFWRU FRGH // Constructs a new Point at the given initial x/y position. public Point(int initialX, int initialY) { x = initialX; y = initialY; } /LNH LQVWDQFH PHWKRGV FRQVWUXFWRUV H[HFXWH RQ D SDUWLFXODU REMHFW WKH RQH WKDW V EHLQJ FUHDWHG ZLWK WKH new NH\ZRUG DQG FDQ UHIHU WR WKDW REMHFW V GDWD ILHOGV DQG PHWKRGV GLUHFWO\ ,Q RXU FDVH ZH VWRUH LQLWLDO; DQG LQLWLDO< SDUDPHWHU YDOXHV LQWR RXU 3RLQW V ILHOGV [ DQG \ +HUH LV WKH FRPSOHWH FRGH IRU WKH WKLUG YHUVLRQ RI RXU 3RLQW FODVV 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 // A Point object represents an ordered pair of (x, y) coordinates. // Third version: state and behavior with constructor. public class Point { int x; int y; // Constructs a new Point at the given initial x/y position. public Point(int initialX, int initialY) { x = initialX; y = initialY; } // Returns the direct distance between this Point and the origin (0, 0). public double distanceFromOrigin() { return Math.sqrt(x * x + y * y); } // Shifts the position of this Point object by the given amount // in each direction. public void translate(int dx, int dy) { x += dx; y += dy; } } 7KH JHQHUDO V\QWD[ IRU FRQVWUXFWRUV LV WKH IROORZLQJ public <class name>(<type> <name>, ..., <type> <name>) { <statement>; <statement>; ... <statement>; } 440 $IWHU DGGLQJ WKH FRQVWUXFWRU RXU FOLHQW FRGH EHFRPHV VKRUWHU DQG VLPSOHU EHFDXVH LW FDQ FUHDWH D 3RLQW DQG LQLWLDOL]H LWV [ \ FRRUGLQDWHV LQ D VLQJOH OLQH 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 // A program that deals with 2D points. // Third version, to accompany Point class with constructor. public class PointMain { public static void main(String args) { // create two Point objects Point p1 = new Point(7, 2); // calling the constructor Point p2 = new Point(4, 3); // calling the constructor // print each point and its distance from origin System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); System.out.println("distance from origin = " + p1.distanceFromOrigin()); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); System.out.println("distance from origin = " + p2.distanceFromOrigin()); // translate each point to a new location p1.translate(11, 6); p2.translate(1, 7); // print the points again System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); } } :KHQ D FODVV GRHVQ W KDYH D FRQVWUXFWRU OLNH RXU 3RLQW FODVV EHIRUH WKLV VHFWLRQ -DYD DXWRPDWLFDOO\ VXSSOLHV DQ HPSW\ GHIDXOW FRQVWUXFWRU WKDW DFFHSWV QR SDUDPHWHUV 7KDW LV ZK\ LW ZDV SUHYLRXVO\ OHJDO WR FRQVWUXFW D new Point() +RZHYHU ZKHQ ZH ZULWH D FRQVWUXFWRU RI RXU RZQ -DYD GRHVQ W VXSSO\ WKH GHIDXOW HPSW\ FRQVWUXFWRU 6R LW ZRXOG QRZ EH LOOHJDO WR FRQVWUXFW 3RLQW REMHFWV ZLWKRXW SDVVLQJ LQ WKH LQLWLDO [ DQG \ SDUDPHWHUV Point p1 = new Point(); // this will not compile, for now ,Q D ODWHU VHFWLRQ RI WKLV FKDSWHU ZH OO ZULWH DGGLWLRQDO FRGH WR UHVWRUH WKLV DELOLW\ &RPPRQ 3URJUDPPLQJ (UURU :ULWLQJ YRLG RQ &RQVWUXFWRU 0DQ\ QHZ SURJUDPPHUV DFFLGHQWDOO\ ZULWH WKH NH\ZRUG void RQ WKHLU FRQVWUXFWRU VLQFH WKH\ YH JRWWHQ VR XVHG WR ZULWLQJ D UHWXUQ W\SH RQ HYHU\ PHWKRG // This code has a bug. public void Point(int initialX, int initialY) { x = initialX; y = initialY; } 7KLV LV DFWXDOO\ D YHU\ WULFN\ DQG DQQR\LQJ EXJ &RQVWUXFWRUV DUHQ W VXSSRVHG WR KDYH DQ\ UHWXUQ W\SH ZULWWHQ RQ WKHLU KHDGHUV :KHQ \RX GR ZULWH D UHWXUQ W\SH VXFK DV YRLG ZKDW \RX YH FUHDWHG LV QRW D FRQVWUXFWRU EXW UDWKHU D QRUPDO LQVWDQFH PHWKRG ZKRVH QDPH LV 3RLQW ZKLFK DFFHSWV [ DQG \ SDUDPHWHUV DQG UHWXUQV YRLG 7KLV LV WRXJK WR FDWFK EHFDXVH WKH 3RLQW MDYD ILOH DOORZV WKLV DQG VWLOO FRPSLOHV VXFFHVVIXOO\ 441 7KH SODFH ZKHUH \RX ZLOO VHH DQ HUURU LV ZKHQ \RX WU\ WR FDOO \RXU FRQVWUXFWRU \RX WKRXJKW \RX MXVW ZURWH EXW DFWXDOO\ LVQ W D FRQVWUXFWRU 7KH FOLHQW FRGH WKDW WULHV WR FRQVWUXFW WKH 3RLQW REMHFW ZLOO FRPSODLQ WKDW LW FDQ W ILQG DQ LQW LQW FRQVWUXFWRU IRU D 3RLQW PointMain.java:7: cannot find symbol symbol : constructor Point(int,int) location: class Point Point p1 = new Point(7, 2); ,I \RX VHH FDQQRW ILQG V\PERO FRQVWUXFWRU HUURUV DQG \RX ZHUH SRVLWLYH WKDW \RX ZURWH D FRQVWUXFWRU GRXEOH FKHFN LWV KHDGHU WR PDNH VXUH WKHUH V QR UHWXUQ W\SH &RPPRQ 3URJUDPPLQJ (UURU 5HGHFODULQJ 'DWD )LHOGV LQ &RQVWUXFWRU $QRWKHU FRPPRQ EXJ ZLWK FRQVWUXFWRUV LV WR PLVWDNHQO\ UHGHFODUH GDWD ILHOGV E\ ZULWLQJ WKHLU W\SHV +HUH V DQ H[DPSOH WKDW VKRZV WKH PLVWDNH // This constructor code has a bug. public Point(int initialX, int initialY) { int x = initialX; int y = initialY; } 7KH SUHFHGLQJ FRGH KDV D YHU\ EL]DUUH EHKDYLRU ,W FRPSLOHV VXFFHVVIXOO\ EXW ZKHQ WKH FOLHQW FRGH FRQVWUXFWV D 3RLQW REMHFW LWV LQLWLDO FRRUGLQDWHV DUH DOZD\V UHJDUGOHVV RI ZKDW SDUDPHWHU YDOXHV DUH SDVVHG WR WKH FRQVWUXFWRU // This client code will print that p1 is (0, 0) ! Point p1 = new Point(7, 2); System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); 7KH SUREOHP LV WKDW UDWKHU WKDQ VWRULQJ LQLWLDO; DQG LQLWLDO< LQWR WKH 3RLQW V GDWD ILHOGV [ DQG \ ZH YH DFWXDOO\ GHFODUHG ORFDO YDULDEOHV QDPHG [ DQG \ LQVLGH WKH 3RLQW V FRQVWUXFWRU :H VWRUH LQLWLDO; DQG LQLWLDO< LQWR WKRVH ORFDO YDULDEOHV ZKLFK DUH WKURZQ DZD\ ZKHQ WKH FRQVWUXFWRU ILQLVKHV UXQQLQJ 1R YDOXHV DUH HYHU DVVLJQHG WR WKH [ DQG \ GDWD ILHOGV LQ WKH FRQVWUXFWRU VR WKH\ UHFHLYH DXWRPDWLF LQLWLDOL]DWLRQ WR :H VD\ WKDW WKHVH ORFDO [ DQG \ YDULDEOHV VKDGRZ RXU [ DQG \ GDWD ILHOGV EHFDXVH WKH\ REVFXUH WKH ILHOGV ZH LQWHQGHG WR VHW ,I \RX REVHUYH WKDW \RXU FRQVWUXFWRU GRHVQ W VHHP WR EH VHWWLQJ \RXU REMHFW V GDWD ILHOGV FKHFN FORVHO\ WR PDNH VXUH WKDW \RX GLGQ W DFFLGHQWDOO\ GHFODUH ORFDO YDULDEOHV WKDW VKDGRZ \RXU GDWD ILHOGV 7KH NH\ WKLQJ WR ORRN IRU LV WKDW \RX GLGQ W UHZULWH \RXU ILHOGV W\SHV ZKHQ WU\LQJ WR DVVLJQ WKHP LQ WKH FRQVWUXFWRU (QFDSVXODWLRQ 2XU IRXUWK YHUVLRQ RI WKH 3RLQW FODVV ZLOO WDNH DGYDQWDJH RI D FRQFHSW NQRZQ DV HQFDSVXODWLRQ 7R XQGHUVWDQG WKH QRWLRQ RI HQFDSVXODWLRQ UHFDOO WKH QRQ SURJUDPPLQJ DQDORJ\ RI UDGLRV DV REMHFWV $OPRVW HYHU\RQH NQRZV KRZ WR XVH D UDGLR EXW IHZ NQRZ KRZ WR EXLOG D UDGLR RU KRZ WKH FLUFXLWU\ LQVLGH D UDGLR ZRUNV ,W LV D EHQHILW RI WKH UDGLR V GHVLJQ WKDW ZH GRQ W QHHG WR NQRZ WKRVH NLQGV RI GHWDLOV 442 :H DOO XQGHUVWDQG WKH HVVHQWLDO SURSHUWLHV RI D UDGLR ZKDW LW GRHV EXW RQO\ D IHZ RI XV XQGHUVWDQG WKH LQWHUQDO GHWDLOV RI WKH UDGLR KRZ LW ZRUNV 7KLV OHDGV WR DQ LPSRUWDQW GLFKRWRP\ RI DQ H[WHUQDO YLHZ RI DQ REMHFW YHUVXV DQ LQWHUQDO YLHZ RI DQ REMHFW )URP WKH RXWVLGH ZH MXVW VHH EHKDYLRU )URP WKH LQVLGH ZH VHH LQWHUQDO VWDWH WKDW LV XVHG WR DFFRPSOLVK WKDW EHKDYLRU 7KH LQWHUQDO DQG H[WHUQDO YLHZV RI D UDGLR UDGLR LQWHUQDO YLHZ UDGLR H[WHUQDO YLHZ %\ IRFXVLQJ RQ WKH UDGLR V H[WHUQDO EHKDYLRU ZH FDQ XVH LW HDVLO\ ZKLOH LJQRULQJ LWV GHWDLOV WKDW DUH XQLPSRUWDQW WR XV 7KLV LV DQ H[DPSOH RI DQ LPSRUWDQW FRPSXWHU VFLHQFH FRQFHSW NQRZQ DV DEVWUDFWLRQ $EVWUDFWLRQ )RFXVLQJ RQ DQ LWHP V HVVHQWLDO SURSHUWLHV ZKLOH LJQRULQJ LWV LQQHU GHWDLOV ,Q IDFW UDGLRV DQG RWKHU HOHFWURQLF GHYLFHV KDYH D FDVH RU FKDVVLV WKDW KRXVHV DOO RI WKH HOHFWURQLFV VR WKDW ZH GRQ W HYHQ VHH WKHP IURP WKH RXWVLGH ,QVWHDG ZH KDYH D VHW RI GLDOV DQG EXWWRQV DQG GLVSOD\V WKDW DOORZ XV WR PDQLSXODWH WKH UDGLR ZLWKRXW KDYLQJ WR GHDO ZLWK DOO RI WKH FLUFXLWU\ WKDW PDNHV LW ZRUN ,Q IDFW \RX ZRXOGQ W ZDQW VRPHRQH WR JLYH \RX D IXOO\ IXQFWLRQDO UDGLR WKDW KDG ZLUHV DQG FDSDFLWRUV KDQJLQJ RXW RI LW EHFDXVH WKRVH GHWDLOV PDNH WKH LQWHUIDFH WR WKH UDGLR XQFOHDQ DQG OHVV SOHDVDQW WR XVH :KHQ DSSOLHG WR SURJUDPPLQJ KLGLQJ LQWHUQDO VWDWH IURP RXWVLGH YLHZ LV FDOOHG HQFDSVXODWLRQ (QFDSVXODWLRQ +LGLQJ WKH LPSOHPHQWDWLRQ GHWDLOV RI DQ REMHFW IURP WKH FOLHQWV RI WKH REMHFW :KHQ REMHFWV DUH SURSHUO\ HQFDSVXODWHG WKH FOLHQWV RI DQ REMHFW FDQQRW GLUHFWO\ DFFHVV RU PRGLI\ WKH LQWHUQDO ZRUNLQJV RI WKH REMHFW QRU GR WKH\ QHHG WR GR VR 2QO\ WKH LPSOHPHQWRU RI WKH FODVV QHHGV WR NQRZ DERXW WKRVH GHWDLOV ,Q SUHYLRXV FKDSWHUV \RX KDYH DOUHDG\ WDNHQ DGYDQWDJH RI WKH DEVWUDFWLRQ SURYLGHG E\ ZHOO HQFDSVXODWHG REMHFWV <RX KDYH XVHG 6FDQQHU REMHFWV WR UHDG GDWD IURP WKH FRQVROH ZLWKRXW NQRZLQJ H[DFWO\ KRZ WKH 6FDQQHU VWRUHG DQG WRNHQL]HG WKH LQSXW GDWD <RX KDYH XVHG 5DQGRP REMHFWV WR FUHDWH UDQGRP QXPEHUV ZLWKRXW NQRZLQJ H[DFWO\ ZKDW DOJRULWKP WKH UDQGRP QXPEHU JHQHUDWRU XVHG 443 %XW VR IDU RXU 3RLQW FODVV LV QRW HQFDSVXODWHG :H YH EXLOW D ZRUNLQJ UDGLR EXW LWV ZLUHV LWV [ DQG \ GDWD ILHOGV DUH VWLOO KDQJLQJ RXW 8VLQJ HQFDSVXODWLRQ ZH OO SXW D FDVLQJ DURXQG RXU 3RLQW REMHFWV VR WKDW FOLHQWV ZLOO RQO\ QHHG WR XVH D 3RLQW V PHWKRGV DQG QRW DFFHVV WKH ILHOGV GLUHFWO\ 3ULYDWH 'DWD )LHOGV 7R HQFDSVXODWH GDWD ILHOGV RI DQ REMHFW ZH GHFODUH WKHP WR EH SULYDWH E\ ZULWLQJ WKH NH\ZRUG private DW WKH VWDUW RI WKH GHFODUDWLRQ RI HDFK ILHOG 7KH ILHOGV RI RXU 3RLQW W\SH ZRXOG EH GHFODUHG DV IROORZV // encapsulated data fields of Point objects private int x; private int y; :H KDYHQ W VKRZQ D V\QWD[ WHPSODWH \HW IRU GDWD ILHOGV EHFDXVH ZH ZDQWHG WR VKRZ LW WKH SUHIHUUHG ZD\ ZLWK WKH ILHOGV HQFDSVXODWHG DQG SULYDWH 7KH SUHIHUUHG V\QWD[ IRU GHFODULQJ GDWD ILHOGV LV WKH IROORZLQJ private <type> <name>; )LHOGV FDQ DOVR EH GHFODUHG ZLWK DQ LQLWLDO YDOXH VXFK DV private <type> <name> = <value>; 'HFODULQJ ILHOGV SULYDWH HQFDSVXODWHV WKH VWDWH RI WKH REMHFW LQ WKH VDPH ZD\ WKDW D UDGLR V FDVLQJ SURWHFWV WKH XVHU IURP VHHLQJ WKH ZLUHV DQG FLUFXLWU\ LQVLGH WKH UDGLR 3ULYDWH ILHOGV DUH YLVLEOH WR DOO RI WKH FRGH LQVLGH WKH 3RLQW FODVV LQVLGH WKH 3RLQW MDYD ILOH EXW QRW DQ\ZKHUH HOVH 7KLV PHDQV WKDW ZH FDQ QR ORQJHU GLUHFWO\ UHIHU WR D 3RLQW V [ RU \ GDWD ILHOGV LQ RXU FOLHQW FRGH 7KH IROORZLQJ FOLHQW FRGH ZRXOG QRW FRPSLOH VXFFHVVIXOO\ // This client code doesn't work with encapsulated points System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); 7KH FRPSLOHU SURGXFHV HUURU PHVVDJHV VXFK DV WKH IROORZLQJ PointMain.java:11: x has private access in Point PointMain.java:11: y has private access in Point 7R SUHVHUYH WKH IXQFWLRQDOLW\ RI RXU FOLHQW SURJUDP ZH QHHG WR SURYLGH D ZD\ IRU FOLHQW FRGH WR DFFHVV D 3RLQW V GDWD ILHOG YDOXHV :H ZLOO GR WKLV E\ DGGLQJ VRPH QHZ DFFHVVRU PHWKRGV WR WKH 3RLQW FODVV ,I WKH YDOXH RI DQ REMHFW V GDWD ILHOG PLJKW EH XVHIXO H[WHUQDOO\ LW LV FRPPRQ WR ZULWH DQ DFFHVVRU WR UHWXUQ WKDW YDOXH +HUH DUH DFFHVVRUV WKDW SURYLGH DFFHVV WR D 3RLQW V [ DQG \ ILHOGV // Returns the x-coordinate of this Point. public int getX() { return x; } // Returns the y-coordinate of this Point. public int getY() { return y; } 444 7KH FOLHQW FRGH WKDW ZLVKHV WR SULQW D 3RLQW V [ DQG \ YDOXHV PXVW EH FKDQJHG WR WKH IROORZLQJ // This code works with our encapsulated Points System.out.println("p1 is (" + p1.getX() + ", " + p1.getY() + ")"); ,W SUREDEO\ VHHPV RGG WKDW ZH MXVW JUDQWHG WKH FOLHQW DFFHVV WR D 3RLQW V [ DQG \ GDWD ZKHQ ZH MXVW ILQLVKHG HQFDSVXODWLQJ WKRVH VDPH GDWD ILHOGV WR UHVWULFW DFFHVV WR WKHP %XW KDYLQJ DFFHVVRUV OLNH JHW; DQG JHW< GRHVQ W EUHDN WKH HQFDSVXODWLRQ RI WKH REMHFW 7KH DFFHVVRU PHWKRGV DFWXDOO\ MXVW UHWXUQ D FRS\ RI WKH ILHOG YDOXHV WR WKH FOLHQW ZKLFK OHWV WKH FOLHQW VHH WKH [ RU \ YDOXH EXW GRHVQ W JLYH WKH FOLHQW DQ\ ZD\ WR FKDQJH LW ,Q RWKHU ZRUGV WKHVH DFFHVVRU PHWKRGV JLYH WKH FOLHQW UHDG RQO\ DFFHVV WR WKH VWDWH RI WKH REMHFW $QRWKHU EHQHILW RI HQFDSVXODWLQJ RXU 3RLQW REMHFWV LV WKDW ZH FRXOG ODWHU FKDQJH WKH LQWHUQDO VWUXFWXUH RI RXU 3RLQW FODVV ZLWKRXW KDYLQJ WR PRGLI\ RXU FOLHQW FRGH )RU H[DPSOH VRPHWLPHV LW LV XVHIXO WR H[SUHVV ' SRLQWV LQ SRODU FRRUGLQDWHV XVLQJ D UDGLXV U DQG DQ DQJOH WKHWD ,Q WKLV UHSUHVHQWDWLRQ D 3RLQW V [ \ FRRUGLQDWHV DUH QRW VWRUHG GLUHFWO\ EXW FDQ EH FRPSXWHG DV U FRV WKHWD U VLQ WKHWD 1RZ WKDW RXU 3RLQW FODVV LV HQFDSVXODWHG ZH FRXOG PRGLI\ RXU HQFDSVXODWHG 3RLQW WR XVH U DQG WKHWD GDWD ILHOGV LQWHUQDOO\ WKHQ PRGLI\ WKH JHW; DQG JHW< PHWKRGV WR FRPSXWH DQG UHWXUQ WKH DSSURSULDWH YDOXHV 1RZ WKDW RXU 3RLQW FODVV LV HQFDSVXODWHG RQH GUDZEDFN LV WKDW LW V QR ORQJHU HDV\ IRU WKH FOLHQW FRGH WR VHW D 3RLQW WR D QHZ ORFDWLRQ )RU FRQYHQLHQFH ZH OO DGG D QHZ PXWDWRU WR RXU HQFDSVXODWHG 3RLQW FODVV FDOOHG VHW/RFDWLRQ WKDW VHWV ERWK WKH [ DQG \ GDWD ILHOGV RQ WKH REMHFW WR QHZ YDOXHV SDVVHG DV SDUDPHWHUV // Sets this Point's x/y coordinates to the given values. public void setLocation(int newX, int newY) { x = newX; y = newY; } 1RWLFH WKDW WKH 3RLQW FODVV QRZ KDV VRPH UHGXQGDQF\ EHWZHHQ LWV FRQVWUXFWRU DQG LWV VHW/RFDWLRQ PHWKRG 7KH WZR ERGLHV DUH HVVHQWLDOO\ WKH VDPH VHWWLQJ WKH 3RLQW WR KDYH QHZ [ DQG \ FRRUGLQDWHV :H FDQ HOLPLQDWH WKLV UHGXQGDQF\ E\ KDYLQJ WKH FRQVWUXFWRU FDOO VHW/RFDWLRQ UDWKHU WKDQ VHWWLQJ WKH ILHOG YDOXHV PDQXDOO\ ,W LV OHJDO IRU DQ REMHFW WR FDOO LWV RZQ LQVWDQFH PHWKRGV IURP LWV FRQVWUXFWRU RU RWKHU LQVWDQFH PHWKRGV 7KLV LV GRQH E\ ZULWLQJ WKH QDPH RI WKH PHWKRG \RX ZDQW WR FDOO IROORZHG E\ DQ\ SDUDPHWHUV LQ SDUHQWKHVHV :H GRQ W KDYH WR VSHFLI\ ZKLFK REMHFW WR FDOO WKH LQVWDQFH PHWKRG XSRQ EHFDXVH LW V LPSOLFLW WKDW WKH REMHFW ZDQWV WR FDOO WKH PHWKRG RQ LWVHOI // Constructs a new Point at the given initial x/y position. public Point(int initialX, int initialY) { setLocation(initialX, initialY); } 7KHUH V D ELW PRUH UHGXQGDQF\ ZH FDQ HOLPLQDWH XVLQJ WKLV WHFKQLTXH 7UDQVODWLQJ D 3RLQW FDQ EH WKRXJKW RI DV VHWWLQJ LWV ORFDWLRQ WR WKH ROG ORFDWLRQ SOXV WKH G[ DQG G\ VR ZH FDQ PRGLI\ WKH WUDQVODWH PHWKRG WR FDOO WKH VHW/RFDWLRQ PHWKRG 445 // Shifts the position of this Point object by the given amount // in each direction. public void translate(int dx, int dy) { setLocation(x + dx, y + dy); } 1RZ WKDW ZH YH VHHQ DOO WKH PDMRU HOHPHQWV RI D ZHOO HQFDSVXODWHG FODVV LW V WLPH WR ORRN DW D SURSHU V\QWD[ WHPSODWH IRU DQ HQWLUH FODVV -DYD V VW\OH JXLGHOLQHV VXJJHVW SXWWLQJ GDWD ILHOGV DW WKH WRS RI WKH FODVV IROORZHG E\ FRQVWUXFWRUV IROORZHG E\ PHWKRGV public class <class name> { // data fields private <type> <name>; private <type> <name>; ... // constructors public <class name>(<type> <name>, ..., <type> <name>) { <statement(s)>; } ... // methods public <type> <name>(<type> <name>, ..., <type> <name>) { <statement(s)>; } ... } &ODVV ,QYDULDQWV $QRWKHU EHQHILW RI HQFDSVXODWHG REMHFWV LV WKDW ZH FDQ QRZ SODFH FRQVWUDLQWV RQ WKH VWDWH RI DQ REMHFW LI ZH ZLVK WR GR VR )RU H[DPSOH VXSSRVH ZH ZDQW WR HQVXUH WKDW HYHU\ 3RLQW V [ DQG \ ILHOG YDOXHV DUH QRQ QHJDWLYH 7KLV PLJKW EH XVHIXO LI 3RLQWV ZHUH EHLQJ XVHG WR UHSUHVHQW FRRUGLQDWHV RQ WKH VFUHHQ ZKLFK DUH QHYHU QHJDWLYH 7KH FRQFHSW RI D VWDWHPHQW WKDW LV DOZD\V WUXH DERXW DQ REMHFW LV VRPHWLPHV FDOOHG D FODVV LQYDULDQW $ FODVV LQYDULDQW LV UHODWHG WR WKH LGHD RI SUHFRQGLWLRQV DQG SRVWFRQGLWLRQV SUHVHQWHG LQ &KDSWHU &ODVV ,QYDULDQW $ ORJLFDO VWDWHPHQW DERXW DQ REMHFW V VWDWH WKDW LV DOZD\V WUXH IRU WKH OLIHWLPH RI WKDW REMHFW 7R HQIRUFH RXU FODVV LQYDULDQW ZH PXVW HQVXUH WKDW QR 3RLQW LV FUHDWHG ZLWK D QHJDWLYH [ RU \ YDOXH DQG DOVR WKDW QR 3RLQW FDQ EH PRYHG RQWR D QHJDWLYH [ \ YDOXH 7KLV PHDQV WKDW WKH [ \ FRRUGLQDWHV PXVW EH FKHFNHG HYHU\ WLPH WKH 3RLQW V FRQVWUXFWRU WUDQVODWH PHWKRG RU VHW/RFDWLRQ PHWKRG LV FDOOHG 6LQFH ZH UHPRYHG UHGXQGDQF\ E\ KDYLQJ WKH FRQVWUXFWRU DQG WUDQVODWH PHWKRG FDOO VHW/RFDWLRQ ZH FDQ DFWXDOO\ HQIRUFH WKH LQYDULDQW ZLWK D VLQJOH FKHFN LQ WKH VHW/RFDWLRQ PHWKRG ,I WKH QHZ [ RU \ YDOXH LV QHJDWLYH ZH OO WKURZ DQ ,OOHJDO$UJXPHQW([FHSWLRQ WR QRWLI\ WKH FOLHQW RI WKHLU HUURU 446 // Sets this Point's x/y coordinates to the given values. public void setLocation(int newX, int newY) { if (newX < 0 || newY < 0) { throw new IllegalArgumentException(); } x = newX; y = newY; } ,I WKH 3RLQW FODVV ZHUHQ W HQFDSVXODWHG ZH ZRXOGQ W EH DEOH WR SURSHUO\ HQIRUFH RXU LQYDULDQW &OLHQW FRGH ZRXOG EH DEOH WR PDNH D 3RLQW V ORFDWLRQ QHJDWLYH E\ VHWWLQJ LWV [ RU \ GDWD ILHOGV YDOXHV GLUHFWO\ )RU H[DPSOH WKLV SLHFH RI PDOLFLRXV FOLHQW FRGH ZRXOG VXFFHHG LQ VHWWLQJ WKH 3RLQW V [ YDOXH WR // If our Point objects were not encapsulated, // this client code would break the class invariant. Point p1 = new Point(7, 2); p1.x = -1; System.out.println("Haha, I set its x value to " + p1.x); :LWK HQFDSVXODWLRQ WKH 3RLQW FODVV KDV PXFK EHWWHU FRQWURO RYHU KRZ LW V XVHG E\ LWV FOLHQWV VR LW V QRW SRVVLEOH IRU D PLVJXLGHG FOLHQW SURJUDP WR YLRODWH RXU FODVV LQYDULDQW +HUH LV WKH IRXUWK FRPSOHWH YHUVLRQ RXU 3RLQW FODVV QRZ LQFOXGLQJ HQFDSVXODWLRQ DQG RXU QRQ QHJDWLYLW\ FODVV LQYDULDQW &KDQQHOLQJ DOO FRGH WKDW VHWV WKH 3RLQW V SRVLWLRQ WKURXJK WKH VHW/RFDWLRQ PHWKRG SURYHV XVHIXO EHFDXVH ZH RQO\ KDYH WR FKHFN IRU WKH [ DQG \ EHLQJ OHVV WKDQ RQFH 447 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 // A Point object represents an ordered pair of (x, y) coordinates. // Fourth version: encapsulated, with a class invariant that x, y >= 0. public class Point { private int x; private int y; // Constructs a new Point at the given initial x/y position. public Point(int initialX, int initialY) { setLocation(initialX, initialY); } // Returns the direct distance between this Point and the origin (0, 0). public double distanceFromOrigin() { return Math.sqrt(x * x + y * y); } // Returns the x-coordinate of this Point. public int getX() { return x; } // Returns the y-coordinate of this Point. public int getY() { return y; } // Sets this Point's x/y coordinates to the given values. public void setLocation(int newX, int newY) { if (newX < 0 || newY < 0) { throw new IllegalArgumentException(); } x = newX; y = newY; } // Shifts the position of this Point object by the given amount // in each direction. public void translate(int dx, int dy) { setLocation(x + dx, y + dy); } } +HUH V WKH FRUUHVSRQGLQJ IRXUWK YHUVLRQ RI RXU FOLHQW SURJUDP 7KH FOLHQW LV DFWXDOO\ D ELW OHVV FOHDQ QRZ EHFDXVH LW PXVW QRZ FDOO WKH JHW; DQG JHW< PHWKRGV RQ D 3RLQW WR SULQW LWV FRRUGLQDWHV +RZHYHU WKLV PRGHUDWH LQFRQYHQLHQFH WR WKH FOLHQW FRGH LV QHFHVVDU\ WR SUHVHUYH WKH HQFDSVXODWLRQ DQG FODVV LQYDULDQW RQ RXU 3RLQW REMHFWV 448 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 // A program that deals with 2D points. // Fourth version, to accompany encapsulated Point class. public class PointMain { public static void main(String args) { // create two Point objects Point p1 = new Point(7, 2); Point p2 = new Point(4, 3); // print each point and its distance from origin System.out.println("p1 is (" + p1.getX() + ", " + p1.getY() + ")"); System.out.println("distance from origin = " + p1.distanceFromOrigin()); System.out.println("p2 is (" + p2.getX() + ", " + p2.getY() + ")"); System.out.println("distance from origin = " + p2.distanceFromOrigin()); // translate each point to a new location p1.translate(11, 6); p2.translate(1, 7); // print the points again System.out.println("p1 is (" + p1.getX() + ", " + p1.getY() + ")"); System.out.println("p2 is (" + p2.getX() + ", " + p2.getY() + ")"); } } 0RUH ,QVWDQFH 0HWKRGV ,Q WKLV VHFWLRQ ZH OO DGG VRPH VSHFLDO LQVWDQFH PHWKRGV WR RXU 3RLQW WR PDNH LW HDVLHU DQG PRUH FRQYHQLHQW WR XVH LQ FOLHQW FRGH :H OO ZULWH D PHWKRG WR PDNH LW HDV\ WR SULQW 3RLQW REMHFWV DQG DQRWKHU WR FRPSDUH WKHP WR HDFK RWKHU IRU HTXDOLW\ 7KH WR6WULQJ 0HWKRG -DYD REMHFWV NQRZ KRZ WR SULQW WKHPVHOYHV EHFDXVH WKH\ FRQWDLQ D VSHFLDO PHWKRG QDPHG WR6WULQJ 7KH WR6WULQJ LQVWDQFH PHWKRG UHWXUQV D 6WULQJ UHSUHVHQWDWLRQ RI WKH REMHFW -DYD V 3RLQW W\SH KDG D WR6WULQJ PHWKRG WKDW ZH XVHG WR ZULWH FRGH OLNH WKH IROORZLQJ // using Java's Point type Point p1 = new Point(7, 2); System.out.println("p1 is " + p1.toString()); 5HFDOO WKDW ZULWLQJ .toString() ZDV RSWLRQDO LQ PDQ\ FDVHV DQG WKH DERYH SULQWOQ VWDWHPHQW FRXOG KDYH EHHQ ZULWWHQ DV System.out.println("p1 is " + p); (LWKHU YHUVLRQ RI WKH SUHYLRXV FRGH SURGXFHV WKH RXWSXW ZKHQ UXQ ZLWK -DYD V 3RLQW REMHFWV p is java.awt.Point[x=7,y=2] 449 2XU RZQ 3RLQW W\SH GRHVQ W KDYH WKLV IXQFWLRQDOLW\ :KHQ ZH WU\ WR UXQ WKH VDPH FRGH ZLWK RXU RZQ 3RLQW FODVV ZH JHW D VWUDQJH UHVXOW 7KH FRGH GRHV FRPSLOHV DQG UXQ GHVSLWH WKH IDFW WKDW ZH KDYHQ W GHILQHG DQ\ WR6WULQJ PHWKRG IRU RXU 3RLQW W\SH 7KH RXWSXW WKH WR6WULQJ FDOO SURGXFHV RQ RXU 3RLQW REMHFWV LV EL]DUUH +HUH V D SRVVLEOH RXWSXW RI WKH VDPH FRGH VKRZQ SUHYLRXVO\ ZKHQ UXQ ZLWK RXU RZQ 3RLQW FODVV p = Point@119c082 7KH UHDVRQ WKH FRGH FRPSLOHV GHVSLWH RXU QRW KDYLQJ ZULWWHQ DQ\ WR6WULQJ PHWKRG LV WKDW WKHUH DUH FHUWDLQ PHWKRGV HYHU\ -DYD REMHFW FRQWDLQV E\ GHIDXOW 7KH WR6WULQJ PHWKRG LV RQH RI WKHVH WZR RWKHUV DUH QDPHG HTXDOV DQG JHW&ODVV :KHQ GHILQLQJ D QHZ W\SH RI REMHFWV -DYD JLYHV \RXU REMHFWV D GHIDXOW YHUVLRQ RI HDFK RI WKHVH PHWKRGV DQG D IHZ RWKHUV ,I \RX OLNH \RX FDQ UHSODFH WKH GHIDXOW YHUVLRQ ZLWK RQH RI \RXU RZQ 7KLV LV DFWXDOO\ DQ H[DPSOH RI D FRQFHSW FDOOHG LQKHULWDQFH ZKLFK ZH OO GLVFXVV LQ WKH QH[W FKDSWHU ,Q WKH FDVH RI WR6WULQJ WKH GHIDXOW YHUVLRQ RI WKH PHWKRG SULQWV WKH W\SH RI WKH REMHFW DQ # VLJQ DQG D KH[DGHFLPDO EDVH QXPEHU 7KLV LVQ W D YHU\ XVHIXO PHVVDJH WR UHSUHVHQW D 3RLQW REMHFW VR ZH OO ZULWH RXU RZQ WR6WULQJ PHWKRG LQ WKH 3RLQW FODVV WKDW UHWXUQV D 6WULQJ UHSUHVHQWLQJ WKH VWDWH RI D 3RLQW REMHFW 7KH WR6WULQJ PHWKRG LV DQ DFFHVVRU WKDW UHTXLUHV QR SDUDPHWHUV DQG KDV D UHWXUQ W\SH RI 6WULQJ // Returns a String representation of this Point object. public String toString() { return "(" + x + ", " + y + ")"; } :LWK RXU WR6WULQJ PHWKRG WKH VDPH FRGH VDPSOH VKRZQ SUHYLRXVO\ QRZ SURGXFHV WKH IROORZLQJ RXWSXW :H GLGQ W ZULWH RXU WR6WULQJ RXWSXW WR PDWFK WKDW RI -DYD V 3RLQW W\SH LQVWHDG RSWLQJ IRU D VKRUWHU DQG PRUH UHDGDEOH YHUVLRQ p = (7, 2) 6XQ V -DYD JXLGHOLQHV UHFRPPHQG ZULWLQJ D WR6WULQJ PHWKRG LQ HYHU\ FODVV \RX ZULWH 7KH HTXDOV 0HWKRG (YHU\ -DYD REMHFW FRQWDLQV D VSHFLDO PHWKRG QDPHG HTXDOV WKDW LW XVHV WR FRPSDUH LWVHOI WR RWKHU REMHFWV UHWXUQLQJ WUXH LI WKH REMHFWV KDYH WKH VDPH VWDWH DQG IDOVH RWKHUZLVH :H VDZ LQ SUHYLRXV FKDSWHUV WKDW WKH RSHUDWRU GRHV QRW EHKDYH SURSHUO\ ZKHQ XVHG RQ REMHFWV DQG WKDW WKH HTXDOV PHWKRG VKRXOG EH XVHG LQVWHDG IRU REMHFW FRPSDULVRQV &RQVLGHU WKH IROORZLQJ WZR 3RLQW REMHFWV UHIHUHQFHG E\ WKUHH YDULDEOHV 7KH WZR REMHFWV DUH GHFODUHG ZLWK WKH VDPH VWDWH Point p1 = new Point(7, 2); Point p2 = new Point(7, 2); Point p3 = p2; // using Java's Point type 7KH IROORZLQJ GLDJUDP UHSUHVHQWV WKH VWDWH RI WKHVH REMHFWV DQG WKH YDULDEOHV WKDW UHIHU WR WKHP 1RWLFH WKDW S LV QRW D QHZ REMHFW EXW D VHFRQG UHIHUHQFH WR WKH VDPH REMHFW DV S 7KLV PHDQV WKDW D FKDQJH WR S VXFK DV D FDOO RI LWV WUDQVODWH PHWKRG ZRXOG DOVR EH UHIOHFWHG LQ S 450 +--------------------+ +---+ | +----+ +----+ | p1 | +-+--> | x | 7 | y | 2 | | +---+ | +----+ +----+ | +--------------------+ +--------------------+ +---+ | +----+ +----+ | p2 | +-+--> | x | 7 | y | 2 | | +---+ | +----+ +----+ | +--------------------+ ^ +---+ | p3 | --+--------------+ +---+ ,Q &KDSWHU ZH VDZ WKDW WKH RSHUDWRU SHUIRUPV D FRPSDULVRQ RI LGHQWLW\ ZKHQ XVHG RQ REMHFWV ,Q RWKHU ZRUGV DQ H[SUHVVLRQ RI WKH IRUP a == b HYDOXDWHV WR WUXH LI WKH WZR YDULDEOHV UHIHU WR WKH VDPH REMHFW DQG IDOVH RWKHUZLVH :LWK WKH SUHFHGLQJ 3RLQW REMHFWV WKH H[SUHVVLRQ p1 == p2 ZRXOG HYDOXDWH WR IDOVH EHFDXVH S DQG S DUH QRW SK\VLFDOO\ WKH VDPH REMHFW 7KH H[SUHVVLRQ p2 == p3 ZRXOG HYDOXDWH WR WUXH EHFDXVH S UHIHUV WR WKH VDPH REMHFW DV S 7KLV LV QRW XVXDOO\ WKH EHKDYLRU ZH ZDQW ZKHQ FRPSDULQJ REMHFWV RIWHQ ZH ZDQW WR NQRZ ZKHWKHU WZR GLIIHUHQW REMHFWV KDYH WKH VDPH LQWHUQDO VWDWH 7KH HTXDOV PHWKRG SHUIRUPV D FRPSDULVRQ RI WZR REMHFWV VWDWHV DQG UHWXUQV WUXH LI WKH VWDWHV DUH WKH VDPH :LWK WKH SUHFHGLQJ 3RLQW REMHFWV WKH H[SUHVVLRQV p1.equals(p2) p1.equals(p3) DQG p2.equals(p3) DOO HYDOXDWH WR WUXH EHFDXVH WKH 3RLQWV DOO KDYH WKH VDPH [ \ FRRUGLQDWHV ,I ZH XVHG RXU RZQ 3RLQW W\SH WR GHFODUH WKH VDPH WKUHH 3RLQW YDULDEOHV WKH HTXDOV PHWKRG ZRXOG QRW EHKDYH FRUUHFWO\ 7KH FRGH ZRXOG FRPSLOH VXFFHVVIXOO\ EXW WKH HTXDOV PHWKRG ZRXOG EHKDYH WKH VDPH ZD\ DV WKH RSHUDWRU LW ZRXOG FRPSDUH WKH UHIHUHQFHV DQG RQO\ HYDOXDWH WR WUXH LI WKH WZR YDULDEOHV UHIHUUHG WR WKH VDPH REMHFW 6R WKH H[SUHVVLRQ p2.equals(p3) ZRXOG EH WUXH EXW WKH H[SUHVVLRQ p1.equals(p2) ZRXOG EH IDOVH 7KH UHDVRQ WKH FRGH FRPSLOHV DW DOO LV EHFDXVH OLNH WR6WULQJ HTXDOV LV D PHWKRG WKDW HYHU\ -DYD REMHFW FRQWDLQV HYHQ LI WKDW REMHFW V FODVV GRHVQ W GHILQH DQ HTXDOV PHWKRG 7KH GHIDXOW EHKDYLRU RI HTXDOV LV WR EHKDYH WKH VDPH ZD\ DV WKH RSHUDWRU WKDW LV WR FRPSDUH WKH UHIHUHQFHV RI WKH WZR REMHFWV $V ZLWK WKH XJO\ GHIDXOW WR6WULQJ RXWSXW WKH GHIDXOW EHKDYLRU RIWHQ LVQ W ZKDW ZH ZDQW 7R FRUUHFW WKH EHKDYLRU RI HTXDOV ZH FDQ GHILQH DQ HTXDOV PHWKRG LQ RXU 3RLQW FODVV 7KH HTXDOV PHWKRG PXVW DFFHSW DQ 2EMHFW DV LWV SDUDPHWHU DQG UHWXUQ D ERROHDQ YDOXH $ 3RLQW V HTXDOV PHWKRG VKRXOG WHVW ZKHWKHU LWV RZQ VWDWH LV HTXDO WR WKDW RI WKH SDUDPHWHU REMHFW )RU RXU 3RLQW REMHFWV KDYLQJ WKH VDPH VWDWH PHDQV KDYLQJ WKH VDPH [ DQG \ GDWD ILHOG YDOXHV <RX PLJKW WKLQN WKDW WKH IROORZLQJ FRGH FRUUHFWO\ LPSOHPHQWV WKH HTXDOV PHWKRG EXW LW KDV D IODZ // A flawed implementation of an equals method. public boolean equals(Point other) { return x == other.x && y == other.y; } 451 7KH IODZ LQ WKH SUHFHGLQJ FRGH LV LQ WKH PHWKRG V KHDGHU :KHQ \RX ZULWH DQ HTXDOV PHWKRG \RX UH ZULWLQJ D PHWKRG WKDW FRPSDUHV WKLV 3RLQW WR DQ\ RWKHU REMHFW QRW MXVW RWKHU 3RLQW REMHFWV )RU H[DPSOH LW VKRXOG EH OHJDO WR FRPSDUH D 3RLQW DQG D 6WULQJ ZLWK DQ H[SUHVVLRQ VXFK DV p1.equals("hello") WKH HTXDOV PHWKRG VKRXOG UHWXUQ IDOVH LQ VXFK D FDVH EHFDXVH WKH SDUDPHWHU LVQ W D 3RLQW 7KH IL[ IRU WKH HTXDOV PHWKRG KHDGHU LV WR PDNH LW DFFHSW D SDUDPHWHU RI W\SH 2EMHFW 2EMHFW LV D FODVV LQ -DYD WKDW FDQ UHSUHVHQW DQ\ W\SH RI REMHFW %\ VD\LQJ WKDW RXU PHWKRG DFFHSWV DQ 2EMHFW DV D SDUDPHWHU ZH UH PDNLQJ LW OHJDO WR SDVV DQ\ -DYD REMHFW DV WKH SDUDPHWHU WR WKH PHWKRG :KHQ WKH PHWKRG LV FDOOHG ZH OO QHHG WR H[DPLQH WKH W\SH RI WKH REMHFW DQG UHWXUQ D IDOVH UHVXOW LI LW LVQ W D 3RLQW 7KH IROORZLQJ SVHXGRFRGH VKRZV WKH SDWWHUQ WR IROORZ public boolean equals(Object o) { if (o is not a Point object) { return false. } else { compare the x and y values. } } 7KH ILUVW WKLQJ RXU FRGH QHHGV WR GR LV WR H[FOXGH REMHFWV WKDW DUHQ W 3RLQWV 7KHUH LV DQ RSHUDWRU QDPHG LQVWDQFHRI WKDW WHVWV ZKHWKHU D YDULDEOH UHIHUV WR DQ REMHFW RI D JLYHQ W\SH 7KH EHVW ZD\ WR H[DPLQH WKH W\SH RI WKH 2EMHFW R LV WR XVH LQVWDQFHRI WR VHH ZKHWKHU R LV DQ LQVWDQFH RI WKH 3RLQW W\SH $Q LQVWDQFHRI WHVW LV D ELQDU\ H[SUHVVLRQ WKDW WDNHV WKH IROORZLQJ IRUP DQG HYDOXDWHV WR SURGXFH D ERROHDQ UHVXOW <expression> instanceof <type> 7KH LQVWDQFHRI RSHUDWRU LV VWUDQJH EHFDXVH LW LVQ W D PHWKRG DQG WKHUHIRUH GRHVQ W XVH SDUHQWKHVHV RU GRWV RU DQ\ RWKHU QRWDWLRQ ,W LV VHSDUDWHG IURP LWV RSHUDQGV E\ VSDFHV 7KH RSHUDQG RQ WKH OHIW VLGH LV JHQHUDOO\ WKH YDULDEOH DQG WKH W\SH RQ WKH ULJKW LV WKH FODVV \RX ZLVK WR WHVW DJDLQVW ,Q RXU FDVH WKH IROORZLQJ FRGH SHUIRUPV WKH LQVWDQFHRI WHVW 1RWLFH WKDW ZH KDYH WR SDUHQWKHVL]H WKH LQVWDQFHRI WHVW EHFDXVH ZH ZLVK WR QHJDWH LW ZH UH WU\LQJ WR H[FOXGH WKLQJV WKDW DUH QRW DQ LQVWDQFH RI WKH 3RLQW W\SH // reject non-Point objects if (!(o instanceof Point)) { return false; } $ QLFH VLGH EHQHILW RI WKH LQVWDQFHRI RSHUDWRU LV WKDW LW SURGXFHV D IDOVH UHVXOW ZKHQ R LV QXOO 7KLV ZLOO PDNH VXUH WKDW LI WKH FOLHQW FRGH FRQWDLQV DQ H[SUHVVLRQ VXFK DV p1.equals(null) LW ZLOO FRUUHFWO\ UHWXUQ IDOVH UDWKHU WKDQ WKURZLQJ D 1XOO3RLQWHU([FHSWLRQ 1RZ WKDW ZH YH FKHFNHG IRU QRQ 3RLQW REMHFWV ZH ZDQW WR KDQGOH WKH FDVH ZKHUH 2EMHFW R GRHV UHIHU WR D 3RLQW REMHFW <RX PLJKW WKLQN WKDW WKH IROORZLQJ FRGH ZRXOG FRUUHFWO\ FRPSDUH WKH WZR 3RLQW REMHFWV DQG UHWXUQ WKH SURSHU UHVXOW 8QIRUWXQDWHO\ LW GRHV QRW HYHQ FRPSLOH VXFFHVVIXOO\ return x == o.x && y == o.y; // This code does not compile 452 7KH SUHFHGLQJ FRGH SURGXFHV HUURUV VXFK DV WKH IROORZLQJ IRU HDFK RI R V GDWD ILHOGV ZH WU\ WR DFFHVV Point.java:36: cannot find symbol symbol : variable x location: class java.lang.Object 7KH -DYD FRPSLOHU GRHVQ W DOORZ XV WR ZULWH DQ H[SUHVVLRQ VXFK DV o.x EHFDXVH LW GRHVQ W NQRZ DKHDG RI WLPH ZKHWKHU R V REMHFW ZLOO KDYH D GDWD ILHOG QDPHG [ 7KLV LV WKH FDVH HYHQ WKRXJK RXU FRGH WHVWV WR PDNH VXUH WKDW R LV RI W\SH 3RLQW WKH FRPSLOHU VWLOO GRHVQ W WUXVW XV ,I ZH ZDQW WR WUHDW R DV D 3RLQW REMHFW ZH PXVW FDVW LW IURP W\SH 2EMHFW WR W\SH 3RLQW <RX KDYH XVHG W\SHFDVWLQJ WR FRQYHUW EHWZHHQ SULPLWLYH W\SHV VXFK DV FDVWLQJ GRXEOH WR LQW &DVWLQJ EHWZHHQ REMHFW W\SHV KDV D GLIIHUHQW PHDQLQJ $ FDVW RI DQ REMHFW LV D SURPLVH WR WKH FRPSLOHU 7KH FDVW LV \RXU DVVXUDQFH WKDW WKH UHIHUHQFH DFWXDOO\ UHIHUV WR D GLIIHUHQW W\SH DQG WKDW WKH FRPSLOHU FDQ WUHDW LW DV WKDW W\SH ,Q RXU FDVH ZH OO ZULWH D VWDWHPHQW WKDW FDVWV R LQWR D 3RLQW REMHFW VR WKH FRPSLOHU ZLOO WUXVW WKDW ZH FDQ DFFHVV LWV [ DQG \ GDWD ILHOGV +HUH V WKH FRUUHFW FRGH // cast into Point and compare Point other = (Point) o; return x == other.x && y == other.y; $V \RX FDQ VHH ZULWLQJ D FRUUHFW HTXDOV PHWKRG LV WULFN\ :H KDYH WR MXPS RYHU VHYHUDO KXUGOHV UHODWHG WR ZRUNLQJ ZLWK W\SHV RI REMHFWV DQG SODFDWLQJ WKH -DYD FRPSLOHU 7KH EHQHILW RI DOO WKLV LV WKDW QRZ RXU 3RLQW REMHFWV FDQ EH FRPSDUHG IRU HTXDOLW\ DJDLQVW DQ\ RWKHU REMHFW 3XWWLQJ LW DOO WRJHWKHU KHUH LV WKH ILQLVKHG FRGH IRU RXU HTXDOV PHWKRG // Returns whether o refers to a Point with the same x/y // coordinates as this Point. public boolean equals(Object o) { if (!(o instanceof Point)) { return false; } Point other = (Point) o; return x == other.x && y == other.y; } 7KLV FRGH DOVR LOOXVWUDWHV D VXEWOHW\ DERXW WKH SULYDWH NH\ZRUG 1RWLFH WKDW WKH ERG\ RI HTXDOV UHIHUV WR WKH RWKHU [ DQG RWKHU \ ILHOGV GLUHFWO\ 7KLV LV DFWXDOO\ OHJDO SULYDWH GDWD ILHOGV DUH YLVLEOH WR DOO REMHFWV RI WKDW FODVV VR RQH 3RLQW FDQ GLUHFWO\ DFFHVV WKH GDWD RI DQRWKHU 0DQ\ FODVVHV LPSOHPHQW HTXDOV PHWKRGV OLNH RXUV VR PXFK RI WKH SUHFHGLQJ HTXDOV FRGH FDQ EH UHXVHG DV ERLOHUSODWH FRGH +HUH V D WHPSODWH IRU D ZHOO IRUPHG HTXDOV PHWKRG 7KH LQVWDQFHRI WHVW DQG W\SHFDVW DUH OLNHO\ WKH ILUVW WZR WKLQJV \RX OO ZDQW WR GR LQ DQ\ HTXDOV PHWKRG \RX ZULWH 453 public boolean equals(Object o) { if ((!o instanceof <type>)) { return false; } <type> other = (<type>) o; <compare the data and return the result>; } +HUH LV WKH ILIWK YHUVLRQ RI RXU 3RLQW FODVV DIWHU DOO RI WKH FKDQJHV LQ WKLV VHFWLRQ LQFOXGLQJ WKH WR6WULQJ DQG HTXDOV PHWKRGV 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 // A Point object represents an ordered pair of (x, y) coordinates. // Fifth version: Advanced features (toString, equals, // and a class invariant that x, y >= 0). public class Point { private int x; private int y; // Constructs a new Point at the given initial x/y position. // pre: initialX >= 0 && initialY >= 0 public Point(int initialX, int initialY) { setLocation(initialX, initialY); } // Returns the direct distance between this Point and the origin (0, 0). public double distanceFromOrigin() { return Math.sqrt(x * x + y * y); } // Returns the x-coordinate of this Point. public int getX() { return x; } // Returns the y-coordinate of this Point. public int getY() { return y; } // Returns whether o refers to a Point with the same x/y // coordinates as this Point. public boolean equals(Object o) { if (!(o instanceof Point)) { return false; } Point other = (Point) o; return x == other.x && y == other.y; } // Sets this Point's x/y coordinates to the given values. // pre: newX >= 0 && newY >= 0 public void setLocation(int newX, int newY) { if (newX < 0 || newY < 0) { throw new IllegalArgumentException(); } 454 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 x = newX; y = newY; } // Returns a String representation of this Point object. public String toString() { return "(" + x + ", " + y + ")"; } // Shifts the position of this Point object by the given amount // in each direction. public void translate(int dx, int dy) { setLocation(x + dx, y + dy); } } +HUH LV WKH ILQDO YHUVLRQ RI WKH FOLHQW SURJUDP ZKLFK QRZ WDNHV DGYDQWDJH RI WKH WR6WULQJ PHWKRG ZKHQ SULQWLQJ 3RLQW REMHFWV 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 // A program that deals with 2D points. // Fifth version, to accompany Point class with toString method. public class PointMain { public static void main(String args) { // create two Point objects Point p1 = new Point(7, 2); Point p2 = new Point(4, 3); // print each point and its distance from origin System.out.println("p1 is " + p1); System.out.println("distance from origin = " + p1.distanceFromOrigin()); System.out.println("p2 is " + p2); System.out.println("distance from origin = " + p2.distanceFromOrigin()); // translate each point to a new location p1.translate(11, 6); p2.translate(1, 7); // print the points again System.out.println("p1 is " + p1); System.out.println("p2 is " + p2); } } 7KH WKLV .H\ZRUG :KHQ ZH GLVFXVVHG LQVWDQFH PHWKRGV ZH VDZ WKDW DQ LQVWDQFH PHWKRG KDV DQ LPSOLFLW NQRZOHGJH RI WKH REMHFW LW V RSHUDWLQJ XSRQ DQG FDQ GLUHFWO\ UHIHU WR WKDW REMHFW V GDWD ILHOGV DQG PHWKRGV %XW ZH GLGQ W GLVFXVV WKH PHFKDQLFV RI H[DFWO\ KRZ WKLV ZRUNV LQ -DYD 7KH UHDVRQ WKDW LQVWDQFH PHWKRGV NQRZ ZKLFK REMHFW WKH\ UH RSHUDWLQJ XSRQ LV EHFDXVH RI D VSHFLDO UHIHUHQFH QDPHG this :KHQ \RX UHIHU WR D GDWD ILHOG VXFK DV [ LQ \RXU FRGH UHDOO\ \RX UH UHIHUULQJ WR this.x 455 $Q\ZKHUH LQ WKH 3RLQW FODVV WKDW \RX UHIHU WR D GDWD ILHOG RU PHWKRG RI WKH FXUUHQW 3RLQW \RX PD\ RSWLRQDOO\ ZULWH this. LQ IURQW RI WKH ILHOG RU PHWKRG QDPH )RU H[DPSOH KHUH V DQ LPSOHPHQWDWLRQ RI WKH WUDQVODWH PHWKRG WKDW XVHV WKH this NH\ZRUG ZKHQ DFFHVVLQJ LWV GDWD ILHOGV 7KLV VW\OH LV OHVV FRPPRQ EXW VRPH SURJUDPPHUV SUHIHU LW EHFDXVH LW LV PRUH H[SOLFLW DERXW ZKDW LV JRLQJ RQ // An implementation of translate public void translate(int dx, int this.x += dx; // same as x this.y += dy; // same as y } using the this keyword. dy) { += dx; += dy; 7KLQNLQJ DERXW WKH this NH\ZRUG FDQ SUHVHQW D FOHDUHU SLFWXUH RI WKH PHFKDQLFV RI ZKDW LV UHDOO\ JRLQJ RQ GXULQJ DQ LQVWDQFH PHWKRG FDOO (DUOLHU LQ WKH FKDSWHU ZH GLDJUDPPHG WKH EHKDYLRU RI VRPH LQVWDQFH PHWKRG FDOOV RQ WZR 3RLQW REMHFWV /HW V UHYLVLW WKH VDPH H[DPSOH ZLWK WKH this NH\ZRUG &RQVLGHU WKH IROORZLQJ WZR 3RLQW REMHFWV Point p1 = new Point(7, 2); Point p2 = new Point(4, 3); $IWHU FRQVWUXFWLQJ WKH 3RLQW REMHFWV ZH PDNH WKH IROORZLQJ PHWKRG FDOOV p1.translate(11, 6); p2.translate(1, 7); (VVHQWLDOO\ WKH EHKDYLRU RI WKHVH WZR LQVWDQFH PHWKRG FDOOV LV WKH IROORZLQJ 6HW 3RLQW FODVV V this UHIHUHQFH WR UHIHU WR WKH VDPH REMHFW DV S ([HFXWH WKH WUDQVODWH PHWKRG ZLWK SDUDPHWHUV 6HW 3RLQW FODVV V this UHIHUHQFH WR UHIHU WR WKH VDPH REMHFW DV S ([HFXWH WKH WUDQVODWH PHWKRG ZLWK SDUDPHWHUV 'XULQJ WKH ILUVW LQVWDQFH PHWKRG FDOO WKH this UHIHUHQFH UHIHUV WR WKH VDPH REMHFW DV S WKH PHWKRG FDOO DGMXVWV WKH [ \ FRRUGLQDWHV RI S V REMHFW +--------------------+ +---+ | +----+ +----+ | p1 | +-+-->| x | 18 | y | 8 | | +---+ | +----+ +----+ | +--------------------+ ^ +---+ | this | +-+---+ +---+ +--------------------+ +---+ | +----+ +----+ | p2 | +-+-->| x | 4 | y | 3 | | +---+ | +----+ +----+ | +--------------------+ 7KHUHIRUH 'XULQJ WKH VHFRQG PHWKRG FDOO WKH this UHIHUHQFH UHIHUV WR WKH VDPH REMHFW DV S ERG\ RI WKH WUDQVODWH PHWKRG FKDQJH WKH [ DQG \ ILHOGV RI S V REMHFW 6R WKH OLQHV LQ WKH 456 +--------------------+ +---+ | +----+ +----+ | p2 | +-+-->| x | 5 | y | 10 | | +---+ | +----+ +----+ | +--------------------+ ^ +---+ | this | +-+-------------------------------------------+ +---+ +--------------------+ +---+ | +----+ +----+ | p1 | +-+-->| x | 18 | y | 8 | | +---+ | +----+ +----+ | +--------------------+ 7KH this NH\ZRUG GRHV KDYH VRPH RWKHU XVHV <RX FDQ XVH this LI \RX QHHG WKH FXUUHQW REMHFW WR SDVV LWVHOI DV D SDUDPHWHU WR D PHWKRG )RU H[DPSOH LI \RX ZDQW WR SULQW WKH FXUUHQW REMHFW V VWDWH IRU GHEXJJLQJ DIWHU LW V EHHQ FRQVWUXFWHG \RX FDQ ZULWH D SULQWOQ VWDWHPHQW ZLWK this DV WKH SDUDPHWHU public Point(int initialX, int initialY) { this.setLocation(initialX, initialY); System.out.println(this); // for debugging } $ FRPPRQ XVDJH RI WKH this NH\ZRUG LV WR GHDO ZLWK VKDGRZHG YDULDEOHV $ VKDGRZHG YDULDEOH RFFXUV ZKHQ WZR YDULDEOHV ZLWK WKH VDPH QDPH H[LVW DW WKH VDPH SODFH LQ WKH FRGH 1RUPDOO\ WKLV FDQ W KDSSHQ EHFDXVH ZH FDQ W GHFODUH WZR YDULDEOHV ZLWK WKH VDPH QDPH %XW RQH H[FHSWLRQ WR WKLV UXOH LV WKDW LW LV OHJDO IRU DQ REMHFW V GDWD ILHOG WR KDYH WKH VDPH QDPH DV D SDUDPHWHU RU ORFDO YDULDEOH LQ RQH RI LWV PHWKRGV 6R LW V OHJDO WR KDYH D PHWKRG KHDGHU VXFK DV WKH IROORZLQJ HYHQ WKRXJK RXU GDWD ILHOGV DUH QDPHG [ DQG \ public void setLocation(int x, int y) { 6KDGRZLQJ RFFXUV KHUH EHFDXVH LI \RX UHIHU WR [ WKH SDUDPHWHU [ ZLOO EH XVHG DQG QRW WKH GDWD ILHOG [ +RZHYHU LI \RX ZULWH this.x WKH GDWD ILHOG [ ZLOO EH XVHG 6RPH SURJUDPPHUV OLNH WKLV VW\OH ZKHUH YDULDEOHV WDNH WKH VDPH QDPH DV ILHOGV LI WKH\ ZLOO EH VWRUHG LQWR WKRVH ILHOGV ,W VDYHV \RX IURP KDYLQJ WR XVH VWUDQJH SDUDPHWHU QDPHV OLNH LQLWLDO; RU QHZ< +HUH V DQ H[DPSOH RI WKH VHW/RFDWLRQ PHWKRG XVLQJ [ DQG \ DV LWV SDUDPHWHU QDPHV // Sets this Point's x/y coordinates to the given values. public void setLocation(int x, int y) { if (x < 0 || y < 0) { throw new IllegalArgumentException(); } this.x = x; this.y = y; } 7KH SULQFLSOH RI VKDGRZLQJ DSSOLHV WKH VDPH WR FRQVWUXFWRUV :H FDQ PRGLI\ RXU 3RLQW FRQVWUXFWRU V SDUDPHWHU QDPHV WR EH MXVW [ DQG \ DV ZHOO VLQFH WKH FRQVWUXFWRU V ERG\ SDVVHV WKHLU YDOXHV WR VHW/RFDWLRQ // Constructs a new Point at the given initial x/y position. public Point(int x, int y) { setLocation(x, y); } 457 0XOWLSOH &RQVWUXFWRUV 7KH WKLV NH\ZRUG FDQ DOVR EH XVHG WR KHOS ZULWH PXOWLSOH FRQVWUXFWRUV ,W V OHJDO IRU D FODVV WR KDYH PRUH WKDQ RQH FRQVWUXFWRU DV ORQJ DV WKH\ DFFHSW GLIIHUHQW SDUDPHWHUV 3UHVHQWO\ RXU 3RLQW FODVV KDV D FRQVWUXFWRU WKDW UHTXLUHV WZR SDUDPHWHUV WKH 3RLQW V LQLWLDO [ DQG \ FRRUGLQDWHV // Constructs a new Point at the given initial x/y position. public Point(int x, int y) { setLocation(x, y); } %HIRUH ZH ZURWH RXU FRQVWUXFWRU LW ZDV OHJDO WR FUHDWH D 3RLQW REMHFW WKDW UHSUHVHQWHG WKH RULJLQ E\ SDVVLQJ QR SDUDPHWHUV ZKHQ FRQVWUXFWLQJ LW 7KLV LVQ W OHJDO DQ\ PRUH QRZ WKDW ZH DGGHG RXU WZR SDUDPHWHU FRQVWUXFWRU +RZHYHU ZH FDQ PDNH LW OHJDO DJDLQ E\ DGGLQJ D VHFRQG FRQVWUXFWRU WR RXU 3RLQW FODVV 2XU QHZ SDUDPHWHUOHVV FRQVWUXFWRU PLJKW ORRN OLNH WKLV // Constructs a Point object at the origin, (0, 0). public Point() { x = 0; y = 0; } 1RZ LW ZRXOG EH SRVVLEOH WR FRQVWUXFW 3RLQWV LQ WZR ZD\V Point p1 = new Point(5, -2); Point p2 = new Point(); // (5, -2) // (0, 0) %XW E\ DGGLQJ D VHFRQG FRQVWUXFWRU ZH KDYH DJDLQ LQWURGXFHG D ELW RI UHGXQGDQF\ LQWR RXU FODVV %RWK FRQVWUXFWRUV SHUIRUP VLPLODU DFWLRQV WKH RQO\ GLIIHUHQFH LV H[DFWO\ ZKDW LQLWLDO YDOXH WKH [ DQG \ GDWD ILHOGV UHFHLYH $ XVHIXO REVHUYDWLRQ KHUH LV WKDW WKH QHZ FRQVWUXFWRU FDQ EH H[SUHVVHG LQ WHUPV RI WKH ROG FRQVWUXFWRU 7KH IROORZLQJ WZR OLQHV KDYH WKH VDPH HIIHFW Point p1 = new Point(); Point p2 = new Point(0, 0); // construct Point at (0, 0) // construct Point at (0, 0) :H FDQ H[SUHVV WKLV LQ RXU 3RLQW FODVV E\ KDYLQJ WKH SDUDPHWHUOHVV FRQVWUXFWRU FDOO WKH WZR SDUDPHWHU FRQVWUXFWRU 7KH V\QWD[ IRU RQH FRQVWUXFWRU WR FDOO DQRWKHU LV D ELW VWUDQJH LQ LWV ERG\ ZH ZULWH WKH NH\ZRUG this IROORZHG E\ WKH SDUDPHWHUV WR SDVV WR WKH RWKHU FRQVWUXFWRU LQ SDUHQWKHVHV ,Q RXU FDVH ZH ZDQW WR SDVV SDUDPHWHU YDOXHV RI DQG WR LQLWLDOL]H HDFK ILHOG // Constructs a new Point object at the origin, (0, 0). public Point() { this(0, 0); } 7KH XVDJH RI this FDXVHV WKH SDUDPHWHUOHVV FRQVWUXFWRU WR XWLOL]H WKH EHKDYLRU RI WKH RWKHU FRQVWUXFWRU 7KH IROORZLQJ GLDJUDP GHSLFWV WKLV EHKDYLRU 1RWLFH WKDW ZH UH DOVR XVLQJ WKH this NH\ZRUG WR FDOO PHWKRGV DQG WR GLVDPELJXDWH RXU GDWD ILHOGV IURP RXU SDUDPHWHUV 458 +------------------+ | public Point() { | | this(0, 0); | | } \ | +-------\----------+ \ +-\----------------------------+ | public Point(int x, int y) { | | this.setLocation(x, y); | | } \ | +------------\-----------------+ \ +-\---------------------------------------+ | public void setLocation(int x, int y) { | | this.x = x; | | this.y = y; | | } | +-----------------------------------------+ 7KH JHQHUDO V\QWD[ IRU RQH FRQVWUXFWRU WR FDOO DQRWKHU LV WKH IROORZLQJ this(<expression>, <expression>, ..., <expression>); 7KLV LV UHDOO\ MXVW WKH QRUPDO V\QWD[ IRU D PHWKRG FDOO H[FHSW IRU WKH IDFW WKDW ZH XVH WKH VSHFLDO this NH\ZRUG ZKHUH ZH ZRXOG QRUPDOO\ SXW WKH QDPH RI D PHWKRG +HUH LV WKH ILQDO YHUVLRQ RI RXU 3RLQW FODVV DIWHU DOO RI WKH FKDQJHV LQ WKLV VHFWLRQ 7KLV YHUVLRQ RI WKH FODVV XVHV WKH this NH\ZRUG WKURXJKRXW 7KH FOLHQW SURJUDP LV XQPRGLILHG EHFDXVH LW GRHV QRW FRQVWUXFW DQ\ 3RLQWV DW 459 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 53 54 55 56 57 58 59 // A Point object represents an ordered pair of (x, y) coordinates. // Sixth version: multiple constructors and this keyword. public class Point { private int x; private int y; // Constructs a new Point object at the origin, (0, 0). public Point() { this(0, 0); } // Constructs a new Point at the given initial x/y position. public Point(int x, int y) { setLocation(x, y); } // Returns the direct distance between this Point and the origin (0, 0). public double distanceFromOrigin() { return Math.sqrt(x * x + y * y); } // Returns the x-coordinate of this Point. public int getX() { return x; } // Returns the y-coordinate of this Point. public int getY() { return y; } // Returns whether o refers to a Point with the same x/y // coordinates as this Point. public boolean equals(Object o) { if (!(o instanceof Point)) { return false; } Point other = (Point) o; return x == other.x && y == other.y; } // Sets this Point's x/y coordinates to the given values. public void setLocation(int x, int y) { if (x < 0 || y < 0) { throw new IllegalArgumentException(); } this.x = x; this.y = y; } // Returns a String representation of this Point object. public String toString() { return "(" + x + ", " + y + ")"; } // Shifts the position of this Point object by the given amount 460 60 61 62 63 64 // in each direction. public void translate(int dx, int dy) { setLocation(x + dx, y + dy); } } &DVH 6WXG\ 'HVLJQLQJ D 6WRFN &ODVV ,Q WKLV VHFWLRQ ZH OO FUHDWH DQRWKHU QHZ FODVV RI REMHFWV FDOOHG 6WRFN IRU D SURJUDP WKDW PDQDJHV D SRUWIROLR RI VKDUHV RI VWRFN WKDW WKH XVHU KDV SXUFKDVHG 7KH XVHU FDQ DGG D QHZ VWRFN WR KLV KHU SRUWIROLR RU FDQ UHFRUG SXUFKDVHV RI VKDUHV RI D VWRFN DW D FHUWDLQ SULFH 7KLV LQIRUPDWLRQ FDQ EH XVHG WR UHSRUW WKH LQYHVWRU V SURILW RU ORVV RQ WKDW VWRFN 7KH LQWHUDFWLRQ ZLWK WKH SURJUDP PLJKW ORRN OLNH WKLV First stock's symbol: AMZN How many purchases did you make? 2 1: How many shares, at what price per share? 50 35.06 2: How many shares, at what price per share? 25 38.52 What is today's price per share? 37.29 Net profit/loss: $80.75 Second stock's symbol: INTC How many purchases did you make? 1 1: How many shares, at what price per share? 15 16.50 What is today's price per share? 17.80 Net profit/loss: $19.5 AMZN was more profitable than INTC. 7KH VWRFN SURJUDP FRXOG EH ZULWWHQ XVLQJ H[LVWLQJ W\SHV VXFK DV GRXEOHV DQG 6WULQJV EXW LW ZRXOG EH FXPEHUVRPH WR NHHS WUDFN RI DOO WKH YDULDEOHV HVSHFLDOO\ LI ZH SXUFKDVHG PXOWLSOH VWRFNV EHFDXVH HDFK ZRXOG QHHG D FRS\ RI WKRVH YDULDEOHV 7R PDNH WKH FOLHQW SURJUDP VKRUWHU DQG VLPSOHU OHW V ZULWH D 6WRFN FODVV (DFK 6WRFN REMHFW ZRXOG UHPHPEHU WKH VKDUHV DQG WRWDO SULFH RI D VLQJOH VWRFN DQG FRXOG UHSRUW WKH SURILW RU ORVV EDVHG RQ WKH FXUUHQW GDLO\ VWRFN SULFH 5HPHPEHU WKDW ZKHQ ZH GHILQH QHZ W\SHV RI REMHFWV ZH VWDUW WR KDYH SURJUDPV WKDW RFFXS\ PRUH WKDQ RQH ILOH 7KH 6WRFN0DQDJHU SURJUDP LWVHOI ZLOO UHVLGH LQ WKH ILOH 6WRFN0DQDJHU MDYD DQG WKH QHZ 6WRFN FODVV ZH OO ZULWH ZLOO JR LQWR D VHSDUDWH ILOH 6WRFN MDYD :KHQ ZH FRPSLOH DQG UXQ 6WRFN0DQDJHU MDYD LW ZLOO DOVR GHSHQG RQ 6WRFN MDYD WR UXQ SURSHUO\ :H OO NHHS WKHVH WZR ILOHV LQ WKH VDPH IROGHU VR WKDW WKH -DYD FRPSLOHU FDQ ILQG WKHP DQG FRPSLOH WKHP WRJHWKHU 6WRFN %HKDYLRU :KHQ GHVLJQLQJ D FODVV WKH ILUVW TXHVWLRQ WR EH DVNHG LV :KDW EHKDYLRU VKRXOG WKHVH REMHFWV KDYH" :H VWDUWHG RXU 3RLQW FODVV E\ VSHFLI\LQJ LWV GDWD ILHOGV EXW WKDW ZDV EHFDXVH ILHOGV DUH FRQFHSWXDOO\ VLPSOHU WKDQ LQVWDQFH PHWKRGV 7KH REMHFWV EHKDYLRU LV ZKDW WKH FOLHQW SURJUDP ZLOO XVH VR KDYLQJ WKH ULJKW EHKDYLRU LQ HDFK REMHFW LV YHU\ LPSRUWDQW 461 $QRWKHU LVVXH WR FRQVLGHU LV WKH GLYLVLRQ RI EHKDYLRU EHWZHHQ WKH FOLHQW SURJUDP DQG WKH 6WRFN FODVV :KLFK FRGH VKRXOG JR LQ ZKDW ILOH" $ JRRG UXOH RI WKXPE LV WR RQO\ VWRUH EHKDYLRU LQ WKH 6WRFN FODVV LI LW V FORVHO\ UHODWHG WR VWRFN GDWD $QRWKHU XVHIXO GHVLJQ UXOH LV WR DYRLG SXWWLQJ FRQVROH , 2 FRGH VXFK DV 6\VWHP RXW SULQWOQ VWDWHPHQWV RU 6FDQQHU FRPPDQGV LQ \RXU 6WRFN FODVV 7KH , 2 FRGH LV EHVW OHIW LQ WKH FOLHQW SURJUDP VR WKDW WKH 6WRFN FODVV FDQ EH XVHG ZLWK PDQ\ GLIIHUHQW NLQGV RI FOLHQW SURJUDPV $ 6WRFN REMHFW QHHGV WR EH DEOH WR SHUIRUP WKH IROORZLQJ EHKDYLRU UHFRUGLQJ D SXUFKDVH RI VKDUHV RI WKH VWRFN DW D JLYHQ SULFH UHSRUWLQJ WKH WRWDO SURILW ORVV RQ WKH VWRFN EDVHG RQ WKH FXUUHQW SULFH SHU VKDUH RI WKH VWRFN :H OO UHSUHVHQW WKHVH EHKDYLRUV DV PHWKRGV QDPHG SXUFKDVH DQG JHW3URILW 7KH SXUFKDVH PHWKRG ZLOO DFFHSW SDUDPHWHUV IRU WKH QXPEHU RI VKDUHV DQG SULFH SHU VKDUH 7KH JHW3URILW PHWKRG ZLOO DFFHSW WKH FXUUHQW SULFH SHU VKDUH RI WKH VWRFN DQG ZLOO UHWXUQ WKH WRWDO SURILW +HUH V D VNHOHWRQ RI RXU 6WRFN FODVV VR IDU // Incomplete Stock class. public class Stock { <fields> <constructor(s)> public double getProfit(double currentPrice) { <implementation> } public void purchase(int shares, double pricePerShare) { <implementation> } } 6WRFN )LHOGV $IWHU GHFLGLQJ RQ WKH UHTXLUHG EHKDYLRU IRU D FODVV WKH QH[W TXHVWLRQ WR EH DVNHG LV :KDW GDWD VKRXOG HDFK 6WRFN REMHFW VWRUH WR LPSOHPHQW LWV EHKDYLRU" (DFK VWRFN LQ WKH FOLHQW SURJUDP KDV VHYHUDO UHODWHG SLHFHV RI GDWD WKH VWRFN V\PERO VXFK DV $0=1 WKH QXPEHU RI VKDUHV SXUFKDVHG WKH FRVW IRU WKH VKDUHV WKDW KDYH EHHQ SXUFKDVHG :H FRXOG WU\ WR VWRUH HDFK SXUFKDVH RI WKH VWRFN VHSDUDWHO\ EXW DOO ZH UHDOO\ QHHG WR NQRZ LV WKH WRWDO QXPEHU RI VKDUHV LQ DOO SXUFKDVHV DQG WKH WRWDO FRVW RI DOO VKDUHV FRPELQHG :LWK WKLV LQ PLQG KHUH LV D OLVW RI WKH ILHOGV RXU 6WRFN REMHFWV ZLOO QHHG private String symbol; private int totalShares; private double totalCost; // stock symbol, such as "YHOO" // total number of shares purchased // total cost for all shares purchased 462 $ YDULDEOH VWRFN DW D WRWDO FRVW RI stock1 +----+ | ---+---> +----+ UHIHUULQJ WR D 6WRFN REMHFW ZLWK V\PERO RI $0=1 DQG WRWDO VKDUHV SXUFKDVHG ZRXOG ORRN OLNH WKLV +---------------------------------------+ | totalShares totalCost symbol | | +----+ +-------+ +----+ | +--------+ | | 5 | | 250.4 | | ---+---+--> | "AMZN" | | +----+ +-------+ +----+ | +--------+ +---------------------------------------+ 7KH V\PERO ILHOG KDV DQ DUURZ WR DQ H[WHUQDO ER[ EHFDXVH V\PERO LV D UHIHUHQFH WR D 6WULQJ REMHFW 7KLV LV DQ H[DPSOH RI KRZ REMHFWV GDWD ILHOGV FDQ EH UHIHUHQFHV WR RWKHU REMHFWV 0DQ\ 6WRFN REMHFWV FDQ EH FUHDWHG DQG HDFK ZLOO KDYH LWV RZQ FRS\ RI WKH GDWD ILHOGV ,W ZRXOG EH SRVVLEOH WR FUHDWH D VHFRQG 6WRFN REMHFW ZLWK V\PERO ,17& DQG VKDUHV SXUFKDVHG DW D WRWDO FRVW RI ,I LW ZHUH VWRUHG LQ D YDULDEOH QDPHG VWRFN LWV VWDWH ZRXOG ORRN OLNH WKLV stock2 +----+ | ---+---> +----+ +---------------------------------------+ | totalShares totalCost symbol | | +----+ +-------+ +----+ | +--------+ | | 12 | | 575.6 | | ---+---+--->| "INTC" | | +----+ +-------+ +----+ | +--------+ +---------------------------------------+ 7KH FXUUHQW SULFH SHU VKDUH LV DOVR D SLHFH RI GDWD UHODWHG WR D 6WRFN %XW ZH ZRQ W WUHDW LW DV D GDWD ILHOG EHFDXVH LW V RQO\ XVHG WR ILQG WKH 6WRFN V SURILW ,I D YDOXH LV RQO\ XVHG LQ RQH PHWKRG RI WKH FODVV DQG LV SDVVHG DV D SDUDPHWHU WR WKDW PHWKRG LW V EHVW QRW WR PDNH LW D GDWD ILHOG &OXWWHULQJ D FODVV ZLWK WRR PDQ\ GDWD ILHOGV UHGXFHV WKH FRKHUHQFH RI WKH REMHFW DQG FDQ PDNH WKH FRGH KDUGHU WR UHDG 6WRFN &RQVWUXFWRU 1RZ WKDW ZH YH GHFLGHG RQ WKH 6WRFN V VWDWH DQG EHKDYLRU OHW V WKLQN DERXW KRZ WR FRQVWUXFW 6WRFN REMHFWV VR WKDW WKH FOLHQW SURJUDP FDQ FUHDWH WZR VWRFNV DQG UHFRUG SXUFKDVHV RI WKHP ,W PD\ EH WHPSWLQJ WR ZULWH D FRQVWUXFWRU WKDW DFFHSWV WKUHH SDUDPHWHUV RQH IRU HDFK RI WKH 6WRFN V GDWD ILHOGV %XW WKH LQLWLDO VWDWH RI D JLYHQ 6WRFN REMHFW LV WKDW QR VKDUHV KDYH EHHQ SXUFKDVHG 6R ZH DFWXDOO\ NQRZ WKH LQLWLDO WRWDO VKDUHV DQG WKH LQLWLDO WRWDO FRVW ZLWKRXW WKH FOLHQW V KHOS 7KHUHIRUH RXU 6WRFN FRQVWUXFWRU VKRXOG DFFHSW MXVW RQH SDUDPHWHU WKH V\PERO IRU WKH 6WRFN :H ZLOO VDYH WKH V\PERO YDOXH LQWR WKH REMHFW V symbol GDWD ILHOG // Creates a new Stock with the given symbol and no shares purchased. public Stock(String theSymbol) { symbol = theSymbol; totalShares = 0; totalCost = 0.00; } :KHQ D FRQVWUXFWRU WDNHV DQ REMHFW DV DQ SDUDPHWHU VXFK DV WKH 6WULQJ V\PERO LW PLJKW PDNH VHQVH WR FKHFN WKDW SDUDPHWHU V YDOXH WR PDNH VXUH LW LVQ W QXOO 2QH SRVVLEOH ZD\ WR KDQGOH WKLV FDVH ZRXOG EH WR WKURZ DQ H[FHSWLRQ LI D QXOO V\PERO LV SDVVHG ZKHQ FUHDWLQJ D 6WRFN REMHFW 7KH IROORZLQJ OLQHV FRXOG EH LQVHUWHG DW WKH VWDUW RI WKH 6WRFN V FRQVWUXFWRU 463 if (theSymbol == null) { throw new NullPointerException("null symbol"); } 6XQ UHFRPPHQGV WKDW \RX WKURZ D 1XOO3RLQWHU([FHSWLRQ ZKHQ D SDUDPHWHU V YDOXH LV QXOO EXW VKRXOG QRW EH )RU RWKHU LQYDOLG SDUDPHWHU YDOXHV WKURZ DQ ,OOHJDO$UJXPHQW([FHSWLRQ +HUH V D VNHOHWRQ RI RXU 6WRFN FODVV VR IDU // Incomplete Stock class with fields and constructor. public class Stock { private String symbol; // stock symbol, such as "YHOO" private int totalShares; // total number of shares purchased private double totalCost; // total cost for all shares purchased // Creates a new Stock with the given symbol and no shares purchased. public Stock(String theSymbol) { if (theSymbol == null) { throw new NullPointerException("null symbol"); } symbol = theSymbol; totalShares = 0; totalCost = 0.00; } public double getProfit(double currentPrice) { <implementation> } public void purchase(int shares, double pricePerShare) { <implementation> } } 6WRFN 0HWKRG ,PSOHPHQWDWLRQ 1RZ WKDW ZH KDYH FUHDWHG WKH 6WRFN VWDWH DQG FRQVWUXFWRU OHW V ZULWH WKH ERGLHV RI RXU PHWKRGV )LUVW ZH OO ORRN DW WKH SXUFKDVH PHWKRG ZKLFK DFFHSWV D QXPEHU RI VKDUHV DQG D SULFH SHU VKDUH DV SDUDPHWHUV DQG PRGLILHV WKH 6WRFN REMHFW V VWDWH WR UHFRUG WKDW SXUFKDVH 5HFRUGLQJ WKH SXUFKDVH FRQVLVWV RI DGGLQJ WKH QXPEHU RI VKDUHV WR RXU WRWDO DQG DGGLQJ WKH SULFH SDLG IRU WKHVH VKDUHV WR RXU WRWDO FRVW 7KH SULFH SDLG LV HTXDO WR WKH QXPEHU RI VKDUHV WLPHV WKH SULFH SHU VKDUH +HUH V WKH FRGH IRU WKH SXUFKDVH PHWKRG WR LPSOHPHQW WKLV EHKDYLRU // Records a purchase of the given number of shares of this stock // at the given price per share. public void purchase(int shares, double pricePerShare) { totalShares += shares; totalCost += shares * pricePerShare; } 464 $V ZLWK WKH FRQVWUXFWRU LW PLJKW PDNH VHQVH KHUH WR FKHFN WKH SDUDPHWHUV SDVVHG LQ WR PDNH VXUH WKH\ DUH YDOLG ,Q WKLV FDVH D YDOLG QXPEHU RI VKDUHV DQG SULFH SHU VKDUH ZRXOG EH RQHV WKDW DUH QRQ QHJDWLYH 7KH IROORZLQJ OLQHV FRXOG EH LQVHUWHG DW WKH VWDUW RI RXU SXUFKDVH PHWKRG WR SHUIRUP WKLV WHVW if (shares < 0 || pricePerShare < 0) { throw new IllegalArgumentException("negative shares or price"); } 1RZ OHW V ZULWH WKH JHW3URILW PHWKRG ZKLFK FRPSXWHV KRZ PXFK PRQH\ ZH YH PDGH RU ORVW RQ RXU 6WRFN 7KH SURILW RI D 6WRFN LV HTXDO WR LWV PDUNHW YDOXH PLQXV ZKDW ZH SDLG IRU LW LI ZH SDLG IRU RXU VKDUHV DQG WKH\ QRZ KDYH D PDUNHW YDOXH RI RXU SURILW LV :H DOUHDG\ NQRZ ZKDW ZH SDLG IRU WKH 6WRFN EHFDXVH ZH UH NHHSLQJ WUDFN RI LW LQ RXU WRWDO&RVW ILHOG %XW ZH GRQ W NQRZ WKH PDUNHW YDOXH \HW VR ZH OO KDYH WR FRPSXWH LW E\ PXOWLSO\LQJ RXU WRWDO6KDUHV ILHOG E\ WKH FXUUHQW SULFH SHU VKDUH 2QFH ZH KDYH WKH PDUNHW YDOXH ZH OO MXVW VXEWUDFW WKH WRWDO&RVW ZH SDLG WR ILQG RXW RXU SURILW // Returns the total profit or loss earned on the shares of this stock, // based on the given price per share. public double getProfit(double currentPrice) { double marketValue = totalShares * currentPrice; return marketValue - totalCost; } $V ZLWK WKH RWKHU PHWKRGV ZH VKRXOG FKHFN IRU LOOHJDO SDUDPHWHU YDOXHV ,Q WKLV FDVH ZH VKRXOGQ W DOORZ D QHJDWLYH FXUUHQW SULFH SHU VKDUH VR WKH IROORZLQJ FRGH FDQ EH SODFHG DW WKH VWDUW RI WKH JHW3URILW PHWKRG if (currentPrice < 0.0) { throw new IllegalArgumentException("negative current price"); } 7KHUH DUH RWKHU PHWKRGV WKDW ZRXOG EH XVHIXO WR KDYH LQ RXU 6WRFN REMHFWV DQG DUH H[FOXGHG IRU VSDFH UHDVRQV )RU H[DPSOH LW ZRXOG SUREDEO\ DOVR EH JRRG WR LPSOHPHQW DFFHVVRUV IRU WKH 6WRFN V GDWD WKH V\PERO QXPEHU RI VKDUHV DQG VR RQ $OVR XVHIXO ZRXOG EH D WR6WULQJ PHWKRG VR WKDW 6WRFN REMHFWV FRXOG EH SULQWHG HDVLO\ 7KHVH PHWKRGV DUH OHIW IRU \RX WR LPSOHPHQW DV H[HUFLVHV 7KH &RPSOHWH 6WRFN &ODVV $IWHU DOO GDWD ILHOGV FRQVWUXFWRU DQG PHWKRGV RI RXU 6WRFN FODVV DUH ZULWWHQ WKH FODVV ZRXOG ORRN OLNH WKLV 465 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 // A Stock object represents purchases of shares of a particular stock. public class Stock { private String symbol; // stock symbol, such as "YHOO" private int totalShares; // total number of shares purchased private double totalCost; // total cost for all shares purchased // Creates a new Stock with the given symbol and no shares purchased. // Precondition: symbol != null public Stock(String theSymbol) { if (theSymbol == null) { throw new NullPointerException("null symbol"); } symbol = theSymbol; totalShares = 0; totalCost = 0.00; } // Returns the total profit or loss earned on the shares of this stock, // based on the given price per share. // Precondition: currentPrice >= 0.0 public double getProfit(double currentPrice) { if (currentPrice < 0.0) { throw new IllegalArgumentException("negative current price"); } double marketValue = totalShares * currentPrice; return marketValue - totalCost; } // Records a purchase of the given number of shares of this stock // at the given price per share. // Precondition: numShares >= 0 && pricePerShare >= 0.00 public void purchase(int shares, double pricePerShare) { if (shares < 0 || pricePerShare < 0.0) { throw new IllegalArgumentException("negative shares or price"); } totalShares += shares; totalCost += shares * pricePerShare; } } 7KH FOLHQW FRGH WR XVH WKH 6WRFN FODVV PLJKW ORRN OLNH WKLV 466 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 53 54 55 56 57 58 59 // This program tracks the user's purchases of two stocks, // computing and reporting which stock is more profitable. public class StockManager { public static void main(String args) { Scanner console = new Scanner(System.in); // first stock System.out.print("First stock's symbol: "); String symbol1 = console.next(); // Create a Stock object to represent my purchases of this stock Stock stock1 = new Stock(symbol1); double profit1 = makePurchases(stock1, console); // second stock System.out.print("Second stock's symbol: "); String symbol2 = console.next(); Stock stock2 = new Stock(symbol2); double profit2 = makePurchases(stock2, console); // report which stock made more money if (profit1 > profit2) { System.out.println(symbol1 + " was more " + "profitable than " + symbol2 + "."); } else if (profit2 > profit1) { System.out.println(symbol2 + " was more " + "profitable than " + symbol1 + "."); } else { System.out.println(symbol1 + " and " + symbol2 + " are equally profitable."); } } // Makes several purchases of stock and then returns the profit. public static double makePurchases(Stock currentStock, Scanner console) { System.out.print("How many purchases did you make? "); int numPurchases = console.nextInt(); // Ask about each purchase for (int i = 1; i <= numPurchases; i++) { System.out.print(i + ": How many shares, at what price per share? "); int numShares = console.nextInt(); double pricePerShare = console.nextDouble(); // Ask the Stock object to store this purchase currentStock.purchase(numShares, pricePerShare); } // Use the Stock object to compute how much money I made / lost System.out.print("What is today's price per share? "); double currentPrice = console.nextDouble(); double profit = currentStock.getProfit(currentPrice); System.out.println("Net profit/loss: $" + profit); System.out.println(); return profit; } } 467 &KDSWHU 6XPPDU\ 2EMHFW RULHQWHG SURJUDPPLQJ LV D GLIIHUHQW SKLORVRSK\ RI ZULWLQJ SURJUDPV WKDW IRFXVHV RQ QRXQV RU HQWLWLHV LQ D SURJUDP UDWKHU WKDQ YHUEV RU DFWLRQV RI D SURJUDP ,Q REMHFW RULHQWHG SURJUDPPLQJ VWDWH DQG EHKDYLRU DUH JURXSHG LQWR REMHFWV WKDW FRPPXQLFDWH ZLWK HDFK RWKHU $ FODVV VHUYHV DV WKH EOXHSULQW IRU D QHZ W\SH RI REMHFWV VSHFLI\LQJ WKHLU GDWD DQG EHKDYLRU 7KH FODVV FDQ EH DVNHG WR FRQVWUXFW PDQ\ REMHFWV DOVR FDOOHG LQVWDQFHV RI LWV W\SH 7KH GDWD IRU HDFK REMHFW LV VSHFLILHG XVLQJ VSHFLDO YDULDEOHV FDOOHG ILHOGV 7KH EHKDYLRU RI HDFK REMHFW LV VSHFLILHG E\ ZULWLQJ LQVWDQFH PHWKRGV LQ WKH FODVV ,QVWDQFH PHWKRGV H[LVW LQVLGH DQ REMHFW DQG FDQ DFFHVV DQG DFW RQ WKDW REMHFW V LQWHUQDO VWDWH $ FODVV FDQ GHILQH D VSHFLDO PHWKRG QDPHG D FRQVWUXFWRU WKDW FUHDWHV DQG UHWXUQV D QHZ REMHFW DQG LQLWLDOL]HV LWV VWDWH 7KH FRQVWUXFWRU LV WKH PHWKRG WKDW ZLOO EH FDOOHG ZKHQ H[WHUQDO FOLHQW FRGH FUHDWHV D QHZ REMHFW RI \RXU W\SH XVLQJ WKH new NH\ZRUG 8VXDOO\ RQH REMHFW FDQ FRPPXQLFDWH ZLWK RWKHUV GHVSLWH QRW NQRZLQJ DOO GHWDLOV DERXW KRZ WKH RWKHU REMHFWV ZRUN D SULQFLSOH NQRZQ DV DEVWUDFWLRQ 0RVW REMHFWV SURWHFW WKHLU LQWHUQDO GDWD IURP XQZDQWHG H[WHUQDO PRGLILFDWLRQ D SULQFLSOH NQRZQ DV HQFDSVXODWLRQ (QFDSVXODWLRQ LV SURYLGHG E\ GHFODULQJ ILHOGV ZLWK WKH private PRGLILHU ZKLFK SUHYHQWV RWKHU FODVVHV IURP GLUHFWO\ PRGLI\LQJ WKHLU YDOXHV 7ZR FRPPRQ FDWHJRULHV RI REMHFW PHWKRGV DUH DFFHVVRUV DQG PXWDWRUV $FFHVVRUV SURYLGH XV ZLWK LQIRUPDWLRQ DERXW WKH REMHFW VXFK DV WKH OHQJWK PHWKRG RI D 6WULQJ REMHFW RU WKH JHW; PHWKRG RI D 3RLQW REMHFW 0XWDWRUV DOORZ XV WR PRGLI\ WKH VWDWH RI WKH REMHFW VXFK DV WKH WUDQVODWH PHWKRG RI D 3RLQW REMHFW 2EMHFWV FDQ EH PDGH HDVLO\ SULQWDEOH E\ ZULWLQJ D toString PHWKRG 2EMHFWV FDQ EH PDGH WHVWDEOH IRU HTXDOLW\ E\ ZULWLQJ DQ equals PHWKRG 7KH this NH\ZRUG FDQ EH XVHG ZKHQ DQ REMHFW ZLVKHV WR UHIHU WR LWVHOI 7KH this NH\ZRUG LV DOVR XVHG ZKHQ D FODVV KDV PXOWLSOH FRQVWUXFWRU DQG RQH FRQVWUXFWRU ZLVKHV WR FDOO DQRWKHU 6HOI &KHFN 3UREOHPV 6HFWLRQ 2EMHFW 2ULHQWHG 3URJUDPPLQJ &RQFHSWV 'HVFULEH WKH GLIIHUHQFH EHWZHHQ REMHFW RULHQWHG SURJUDPPLQJ DQG SURFHGXUDO SURJUDPPLQJ :KDW LV DQ REMHFW" +RZ LV DQ REMHFW GLIIHUHQW IURP D FODVV" *LYH WKUHH H[DPSOHV RI W\SHV RI REMHFWV \RX YH XVHG VR IDU 468 6HFWLRQ 2EMHFW 6WDWH )LHOGV ([SODLQ WKH GLIIHUHQFHV EHWZHHQ D GDWD ILHOG DQG D SDUDPHWHU :KDW LV WKH GLIIHUHQFH LQ WKHLU V\QWD[" :KDW LV WKH GLIIHUHQFH LQ WKHLU VFRSH DQG KRZ WKH\ PD\ EH XVHG" 6HFWLRQ 2EMHFW %HKDYLRU 0HWKRGV ([SODLQ WKH GLIIHUHQFHV EHWZHHQ D VWDWLF PHWKRG DQG DQ LQVWDQFH PHWKRG :KDW LV WKH GLIIHUHQFH LQ WKHLU V\QWD[" :KDW LV WKH GLIIHUHQFH LQ KRZ WKH\ DUH XVHG" :KDW LV WKH GLIIHUHQFH EHWZHHQ DQ DFFHVVRU DQG D PXWDWRU" :KDW DUH WKH QDPLQJ FRQYHQWLRQV XVHG ZLWK DFFHVVRUV DQG PXWDWRUV" 6HFWLRQ 2EMHFW ,QLWLDOL]DWLRQ &RQVWUXFWRUV :KDW LV D FRQVWUXFWRU" +RZ LV D FRQVWUXFWRU GLIIHUHQW IURP D PHWKRG" :KDW DUH WZR PDMRU SUREOHPV ZLWK WKH IROORZLQJ FRQVWUXFWRU" public void Point(int initialX, int initialY) { int x = initialX; int y = initialY; } 6HFWLRQ (QFDSVXODWLRQ :KDW LV DEVWUDFWLRQ" +RZ GR REMHFWV SURYLGH DQ DEVWUDFWLRQ" :KDW LV WKH GLIIHUHQFH EHWZHHQ WKH public DQG private NH\ZRUGV" :KDW LWHPV VKRXOG EH GHFODUHG private" :KHQ GDWD ILHOGV DUH PDGH private FOLHQW SURJUDPV FDQQRW VHH WKHP GLUHFWO\ +RZ GR \RX DOORZ FODVVHV DFFHVV WR UHDG WKHVH ILHOGV YDOXHV ZLWKRXW OHWWLQJ WKH FOLHQW EUHDN WKH REMHFW V HQFDSVXODWLRQ" 6HFWLRQ 0RUH ,QVWDQFH 0HWKRGV +RZ GR \RX ZULWH D FODVV ZKRVH REMHFWV FDQ EH HDVLO\ SULQWHG RQ WKH FRQVROH" :ULWH D PRGLILHG YHUVLRQ RI WKH toString PHWKRG RQ WKH 3RLQW FODVV VXFK WKDW LWV WR6WULQJ RXWSXW PDWFKHV WKH IRUPDW RI WKH WR6WULQJ IURP -DYD V 3RLQW REMHFWV +HUH LV DQ H[DPSOH java.awt.Point[x=3,y=-8] 469 6HFWLRQ 7KH WKLV .H\ZRUG :KDW LV WKH PHDQLQJ RI WKH this NH\ZRUG" +RZ LV LW XVHG ZLWK GDWD ILHOGV DQG PHWKRGV" $GG D FRQVWUXFWRU WR WKH 3RLQW FODVV WKDW DFFHSWV DQRWKHU 3RLQW DV D SDUDPHWHU DQG LQLWLDOL]HV WKLV QHZ 3RLQW WR KDYH WKH VDPH [ \ YDOXHV :K\ GRHVQ W WKH RSHUDWRU ZRUN FRUUHFWO\ ZLWK REMHFWV" +RZ GR \RX VSHFLI\ D ZD\ WR FRPSDUH REMHFWV RI \RXU FODVV IRU HTXDOLW\" 6HFWLRQ &DVH 6WXG\ 'HVLJQLQJ D 6WRFN &ODVV $GG WKH IROORZLQJ PHWKRGV WR WKH 6WRFN FODVV public String getSymbol() 5HWXUQV WKLV 6WRFN V V\PERO YDOXH VXFK DV $0=1 public int getShares() 5HWXUQV WKLV 6WRFN V QXPEHU RI VKDUHV YDOXH public double getTotalCost() 5HWXUQV WKLV 6WRFN V WRWDO FRVW YDOXH ([HUFLVHV $GG WKH IROORZLQJ PHWKRG WR \RXU 3RLQW FODVV public double getDistance(Point other) 5HWXUQV WKH GLVWDQFH EHWZHHQ WKH FXUUHQW 3RLQW REMHFW DQG WKH JLYHQ RWKHU 3RLQW REMHFW 7KH GLVWDQFH EHWZHHQ WZR SRLQWV LV HTXDO WR WKH VTXDUH URRW RI WKH VTXDUHV RI WKH GLIIHUHQFHV RI [ DQG \ FRRUGLQDWHV ,Q RWKHU ZRUGV WKH GLVWDQFH EHWZHHQ WZR SRLQWV [ \ DQG [ \ FDQ EH H[SUHVVHG DV WKH VTXDUH URRW RI [ [ FRRUGLQDWHV VKRXOG UHWXUQ D GLVWDQFH RI \ \ 7ZR SRLQWV ZLWK WKH VDPH [ \ $GG WKH IROORZLQJ PHWKRG WR \RXU 3RLQW FODVV public int getManhattanDistance(Point other) 5HWXUQV WKH 0DQKDWWDQ GLVWDQFH EHWZHHQ WKH FXUUHQW 3RLQW REMHFW DQG WKH JLYHQ RWKHU 3RLQW REMHFW 7KH 0DQKDWWDQ GLVWDQFH UHIHUV WR KRZ IDU DSDUW WZR SODFHV DUH LI WKH SHUVRQ FDQ RQO\ WUDYHO VWUDLJKW KRUL]RQWDOO\ RU YHUWLFDOO\ DV WKRXJK GULYLQJ RQ WKH VWUHHWV RI 0DQKDWWDQ ,Q RXU FDVH WKH 0DQKDWWDQ GLVWDQFH LV WKH VXP RI WKH DEVROXWH YDOXHV RI WKH GLIIHUHQFHV LQ WKHLU FRRUGLQDWHV LQ RWKHU ZRUGV WKH GLIIHUHQFH LQ [ SOXV WKH GLIIHUHQFH LQ \ EHWZHHQ WKH SRLQWV $GG WKH IROORZLQJ PHWKRG WR \RXU 3RLQW FODVV 470 public double getSlope(Point other) 5HWXUQV WKH VORSH RI WKH OLQH GUDZQ EHWZHHQ WKLV 3RLQW DQG WKH JLYHQ RWKHU 3RLQW 8VH WKH IRUPXOD \ \ [ [ WR GHWHUPLQH WKH VORSH EHWZHHQ WZR SRLQWV [ \ DQG [ \ 1RWH WKDW WKLV IRUPXOD IDLOV IRU SRLQWV ZLWK LGHQWLFDO [ FRRUGLQDWHV VR WKURZ DQ ,OOHJDO$UJXPHQW([FHSWLRQ LQ WKLV FDVH $GG WKH IROORZLQJ PHWKRG WR \RXU 3RLQW FODVV public boolean isColinear(Point p1, Point p2) 5HWXUQV ZKHWKHU WKLV 3RLQW LV FROLQHDU ZLWK WKH JLYHQ WZR RWKHU SRLQWV 3RLQWV DUH FROLQHDU LI D VWUDLJKW OLQH FDQ EH GUDZQ WKDW FRQQHFWV WKHP 7ZR EDVLF H[DPSOHV DUH WKUHH SRLQWV WKDW KDYH WKH VDPH [ RU \ FRRUGLQDWH 7KH PRUH JHQHUDO FDVH FDQ EH GHWHUPLQHG E\ FDOFXODWLQJ WKH VORSH RI WKH OLQH EHWZHHQ HDFK SDLU RI SRLQWV DQG VHHLQJ WKDW WKLV VORSH LV WKH VDPH IRU DOO SDLUV RI SRLQWV 8VH WKH IRUPXOD \ \ [ [ WR GHWHUPLQH WKH VORSH EHWZHHQ WZR SRLQWV [ \ DQG [ \ 1RWH WKDW WKLV IRUPXOD IDLOV IRU SRLQWV ZLWK LGHQWLFDO [ FRRUGLQDWHV VR WKLV ZLOO KDYH WR EH VSHFLDO FDVHG LQ \RXU FRGH 6LQFH -DYD V GRXEOH W\SH LV LPSUHFLVH URXQG DOO VORSH YDOXHV WR D UHDVRQDEOH DFFXUDF\ VXFK DV GLJLWV SDVW WKH GHFLPDO SRLQW EHIRUH \RX FRPSDUH WKHP $GG WKH IROORZLQJ VWDWLF PHWKRG WR \RXU 3RLQW FODVV public static Point parsePoint(String str) ([DPLQHV WKH JLYHQ 6WULQJ DQG FRQYHUWV LW LQWR DQ DSSURSULDWH 3RLQW REMHFW ZKLFK LV UHWXUQHG )RU H[DPSOH Point.parsePoint("(-2, 3)") VKRXOG UHWXUQ D QHZ 3RLQW ZLWK [ YDOXH RI DQG \ YDOXH RI ,W VKRXOG DOZD\V EH WKH FDVH IRU DQ\ 3RLQW S WKDW Point.parsePoint(p.toString()).equals(p) ,I \RX KDYH UHZULWWHQ \RXU WR6WULQJ PHWKRG WR PDWFK WKDW RI -DYD V 3RLQW FODVV PDNH \RXU SDUVH3RLQW PHWKRG XVH WKDW IRUPDW )RU H[DPSOH Point.parsePoint("java.awt.Point[x=-2, y=3]") VKRXOG UHWXUQ D QHZ 3RLQW ZLWK [ YDOXH RI DQG \ YDOXH RI $GG WKH IROORZLQJ PHWKRG WR WKH 6WRFN FODVV SURYLGHG public void clear() 5HVHWV WKLV 6WRFN V QXPEHU RI VKDUHV SXUFKDVHG DQG WRWDO FRVW WR $GG WKH IROORZLQJ PHWKRG WR WKH 6WRFN FODVV SURYLGHG public String toString() 5HWXUQV D 6WULQJ UHSUHVHQWDWLRQ RI WKLV 6WRFN REMHFW VXFK DV "AMZN (75 shares, $2716.0 total cost)" $GG WKH IROORZLQJ PHWKRG WR WKH 6WRFN FODVV SURYLGHG public boolean equals(Object o) 5HWXUQV WUXH LI WKH JLYHQ REMHFW R LV D 6WRFN REMHFW ZLWK WKH VDPH V\PERO QXPEHU RI VKDUHV SXUFKDVHG DQG WRWDO FRVW DV WKLV RQH :ULWH D FODVV QDPHG Line WKDW UHSUHVHQWV D OLQH VHJPHQW EHWZHHQ WZR 3RLQWV <RXU /LQH REMHFWV VKRXOG KDYH WKH IROORZLQJ PHWKRGV 471 public Line(Point p1, Point p2) &RQVWUXFWV D QHZ /LQH WKDW FRQWDLQV WKH JLYHQ WZR 3RLQWV public Point getP1() 5HWXUQV WKLV /LQH V ILUVW HQGSRLQW public Point getP2() 5HWXUQV WKLV /LQH V VHFRQG HQGSRLQW public String toString() 5HWXUQV D 6WULQJ UHSUHVHQWDWLRQ RI WKLV /LQH VXFK DV > $GG WKH IROORZLQJ PHWKRG WR \RXU /LQH FODVV public double getSlope() @ 5HWXUQV WKH VORSH RI WKLV /LQH 7KH VORSH RI D OLQH EHWZHHQ SRLQWV [ \ DQG [ \ LV HTXDO WR \ \ [ [ ,I [ [ WKH GHQRPLQDWRU LV ]HUR DQG WKH VORSH LV XQGHILQHG VR \RX PD\ WKURZ DQ ([FHSWLRQ LQ WKLV FDVH $GG WKH IROORZLQJ FRQVWUXFWRU WR \RXU /LQH FODVV public Line(int x1, int y1, int x2, int y2) &RQVWUXFWV D QHZ /LQH WKDW FRQWDLQV WKH JLYHQ WZR 3RLQWV $GG WKH IROORZLQJ PHWKRG WR \RXU /LQH FODVV public boolean equals(Object o) 5HWXUQV ZKHWKHU WKH JLYHQ RWKHU REMHFW R LV D /LQH ZLWK WKH VDPH HQGSRLQWV DV WKLV /LQH $GG WKH IROORZLQJ PHWKRG WR \RXU /LQH FODVV public boolean isColinear(Point p) 5HWXUQV WUXH LI WKH JLYHQ SRLQW LV FROLQHDU ZLWK WKH SRLQWV RI WKLV /LQH WKDW LV LI WKLV /LQH VWUHWFKHG LQILQLWHO\ ZRXOG LW KLW WKH JLYHQ 3RLQW" 3RLQWV DUH FROLQHDU LI D VWUDLJKW OLQH FDQ EH GUDZQ WKDW FRQQHFWV WKHP 7ZR EDVLF H[DPSOHV DUH WKUHH SRLQWV WKDW KDYH WKH VDPH [ RU \ FRRUGLQDWH 7KH PRUH JHQHUDO FDVH FDQ EH GHWHUPLQHG E\ FDOFXODWLQJ WKH VORSH RI WKH OLQH EHWZHHQ HDFK SDLU RI SRLQWV DQG VHHLQJ WKDW WKLV VORSH LV WKH VDPH IRU DOO SDLUV RI SRLQWV 8VH WKH IRUPXOD \ \ [ [ WR GHWHUPLQH WKH VORSH EHWZHHQ WZR SRLQWV [ \ DQG [ \ 1RWH WKDW WKLV IRUPXOD IDLOV IRU SRLQWV ZLWK LGHQWLFDO [ FRRUGLQDWHV VR WKLV ZLOO KDYH WR EH VSHFLDO FDVHG LQ \RXU FRGH 6LQFH -DYD V GRXEOH W\SH LV LPSUHFLVH URXQG DOO VORSH YDOXHV WR D UHDVRQDEOH DFFXUDF\ VXFK DV GLJLWV SDVW WKH GHFLPDO SRLQW EHIRUH \RX FRPSDUH WKHP :ULWH D FODVV QDPHG Rectangle WKDW UHSUHVHQWV D UHFWDQJXODU 5HFWDQJOH REMHFWV VKRXOG KDYH WKH IROORZLQJ PHWKRGV public Rectangle(int x, int y, int width, int height) GLPHQVLRQDO UHJLRQ <RXU &RQVWUXFWV D QHZ 5HFWDQJOH ZKRVH WRS OHIW FRUQHU LV VSHFLILHG E\ WKH JLYHQ FRRUGLQDWHV DQG ZLWK WKH JLYHQ ZLGWK DQG KHLJKW 7KURZ DQ ,OOHJDO$UJXPHQW([FHSWLRQ RQ D QHJDWLYH ZLGWK RU KHLJKW 472 public int getHeight() 5HWXUQV WKLV 5HFWDQJOH V KHLJKW public int getWidth() 5HWXUQV WKLV 5HFWDQJOH V ZLGWK public int getX() 5HWXUQV WKLV 5HFWDQJOH V [ FRRUGLQDWH public int getY() 5HWXUQV WKLV 5HFWDQJOH V \ FRRUGLQDWH public String toString() 5HWXUQV D 5HFWDQJOH>[ \ 6WULQJ ZLGWK UHSUHVHQWDWLRQ KHLJKW @ RI WKLV 5HFWDQJOH VXFK DV $GG WKH IROORZLQJ PHWKRG WR \RXU 5HFWDQJOH FODVV public boolean equals(Object o) 5HWXUQV ZKHWKHU WKH JLYHQ RWKHU REMHFW R LV D 5HFWDQJOH ZLWK WKH VDPH [ \ FRRUGLQDWHV ZLGWK DQG KHLJKW DV WKLV 5HFWDQJOH $GG WKH IROORZLQJ FRQVWUXFWRU WR \RXU 5HFWDQJOH FODVV public Rectangle(Point p, int width, int height) &RQVWUXFWV D QHZ 5HFWDQJOH ZKRVH WRS OHIW FRUQHU LV VSHFLILHG E\ WKH JLYHQ 3RLQW DQG ZLWK WKH JLYHQ ZLGWK DQG KHLJKW $GG WKH IROORZLQJ PHWKRGV WR \RXU 5HFWDQJOH FODVV public boolean contains(int x, int y) public boolean contains(Point p) 5HWXUQV ZKHWKHU WKH JLYHQ 3RLQW RU FRRUGLQDWHV OLH LQVLGH WKH ERXQGV RI WKLV 5HFWDQJOH $GG WKH IROORZLQJ PHWKRG WR \RXU 5HFWDQJOH FODVV public Rectangle union(Rectangle rect) 5HWXUQV D QHZ 5HFWDQJOH WKDW UHSUHVHQWV WKH DUHD RFFXSLHG E\ WKH WLJKWHVW ERXQGLQJ ER[ WKDW FRQWDLQV ERWK WKLV UHFWDQJOH DQG WKH JLYHQ RWKHU UHFWDQJOH $GG WKH IROORZLQJ PHWKRG WR \RXU 5HFWDQJOH FODVV public Rectangle intersection(Rectangle rect) 5HWXUQV D QHZ 5HFWDQJOH WKDW UHSUHVHQWV WKH ODUJHVW UHFWDQJXODU UHJLRQ FRPSOHWHO\ FRQWDLQHG ZLWKLQ ERWK WKLV UHFWDQJOH DQG WKH JLYHQ RWKHU UHFWDQJOH ,I WKH UHFWDQJOHV GR QRW LQWHUVHFW DW DOO UHWXUQV D UHFWDQJOH ZKRVH ZLGWK DQG KHLJKW DUH ERWK 3URJUDPPLQJ 3URMHFWV :ULWH D FODVV QDPHG RationalNumber WKDW UHSUHVHQWV D IUDFWLRQ ZLWK DQ LQWHJHU QXPHUDWRU DQG GHQRPLQDWRU $ 5DWLRQDO1XPEHU REMHFW VKRXOG KDYH WKH IROORZLQJ PHWKRGV 473 public RationalNumber(int numerator, int denominator) &RQVWUXFWV D QHZ UDWLRQDO QXPEHU WR UHSUHVHQW WKH UDWLR QXPHUDWRU GHQRPLQDWRU 7KH GHQRPLQDWRU FDQQRW EH VR WKURZ DQ ,OOHJDO$UJXPHQW([FHSWLRQ LI LV SDVVHG public RationalNumber() &RQVWUXFWV D QHZ UDWLRQDO QXPEHU WR UHSUHVHQW WKH UDWLR public boolean equals(Object o) 5HWXUQV ZKHWKHU WKH JLYHQ REMHFW R LV D 5DWLRQDO1XPEHU REMHFW ZLWK HTXDO YDOXH WR WKLV RQH 7ZR UDWLRQDO QXPEHUV QHHG QRW KDYH LGHQWLFDO QXPHUDWRU DQG GHQRPLQDWRU WR EH FRQVLGHUHG HTXDO UDWLRV VXFK DV DQG KDYH HTXDO YDOXH VR WKH\ WRR VKRXOG EH FRQVLGHUHG HTXDO public int getDenominator() 5HWXUQV WKLV UDWLRQDO QXPEHU V GHQRPLQDWRU YDOXH IRU H[DPSOH LI WKH UDWLR LV UHWXUQV public int getNumerator() 5HWXUQV WKLV UDWLRQDO QXPEHU V QXPHUDWRU YDOXH IRU H[DPSOH LI WKH UDWLR LV public String toString() UHWXUQV 5HWXUQV D 6WULQJ UHSUHVHQWDWLRQ RI WKLV UDWLRQDO QXPEHU VXFK DV :ULWH D FODVV QDPHG Date WKDW UHSUHVHQWV D GDWH FRQVLVWLQJ RI D \HDU PRQWK DQG GD\ $ 'DWH REMHFW VKRXOG KDYH WKH IROORZLQJ PHWKRGV public Date(int year, int month, int day) &RQVWUXFWV D QHZ 'DWH WR UHSUHVHQW WKH JLYHQ GDWH public void addDays(int days) 0RYHV WKLV 'DWH REMHFW IRUZDUG LQ WLPH E\ WKH JLYHQ QXPEHU RI GD\V public void addWeeks(int weeks) 0RYHV WKLV 'DWH REMHFW IRUZDUG LQ WLPH E\ WKH JLYHQ QXPEHU RI public int daysTo(Date other) GD\ ZHHNV 5HWXUQV WKH QXPEHU RI GD\V WKDW WKLV 'DWH PXVW EH DGMXVWHG WR PDNH LW HTXDO WR WKH JLYHQ RWKHU 'DWH public boolean equals(Object o) 5HWXUQV ZKHWKHU WZR 'DWH REMHFWV UHSUHVHQW WKH VDPH FDOHQGDU GDWH public int getDay() 5HWXUQV WKLV 'DWH V GD\ YDOXH IRU H[DPSOH IRU WKH GDWH public int getMonth() UHWXUQV UHWXUQV UHWXUQV 5HWXUQV WKLV 'DWH V PRQWK YDOXH IRU H[DPSOH IRU WKH GDWH public int getYear() 5HWXUQV WKLV 'DWH V \HDU YDOXH IRU H[DPSOH IRU WKH GDWH 474 public String toString() 5HWXUQV D 6WULQJ UHSUHVHQWDWLRQ RI WKLV 'DWH LQ <HDU 0RQWK 'D\ RUGHU VXFK DV :ULWH D FODVV QDPHG GroceryList WKDW UHSUHVHQWV D SHUVRQ V OLVW RI LWHPV WR EX\ IURP WKH PDUNHW DQG DQRWKHU FODVV QDPHG GroceryItemOrder WKDW UHSUHVHQWV D UHTXHVW WR SXUFKDVH D SDUWLFXODU LWHP LQ D JLYHQ TXDQWLW\ H[DPSOH ER[HV RI FRRNLHV 7KH *URFHU\/LVW FODVV VKRXOG XVH DQ DUUD\ ILHOG WR VWRUH WKH JURFHU\ LWHPV DV ZHOO DV NHHSLQJ WUDFN RI LWV VL]H QXPEHU RI LWHPV LQ WKH OLVW VR IDU $VVXPH WKDW D JURFHU\ OLVW ZLOO KDYH QR PRUH WKDQ LWHPV $ *URFHU\/LVW REMHFW VKRXOG KDYH WKH IROORZLQJ PHWKRGV public GroceryList() &RQVWUXFWV D QHZ HPSW\ JURFHU\ OLVW public void add(GroceryItemOrder item) $GGV WKH JLYHQ LWHP RUGHU WR WKLV OLVW LI WKH OLVW LV QRW IXOO KDV IHZHU WKDQ public double getTotalCost() LWHPV 5HWXUQV WKH WRWDO VXP FRVW RI DOO JURFHU\ LWHP RUGHUV LQ WKLV OLVW 7KH *URFHU\,WHP2UGHU FODVV VKRXOG VWRUH DQ LWHP TXDQWLW\ DQG SULFH SHU XQLW *URFHU\,WHP2UGHU REMHFW VKRXOG KDYH WKH IROORZLQJ PHWKRGV public GroceryItemOrder(String name, int quantity, double pricePerUnit) $ &RQVWUXFWV DQ LWHP RUGHU WR SXUFKDVH WKH LWHP ZLWK WKH JLYHQ QDPH LQ WKH JLYHQ TXDQWLW\ ZKLFK FRVWV WKH JLYHQ SULFH SHU XQLW public double getCost() 5HWXUQV WKH WRWDO FRVW RI WKLV LWHP LQ LWV JLYHQ TXDQWLW\ )RU H[DPSOH WKDW DUH SHU XQLW KDYH D FRVW RI public void setQuantity(int quantity) ER[HV RI FRRNLHV 6HWV WKLV JURFHU\ LWHP V TXDQWLW\ WR EH WKH JLYHQ YDOXH 6WXDUW 5HJHV 0DUW\ 6WHSS 475 476 ...
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