Beruflich Dokumente
Kultur Dokumente
IntroductiontoRAPTOR:DataFilesandOOPMode CreatingandDisplayingDataFiles
InRAPTORwecancreatedatafilesandreadfromthefiles.However,sorting,insertingrecords,or mergingtwodatafilesrequiressomefancyfootworkbutifyoureveryambitious,tryit!
TheRedirect_Output Procedure
Tocreateadatafile,youusea Call to the Redirect_Output procedure.RAPTORprovides twoversionsofthisprocedure. 1. Afilenameisusedasanargumentto Redirect_Output, asshowninthefollowing examples: Redirect_Output("sample.txt") Redirect_Output("C:\MyDocuments\John.Doe\sample")
Notethatinthefirstexample,onlythefilenameisgiven.Inthiscase,thespecifiedtextfile willbecreatedinthesamedirectoryasthecurrentRAPTORprogram.Inthesecond example,thefullpathtothefileisgiven.Also,inthesecondexample,nofileextensionis specified.Inthiscase,thefile sample willbecreatedwithnoextension. 2. Youcaneitherturnonoroff Redirect_Output byincludingasimpleyes/true or no/false argument,asfollows: Redirect_Output(True)or Redirect_Output(yes) Redirect_Output(False) or Redirect_Output(no)
Now,theoutputmustberedirectedtothedatafilebyusinga Calltothe Redirect_Output procedure.Thenameofthedatafileisusedastheargument.Thisfilenamemustbeinside quotationmarks(asshownintheexamplesabove). Next,createthecodetoinputdata.Onevariableisrequiredforeachfieldoftherecordsinthe datafile.The Output boxwill PUT thevalueofthosevariablesintoeachrecord.Forexample,to
1 of 18
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011 createadatafilewithrecordsthathavetwofields, Name and Salary,twovariablesarerequired (probablycalled Name and Salary).Asvaluesfordifferentemployeesareinput,each Name and Salary willbestoredinthedatafileonasingleline. Afterthedatahasbeenentered,the Redirect_Output mustbeturnedoff.A Callisusedto callthe Redirect_Output procedureagain,butthistimeitsturnedoffbyusingeither no or false astheargument. Figure1(followingpage)showsaRAPTORflowchartthatwillwritetworecords,eachwithtwo fields,toadatafilenamed sample.txt. Figure2showsthecontentsofthefilecreated(and openedinNotepad).
2 of 18
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011
Figure1Programtowriterecordsto adatafile
Figure2Textfilecreated
3 of 18
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011
TheEnd_Of_InputFunction
RAPTORsbuiltinfunction, End_Of_Input,canbeusedasthetestconditionofaloop.When readingrecordsinadatafile,ifthisfunctionisusedasthetestcondition,RAPTORwillendthe loopwhenalltherecordshavebeenread. Whenalltherecordshavebeenreadandwrittentothe Master Console, the Redirect_Input proceduremustbeturnedoffwitha Call totheprocedureusing False or no fortheargument.
HowtheContentsoftheFileareStored
The Redirect_Inputproceduredoesnotseparateeachfieldinarecord.Rather,eachrecord isstoredasonelineofstringdata.Each Inputlinereadsallthefieldsinonerecord(oreverything thatisonasinglelineinthedatafile).Therefore,therecordscanbeoutputtotheMaster Console butthefieldscannotbemanipulatedeasilytosort,insert,ormerge.Thiscanbedone, butitrequiresadvancedprogramming. Figure3showsasampleofthecodetoreadrecordsfromadatafile(sample.txt)andthe Master Console display.
4 of 18
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011
Figure3Readingrecordsfromadatafileanddisplayingthem
ObjectOrientedMode
Object-oriented modeallowsyoutocreateclasseswithmethodsandattributes,instantiate objects,andexperimentwithObjectOrientedProgramming(OOP). TouseRAPTORinOOP,youmustselectObject-orientedmode,asshowninFigure4.
5 of 18
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011
Figure4Selecting Object-orientedmode Youwillseetwotabs: UML and main. RAPTORusesatypeofUMLtocreatethestructureofan objectorientedprogram.TheclassesarecreatedintheUML screen;therefore,clickthe UML tab. ThebuttontoaddanewclassisshowninFigure5.Notethatanew Return symbolhasbeen addedtothesymbols.
Figure5Addinganewclass
6 of 18
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011
CreatingaClass
Whenyouclickthe Add New Classbuttontoaddanewclass,a Name boxwillappear.Entera nameforthe Class,asshowninFigure6.
Figure6EnteringaClassname InFigure6,a Class named Cube hasbeencreated.Doubleclickinsidetheclass(Cube)to add members(methodsandattributes).InRAPTOR,notethatattributesare called Fields.A new windowopenstoallowyoutoenterthemembers(seeFigure7).
7 of 18
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011
Example:UsingtheCube ClasstoFindtheVolumeofaCube
Wewilluseaclassnamed Cube thattakesthevalueofasideofacubeandcomputesthecubes volume.Soweneedthefollowing: attributes: Side (anumber)and Volume (anumber) methods: SetSide(), GetSide(), ComputeVolume(),and GetVolume() 8 of 18
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011 Figure8(followingpage)showsthe Class Cube anditsmembers. Notethesyntaxfora Field: A Field mustbegivenadatatype.Thetypeof Side and Volume is int andinthiscase,eachfieldhasbeengivenaninitialvalueof1. Notethesyntaxfora Method. Ifthe Method receivesavaluepassedfrom main,youmust includethatparameter.Forexample, o The Method SetSide() ispassedavalueforthelengthofasidesothesyntaxforthis Method is public void SetSide(int NewSide) o The Method ComputeVolume() usesthevalueofthesideofacubetodoits calculationssoitneedsoneparameter,theintegervariable Side. Thesyntaxis public void ComputeVolume(int Side) o The Method GetVolume() retrievesthevalueofthevolumeofthecubefrom ComputeVolume() sothesyntaxforthis Method is public void GetVolume(int Volume) o The Method GetSide()doesnotneedaparametersothesyntaxis public void GetSide()
9 of 18
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011
Figure8The Class Cube anditsmembers Oncethe Class hasbeencreated,anewtabisautomaticallyadded,withthenameofthe Class (seeFigure9).Nowthecodeforeachofthe Classsmethodsmustbecreated. Clickthe Cubetabtoseefournewtabsoneforeach Method,asshowninFigure10.
10 of 18
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011
11 of 18
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011
Figure10NewtabsforeachnewMethod
CodetheMethods
TheMethods forthisprogramareasfollows: SetSide(NewSide), ComputeVolume(Side), GetVolume(Volume),and GetSide(). SetSide() Method: The SetSide()Method does onethingonly.Itsetsthevalueofthesideofacube,aspassedto itfromthemainprogram,tothevariable NewSide. Thisassignmentisdoneusingthethis keyword.ThecodeforthismethodisshowninFigure11.
12 of 18
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011
Figure11Codeforthe SetSide() method ComputeVolume(Side) Method: The ComputeVolume(Side)Method computesthevolumeofthecube.First,itmustreceive thevalueneededforthecomputation(Side).Then,itmustdothecomputationbycubingthe value.Finally,itneedstoexportthisresultwhenrequested.Figure12showsthecode.
13 of 18
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011
Figure12Codeforthe ComputeVolume() method GetVolume(Volume) Method: TheGetVolume(Volume)Method retrievesthevalueof Volume whenitisaccessedandthen returnsit,asshowninFigure13.
14 of 18
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011
Figure13Codeforthe GetVolume() method GetSide() Method: The GetSide() Method retrieves the value of Side when accessed, as shown in Figure 14.
15 of 18
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011
Figure15Codetoinputasideofacubeandoutputitsvolume
16 of 18
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011
InheritanceandPolymorphism
Onceyouhavemasteredthebasics:creating Classes, Fields, and Methods,andusing dot notationinyourprogram,youcanusetheOOPmodeinRAPTORtocreateandrunmore complicatedprograms. YoucreatechildclassesthatinheritfromaparentclassintheUMLscreen.Figure16(following page)showstheassociationbetweenaparent Class named Animal andtwochild Classes (subclasses)named Frog and Snake. Usethe New Association buttontosetthe inheritancebetweentheparentandchild,asindicatedinFigure16. Inthisexample, Frog and Snake inherit the showAnimalStuff() Method from Animal buteachchildclasshasitsown Method for makeSound() and showAnimalType().The OOPcharacteristicsofbothpolymorphismandinheritancearedemonstratedbythisexample. [SpecialthankstoGeorgeL.Marshall,Jr.fromCalhounCommunityCollegeattheResearchPark CampusinHuntsville,AlabamafortheAnimalexample.]
17 of 18
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011
18 of 18