《2023年面向对象程序设计实验指导书.pdf》由会员分享,可在线阅读,更多相关《2023年面向对象程序设计实验指导书.pdf(76页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、面 向 对 象 程 序 设 计实 验 指 导 书黄 利 萍 刘 国 奇东北大学软件学院2023.9*目 录面向对象程序设计。错误!未定义书签。实验指导书错误!未定义书签。前 言错误!未定义书签。实验规定。错误!未定义书签。E x periment 1 I mpleme n t ing t h e C o 1 1 ect i o n s i n t he G ourmet Co f fee System(4 Hours)错误!未定义书签。B ackground。错误!未定义书签。D e sc r ipti o n。错误!未定义书签。Files.错误!未定义书签。T a s k s。错误!未定义书
2、签。Experim e n t 2 Usin g Des i g n Patterns i n the Gourmet C o f f ee Sys t e m(4Hou r s)。错误!未定义书签。Backg round。错误!未定义书签。D e scription.错误!未定义书签。F il e s.错误!未定义书签。Task s。错误!未定义书签。Ex p erime n t 3 U s i n g File I/O i n t he G ourme t C offee S y stem(4 Hours)。错误!未定义书签。B a ckgro u nd。错误!未定义书签。Descript
3、ion.错误!未定义书签。F ile s.错误!未定义书签。Tasks.错误!未定义书签。Ex p e rimen t 4 Im p lemeriting a GUI f o r the Gourm e t C o f fee S ystem(4 H ours).错误!未定义书签。B a c k g ro u n d。错误!未定义书签。Des cripti o n.错误!未定义书签。F i les错误!未定义书签。Tasks。错误!未定义书签。前营面向对象的思想可以渗透到需求分析、系统建模、体系结构设计、程序设计与实现、系统测试等多个方面,它是描述现实世界复杂对象的相称直接并且直观的有效手段,
4、对于提高系统质量、开发效率和代码重用率,都有明显的效果。面向对象程序设计课程是软件工程专业的重要专业基础课程之一,该门课程注重实践性和实用性,重要通过面向对象程序设计思想和Java语言结合起来,让学生掌握面向对象程序设计思想,以及纯熟使用Java语言进行面向对象的编程,因此学生不能满足于只听懂老师讲授的课堂内容,看懂书上的程序,应将课堂教学与实践环节紧密结合,使得学生加深对讲授内容的理解,学会上机调试程序。也就是善于发现程序中的错误,并且能不久地排除这些错误,使程序能对的运营。面向对象程序设计 是结合卡耐基梅隆大学的SS D 3而形成的课程,该课程的教学体系和实验体系都很完整,并且东北大学软件
5、学院也提供了良好的教学实验环境,希望同学们可以充足运用实验条件,认真完毕实验,从实验中得到应有的锻炼和培养。希望同学们在使用本实验指导书及进行实验的过程中,可以帮助我们不断地发现问题,并提出建议,使 面向对象程序设计真正可以帮助同学们学习。实验规定 面向对象程序设计课程实验的目的是为了使学生在课堂学习的同时,通过一系列的实验,使学生加深了解和更好地掌握 面向对象程序设计课程教学大纲规定的内容。在 面向对象程序设计的课程实验过程中,规定学生做到:(1)预习实验指导书有关部分,认真做好实验内容的准备,就实验也许出现的情况提前作出思考和分析。(2)仔细观测调试程序过程中出现的各种问题,记录重要问题,
6、作出必要说明和分析。(3)遵守机房纪律,服从辅导教师指挥,爱惜实验设备。(4)实验课程不迟到,如有事不能出席,所缺实验一般不补。(5)本实验采用的开发环境为E clipse,同学在做实验之前规定熟悉该集成开发环境。4E x p e rime n t 1 Impleme n t i n g the G ou rmetCoff e e System(4 Hours)P rerequisites,Goals,and 0 u tc o mesP rer eq uisites:B e fo re you be g i n this exerc i s e z you need mastery of t
7、h e followin g:0 bjec t Oriented P ro gramm i ngo Knowledge of clas s designC 1 ass a tt r i bute sConstructo rs Acc e s s or met h ods Muta t o r metho d so Know 1 edg e of i n h er i tan c e How to i m p 1 ement a sp e cializa t i on/gene r al i z ation re I at i ons h ip u s i n g inh e r i t anc
8、eGoals:Re i nfo r c e y our abi 1 ity to implem e n t Java c lasses us i ng inhe r itance.Out com e s:You will dem o ns t rate ma s t e ry of th e fol 1 owing:Impl e ment i ng the const r uctors,a c c e s s ors,an d mu t a to r s of aJa v a c la s s Using in h e ritan c e to im p 1 ement a spec i al
9、iz a t i on/g e nera 1 iza t i onre I a tlonshipBackgroun dThis ass i g nme n t ask s yo u t o i mplement some of the c 1 as s es i n theGourmet Cof f ee S y s tern s p ecified o n Ex e rcise 2.D e s c r i p t i o nI n t hi s a s s e s s ment,you will i mp 1 ement t he cla s s e s and r e 1 at i o n
10、 s h i psi I 1 u s tra t ed i n th e fo 1 1 owing class di a g ram:Product-code:String-description:String-price:doubleet CodeQ:String+get Description。:String为etPrioe。:doubley qual式object:Object):booleanrtoStringO:StringOrderitem-quantity:int-product1为etPnoduaO:Product4etQuarrtjtO:int+setQuantity(new
11、 Quant it/irrt)均 etSlue。:doubleMo String。:StringCoffeeorigin:String-roast:Siring-la w :String-aroma:String-acidity:String-body:String均et Origin。:String均etRoaslQ:String-etFlavorO:String与etM rraO :String与e tN d itO :String却etBodg:StringrtoStringO:String5G)1fee Brewer-model:StringuiaterSupply:Stringnum
12、ber。忙ups:int翅et岫加I。:String-tetWrterSupplO:Siring4et NjmberOfCupsO:int-rtoStringO:StringF i gu r e 1 P o rti o no f Gou r m e t C of f e e System c 1 ass diagra mThe c 1 a ss s p ecif i cations a re as f ol 1 ows:Clas s P r o du c tTh e class P rod u c t mode 1 s a generic p r od u ct in t he sto r e
13、.Instanc e va r fables:code.The uniq u e code that i de n ti f ies t he p ro d u c t d e s c rip t i o n.A s hort descr i p t io n o f the p roductp r ice.Th e p rice of t he productConst rue tor and methods:p u b lic P r o duct(S trin g i n i tia lC o d e,S trin g i n itia lD e s c rip tio n,doub 1
14、 e 1 n i t i a IP ric e)Construct r that in i tiali z es t h e inst a nee var i ables c o d e,de S cr ip t i o nz and p r i c e.p u b l i c S tr i ng g et C o de().R e tu r ns the v a lue of in s tanc e vari a bIe code.p u b 1 i c S t r i n g g e tD e s c rip ti o n().R eturns the value of in stance
15、 v ar i able de s cr i p tion.p u b l i c double g et P rice (),R e turns the value of i nstance va r iablep r ic e.b oo 1 e a n equ a 1 s(Ob j ect o b j e c t).Overrid es th e m e t hode qua Is in t he class 0 b j e c t.Two Prod u c t o bjects a re equa 1 if th ei r codes are equal.S t r i n g to S
16、 trin g O.Overrides th e me t hod t o S trin g in t he c lass 0b je c t.R e turns t h e st r i ng r e presen t a t i on of a P r odu c t object.The S t r i n g returned has the followi ng format:co d e_ de s c r i p tio n_ p r i c eTh e fields a re separated by an unde r scor e(_).Y o u c a n a ssum
17、 e that th e f i el d s thems e I v e s d o not c ont a i n a n y u n d ersco r es.Class C o f f e eThe c I a ss C o ffe e mod e Is a cof f e e product.I t ext e nds class P r od u c t.In sta nc e vari able s:o rig in.The or i g in of t h e coffee r o a s t.The roast of the coffee f l a v o r.The f
18、1 a v or of t h e coffee aroma.The aroma of the c o ffee a c id it y.The aci d ity of t h e cof fee b ody.The body o f the c offeeCon s t r uctor a nd methods:p u b li c Co f fee(S t r i n g i n i t i a 1 C o d e,St r i n g i n i t ia l De s c rip tio n,d o u b le in it ia 1 P ri c e,S trin g i n i
19、t i a l O rig in,S t r i n g in itia lR o a s t,S t r i ng in i tia lF la v o r,S trin g in i tia lA r o ma,S tri n g i n i t ialA c i d i ty,St r in g i n i t i a 1 Body)Con stru c tor th at i n i tiali z e s t h e ins t a nee v a riab 1 es code,d escrip tion,price,or i gin,r o a s t,f lavo r,aroma
20、,a c i d i t y,a nd bo d y.p u b lic S trin g ge t O rig in ().Re tu r ns the va lu e o f instan c e v a r iable o r ig in.pu b lie S trin g g e t R o as t().Return s the v a lu e o f instancevariab 1 e roast.pub l i e S tr i ng g e tF 1 avo r ().Retu r ns the value o f j n stance v ar i abl e f l a
21、 vor.p u b l i c S t rin g g etA r om a Re t u r ns the value of in s tan c e variab1 e arom a.p u b lic S tr i n g g e t A c id it y O.R e t u rns th e va 1 ue of in s t anee v ariable ac i d it y.p u b lic S trin g g etBody().Retu rns the value of ins t ance variab 1 ebody.S t r in g toS t r i n g
22、().Ov e r r i des t h e metho d toS t r ing i nthe c lass Ob j e c t.R e t urns the strin g re pre s en t ation of a C o ffe e o bject.Th e St r i ng ret u r n e d has th e fo 1 1 ow i n g f o r m a t:code_ d e s c r i p tio n p ric e o r ig i n _ roas t _ f la v o r_a r oma_aci d i t y_bodyThe fiel
23、ds are s epara t e d by a n u n derscore(_ ).You c a n a ssumethat the fiel d s thems e lv e s d o not c ontai n any un d ers c o re s.Cla s s Coffee B r ewerClass C o f f eeBrew e r models a coffee b re wer.It e xte n d s c la s s P roduct.Ins tanc e v a ri able s:mo d el.The m o de 1 of t he coff
24、e e b r ewer wat e rSupp ly.The watersupp y(Po ur-o vero r Au t omati c)numberOfCups.The ca p aci t y of the co f fee brewerConst r u ctor a nd methods:publi c Coff e eBrewe r(S tr i n g in i ti a 1 C o de,S tring in i t i a ID e scrip t i on,doub 1 e i n itia lP r ice,St r ing in i tialM odel,S t r
25、ing ini t ial Wat e rSupp 1 y,i n t in iti a 1 NumberOfCups)Con s tructor th at initi a li z es t he i n st a n c e v ari a bles c o de,d e s c ript io n z p r ice,model,water S u p p l y,and numberOfCup s.p u b lie St r ing g e tM R eturns t h e va 1 u e o f instanc e var i ab 1 e m o de 1 .public
26、S tri n g get Wat e r S u p p ly ().Returns the v a lu e of instan c e va riable wat e r S up p ly.p u b lic in t g etNumberOfCups(9.R e turn s the val u e of i nst a nee v ari ab 1 e n umb e rO f Cups.St r in g to S t r i n g().Override s the me t hod t o S trin g i n t he class 0b j ec t.Re t u rn
27、s th e string represent a tion of a C o ffe e B r e w e r ob j e ct.The S t r ing ret u rn e d h as th e f ollowi n g f o r mat:code_d e s c rip t io n _ p r i ce_m o del_ wa t e r S u p p ly_ numberOfCu psThe f ields are separa ted by an u n d e r scor e(_).You can a ssumethat the f i e Ids themse
28、Iv e s do no t co nt a in any underscores.Cla s s Orde r It e mClass O r d e r lt e m mo d els an item i n an order.Ins t a nee v ar iables:pro d u c t.This in s tance va r able r ep r e s ent s t h e o n e-way a s soci a tionb etwee n 0 r derltem and Product.11 conta ins a reference to a Pro duct o
29、 b jec t.q u a n tity.The q u antity of the p rodu c t i n the o r de r.Constr u ct or a nd meth ods:p u b li c Or d e r Ite m (Product i n i t i a 1 P r o d u c t,in t i n i t ialQ u a n tity)Con s tracto rth a t ini t ializes the i n s tance va r Sables pro d uct and q u a n tity.p u b I ic P rodu
30、 c t g e tP roduct().Returns t he value of th e i ns t anc e v ariable p roduct,a r e fe r ence to a P r oduct o bject.p u b 1 ic in t get Quan t i t y().e.urns the valu e ofth e i n s t anc e v a riabl e qu a n t ity.pub 1 ic vo id setQ u a n t it y (in t newQuantit y?.Se t s the in s tancev a r ia
31、ble q uan t i ty to th e value o f p arameter ne w Q uantity.pu b l i c dou b le ge t Va lu e().Returns the produ ct of q u a n tit y and p r ic e.St r in g t o S t r i n g().Overrides t h e me t h od to S trin g in the class0 b j e ct.Return s the st r ing re p resenta t io n of a n Orde r Item o b
32、j ect.Th e S trin g repres e nta t ion has t h e following f o rmat:q u a n tity p ro d u c t-c o d e produc t-p ric eThe f iel d s a r e s e p ara t e d by a sp a ce.Y o u can ass u me t hat t h e f i eldst hemse 1 ves do no t con t ain any spaces.T e s t d r iver c lasse sCom p I e te i mplem e nt
33、 a tions of th e f oil o wi n g te s t dr i v e rs ar e p ro v id e di n t h e st u dent archiv e.Use thes e te s t dri v e r s t o verify th a t yo u r co d e wo r ks corre ctly.Class T e s t P ro d uct Cla s s T estC offee c 1 ass TestC o f f e e B r e w e rClass Test O rd e rlte mFil e sThe f o l
34、lowin g fil e s a re n c eded to comp 1 ete th is a s s ig n m e nt:s tudent f i 1 e s.z ip D ow nload t hi s file.This archi v e c o ntainst h e fol 1 owin g:o Tes t Produ c t.ja v ao Te s tCoffee.ja vao T estC off e e B r e w er.ja v ao TestO r d e r l t e m.ja vaT a sksI mp 1 emen t clas s es P r
35、 oduc t,C o ff e e,C o ff e eBre we r z and 0 rde r Item.Doc u men t usi n g J a v adoc and fo il o w S u ns c o de c onv e ntio n s.The follow i n gsteps will gui d e yo u th ro u g h this a s s i gnment.Work incremental 1 y a nd tes teach increment.Save often.1.Ext r a ct t h e f i l e s by i s su
36、 i ng t h e fol 1 owing command a t the c ommand p rom p t:C:unz i p s t ude n t file s,z ip2.Then,impl em ent c 1 ass Pr o d u c t f rom sera t ch.Use T e s t Pro duct driver to tes t yo u r impl e mentation.3.Next,im p 1 em e nt class Cof f e e f r om scrat c h.Use T estC offeedriver t o tes t you
37、 r imp lem e nta t ion.4.Then,implem e nt cl a s s C o f f ee B rew e r f r om sc r a t c h.U s e TestCoff ee B rewe r dr i ver to tes t your im p l ementat i o n.5.Fina 1 ly,imp 1 e me n t c 1 as s Orde r Item from s cr a tch.Us e T e st 0rderltem driver to t est yo u r im p 1 eme n tat i o n.S ubm
38、i s s ionU pon compl e tio n z su b mit on ly th e followi n g:1.P r odu c t.java2.Cof f ee.j a v a3.C o ffe e B r ew e r.j a v a4.Order I t em.ja v aE x p erim e nt 2 Im p lemen tin g th e Co I Ie c t ions in the Gourmet Cof f e e Sy s tem(4 Ho u rs)P r e r e qui s ites,Goa 1 s,an d Ou tcomesPrereq
39、ui s ite s:B e fore yo u b e gin th i s e x e r ci s e,y ou n e ed mastery o f the fo How i ng:Coll ectio nso u s e of class A r ra y L is to Use of i t e r ator sGo al s:Reinfo r c e your abil i t y t o i mplemen t classes th a t use c o I 1 e ctionsOutc om es:You will dem o nstra te mastery o f t
40、he following:Implement i n g a J a v a clas s th at u s es collectionsBac k gr o u ndIn t his a s s ignmen t,you will i m p I e me n t the c 1 asses in th e Gourmet C offe c S ys tem that use collections.Des criptionTh e fo 1 lowi ng c lass d i a g ram o f the Go urmet Coffe e System highlights th e
41、 c1 a s ses t hat us e co 1 I e c ti o ns:Catalog+add Product(product:Product)4et Product(code:String):ProductHteratorO:teratoretNumberOffroductsO:intcatalog一1products、/O.xProduct-code:String-description:String-price:double+get8deO:String+get Description。:String+getPriceO:double4quals(object:Object)
42、:booleanrtoSlringO:StringA-product一1Orderitem-quantity:int现etProduct。:Productet Quantity):int+5etQuarrtfty(neu)Quantity int)气 etSlue。:doublertoSlringO:StringO.x/-itemsCoffee-origin:Siring-roast:Siring-law r:Siring-aroma:String-acidity:String-body:String也et Origin。:StringCoffee Brewer-model:String-wa
43、terSupply:StringnumberOfCups:int-tetModelQ:String均et叫te$uppl):String4et NjmberOfCupsQ:StringrtoSlringO:String-etRoastQ:String气etFlavorQ:StringetAxmaO:String:String4etBodO:Stringrto String。:StringFi g u r e 2 Gourm e t Coffee S ystern class d i agramComplete implemen t at i o n s of the foil o w i ng
44、 cla s s e s a re pro v ide d i n the stu dent arc hiv e:Cof f ee C o ffee B rewer Product Orderl t emG o urmetCoffeeIn t his as s ig n merit,you w i II implem e n t t h e f oil o wing c 1 a sses:C ata 1 o g Order S a l e s Gourm e tC offeeTh e cl a ss s p ecifica tio n s a re a s follows:C lass Cat
45、al o gThe c las s Cat a log mo d e 1 s a p ro d u ct ca talo g.Th i s c lass i mp I ement s t he inte r face I t e rable to being able to i terate th r ough t he p roducts using th e f o r-e ac h loop.I n s tance v a r i a bles:produc t s An Ar r ayL i s t co 1 1 ec t ion th a t conta in s re fe re
46、n c e sto instances of class Pr o du c t.Cons t r u ct or and public methods:p u b lic Cat a 1 og()Creates th e col 1 e c t i o n products,whi c h i sinitial!y empty.p u b li c vo id a d dProduct(P ro d u ct p r oduct)Adds t h e sp e cifi e dprodu c t to the co llectio n products.p u b li c I t e ra
47、 tor i t e ra t o r()Return s a n i t eratoro ve r the in s tances in th e c oil e c t ion p r o d u c ts.pu b 1 ic P r o due t getProduct(S tring c o d e J Re t urns a r efere nee t o th e Pr o du c t instance w i th t he sp e c ified co de.Returns n u ll if t he r e are no p r o d u c ts in t he c
48、atalog wi t h the s pe c ified cod e.p u b lic in t g etNu mberO f Pro d ucts()Re t u rns t he num berof i nstances in the c o 1 lect i on products.C 1 a SS 0 r de rThe c la s s Order m a in t ains a list o f order item s.This clas s im p lemen t s th e int er f a c e Ite r able to be i n g a b I e
49、to i t e rate thro u g h the itemsusing the f o r each loop.Instanc e v a riables:i t e m s A n A rra y L i st colie c t i on t hat c o n tains references t o in st a nces of class Order I t em.Const rue tor and pu blie me th ods:p ub 1 i c Order()c r e a t es t h e co 1 1 ect i on i t em s,whi c h
50、i s i nit i all y em p ty.pub 1 ic v o id a d d i t em(O r d e rlte m order I t e m)Ad d s the spe ci f ied o rd e r item to the c ol 1 ec t ion i t ems.p ubl i c v o id remo v eltem(0 r d e r I t em o rd e rlte m)Rem oves t h e s p ecifi e d o rder item from the col 1 e c tio n items.publ i c I t e