Download - Chapter 12 Projects
8/16/2019 Chapter 12 Projects
http://slidepdf.com/reader/full/chapter-12-projects 1/20
Chapter 12 Projects
1. [after §12.2] Savings Accounts *:
Write a program that esta!ishes t"o savings accounts "ith saver1 having account numer 1###2"ith an initia! a!ance of $2%###% an& saver2 having account 1###' "ith an initia! a!ance of
$'%###. Set a common rate of interest at () per ear. At the en& of each month% up&ate the a!ance a&&ing one month+s interest to the a!ance% so that the a!ance compoun&s month!. Print an outputthat &isp!as the month numer an& the account numer an& the a!ance for each of the t"o accounts.,se month # to &isp!a the initia! a!ances% month 1 to &isp!a the a!ances after the first month+sinterest% an& so on. At the en& of the ear% &isp!a the tota! a!ance for oth accounts comine&% !i-ethis:
utput:
Monthly balances for one year with 0.05 annual interest:
Month Account # Balance Account # Balance----- --------- ------- --------- ------- 0 10002 2000.00 10003 3000.00 1 10002 2008.33 10003 3012.50 2 10002 201.!0 10003 3025.05 3 10002 2025.10 10003 303!. " 10002 2033.5" 10003 3050.31 5 10002 20"2.02 10003 303.02 10002 2050.52 10003 30!5.! ! 10002 205.0! 10003 3088.0 8 10002 20!.5 10003 3101."! 10002 20!.2 10003 311".3 10 10002 208".1 10003 312!.3! 11 10002 203.0 10003 31"0."0 12 10002 2102.32 10003 3153."
$inal balance of both accounts co%bine&: 5255.81
,se t"o c!asses% a 'avin(sAccount c!ass an& a 'avin(sAccount)river c!ass.
/n the 'avin(sAccount c!ass% &ec!are a c!ass varia!e ca!!e& annual*nterest+ate% an
instance constant ca!!e& A,,//MB+% an& an instance varia!e ca!!e& balance. Provi&e a
t"o0parameter constructor to initia!ie the instance constant an& instance varia!e% an& provi&eaccessors for the instance constant an& instance varia!e. Provi&e an a&&Monthly*nterest
metho& to up&ate the a!ance% a&&ing balance annual*nterest+ate 4 123 to the
current a!ance. A!so provi&e a c!ass metho& that sets the annua! interest rate.
/n the 'avin(sAccount)river c!ass% instantiate the saver1 an& saver2 ojects. Set the
annua! interest rate at #.#(. Print the ta!e hea&ing !ines. ,se a for !oop to print the initia! account
numers an& a!ances an& the account numers an& a!ances each month after that month+s interesthas een a&&e&. After the !ast month+s printout% compute an& &isp!a the tota! of oth a!ances.
8/16/2019 Chapter 12 Projects
http://slidepdf.com/reader/full/chapter-12-projects 2/20
2. [after §12.4] Statistics 5unctions ***:
Statisticians use various proai!it &istriutions an& specia! statistica! functions. Statistics te6ts often present these functions in ta!es in their appen&ices. 7here are a!so soft"are pac-ages that containthese functions. 8o"ever% if a program ou+re "riting nee&s to use these functions% the ta!es in the
ac-s of te6too-s an& other peop!e+s soft"are pac-ages are not "hat ou "ant. 9ou "ant co&e forthose functions in our program
7his project+s &escription gives ou a!gorithms for severa! popu!ar statistica! functions. 7he fo!!o"ing,;< c!ass &iagram sho"s ho" the are re!ate&.
8/16/2019 Chapter 12 Projects
http://slidepdf.com/reader/full/chapter-12-projects 3/20
Gamma
=/;A> : int ? 1##=@ : &ou!e ? 1.#e01#
=!ogBammaa : &ou!e3 : &ou!e
=gammaa : &ou!e3 : &ou!e=gammaa : &ou!e% 6 : &ou!e3 : &ou!e=factoria!n : int3 : &ou!e=mainargs : String[]3 : voi&
IncompleteGamma
=incomp!eteBammaa : &ou!e% t : &ou!e3 : &ou!e0seriesa : &ou!e% t : &ou!e3 : &ou!e0continuous5ractiona : &ou!e% t : &ou!e3 : &ou!e=cumu!ativePoissonm : int% 6 : &ou!e3 : &ou!e=cumu!ativeorma!6 : &ou!e3 : &ou!e=chiSDuarechiSDuare& : &ou!e% &egrees5ree&om : int3 : &ou!e=mainargs : String[]3 : voi&
IncompleteBeta
=incomp!eteEetaa : &ou!e% : &ou!e% 6 : &ou!e3 : &ou!e0continuous5ractiona : &ou!e% : &ou!e% 6 : &ou!e3 : &ou!e=stu&ent7&eg5ree&om : int% t : &ou!e3 : &ou!e=mainargs : String[]3 : voi&
Beta
=etaa : &ou!e% : &ou!e3 : &ou!e=etaa : &ou!e% : &ou!e% 6 : &ou!e3 : &ou!e=!ogEetaa : &ou!e% : &ou!e3 : &ou!e=mainargs : String[]3 : voi&
Binomial
=cominationsn : int% - : int3 : &ou!e=coefficientsn : int3 : &ou!e[]=proai!itn : int% - : int% p &ou!e3 : &ou!e=cumu!ativeProai!itn : int% - : int% p : &ou!e3 : &ou!e=mainargs : String[]3 : voi&
8/16/2019 Chapter 12 Projects
http://slidepdf.com/reader/full/chapter-12-projects 4/20
As in Fava+s Math c!ass% everthing shou!& e static. Bive each c!ass its o"n %ain &river% to
&emonstrate the functions in that c!ass.
a) a%%a c!ass
Provi&e /;A> an& @ constants as common va!ues for t"o &erive& c!asses G the
*nco%6letea%%a c!ass an& the *nco%6leteBeta c!ass.
/n effect% the gamma function returns the Hfactoria!I of a rea! numer. 7he lo(a%%a metho& is
use& not on! for the gamma function itse!f ut a!so for man other statistica! functions. Weusua!! compute the natura! !ogarithm of gamma instea& of gamma% ecause gamma itse!f caneasi! overf!o"% an& most app!ications use ratios of gamma% an& the ratios are tpica!! muchsma!!er than the in&ivi&ua! gamma va!ues. 7he a!gorithm for lo(a%%a is a HmagicI recipe
invente& C. <ancos% an& "e+!! just give it to ou.
5irst% &efine an arra of J constants:coef7 9 !.18001!3; -8.50532033; 2".01"0822; -1.231!351; 0.120858003e-2; -0.53382e-5<
7hen% provi&e an if statement that terminates e6ecution if the parameter% a% is negative. 7hen &o
this:n 9 a = 1te%6 9 n > 5.5te%6 9 te%6 = ?n > 0.5@ lo(e?te%6@su% 1for all coef n 9 n > 1 su% 9 coef 4 nreturn =te%6 > lo(e?2.50282!"5 su%@
/n the (a%%a metho&% just e6ponentiate lo(a%%a. When the argument% a% is an integer%
(a%%a shou!& return a va!ue that is eDua! to the factoria! of a - 13. When a is not integer%
(a%%a shou!& return a va!ue that varies continuous! et"een the va!ues returne& the a&jacent
integers. /n other "or&s% (a%%aa3% represente& in mathematica! formu!as as Ka3% is a f!oating
point version of factoria!a - 13% or a - 13. 8o" aout that
7he gamma &istriution function% La%3% generate& the t"o0argument (a%%a metho&% shou!&
simp! &isp!a the formu!a:
La%t3 ? a-13 e-t M Ka3
7his gives the proai!it of ta-ing a time some"here in the range et"een t G N an& t = N to
comp!ete a tas-. /n genera!% the tas- is compose& of severa! ran&om0!ength sutas-s% "hich must e &one seDuentia!! G not in para!!e!. 7he parameter% a% appro6imate! represents the numer of
in&epen&ent sutas-s in the overa!! tas-.
8ere is "hat the gamma &istriution function !oo-s !i-e for various va!ues of a:
8/16/2019 Chapter 12 Projects
http://slidepdf.com/reader/full/chapter-12-projects 5/20
7he ascissa is t% an& the or&inate is the va!ue of La% t3. 7he !ac- curve is for a ? #.(. 7he
magenta curve is for a ? 1.#. 7he e!!o" curve is for a ? 2.#. 7he turDuoise curve is for a ? 4.#.When a ? 1.#% there is e6act! one sutas-% an& the gamma &istriution ecomes the simp!e
e6ponentia! &istriution. 7he area un&er each of these curves is 1.#.
/n the factorial metho&% use t"o a!ternative a!gorithms. /f n O? 2#% fi!! the static arra from
nCi ? # up to the current! nee&e& factoria! mu!tip!ing the previous arra e!ement the
ne6t !ong integer. /f n 2#% lon( overf!o"s% an& ou must use e6p!ogBamman > 133.
/n the %ain metho&% as- the user to enter a an& print gammaa3 an& factoria!a - 13. 7hen as-
the user to enter t an& print gammaa%t3. 7he Ka3 va!ues shou!& e the same as a - 13 "hen a
is an integer% an& ou can chec- our gammaa3 metho& comparing its resu!ts "ith han&0
ca!cu!ate& va!ues of the factoria!. 9ou can chec- our gammaa%t3 metho& for a ? #.(% 1.#% 2.#%an& 4.#% comparing its outputs at various t "ith the curves &isp!ae& aove.
b) *nco%6letea%%a c!ass
7he incomp!ete gamma function% QKa%t3% gives the proai!it of comp!eting a seDuentia!
in&epen&ent ran&om processes in some time !ess than t. 7his is the area un&er one of the gamma
curves !i-e those sho"n aove3 to the !eft of some particu!ar time% t. So% for an va!ue of a% the
incomp!ete gamma function rises monotonica!! from # to 1.# as t increases from # to =infinit.
7he a!gorithm for computing the incomp!ete gamma function is another magic recipe.1 /n theinco%6letea%%a metho&% print a &iagnostic message an& terminate e6ecution if a D # or if
t D #. /f t D a > 1% ca!! the series he!per metho& an& return the va!ue the he!per returns.ther"ise% ca!! the continuous$raction he!per metho&% an& return 1.# minus the va!ue the
he!per returns.
/n the series metho&% initia!ie: aElus 9 a% ter% 9 14a% an& su% 9 ter%. 7hen
imp!ement this a!gorithm: for i fro% 0 to *MAF
1 Ease& on a!gorithms in Section J.2 in Press% et. a!.% Numerical Recipes in C % Camri&ge ,niversit Press 1RR13.
a ? #.(
a ? 1.#
a ? 2.#a ? 4.#
8/16/2019 Chapter 12 Projects
http://slidepdf.com/reader/full/chapter-12-projects 6/20
aElus>> ter% 9 ter% t 4 aElus su% 9 su% > ter% if ?abs?ter% 4 su%@ D +++@ breaG
After the for !oop% if i H *MAF% print a message in&icating not enough iterations. 7hen return
the va!ue:
su% e6?-t > a lo(e?t@ - lo(a%%a?a@@ otice ho" ou "i!! norma!ie the ans"er sutracting Ka3 in the e6ponent. 7his "i!! uti!ie a
metho& inherite& from the a%%a c!ass.
/n the continuous$raction metho&% initia!ie:
fac 9 1.0a0 9 1.0Ib0 9 0.0Ia1 9 tIb1 9 1.0I(ol& 9 0.0I
( 9 0.0.
7hen imp!ement this a!gorithm:for i fro% 0 to *MAF i61 9 i > 1 i61%a 9 i61 - a a0 9 ?a1 > a0 i61%a@ fac b0 9 ?b1 > b0 i61%a@ fac i61f 9 i61 fac a1 9 t a0 > i61f a1 b1 9 t b0 > i61f b1 if a1 H 1.0 44 renor%aliJe fac 9 1.0 4 a1 ( 9 b1 fac
if abs??( - (ol&@ 4 (@ D +++ breaG else (ol& 9 (
After the for !oop% if i H *MAF% print a message in&icating not enough iterations. 7hen return
the va!ue:( Math.e6?-t > a Math.lo(?t@ - lo(a%%a?a@@
Again% notice ho" ou "i!! norma!ie sutracting Ka3 in the e6ponent.
7he cu%ulativeEoisson metho& is suppose& to generate the proai!it of having no more
than HmI events in a time interva! in "hich the e6pecte& numer of events is . 7his metho&
shou!& return the va!ue generate& :
1.0 - inco%6letea%%a?% > 1; @
7he cu%ulative/or%al metho& is suppose& to generate the area un&er a norma!ie&
Baussian from Ginfinit up to an aritrar input va!ue% 6. 5or this metho&% first compute:value 9 inco%6letea%%a?0.5; 4 2.0@
7hen% if H # return:0.5 > 0.5 value
ther"ise% return:
8/16/2019 Chapter 12 Projects
http://slidepdf.com/reader/full/chapter-12-projects 7/20
0.5 - 0.5 value
Assume some ran&om process can e characterie& a particu!ar &istriution curve% !i-e one ofthe gamma &istriution curves sho"n aove. 7hen% "ith vertica! !ines% partition that assume&curve into bins% an& fin& the fraction of the tota! area un&er the curve in each in minus the area et"een the a&jacent pair of vertica! !ines that oun& that in. 7hat area is the fraction of a!!
e6perimenta! samp!es that shou!& fa!! in that in% if the assume& theoretica! &istriution is correct.Chi Square is the sum over a!! ins of the sDuare of the &ifference et"een the oserve& numerof oservations in a in an& the theoretica!! e6pecte& numer of oservations for that in% "itheach sDuare& &ifference &ivi&e& the theoretica!! e6pecte& numer for that in. A !arge va!ueof Chi SDuare sas the assume& &istriution &oes not fit the &ata ver "e!!. 7he Duestion is% ho"!arge is too !argeT 5or the ans"er% a user shou!& input the ca!cu!ate& Chi SDuare va!ue% a!ong "ith&egrees of free&om numer of ins minus numer of parameters a&juste& to fit the &istriutionfunction to the &ata3 into the chi'Kuare metho&. 7he proai!it returne& the chi'Kuare
metho& is the proai!it that a correct! assume& &istriution cou!& resu!t in a Chi SDuare va!ueat !east as !arge as the one input. /f the va!ue returne& is greater than #.1% the assume& &istriutionis e!ieva!e. /f it is !ess than #.##1% the assume& &istriution is suspect.
Eegin the chi'Kuare metho& printing a &iagnostic message an& terminating e6ecution if theinput Chi SDuare va!ue is !ess than ero or the &egrees of free&om is !ess than one. /f the input isU% return the va!ue given :
1.0 - inco%6letea%%a?0.5 &e(rees$ree&o%; 0.5 chi'Kuare@
/n the %ain metho&% as- the user to se!ect among the a!ternatives: incomp!eteBammag3%
cumu!ativePoissonp3% norma!n3% chiSDuare&63. 5or the Vg+ case% as- the user to enter va!ues forHaI an& HtI an& print the va!ue returne& the inco%6letea%%a metho&. 5or the Vp+ case%
as- the user to input the ma6imum numer of events an& the e6pecte& numer of events% an& printthe va!ue returne& the cu%ulativeEoisson metho&. 5or the Vn+ case% as- the user to enter
a va!ue for the norma!ie& Baussian ran&om varia!e% % an& print the va!ue returne& the
cu%ulative/or%al metho&. 5or the V6+ case% as- the user to enter va!ues for Chi SDuare an&
&egrees of free&om% an& print the va!ue returne& the chi'Kuare metho&.
8/16/2019 Chapter 12 Projects
http://slidepdf.com/reader/full/chapter-12-projects 8/20
c) Beta c!ass
7he eta function is a%b3 ? Ka3 Kb3 M Ka > b3. 7he lo(Beta metho& shou!& return the
va!ue:lo(a%%a?a@ > lo(a%%a?b@ - lo(a%%a?a > b@
7he t"o0parameter beta metho& shou!& return the va!ue:
e6?lo(Beta?a; b@@.
7he eta &istriution function% Xa%b%3% is given :
Xa%b%3 ? ?a = 1.03 1.# = 3b = 1.03 M a%b3
7his &istriution function provi&es a "i&e range of usefu! shapes in the range #.# D D 1.#:
When a 1 an& b 1% it generates the uniform &istriution. When a 2 an& b 1% it generates
a !inear up0ramp. When a 1 an& b 2% it generates a !inear &o"n ramp. When a 2 an& b 2%
it generates an inverte& parao!a. When a D 1 an& b D 1% it generates a o"! "ith infinite! high
si&es. 7he areas un&er a!! of these curves are e6act! eDua! to 1.#. 7he three0parameter beta
metho& shou!& return the va!ue generate& the java e6pression:Math.e6??a - 1@ Math.lo(?@ > ?b - 1.0@ Math.lo(?1.0 - @ - lo(Beta?a; b@@
7he %ain metho& shou!& as- the user to enter va!ues for HaI an& H%I an& print the va!uereturne& the t"o0parameter beta metho&. 7hen it shou!& as- the user to enter a va!ue for H6%I
an& print the va!ue returne& the three0parameter beta metho&.
Samp!e session:
nter Beta 6ara%eters a an& b: 5.0 1.5
Beta?a;b@ 0.0!3881!38!828!!
Samp!e sessions:
'elect inco%6letea%%a?(@; cu%Eoisson?6@; cu%/or%al?n@; chi'Kuare&?@: g
nter nu%ber of seKuential ran&o% 6rocesses: 4
nter nor%aliJe& ti%e: 3
,o%6letion 6robability 0.352!81112"8503
'elect inco%6letea%%a?(@; cu%Eoisson?6@; cu%/or%al?n@; chi'Kuare&?@: pnter e6ecte& nu%ber of events: 4
nter %ai%u% nu%ber of events: 3
6robability of no %ore than 3 events is 0."33"!012028151
'elect inco%6letea%%a?(@; cu%Eoisson?6@; cu%/or%al?n@; chi'Kuare&?@: n
nter nor%aliJe& aussian ar(u%ent; : 2.0
,u%ulative /or%al 0.!!2"880"82
'elect inco%6letea%%a?(@; cu%Eoisson?6@; cu%/or%al?n@; chi'Kuare&?@: x
nter ,hi 'Kuare value: 22.307
nter &e(rees of free&o%: 15,hi-'Kuare Erobability 0.10000318838185
8/16/2019 Chapter 12 Projects
http://slidepdf.com/reader/full/chapter-12-projects 9/20
nter : 0.9
beta?a;b;@ 2.80823"11802558"
d) *nco%6leteBeta c!ass
7he incomp!ete eta function% QEa%b%3% gives the area un&er Xa%b%3 et"een #.# an& . So%
the incomp!ete eta function rises monotonica!! from # to 1.# as increases from # to 1.#.
/n the inco%6leteBeta metho&% if D # or H 1.#% print an out0of0range message% an&
terminate e6ecution. 7hen if is eDua! to either #.# or 1.#% ma-e the assignment:%ulti6lier 9 0.0
ther"ise% ma-e the assignment:%ulti6lier 9 e6?a lo(e?@ > b lo(e?1.0 - @ - lo(Beta?a; b@@
7hen if D a > 1.#3 4 a > b > 2.#3 an& a H #% return the va!ue:%ulti6lier continuous$raction?a; b; @ 4 a
ther"ise return the va!ue:1.0 - %ulti6lier continuous$raction?b; a; ?1.0 - @@ 4 b
7he continuous$raction metho& &oes the rea! "or- in the eva!uation of the incomp!ete
eta function. /t+s another HmagicI recipe. Start initia!iing severa! varia!es:a61 9 ?a > 1.0@
a%1 9 ?a - 1.0@
a6b 9 ?a > b@
aJ 9 1.0
a% 9 1.0
bJ 9 1.0 - a6b 4 a61
b% 9 1.0.
7hen imp!ement this a!gorithm:
for i fro% 1 throu(h *MAF two* 9 i > i nu%erator 9 i ?b - i@ 4 ??a%1 > two*@ ?a > two*@@ a6 9 aJ > nu%erator a% b6 9 bJ > nu%erator b% nu%erator 9 -?a > i@ ?a6b > i@ 4 ??a61 > two*@ ?a > two*@@ a66 9 a6 > nu%erator aJ b66 9 b6 > nu%erator bJ al& 9 aJ a% 9 a6 4 b66 b% 9 b6 4 b66
aJ 9 a66 4 b66 bJ 9 1.0 if abs?aJ - al&@ D +++ abs?aJ@ breaG
After the for !oop% if i H *MAF% print a message in&icating e6cessive error. An& fina!!% return
the fina! va!ue of aJ.
8/16/2019 Chapter 12 Projects
http://slidepdf.com/reader/full/chapter-12-projects 10/20
7he stu&ent metho& computes the proai!it that the true mean of a measure& phenomenon
is "ithin a range that is p!us or minus [t measure& stan&ar& &eviation3] from the measure&
mean.2 7o use this metho&% ou compute t &ivi&ing the &ifference et"een the mean of the
measurements an& some reference va!ue3 the stan&ar& &eviation &etermine& from the samemeasurements. 7hen ou ca!! the stu&ent metho& "ith this compute& va!ue of t as the first
argument% an& &egrees of free&om one !ess than the numer of measurements3 as the secon&argument. /f the proai!it returne& stu&ent is !ess than some accepta! sma!! va!ue%
!i-e #.#2% the chance that the true mean of the measure& phenomenon is on the other si&e of thereference va!ue is on! 1) ha!f of this returne& proai!it3.
/mp!ement the stu&ent metho& ma-ing the assignment: 9 &e($ree&o% 4 ?&e($ree&o% > t t@
An& then returning the va!ue returne& :inco%6leteBeta?0.5 &e($ree&o%; 0.5; @
/n the main metho&% as- the user to se!ect either incomp!eteEetab3 or stu&ent7t3. 5or case V+%
as- the user for Eeta arguments HaI an& HI% an& print the va!ue returne& the
inco%6leteBeta metho&. 5or case Vt+% as- the user for &egrees of free&om an& the stu&ent t
va!ue% an& print the va!ue returne& the stu&ent metho&.
Samp!e sessions:
'elect inco%6leteBeta?b@ or stu&ent?t@: b
nter Beta 6ara%eters a an& b: 5.0 1.5
nter : 0.9
inco%6lete beta function 0.!!1!213"30!82
'elect inco%6leteBeta?b@ or stu&ent?t@: t
nter &e(rees of free&o%: 15
nter value of stu&ent t: 1.753
two-si&e& overla6 6robability 0.1000088215"!2
e) Bino%ial c!ass
7his c!ass "i!! use the one0parameter (a%%a metho& inherite& from the a%%a c!ass an& the
inco%6leteBeta metho& inherite& from the *nco%6leteBeta c!ass.
/n the co%binations metho&% if G D # or G H n% print an outsi&e0of0range message an&
terminate the e6ecution. 7hen% imp!ement this:lo(,oef 9 lo(a%%a?n > 1@ - lo(a%%a?G > 1@ - lo(a%%a?n - G > 1@
coef 9 e6?lo(,oef@An& return the roun&e& va!ue of coef.
/n the coefficients metho&% create an arra of !ength n > 1% !oa& each e!ement% G% of this
arra "ith the va!ue returne& co%binations?n; G@% an& return a reference to the ne"
arra. 7he e!ements of this arra shou!& e eDua! to the coefficients of a inomia!.
2 See% for e6amp!e% Section 4.( in <a" an& Ue!ton% Simulation Modeling & Analysis% ;cBra"08i!! 1RY23.
8/16/2019 Chapter 12 Projects
http://slidepdf.com/reader/full/chapter-12-projects 11/20
/n the 6robability metho&% if G H n% return #.#. ther"ise% return the va!ue of this
e6pression:co%binations?n;G@ 6G ?1 - 6@?n - G@
/f the proai!it of a inar event is 6 per tria!% this returns the proai!it of that event
occurring e6act! G times in n tria!s.
/n the cu%ulativeErobability metho&% if G H n return #.#. ther"ise% return the va!ue of
this e6pression:inco%6leteBeta?G; ?n - G > 1@; 6@
Assuming the proai!it of a inar event is 6 per tria!% this returns the proai!it of that event
occurring G or more times in n tria!s.
/n the %ain metho&% as- the user to se!ect et"een coefficientsc3 or proai!itp3. 5or the Vc+
case% as- the user to enter the or&er% n. 7hen print the va!ue of each of the coefficients in the arra
returne& a ca!! to the coefficients metho&. 5or the Vp+ case% as- the user to enter the tota!
numer of tria!s% the numer of successes or minimum successes% an& the sing!e0tria! proai!it.7hen print the va!ue returne& a ca!! to the 6robability metho&% an& a!so print the va!ue
returne& a ca!! to the cu%ulativeErobability metho&.
Samp!e sessions:
'elect coefficients?c@ or 6robability?6@: c
nter or&er: 4
1.0".0.0".01.0
'elect coefficients?c@ or 6robability?6@: p
nter total nu%ber of trials: 6
nter successes or %ini%u% successes: 4
nter sin(le-trial 6robability: .5
6robability of " successes 0.23"3!56robability of at least " successes 0.3"3!50000023"213
1. [after §12.(] Car Program *:
9our unc!e is tring to -eep trac- of his ne"0car an& use&0car !ots "riting a Fava program. 8e nee&sour he!p in setting up his c!asses.
/mp!ement a superc!ass name& ,ar that contains a 6rice instance varia!e% a (etErice metho&% an&
a 10parameter constructor. 7he (etErice metho& is a simp!e accessor metho& that returns the price
instance varia!e+s va!ue. 7he 10parameter constructor receives a cost parameter an& assigns a va!ue tothe price instance varia!e ase& on this formu!a:
6rice cost 2I
8/16/2019 Chapter 12 Projects
http://slidepdf.com/reader/full/chapter-12-projects 12/20
/mp!ement t"o c!asses name& /ew,ar an& se&,ar the are oth &erive& from the ,ar superc!ass.
/ew,ar shou!& contain a color instance varia!e the car+s co!or3. se&,ar shou!& contain a
%ilea(e instance varia!e the car+s o&ometer rea&ing3. 7he /ew,ar an& se&,ar c!asses shou!&
each contain a 20parameter constructor% an eKuals metho&% an& a &is6lay metho&. /n the interest of
e!egance an& maintainai!it% &on+t forget to have our suc!ass constructors ca!! our superc!assconstructors "hen appropriate. 7he &is6lay metho& shou!& print the va!ues of a!! the instance
varia!es "ithin its c!ass.
Provi&e a &river c!ass that tests our three car c!asses. 9our &river c!ass shou!& contain this %ain
metho&:
6ublic static voi& %ain?'trin(7 ar(s@ /ew,ar new1 new /ew,ar?8000.33; LsilverL@I /ew,ar new2 new /ew,ar?8000.33; LsilverL@I if ?new1.eKuals?new2@@ new1.&is6lay?@I <
se&,ar use&1 new se&,ar?2500; 100000@I se&,ar use&2 new se&,ar?2500; 100000@I if ?use&1.eKuals?use&2@@ use&1.&is6lay?@I << 44 en& %ain
utput:
6rice 1;000.; color silver6rice 5;000.00; %ilea(e 100;000
2. [after §12.1#] Bame of 8earts ***:
7his project is a ig one% ut hopefu!! the suject "i!! e fami!iar. 9our tas- is to "rite a programthat p!as the game of 8earts. Start "ith the prototpe suggeste& in the te6t an& enhance it unti! ouget everthing in the fo!!o"ing ,;< c!ass &iagram.
8/16/2019 Chapter 12 Projects
http://slidepdf.com/reader/full/chapter-12-projects 13/20
1
1
1
*
1
1 *
1
*1
Game
=P<A9@S : int0&ec- : Zec- 08an&[] p!aers
07ric-[] tric-s0numerf7ric-s : int ? #0hearts : oo!ean ? fa!se
0DueenfSpa&es : oo!ean ? fa!se
=BamenumerfP!aers : int3=getumerf7ric-s3 : int=get8earts3 : oo!ean=getueenfSpa&es : oo!ean=p!aABame3 : voi&=up&ate8eartsAn&ueen
car& : Car&3 : voi&
0computePoints
p!aerum : int3 : int
Deck
=Zec-3=shuff!e3 : voi&=&ea!Car&3 : Car&
=77A<\CAZS : int?(2
Card
0num : int0suit : int
=Car&num : int% suit : int3=getum3 : int=getSuit3 : int=&isp!a3 : voi&
Trick
0"inner : int0"inningCar& : Car&0hearts : oo!ean ? fa!se0Dueen : oo!ean ? fa!se
=7ric-p!aers : int3
=getWinner3 : int=getWinningCar&3 : Car&=get8earts3 : oo!ean=getueen3 : oo!ean=up&atep!aerum : int% car& : Car&3 : voi&
0isWinnercar& : car&3 : oo!ean
GameDriver
=mainargs : String[]3 : voi&
Hand
=8an&p!aerum : int% numerfCar&s : int3=sort3 : voi&=setShortest3 : voi&=getShortest3 : voi&=p!aACar&game : Bame% tric- : tric-3 : Car&=fin&<o"estsuit : int3 : int0fin&Countsuit : int3 : int0fin&num : int% suit : int3 : int0fin&8ighestsuit : int3 : int0fin&<o"estgame : Bame3 : int0fin&<ast8ighsuit : int3 : int0fin&8ighestEe!o""inningCar& : Car&3 : int
0fin&;i&&!e8ighgame : Bame% suit : int3 : int0fin&8ighest3 : int
=,; : int0shortest : int ? #
0car&s : Car&[]0currentSie : int ? #
=BroupfCar&snumer : num3
=getCurrentSie3 : int=getCar&i : int3 : Car&=a&&Car&car& : Car&3 : voi&=removeCar&in&e6 : int3 : Car&=&isp!a3 : voi&
GroupOfCards
8/16/2019 Chapter 12 Projects
http://slidepdf.com/reader/full/chapter-12-projects 14/20
,ar& c!ass:
Write the ,ar& c!ass first. epresent numers an& face0car& va!ues integers ranging from 2 to
14% "ith 14 eing the ace. epresent suits integers as fo!!o"s: # ? c!us 1 ? &iamon&s 2 ?hearts ' ? spa&es. Write a nice &islay metho& that uses s"itch statements to convert suit
integers to the "or&s% Hc!us%I H&iamon&s%I an& so on% an& the numers 1114 to the "or&sHFac-%I Hueen%I an& so on. ,se the &is6lay metho& to test this c!ass.
rou6f,ar&s c!ass:
Write the rou6f,ar&s ne6t. 7he constructor+s parameter shou!& esta!ish the sie of the
car& arra. 7he (et,urrent'iJe an& (et,ar& metho&s are trivia!. 7he a&&,ar& metho&
shou!& increment current'iJe after a&&ing the input car& to the en& of the current! fi!!e&
part of the car&s arra. 7he re%ove,ar& metho& shou!& retrieve a reference to the car& at
in&e in the car&s arra% &ecrement the current'iJe of the car&s arra% shift a!! arra
e!ements aove in&e &o"n one p!ace% an& return the reference to the car& origina!! at
in&e. ,se the &is6lay metho& to test this c!ass.
)ecG c!ass:
7he )ecG c!ass+s constructor is a!rea& provi&e& in the te6t. 7o shuff!e the &ec-% use a for !oop
that starts "ith unshuffle& (et,urrent'iJe an& steps &o"n to one. /n each iteration%
use Math.ran&o% to pic- an in&e6 in the unshuff!e& range% remove the car& at that in&e6% an&
then a&& it to the high en& of the arra. 7o &ea! a car&% just remove the car& at in&e6 ? #.
ricG c!ass:
7he ricG c!ass is the ne6t easiest one to "rite. 7he constructor+s parameter is the numer of
p!aers% an& the constructor ca!!s the superc!ass+s constructor "ith one !ess than t"ice thisnumer% to a!!o" room in the first tric- for un&e!t car&s. 7he four (et0 metho&s are trivia!. 7he
isNinner metho& shou!& return true un!ess the previous "inning car& is not null an& the
current car& is not in the suit eing p!ae& or its numer is !ess than the "inning car&+s numer./n the u6&ate metho&% if the current car& is the "inner% set "inner eDua! to current p!aer+s
numer an& set the "inning car& eDua! to the current car&. /f the current car& is a heart% sethearts to true. /f the current car& is the Dueen of spa&es% set Kueen to true.
Can& c!ass:
7he Can& c!ass is the most &ifficu!t one to "rite% ecause it inc!u&es man of the ru!es an& the
strateg for p!aing the game. Since each p!aer+s i&entification shou!& remain constant after that p!aer has een instantiate&% ma-e ,; a fina! instance varia!e% an& initia!ie it in the Can&
constructor "ith a va!ue eDua! to the constructor+s first parameter va!ue. 7he secon& parameter isthe ma6imum numer of car&s the p!aer "i!! receive. ,se it for the ase0constructor ca!! argument.
,se a se!ection sort strateg for the sort metho&. Start "ith unsorte& ? current sie of the
arra% an& step &o"n to unsorte& ? 1. /n each step% iterate through the unsorte& part of the
arra% fin& the car& having the greatest va!ue of the e6pression% 1' * suit = numer3% an& move
8/16/2019 Chapter 12 Projects
http://slidepdf.com/reader/full/chapter-12-projects 15/20
this car& to the high en& of the in&e6 range. /f ou &isp!a the resu!t% ou shou!& see the car&ssorte& suit from Ace of spaces &o"n to 2 of c!us.3
,se the set'hortest metho& to &etermine the est suit to p!a ear! in the game% to esta!ish
a voi& as Duic-! as possi!e. Start "ith shortest ? c!us. /f the numer of &iamon&s is !ess
than or eDua! to the numer of c!us% change shortest to &iamon&s. /f the numer of spa&es is
!ess than or eDua! to the shorter of those t"o% an& our spa&es &o not inc!u&e Ace% Uing% orueen% change shortest to spa&es. ,se the fin& metho& to see if ou have an Ace% Uing% or
ueen.3
7he (et'hortest metho& is trivia!.
7he 6layA,ar& metho& is the most &ifficu!t one% ecause it contains man of the ru!es an& a!!
of the strateg for "inning. We &o not -no" the est "a to "rite this metho&% ecause "e arenot perfect hearts p!aers ourse!ves% so this is just a suggestion that "or-s reasona! "e!!. A!! ofthe metho&s !iste& after this one e6cept for the count metho&3 are inten&e& to e use& this
metho& to ma-e this metho& as simp!e as possi!e. <oo- at them efore tring to "rite this
metho&.
/f the current sie of the tric- is ero ou are the first han&3% !et a !oca! integer ca!!e& in&e
eDua! the highest car& in our shortest suit% ut if this suit is a voi& !et in&e6 eDua! the !o"est car&in an suit. /f the current sie of the tric- is one !ess than the tota! numer of p!aers ou are the!ast han&3% an& if the tric- &oes not have the Dueen of spa&es or an hearts% !et in&e eDua! the
va!ue returne& the fin&Ci(hest?int suit@ metho&.
e6t% &ea! "ith cases "here ou are the !ast han& an& there are no a& car&s in the tric-. Sinceman of the he!per metho&s return -1 if the fai!% ou can tighten the co&e in the 6layA,ar&
metho& using an assignment to in&e insi&e an empt e!se0if statement% !i-e this:
else if ??tricG.(et,urrent'iJe?@ (a%e.EOAP+' - 1@ QQ RtricG.(etCearts?@ QQ RtricG.(etSueen?@ QQ ?in&e fin&OastCi(h?suit@@ H 0@I
7hen% see "hat to &o if ou are a mi&&!e han& or the !ast han& "ith a& car&s a!rea& in the tric-:
else if ??in&e fin&Ci(hestBelow?winnin(,ar&@@ H 0@Ielse if ??in&e fin&Mi&&leCi(h?(a%e; suit@@ H 0@I
7hen% see "hat to &o if ou are voi&% an& a!e to &iscar& a a& car& or our o"n:
else if ??in&e fin&?12; 3@@ H 0@I 44 Kueen of '6a&es
else if ??in&e fin&?1"; 3@@ H 0@I 44 Ace of '6aceselse if ??in&e fin&?13; 3@@ H 0@I 44 Tin( of '6a&eselse if ??in&e fin&Ci(hest?2@@ H 0@I 44 heartelse in&e fin&Ci(hest?@I<
8/16/2019 Chapter 12 Projects
http://slidepdf.com/reader/full/chapter-12-projects 16/20
7hen% remove the car& "ith the resu!ting va!ue of in&e% up&ate the tric-% up&ate the game% an&
return the car& se!ecte&. ,se the fin&Oowest?int suit@ metho& to fin& the !o"est c!u &ea!t% to start the game.
9ou+!! a!so nee& it in the rare situation "hen ou have the !ea&% hearts have not een ro-en% an&hearts are a!! ou have !eft in our han&. eturn the in&e6 of the !o"est numere& car& in the
in&icate& suit. /f ou have no car&s in that suit% return -1.
5or the count metho&% return the numer of car&s in the suit in&icate& the va!ue of the
parameter.
5or the fin& metho&% return the in&e6 of the car& having suit an& numer eDua! to the parameter
va!ues. /f ou can+t fin& that car&% return -1.
,se the fin&Ci(hest?int suit@metho& to fin& the highest car& in our shortest suit to
&eve!op a voi& as fast as possi!e ear! in the game. A!so use it to se!ect the highest heart to&iscar& on someo& e!se+s suit. A!so use it "hen ou are p!aing !ast an& there are no a& car&s
in the tric-. A!so use as the starting point in the fin&Ci(hestBelow metho& an& the starting point in fin&Mi&&leCi(h metho&. eturn the in&e6 of the car& having the highest numerica!
va!ue in the suit in&icate& the parameter va!ue. /f ou have no car&s in the suit% return -1.
,se the fin&Oowest?a%e (a%e@ metho& "hen !ea&ing% after ou have &eve!ope& our
voi&. eturn the !o"est numer in our han&% ut not a heart unti! after hearts have een ro-en./f hearts have not een ro-en an& a!! ou have !eft is hearts% return -1.
,se the fin&OastCi(h metho& to return the highest car& in the suit !e& "hen there are no a&
car&s in the tric-. /f this car& is the Dueen of spa&es% ho"ever% an& ou have another spa&e% returnthe highest car& ou have e!o" our Dueen.
,se the fin&Ci(hestBelow metho& "hen ou are neither the first nor !ast p!aer in a particu!ar tric-+s p!a seDuence% an& ou are a!e to fo!!o" suit. Biven a reference to the current"inning car& as the parameter va!ue% search through the car&s in our han& "hose suit eDua!s the"inning car&+s suit unti! ou fin& the first one having a numer !ess than the "inning car&+snumer% an& return the in&e6 of that car&% ut if the ne6t car& is a &ifferent suit% terminate thesearch% an& return -1.
,se the fin&Mi&&leCi(h metho& if the fin&Ci(hestBelow metho& returne& -1. ,se
our highest car& in the suit% ut if the suit is spa&es an& the Dueen of spa&es has not een p!ae&et% tr to fin& a spa&e that is not higher than the Fac- of spa&es.
Assuming ou cannot fo!!o" suit an& ou no !onger have the ueen% Ace% or Uing of spa&es an&
no !onger have an hearts% use the fin&Ci(hest?@ metho& to &iscar& the highest remainingcar& in our han&% regar&!ess of suit.
a%e c!ass:
7he a%e c!ass is !ong ut straightfor"ar&. Since the numer of p!aers shou!& remain constant
after a game has een instantiate&% ma-e P<A9@S a fina! instance varia!e% an& initia!ie it in
the a%e constructor "ith a va!ue eDua! to the constructor+s parameter va!ue. 7his constructor
8/16/2019 Chapter 12 Projects
http://slidepdf.com/reader/full/chapter-12-projects 17/20
shou!& instantiate a Can& arra "ith constructor parameter eDua! to the numer of p!aers. /t shou!&
instantiate in&ivi&ua!Can& ojects for each p!aer% "ith p!aer i&entification numer an& ma6imum
numer of car&s in a p!aer+s han& as constructor arguments. /t shou!& a!so instantiate a ricG arra
"ith tota! numer of tric-s as the constructor argument% ut it shou!& not popu!ate this arra "ith anin&ivi&ua! tric-s.
7he (et/u%berfricGs% (etCearts% an& (etSueenf'6a&es metho&s are trivia!.
Eegin the 6layAa%e metho& shuff!ing the &ec-. @va!uate car&sOeft after a!! p!aers get
an eDua! numer of car&s e6amp!e: "ith five p!aers car&sOeft ? 23. <oop through a!! tric-s
an& a&& one &ea!t car& to each p!aer in succession. <oop through a!! p!aers an& for each p!aerca!! sort the set'hortest to sort the han& an& fin& that p!aer+s est voi& opportunit. 5or
that p!aer% print the va!ue of shortest an& &isp!a that p!aer+s han& to get an output !i-e this
Math.ran&o% "i!! ma-e the va!ues var from one e6ecution to the ne6t3:
utput G first part:
6layer 0 shortest 310 of s6a&esAce of heartsTin( of heartsUacG of hearts of hearts2 of &ia%on&sUacG of clubs10 of clubs! of clubs of clubs 6layer 1 shortest 1Tin( of s6a&es of s6a&esSueen of hearts" of hearts3 of hearts of &ia%on&s! of &ia%on&sSueen of clubs5 of clubs3 of clubs 6layer 2 shortest 0UacG of s6a&es! of s6a&es
5 of s6a&es" of s6a&es3 of s6a&es2 of heartsAce of &ia%on&s8 of &ia%on&s3 of &ia%on&sAce of clubs 6layer 3 shortest 0
8/16/2019 Chapter 12 Projects
http://slidepdf.com/reader/full/chapter-12-projects 18/20
Sueen of s6a&es8 of s6a&es10 of hearts8 of hearts of heartsTin( of &ia%on&s
Sueen of &ia%on&s10 of &ia%on&s8 of clubs" of clubs 6layer " shortest 1Ace of s6a&es2 of s6a&es! of hearts5 of heartsUacG of &ia%on&s of &ia%on&s5 of &ia%on&s
Tin( of clubs of clubs2 of clubs
/n this !oop% a!so set 6layer/u% eDua! to the i&entification numer of the p!aer having the
!o"est c!u.
7hen !oop through the tota! numer of tric-s. /n each iteration% instantiate a ne" ricG% a&& it to
the tricGs arra% an& increment nu%berfricGs. /f it+s the first tric-% set in&e eDua! to
the in&e6 of the !o"est c!u in the han& of the p!aer having the !o"est c!u% an& set car& eDua!
to a reference to that car&. 8ave that p!aer remove that car&% an& up&ate the tric-. /f it+s not thefirst tric-% set car& eDua! to the va!ue returne& 6layer.6layA,ar&. Zisp!a the p!aer
numer an& car& va!ue for this first p!a of the tric-. 7hen !oop through a!! remaining p!aers%an& for each such p!aer% increment the p!aer numer% using ) P<A9@S to count in a circ!e%assign the va!ue returne& 6layA,ar& to car&% an& &isp!a the p!aer numer an& car&
va!ue. After the !oop through the remaining p!aers is over% set the p!aer numer to the "inner of the tric-. 7hen% if it+s the first tric-% &ea! the rest of the &ec-+s car&s to this tric-. 5or each suchcar&% ca!! u6&ateCeartsAn&Sueen to recor& 8earts ro-en for the game% print Hun&e!t car&I
an& &isp!a that car&.
After a!! the tric-s are &one% print each p!aer+s numer an& score% using a ca!! toco%6uteEoints. ecogniing again that Math.ran&o% "i!! ma-e the &etai!s of each
e6ecution &ifferent% the rest of the output for a game shou!& !oo- !i-e this:
utput G secon& part:
6layer " 2 of clubs6layer 0 UacG of clubs6layer 1 5 of clubs6layer 2 Ace of clubs6layer 3 8 of clubsun&elt car& of s6a&esun&elt car& " of &ia%on&s
8/16/2019 Chapter 12 Projects
http://slidepdf.com/reader/full/chapter-12-projects 19/20
6layer 2 3 of s6a&es6layer 3 8 of s6a&es6layer " 2 of s6a&es6layer 0 10 of s6a&es6layer 1 Tin( of s6a&es
6layer 1 of &ia%on&s6layer 2 8 of &ia%on&s6layer 3 Tin( of &ia%on&s6layer " UacG of &ia%on&s6layer 0 2 of &ia%on&s
6layer 3 " of clubs6layer " Tin( of clubs6layer 0 10 of clubs6layer 1 Sueen of clubsCearts is now broGen
6layer 2 2 of hearts
6layer " of &ia%on&s6layer 0 Ace of hearts6layer 1 ! of &ia%on&s6layer 2 3 of &ia%on&s6layer 3 Sueen of &ia%on&s
6layer 3 of hearts6layer " 5 of hearts6layer 0 Tin( of hearts6layer 1 Sueen of hearts6layer 2 Ace of &ia%on&s
6layer 0 of clubs6layer 1 3 of clubs6layer 2 UacG of s6a&es6layer 3 Sueen of s6a&es6layer " of clubs
6layer " 5 of &ia%on&s6layer 0 UacG of hearts6layer 1 " of hearts6layer 2 ! of s6a&es6layer 3 10 of &ia%on&s
6layer 3 8 of hearts6layer " ! of hearts6layer 0 of hearts6layer 1 3 of hearts6layer 2 5 of s6a&es
6layer 0 ! of clubs6layer 1 of s6a&es
8/16/2019 Chapter 12 Projects
http://slidepdf.com/reader/full/chapter-12-projects 20/20
6layer 2 " of s6a&es6layer 3 10 of hearts6layer " Ace of s6a&esElayer 0 score Elayer 1 score 0Elayer 2 score 0
Elayer 3 score 3Elayer " score 1"Elay another (a%e ?y4n@V
/n the u6&ateCeartsAn&Sueen metho&% if the parameter car&+s suit is hearts an& hearts is
sti!! false% output H8earts is no" ro-enI an& set hearts to true. /f the parameter car& is
the Dueen of spaces set Kueenf'6a&es to true.
/n the co%6uteEoints metho&% !oop through a!! tric-s. /f a particu!ar tric-+s "inner eDua!s this
metho&+s parameter va!ue% !oop through a!! car&s in that tric-. /f a car& is the Dueen of spa&es% a&&1' points. 5or each car& that is a heart% a&& 1 point. eturn the tota! numer of points.