1、 面向对象程序设计 实验指导书 宋航 刘国奇 东北大学软件学院 .9 目 录 面向对象程序设计 0 实验指导书 0 前 言 2 实验要求 3 Experiment 1 Implementing the Collections in the Gourmet Coffee System( 4 Hours) 4 Background 4 Description 4 Files 12 Tasks 13 Experiment 2 Using Desi
2、gn Patterns in the Gourmet Coffee System( 4 Hours) 16 Background 16 Description 16 Files 23 Tasks 23 Experiment 3 Using File I/O in the Gourmet Coffee System( 4 Hours) 26 Background 26 Description 26 Files 31 Tasks 32 Experiment 4 Implementing a GUI for the Gourmet Coffee System( 4 Hours
3、) 35 Background 35 Description 35 Files 38 Tasks 39 前 言 面向对象的思想能够渗透到需求分析、 系统建模、 体系结构设计、 程序设计与实现、 系统测试等多个方面, 它是描述现实世界复杂对象的相当直接而且直观的有效手段, 对于提高系统质量、 开发效率和代码重用率, 都有明显的效果。 《面向对象程序设计》课程是软件工程专业的重要专业基础课程之一, 该门课程注重实践性和实用性, 主要经过面向对象程序设计思想和Java语言结合起来, 让学生掌握面向对象程序设计思想, 以及熟练使用Java语言进行面向对象的编程, 因此学生不能满
4、足于只听懂老师讲授的课堂内容, 看懂书上的程序, 应将课堂教学与实践环节紧密结合, 使得学生加深对讲授内容的理解, 学会上机调试程序。也就是善于发现程序中的错误, 而且能很快地排除这些错误, 使程序能正确运行。 《面向对象程序设计》是结合卡耐基梅隆大学的SSD3而形成的课程, 该课程的教学体系和实验体系都很完整, 而且东北大学软件学院也提供了良好的教学实验环境, 希望同学们能够充分利用实验条件, 认真完成实验, 从实验中得到应有的锻炼和培养。 希望同学们在使用本实验指导书及进行实验的过程中, 能够帮助我们不断地发现问题, 并提出建议, 使《面向对象程序设计》真正能够帮助同学们学习。
5、 实验要求 《面向对象程序设计》课程实验的目的是为了使学生在课堂学习的同时, 经过一系列的实验, 使学生加深了解和更好地掌握《面向对象程序设计》课程教学大纲要求的内容。 在《面向对象程序设计》的课程实验过程中, 要求学生做到: ( 1) 预习实验指导书有关部分, 认真做好实验内容的准备, 就实验可能出现的情况提前作出思考和分析。 ( 2) 仔细观察调试程序过程中出现的各种问题, 记录主要问题, 作出必要说明和分析。 ( 3) 遵守机房纪律, 服从辅导教师指挥, 爱护实验设备。 ( 4) 实验课程不迟到, 如有事不能出席, 所缺
6、实验一般不补。 ( 5) 本实验采用的开发环境为Eclipse, 同学在做实验之前要求熟悉该集成开发环境。 Experiment 1 Implementing the Gourmet Coffee System(4 Hours) Prerequisites, Goals, and Outcomes Prerequisites: Before you begin this exercise, you need mastery of the following: · Object Oriented Programming o Knowledge of class design
7、 § Class attributes § Constructors § Accessor methods § Mutator methods o Knowledge of inheritance § How to implement a specialization/generalization relationship using inheritance Goals: Reinforce your ability to implement Java classes using inheritance. Outcomes: You will demonstra
8、te mastery of the following: · Implementing the constructors, accessors, and mutators of a Java class · Using inheritance to implement a specialization/generalization relationship Background This assignment asks you to implement some of the classes in the Gourmet Coffee System specified on E
9、xercise 2. Description In this assessment, you will implement the classes and relationships illustrated in the following class diagram: Figure 1 Portion of Gourmet Coffee System class diagram The class specifications are as follows: Class Product The class Product models a generic product in
10、 the store. Instance variables: · code. The unique code that identifies the product · description. A short description of the product · price. The price of the product Constructor and methods: · public Product(String initialCode, · String initialDescription, ·
11、 double initialPrice) Constructor that initializes the instance variables code, description, and price. · public String getCode(). Returns the value of instance variable code. · public String getDescription(). Returns the value of instance variable description. · public double getPrice(). Ret
12、urns the value of instance variable price. · boolean equals(Object object). Overrides the method equals in the class Object. Two Product objects are equal if their codes are equal. · String toString(). Overrides the method toString in the class Object. Returns the string representation of a Prod
13、uct object. The String returned has the following format: code_description_price The fields are separated by an underscore ( _ ). You can assume that the fields themselves do not contain any underscores. Class Coffee The class Coffee models a coffee product. It extends class Product. Instance
14、variables: · origin. The origin of the coffee · roast. The roast of the coffee · flavor. The flavor of the coffee · aroma. The aroma of the coffee · acidity. The acidity of the coffee · body. The body of the coffee Constructor and methods: · public Coffee(String initialCode, ·
15、 String initialDescription, · double initialPrice, · String initialOrigin, · String initialRoast, · String initialFlavor, · String initialAroma, · String initialAcidity, · String initialBod
16、y) Constructor that initializes the instance variables code, description, price, origin, roast, flavor, aroma, acidity, and body. · public String getOrigin(). Returns the value of instance variable origin. · public String getRoast(). Returns the value of instance variable roast. · public Strin
17、g getFlavor(). Returns the value of instance variable flavor. · public String getAroma(). Returns the value of instance variable aroma. · public String getAcidity(). Returns the value of instance variable acidity. · public String getBody(). Returns the value of instance variable body. · Stri
18、ng toString(). Overrides the method toString in the class Object. Returns the string representation of a Coffee object. The String returned has the following format: code_description_price_origin_roast_flavor_aroma_acidity_body The fields are separated by an underscore ( _ ). You can assume that
19、the fields themselves do not contain any underscores. Class CoffeeBrewer Class CoffeeBrewer models a coffee brewer. It extends class Product. Instance variables: · model. The model of the coffee brewer · waterSupply. The water supply (Pour-over or Automatic) · numberOfCups. The capacity of t
20、he coffee brewer Constructor and methods: · public CoffeeBrewer(String initialCode, · String initialDescription, · double initialPrice, · String initialModel, · String initialWaterSupply, ·
21、int initialNumberOfCups) Constructor that initializes the instance variables code, description, price, model, waterSupply, and numberOfCups. · public String getModel(). Returns the value of instance variable model. · public String getWaterSupply(). Returns the value of instance variable waterSup
22、ply. · public int getNumberOfCups(). Returns the value of instance variable numberOfCups. · String toString(). Overrides the method toString in the class Object. Returns the string representation of a CoffeeBrewer object. The String returned has the following format: code_description_price_mod
23、el_waterSupply_numberOfCups The fields are separated by an underscore ( _ ). You can assume that the fields themselves do not contain any underscores. Class OrderItem Class OrderItem models an item in an order. Instance variables: · product. This instance variable represents the one-way associ
24、ation between OrderItem and Product. It contains a reference to a Product object. · quantity. The quantity of the product in the order. Constructor and methods: · public OrderItem(Product initialProduct, · int initialQuantity) Constructor that initializes the instance variabl
25、es product and quantity. · public Product getProduct(). Returns the value of the instance variable product, a reference to a Product object. · public int getQuantity(). Returns the value of the instance variable quantity. · public void setQuantity(int newQuantity). Sets the instance variable qu
26、antity to the value of parameter newQuantity. · public double getValue(). Returns the product of quantity and price. · String toString(). Overrides the method toString in the class Object. Returns the string representation of an OrderItem object. The String representation has the following forma
27、t: quantity product-code product-price The fields are separated by a space. You can assume that the fields themselves do not contain any spaces. Test driver classes Complete implementations of the following test drivers are provided in the student archive. Use these test drivers to verify that
28、your code works correctly. · Class TestProduct · Class TestCoffee · Class TestCoffeeBrewer · Class TestOrderItem Files The following files are needed to complete this assignment: · student-files.zip — Download this file. This archive contains the following: o TestProduct.java o Test
29、Coffee.java o TestCoffeeBrewer.java o TestOrderItem.java Tasks Implement classes Product, Coffee, CoffeeBrewer, and OrderItem. Document using Javadoc and follow Sun's code conventions. The following steps will guide you through this assignment. Work incrementally and test each increment. Sav
30、e often. 1. Extract the files by issuing the following command at the command prompt: C:\>unzip student-files.zip 2. Then, implement class Product from scratch. Use TestProduct driver to test your implementation. 3. Next, implement class Coffee from scratch. Use TestCoffee driver to test you
31、r implementation. 4. Then, implement class CoffeeBrewer from scratch. Use TestCoffeeBrewer driver to test your implementation. 5. Finally, implement class OrderItem from scratch. Use TestOrderItem driver to test your implementation. Submission Upon completion, submit only the following: 1. P
32、roduct.java 2. Coffee.java 3. CoffeeBrewer.java 4. OrderItem.java Experiment 2 Implementing the Collections in the Gourmet Coffee System (4 Hours) Prerequisites, Goals, and Outcomes Prerequisites: Before you begin this exercise, you need mastery of the following: · Collections o Use
33、 of class ArrayList o Use of iterators Goals: Reinforce your ability to implement classes that use collections Outcomes: You will demonstrate mastery of the following: · Implementing a Java class that uses collections Background In this assignment, you will implement the classes in the G
34、ourmet Coffee System that use collections. Description The following class diagram of the Gourmet Coffee System highlights the classes that use collections: Figure 2 Gourmet Coffee System class diagram Complete implementations of the following classes are provided in the student archive: · Co
35、ffee · CoffeeBrewer · Product · OrderItem · GourmetCoffee In this assignment, you will implement the following classes: · Catalog · Order · Sales · GourmetCoffee The class specifications are as follows: Class Catalog The class Catalog models a product catalog. This class impleme
36、nts the interface Iterable
37、 products, which is initially empty.
· public void addProduct(Product product) — Adds the specified product to the collection products.
· public Iterator
38、a reference to the Product instance with the specified code. Returns null if there are no products in the catalog with the specified code. · public int getNumberOfProducts() — Returns the number of instances in the collection products. Class Order The class Order maintains a list of order items
39、 This class implements the interface Iterable
40、s the collection items, which is initially empty.
· public void addItem(OrderItem orderItem) — Adds the specified order item to the collection items.
· public void removeItem(OrderItem orderItem) — Removes the specified order item from the collection items.
· public Iterator
41、r() — Returns an iterator over the instances in the collection items. · public OrderItem getItem(Product product) — Returns a reference to the OrderItem instance with the specified product. Returns null if there are no items in the order with the specified product. · public int getNumberOfItems(
42、) — Returns the number of instances in the collection items.
· public double getTotalCost() — Returns the total cost of the order.
Class Sales
The class Sales maintains a list of the orders that have been completed. This class implements the interface Iterable
43、rough the orders using the for-each loop. Instance variables: · orders — An ArrayList collection that contains references to instances of class Order. Constructor and public methods: · public Sales() — Creates the collection orders, which is initially empty. · public void addOrder(Order order
44、) — Adds the specified order to the collection orders.
· public Iterator
45、e creates a console interface to process store orders. Currently, it includes the complete implementation of some of the methods. The methods displayNumberOfOrders and displayTotalQuantityOfProducts are incomplete and should be implemented. The following is a screen shot of the interface: Figure
46、3 Execution of GourmetCoffee Instance variables: · catalog — A Catalog object with the products that can be sold. · currentOrder — An Order object with the information about the current order. · sales — A Sales object with information about all the orders sold by the store. Constructor and p
47、ublic methods: · public GourmetCoffeeSolution() — Initializes the attributes catalog, currentOrder and sales. This constructor is complete and should not be modified. · public void displayCatalog — Displays the catalog. This method is complete and should not be modified. · public void displayPr
48、oductInfo() — Prompts the user for a product code and displays information about the specified product. This method is complete and should not be modified. · public void displayOrder() — Displays the products in the current order. This method is complete and should not be modified. · public void
49、 addModifyProduct() — Prompts the user for a product code and quantity. If the specified product is not already part of the order, it is added; otherwise, the quantity of the product is updated. This method is complete and should not be modified. · public void removeProduct() — Prompts the user fo
50、r a product code and removes the specified product from the current order. This method is complete and should not be modified. · public void saleOrder() — Registers the sale of the current order. This method is complete and should not be modified. · public void displayOrdersSold() — Displays the






