Вы находитесь на странице: 1из 13

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

IntroductiontoRAPTOR:OOPMode ObjectOrientedMode
Object-oriented modeallowsyoutocreateclasseswithmethodsandattributes,instantiate objects,andexperimentwithObjectOrientedProgramming(OOP). TouseRAPTORinOOP,youmustselectObject-orientedmode,asshowninFigure1.

Figure1Selecting Object-orientedmode Youwillseetwotabs: UML and main. RAPTORusesatypeofUMLtocreatethestructureofan objectorientedprogram.TheclassesarecreatedintheUML screen;therefore,clickthe UML tab. ThebuttontoaddanewclassisshowninFigure2.Notethatanew Return symbolhasbeen addedtothesymbols.

1 of 13

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

Figure2Addinganewclass

CreatingaClass
Whenyouclickthe Add New Classbuttontoaddanewclass,a Name boxwillappear.Entera nameforthe Class,asshowninFigure3.

Figure3EnteringaClassname

2 of 13

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 InFigure3,a Class named Cube hasbeencreated.Doubleclickinsidetheclass(Cube)to add members(methodsandattributes).InRAPTOR,notethatattributesare called Fields.A new windowopenstoallowyoutoenterthemembers(seeFigure4).

Figure4AddingmemberstoaClass Fromthispoint,exampleswillbeusedtodemonstratethefeaturesofOOPmodeandindicate howtousetheminaprogram.


3 of 13

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() Figure5(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()

4 of 13

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

Figure5The Class Cube anditsmembers Oncethe Class hasbeencreated,anewtabisautomaticallyadded,withthenameofthe Class (seeFigure6).Nowthecodeforeachofthe Classsmethodsmustbecreated. Clickthe Cubetabtoseefournewtabsoneforeach Method,asshowninFigure7.

5 of 13

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

Figure6Newtabforthe Class Cube

6 of 13

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

Figure7NewtabsforeachnewMethod

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.ThecodeforthismethodisshowninFigure8.

7 of 13

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

Figure8Codeforthe SetSide() method ComputeVolume(Side) Method: The ComputeVolume(Side)Method computesthevolumeofthecube.First,itmustreceive thevalueneededforthecomputation(Side).Then,itmustdothecomputationbycubingthe value.Finally,itneedstoexportthisresultwhenrequested.Figure9showsthecode.

8 of 13

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

Figure9Codeforthe ComputeVolume() method GetVolume(Volume) Method: TheGetVolume(Volume)Method retrievesthevalueof Volume whenitisaccessedandthen returnsit,asshowninFigure10.

9 of 13

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

Figure10Codeforthe GetVolume() method GetSide() Method: The GetSide() Method retrieves the value of Side when accessed, as shown in Figure 11.

Figure11Codeforthe GetSide() method

10 of 13

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

The Main Program


Nowthe Main programcanbecreated.Theprogramforthisexampleisextremelysimple;itwill allowtheusertoenteravalueforthesideofacube,computethevolumeofthatcube,and displaytheresult.Thisisaccomplishedbyinstantiatinganobjectoftype Cube, whichwewill call CubeOne, andusingthemethodsandattributesof Cube.Figure12showshowthisisdonethe RAPTOROOPway.

Figure12Codetoinputasideofacubeandoutputitsvolume

11 of 13

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.Figure13(following page)showstheassociationbetweenaparent Class named Animal andtwochild Classes (subclasses)named Frog and Snake. Usethe New Association buttontosetthe inheritancebetweentheparentandchild,asindicatedinFigure13. Inthisexample, Frog and Snake inherit the showAnimalStuff() Method from Animal buteachchildclasshasitsown Method for makeSound() and showAnimalType().The OOPcharacteristicsofbothpolymorphismandinheritancearedemonstratedbythisexample. [SpecialthankstoGeorgeL.Marshall,Jr.fromCalhounCommunityCollegeattheResearchPark CampusinHuntsville,AlabamafortheAnimalexample.]

12 of 13

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

Figure13ChildClassesinheritfromtheParentClass BycombiningallthefeaturesofRAPTORsOOPmodeandallthatyouhavelearnedinthistext aboutobjectorientedprogramming,itispossibletocreatesomeinterestingandsophisticated programs.

13 of 13