CSE116 Problem Set #4

advertisement
CSE116ProblemSet#4
ForeachofthefollowingproblemsyoushouldstartbywritingseveralJUnitteststhatwillverify
correctbehavior.Onceyouhavewrittenatleastfourdistincttestsforeachoftheproblems,begin
toimplementsolutionstoeachoftheproblems.Besuretorunyourunittestsoften.Theyarenot
necessarilyinorderofdifficulty.
FORWU4YOUWILLBEREQUIREDTOWRITEYOUROWNTESTSDURINGTHEWRITE-UP.PART
OFYOURGRADEWILLDERIVEFROMHOWWELLYOURTESTSCOVERYOURIMPLEMENTATION
CODE.
Foralloftheseproblemsbesureyouunderstandhowtoimplementastatemachine–yourTAwill
discussthedesiredapproachduringrecitationthisweek.
1. DefineamethodnamedacceptwhichtakesaStringasargumentandreturnsaboolean.
TheStringconsistsofasequenceofcharactersdrawnfromtheset{‘D’,‘P’,‘S’,‘W’}.These
charactersrepresent“dry”,“paint”,“sand”and“wipe”,respectively.Implementthe
followingfinitestatemachine:
D
3
0
P
S
W
2
1
Anarrowpointingtoastatebutnotoriginatinginastateidentifiesthestartingstate.Inthis
statediagramstate0isthestartingstate.
Adouble-circleindicatesafinal(oraccepting)state.Inthisdiagramstate2isanaccepting
state.
Ifthereisnotransitionoutofastateonagivencharacter,theinputstringisrejected.
Thismachineacceptsthestrings"SW"and"SWPDSW"butrejectsthestrings"P"and"S".
2. DefineamethodnamedacceptthatacceptsaStringasinputandreturnstrueiftheString
representsavalidtelephonenumberformat,falseotherwise.Validformatsare:
a. (DDD)DDD-DDDD
b. DDD-DDDD
c. +1-DDD-DDD-DDDD
Validexamplesare“(716)688-0315”,“645-3180”and“+1-716-645-3180”.IftheString
doesNOTrepresentatelephonenumberinavalidformat,returnfalse.
USEAFINITESTATEMACHINEAPPROACH.STARTBYDRAWINGTHEMACHINE.
3. DefineamethodnamedacceptthathasasingleStringparameterandwhichreturnsa
HashMap<String,Integer>.SupposingtheparameterisnamedinputFilePath,themethod
must:
a. readthecontentsofafile(identifiedbyinputFilePath)onecharatatime,
b. segmenttheinputintowords,and
c. keeptrackofwordcountsinajava.util.HashMap<String,Integer>.
Awordisdefinedasacontiguoussequenceofcharactersthatdoesnotcontainword
separatorcharacters,wherewordseparatorcharactersare:''(space),'\t'(tab),'\n'
(newline),','(comma)and'.'(period).
Thinkcarefullyaboutthisdefinition,andhowitappliestoaninitialcharactersequencenot
precededbyawordseparator,andalsoafinalcharactersequencenotfollowedbyaword
separator.YoumustuseonlyCharacterFromFileReader1toreadcharactersfromtheinput
file.UseitasanIterator<Character>,keepinginmindtheautoboxingandunboxing
featuresoftheJavalanguage.
Inordertokeepyourcodereadable,breakyourcodeintoseveralmethods.Define
meaningfulprivatehelpermethodsthatyoucallfromtherequiredpublicmethod.
1Thedefinitionofthisclassisfoundascode.CharacterFromFileReaderintheDataStructuresprojectinthe
repository.
Download