面向对象程序设计实验指导书.docx
《面向对象程序设计实验指导书.docx》由会员分享,可在线阅读,更多相关《面向对象程序设计实验指导书.docx(52页珍藏版)》请在冰豆网上搜索。
![面向对象程序设计实验指导书.docx](https://file1.bdocx.com/fileroot1/2023-5/27/ea0c4207-e9e8-4d44-ba22-4a58f437d4ef/ea0c4207-e9e8-4d44-ba22-4a58f437d4ef1.gif)
面向对象程序设计实验指导书
面向对象程序设计
实验指导书
黄利萍刘国奇
东北大学软件学院
2010.9
前言
面向对象的思想可以渗透到需求分析、系统建模、体系结构设计、程序设计与实现、系统测试等多个方面,它是描述现实世界复杂对象的相当直接而且直观的有效手段,对于提高系统质量、开发效率和代码重用率,都有明显的效果。
《面向对象程序设计》课程是软件工程专业的重要专业基础课程之一,该门课程注重实践性和实用性,主要通过面向对象程序设计思想和Java语言结合起来,让学生掌握面向对象程序设计思想,以及熟练使用Java语言进行面向对象的编程,因此学生不能满足于只听懂老师讲授的课堂内容,看懂书上的程序,应将课堂教学与实践环节紧密结合,使得学生加深对讲授内容的理解,学会上机调试程序。
也就是善于发现程序中的错误,并且能很快地排除这些错误,使程序能正确运行。
《面向对象程序设计》是结合卡耐基梅隆大学的SSD3而形成的课程,该课程的教学体系和实验体系都很完整,并且东北大学软件学院也提供了良好的教学实验环境,希望同学们能够充分利用实验条件,认真完成实验,从实验中得到应有的锻炼和培养。
希望同学们在使用本实验指导书及进行实验的过程中,能够帮助我们不断地发现问题,并提出建议,使《面向对象程序设计》真正能够帮助同学们学习。
实验要求
《面向对象程序设计》课程实验的目的是为了使学生在课堂学习的同时,通过一系列的实验,使学生加深了解和更好地掌握《面向对象程序设计》课程教学大纲要求的内容。
在《面向对象程序设计》的课程实验过程中,要求学生做到:
(1)预习实验指导书有关部分,认真做好实验内容的准备,就实验可能出现的情况提前作出思考和分析。
(2)仔细观察调试程序过程中出现的各种问题,记录主要问题,作出必要说明和分析。
(3)遵守机房纪律,服从辅导教师指挥,爱护实验设备。
(4)实验课程不迟到,如有事不能出席,所缺实验一般不补。
(5)本实验采用的开发环境为Eclipse,同学在做实验之前要求熟悉该集成开发环境。
Experiment1ImplementingtheGourmetCoffeeSystem(4Hours)
Prerequisites,Goals,andOutcomes
Prerequisites:
Beforeyoubeginthisexercise,youneedmasteryofthefollowing:
∙ObjectOrientedProgramming
oKnowledgeofclassdesign
▪Classattributes
▪Constructors
▪Accessormethods
▪Mutatormethods
oKnowledgeofinheritance
▪Howtoimplementaspecialization/generalizationrelationshipusinginheritance
Goals:
ReinforceyourabilitytoimplementJavaclassesusinginheritance.
Outcomes:
Youwilldemonstratemasteryofthefollowing:
∙Implementingtheconstructors,accessors,andmutatorsofaJavaclass
∙Usinginheritancetoimplementaspecialization/generalizationrelationship
Background
ThisassignmentasksyoutoimplementsomeoftheclassesintheGourmetCoffeeSystemspecifiedonExercise2.
Description
Inthisassessment,youwillimplementtheclassesandrelationshipsillustratedinthefollowingclassdiagram:
Figure1PortionofGourmetCoffeeSystemclassdiagram
Theclassspecificationsareasfollows:
ClassProduct
TheclassProductmodelsagenericproductinthestore.
Instancevariables:
∙code.Theuniquecodethatidentifiestheproduct
∙description.Ashortdescriptionoftheproduct
∙price.Thepriceoftheproduct
Constructorandmethods:
∙publicProduct(StringinitialCode,
∙StringinitialDescription,
∙doubleinitialPrice)
Constructorthatinitializestheinstancevariablescode,description,andprice.
∙publicStringgetCode().Returnsthevalueofinstancevariablecode.
∙publicStringgetDescription().Returnsthevalueofinstancevariabledescription.
∙publicdoublegetPrice().Returnsthevalueofinstancevariableprice.
∙booleanequals(Objectobject).OverridesthemethodequalsintheclassObject.TwoProductobjectsareequaliftheircodesareequal.
∙StringtoString().OverridesthemethodtoStringintheclassObject.ReturnsthestringrepresentationofaProductobject.TheStringreturnedhasthefollowingformat:
code_description_price
Thefieldsareseparatedbyanunderscore(_).Youcanassumethatthefieldsthemselvesdonotcontainanyunderscores.
ClassCoffee
TheclassCoffeemodelsacoffeeproduct.ItextendsclassProduct.
Instancevariables:
∙origin.Theoriginofthecoffee
∙roast.Theroastofthecoffee
∙flavor.Theflavorofthecoffee
∙aroma.Thearomaofthecoffee
∙acidity.Theacidityofthecoffee
∙body.Thebodyofthecoffee
Constructorandmethods:
∙publicCoffee(StringinitialCode,
∙StringinitialDescription,
∙doubleinitialPrice,
∙StringinitialOrigin,
∙StringinitialRoast,
∙StringinitialFlavor,
∙StringinitialAroma,
∙StringinitialAcidity,
∙StringinitialBody)
Constructorthatinitializestheinstancevariablescode,description,price,origin,roast,flavor,aroma,acidity,andbody.
∙publicStringgetOrigin().Returnsthevalueofinstancevariableorigin.
∙publicStringgetRoast().Returnsthevalueofinstancevariableroast.
∙publicStringgetFlavor().Returnsthevalueofinstancevariableflavor.
∙publicStringgetAroma().Returnsthevalueofinstancevariablearoma.
∙publicStringgetAcidity().Returnsthevalueofinstancevariableacidity.
∙publicStringgetBody().Returnsthevalueofinstancevariablebody.
∙StringtoString().OverridesthemethodtoStringintheclassObject.ReturnsthestringrepresentationofaCoffeeobject.TheStringreturnedhasthefollowingformat:
code_description_price_origin_roast_flavor_aroma_acidity_body
Thefieldsareseparatedbyanunderscore(_).Youcanassumethatthefieldsthemselvesdonotcontainanyunderscores.
ClassCoffeeBrewer
ClassCoffeeBrewermodelsacoffeebrewer.ItextendsclassProduct.
Instancevariables:
∙model.Themodelofthecoffeebrewer
∙waterSupply.Thewatersupply(Pour-overorAutomatic)
∙numberOfCups.Thecapacityofthecoffeebrewer
Constructorandmethods:
∙publicCoffeeBrewer(StringinitialCode,
∙StringinitialDescription,
∙doubleinitialPrice,
∙StringinitialModel,
∙StringinitialWaterSupply,
∙intinitialNumberOfCups)
Constructorthatinitializestheinstancevariablescode,description,price,model,waterSupply,andnumberOfCups.
∙publicStringgetModel().Returnsthevalueofinstancevariablemodel.
∙publicStringgetWaterSupply().ReturnsthevalueofinstancevariablewaterSupply.
∙publicintgetNumberOfCups().ReturnsthevalueofinstancevariablenumberOfCups.
∙StringtoString().OverridesthemethodtoStringintheclassObject.ReturnsthestringrepresentationofaCoffeeBrewerobject.TheStringreturnedhasthefollowingformat:
code_description_price_model_waterSupply_numberOfCups
Thefieldsareseparatedbyanunderscore(_).Youcanassumethatthefieldsthemselvesdonotcontainanyunderscores.
ClassOrderItem
ClassOrderItemmodelsaniteminanorder.
Instancevariables:
∙product.Thisinstancevariablerepresentstheone-wayassociationbetweenOrderItemandProduct.ItcontainsareferencetoaProductobject.
∙quantity.Thequantityoftheproductintheorder.
Constructorandmethods:
∙publicOrderItem(ProductinitialProduct,
∙intinitialQuantity)
Constructorthatinitializestheinstancevariablesproductandquantity.
∙publicProductgetProduct().Returnsthevalueoftheinstancevariableproduct,areferencetoaProductobject.
∙publicintgetQuantity().Returnsthevalueoftheinstancevariablequantity.
∙publicvoidsetQuantity(intnewQuantity).SetstheinstancevariablequantitytothevalueofparameternewQuantity.
∙publicdoublegetValue().Returnstheproductofquantityandprice.
∙StringtoString().OverridesthemethodtoStringintheclassObject.ReturnsthestringrepresentationofanOrderItemobject.TheStringrepresentationhasthefollowingformat:
quantityproduct-codeproduct-price
Thefieldsareseparatedbyaspace.Youcanassumethatthefieldsthemselvesdonotcontainanyspaces.
Testdriverclasses
Completeimplementationsofthefollowingtestdriversareprovidedinthestudentarchive.Usethesetestdriverstoverifythatyourcodeworkscorrectly.
∙ClassTestProduct
∙ClassTestCoffee
∙ClassTestCoffeeBrewer
∙ClassTestOrderItem
Files
Thefollowingfilesareneededtocompletethisassignment:
∙student-files.zip—Downloadthisfile.Thisarchivecontainsthefollowing:
oTestProduct.java
oTestCoffee.java
oTestCoffeeBrewer.java
oTestOrderItem.java
Tasks
ImplementclassesProduct,Coffee,CoffeeBrewer,andOrderItem.DocumentusingJavadocandfollowSun'scodeconventions.Thefollowingstepswillguideyouthroughthisassignment.Workincrementallyandtesteachincrement.Saveoften.
1.Extractthefilesbyissuingthefollowingcommandatthecommandprompt:
C:
\>unzipstudent-files.zip
2.Then,implementclassProductfromscratch.UseTestProductdrivertotestyourimplementation.
3.Next,implementclassCoffeefromscratch.UseTestCoffeedrivertotestyourimplementation.
4.Then,implementclassCoffeeBrewerfromscratch.UseTestCoffeeBrewerdrivertotestyourimplementation.
5.Finally,implementclassOrderItemfromscratch.UseTestOrderItemdrivertotestyourimplementation.
Submission
Uponcompletion,submitonlythefollowing:
1.Product.java
2.Coffee.java
3.CoffeeBrewer.java
4.OrderItem.java
Experiment2ImplementingtheCollectionsintheGourmetCoffeeSystem(4Hours)
Prerequisites,Goals,andOutcomes
Prerequisites:
Beforeyoubeginthisexercise,youneedmasteryofthefollowing:
∙Collections
oUseofclassArrayList
oUseofiterators
Goals:
Reinforceyourabilitytoimplementclassesthatusecollections
Outcomes:
Youwilldemonstratemasteryofthefollowing:
∙ImplementingaJavaclassthatusescollections
Background
Inthisassignment,youwillimplementtheclassesintheGourmetCoffeeSystemthatusecollections.
Description
ThefollowingclassdiagramoftheGourmetCoffeeSystemhighlightstheclassesthatusecollections:
Figure2GourmetCoffeeSystemclassdiagram
Completeimplementationsofthefollowingclassesareprovidedinthestudentarchive:
∙Coffee
∙CoffeeBrewer
∙Product
∙OrderItem
∙GourmetCoffee
Inthisassignment,youwillimplementthefollowingclasses:
∙Catalog
∙Order
∙Sales
∙GourmetCoffee
Theclassspecificationsareasfollows:
ClassCatalog
TheclassCatalogmodelsaproductcatalog.ThisclassimplementstheinterfaceIterabletobeingabletoiteratethroughtheproductsusingthefor-eachloop.
Instancevariables:
∙products—AnArrayListcollectionthatcontainsreferencestoinstancesofclassProduct.
Constructorandpublicmethods:
∙publicCatalog()—Createsthecollectionproducts,whichisinitiallyempty.
∙publicvoidaddProduct(Productproduct)—Addsthespecifie