已阅读5页,还剩64页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
课程编号:面向对象程序设计实验报告姓名学号班级1指导教师实验名称面向对象程序设计实验报告开设学期开设时间第1周第11周报告日期2015.11.11评定成绩评定人评定日期实验1一 实验目的: 加强我自己使用继承来实现Java类的能力二 实验内容:1. 首先根据要求实现超类Product类。以利于以后商品Coffee类, CoffeeBrewer类,Orderitem类的继承。2. 根据要求实现Coffee类,CoffeeBrewer类,Orderitem类。3. 实现如下类图:4.三实验步骤:1.首先根据要求设计出一下的类图。5. 不同的类的具体要求如下(都要继承超类Product类)。(1) Class Producta 含有的变量:code;description;price;b 含有的方法:.public Product(String code,String description,double price) public String getCode().返回实例变量的code. public String getDescription().返回实例变量的 variable description. public double getPrice().返回实例变量的 price. boolean equals(Object object). 重写方法 equals i在类 Object里,如果他们的code是相等的,则产品对象是相等的。 String toString(). 重写方法toString 在类Object里. 返回的格式如下: code_description_pricec 写完具体如下:public class Product private String code;private String description;private double price;public Product(String code,String description,double price) this.code=code;this.description=description;this.price=price;public String getCode() return code;public String getDescription() return description;public double getPrice() return price;Overridepublic boolean equals(Object a) if (a instanceof Product) if (Product) a).code=this.getCode() return true;else return false;return false;Overridepublic String toString() return (this.getCode()+_+this.getDescription()+_+this.getPrice();(2) class Coffeea含有的变量: origin. roast. flavor. aroma. acidity. body. b含有的方法:public Coffee(String code,String description,Double price,String origin,String roast,String flavor,String aroma,String acidity,String body); public String getOrigin().返回实例变量的origin. public String getRoast().返回实例变量的roast. public String getFlavor().返回实例变量的flavor. public String getAroma().返回实例变量的aroma. public String getAcidity().返回实例变量的acidity. public String getBody().返回实例变量的body. String toString(). 重写方法 toString 在类Object里. 返回如下格式:: code_description_price_origin_roast_flavor_aroma_acidity_bodyc写完具体如下:public class Coffee extends Product private String origin;private String roast;private String flavor;private String aroma;private String acidity;private String body;public Coffee(String code,String description,Double price,String origin,String roast,String flavor,String aroma,String acidity,String body)super(code, description, price);this.origin=origin;this.roast=roast;this.flavor=flavor;this.aroma=aroma;this.acidity=acidity;this.body=body;public String getOrigin() return origin;public String getRoast() return roast;public String getFlavor() return flavor;public String getAroma() return aroma;public String getAcidity() return acidity;public String getBody() return body;Overridepublic String toString() return this.getCode()+_+this.getDescription()+_+this.getPrice()+_+this.getOrigin()+_+this.getRoast()+_+this.getFlavor()+_+this.getAroma()+_+this.getAcidity()+_+this.getBody();(3) class CoffeeBrewera含有的变量: model. waterSupply. numberOfCups. b含有的方法: public CoffeeBrewer(String initialCode, String initialDescription, double initialPrice, String initialModel, String initialWaterSupply, int initialNumberOfCups) public String getModel()。返回实例变量的model. public String getWaterSupply().返回实例变量的waterSupply. public int getNumberOfCups().返回实例变量的numberOfCups. String toString(). 重写该方法 toString在类 Object里. 返回如下格式:code_description_price_model_waterSupply_numberOfCupsc写完具体如下:public class CoffeeBrewer extends Productprivate String model;private String waterSupply;private int numberOfCups;public CoffeeBrewer(String code,String description,double price,String model,String waterSupply,int numberOfCups)super(code, description, price);this.model=model;this.waterSupply=waterSupply;this.numberOfCups=numberOfCups;public String getModel() return model;public String getWaterSupply() return waterSupply;public int getNumberOfCups() return numberOfCups;public String toString() return (this.getCode()+_+this.getDescription()+_+this.getPrice()+_+this.getModel()+_+this.getWaterSupply()+_+this.getNumberOfCups();(4)Class OrderItema含有的变量: product. This instance variable represents the one-way association between OrderItem and Product. It contains a reference to a Product object. quantity. The quantity of the product in the order. b含有的方法: public OrderItem(Product initialProduct, int initialQuantity) public Product getProduct(). 返回实例变量的 product, 一个Product 对象 public int getQuantity().返回实例变量的quantity. public void setQuantity(int newQuantity). 设立变量 quantity 以实现 newQuantity. public double getValue().返回实例变量的quantity and price. String toString(). 重写该方法 toString 在类 Object里.返回格式如下:quantity product-code product-pricec写完具体如下:public class OrderItem private Product product;private int quantity;public OrderItem(Product Product,int Quantity) duct=Product;this.quantity=Quantity;public Product getProduct() return product;public int getQuantity() return quantity;public void setQuantity(int newQuantity) this.quantity = newQuantity;public double getValue()return getQuantity()*getProduct().getPrice();Overridepublic String toString() return quantity+ +product.getCode()+ +product.getPrice();四 实验结果:分别测试后都是正确的。结果如下(1)(2)(3)(4)五实验总结:1.通过面向对象的设计,明白了在java中万物皆对象的道理。2,通过实例锻炼了自己的编程思想和编程能力。3.在实验的调试中体会到了程序的维护。实验2一.实验目的:强化你的能力来实现使用集合的类二.实验内容:1.在实验1的基础上,通过增加写class catalog,class gourmetcoffee ,class sales,class order.实现如下类图:三.实验步骤:1.先设计出自己想要实现的类图:2.具体类如下:(1)Class Cataloga.含有的变量 products An ArrayList collection that contains references to instances of class Product. b。含有的方法: public Catalog() 创建一个空集合 products public void addProduct(Product product) 往集合中 增添 product 对象 public Iterator iterator() 返回集合产品实例中的一个迭代器。 public Product getProduct(String code) 返回一个product实例。 public int getNumberOfProducts() 返回集合products. 中product的数量。写完具体如下:import java.util.*;public class Catalog implements Iterable ArrayList products;public Catalog()products=new ArrayList();public void addProduct(Product product) products.add(product);public Iterator getProductsIterator()return products.iterator();private Product product;public Product getProduct(String code) for(Iteratoriterator=iterator();iterator.hasNext();)if (product=iterator.next().getCode().equals(code) return product;return null;public int getNumberOfProducts()return products.size();public Iterator iterator() / TODO Auto-generated method stubreturn products.iterator();(2)Class Order含有的变量 items An ArrayList包含了 OrderItem. 的实例含有的方法: public Order() 创建一个空的集合items public void addItem(OrderItem orderItem) 往集合items. 中增添元素 public void removeItem(OrderItem orderItem) 从集合 items.中国删除元素 public Iterator iterator() 返回一个覆盖该集合 items. 的迭代器。 public OrderItem getItem(Product product) 返回OrderItem 的实例 public int getNumberOfItems() 返回集合 items的大小 public double getTotalCost() 返回总的消费。写完具体如下:import java.util.ArrayList;import java.util.Iterator;public class Order implements IterableArrayListitems;public Order() items=new ArrayList();public void addItem(OrderItem orderItem) items.add(orderItem);public void removeItem(OrderItem orderItem) items.remove(orderItem);Overridepublic Iterator iterator() return items.iterator();private OrderItem item;public OrderItem getItem(Product product) for(Iteratoriterator=items.iterator();iterator.hasNext();)if (item=iterator.next().getProduct().equals(product) return item;return null;public int getNumberOfItems() return items.size();public double getTotalCost() double total=0;for(Iteratoriterator=items.iterator();iterator.hasNext();)total+=iterator.next().getValue();return total;(3)Class Sales含有的变量: orders An ArrayList 集合包含了 Order. 含有的方法:: public Sales() 创建一个空集合orders, public void addOrder(Order order) 往集合orders. 中增添元素 public Iterator iterator() 返回覆盖 orders. 的迭代器 public int getNumberOfOrders() 返回集合orders. 的大小写完具体如下:import java.util.*;public class Sales implements Iterable ArrayList orders;public Sales() orders=new ArrayList();public void addOrder(Order order)orders.add(order);public int getNumberOfOrders()return orders.size();public Iterator iterator() / TODO Auto-generated method stubreturn orders.iterator();(4)在类gourmeCoffee类中按要求增添的代码如下:public void displayNumberOfOrders(Product product) /* PLACE YOUR CODE HERE */int sum=0; for(Iteratoriterator=sales.iterator();iterator.hasNext();)Order order=iterator.next(); if (order.getItem(product)!=null) sum+; stdOut.println(Number of orders that contains the product +product.getCode()+: +sum);/* * Displays the total quantity of product that have has sold for each * product in the catalog. */public void displayTotalQuantityOfProducts() /* PLACE YOUR CODE HERE */for(Iteratorproductiterator=catalog.iterator();productiterator.hasNext();)Product product=productiterator.next();int sum=0; for(Iteratororderiterator=sales.iterator();orderiterator.hasNext();)Order order=orderiterator.next();for(Iteratororderitemiterator=order.iterator();orderitemiterator.hasNext();)OrderItem orderItem=orderitemiterator.next();if (orderItem.getProduct().equals(product) sum+=orderItem.getQuantity(); stdOut.println(product.getCode()+ +sum);四.实验结果。运行结果如下:(1)(2)(3)(4)五.实验总结:1.通过实例编程学会了集合的使用。2.在其中运用迭代器,从而对迭代器有了更加深刻的认识。实验3一.实验目的:增强自己使用简单和策略模式的能力二、实验内容; 1.在实验1的基础上,通过增添接口SalesFormatter ,Class PlainTextSalesFormatter ,Class HTMLSalesFormatter ,Class XMLSalesFormatter 来实现要求的功能。2.实现如下的类图。 三。实验步骤:1.根据要求设计出自己的类图如下:2.具体类如下:(1) Interface SalesFormatter含有的方法: public String formatSales(Sales sales). 生产销售信息的字符串表示形式。具体代码如下:public interface SalesFormatter public String formatSales(Sales sales);(2)Class PlainTextSalesFormatterClass PlainTextSalesFormatter implements 接口 SalesFormatter. 静态变量 singletonInstance.是类 PlainTextSalesFormatter. 的类型。含有的方法: static public PlainTextSalesFormatter getSingletonInstance(). private PlainTextSalesFormatter().声明私有的构造函数,这样就无法访问其他类。私有构造函数使得它不可能为任何其他类来创建类的一个实例plaintextsalesformatter。 public String formatSales(Sales sales). 制作一个字符串,该字符串包含一个纯文本格式的指定的销售信息。每一个订单的销售信息有以下格式: - Order number quantity1 code1 price1 quantity2 code2 price2 . quantityN codeN priceNTotal = totalCost写完具体代码如下:import java.util.Iterator;public class PlainTextSalesFormatter implements SalesFormatterpublic static PlainTextSalesFormatter singletonInstance;public static PlainTextSalesFormatter getSingletonInstance() if (singletonInstance !=null) return singletonInstance;else singletonInstance=new PlainTextSalesFormatter();return singletonInstance;private PlainTextSalesFormatter() Overridepublic String formatSales(Sales sales) String result = ;for(Iteratororderiterator=sales.iterator();orderiterator.hasNext();)Order order=orderiterator.next();result+=-n;result+=Order +order.getNumberOfItems()+n;for(Iteratoritemiterator=order.iterator();itemiterator.hasNext();)OrderItem item=itemiterator.next();result+=item.getQuantity()+ +item.getProduct().getCode()+ +item.getProduct().getPrice()+n;result+=Total = +order.getTotalCost()+n;return result;(3)(3)Class HTMLSalesFormatterClass HTMLSalesFormatter implements 接口SalesFormatter. 这类作为一个单,一个新的对象将不会每次使用HTML格式创建的实现。静态变量 singletonInstance. 是类 HTMLSalesFormatter. 的类型。含有的方法: static public HTMLSalesFormatter getSingletonInstance().静态方法,得到HTMLSalesFormatter单实例类 private HTMLSalesFormatter().声明私有的构造函数,这样就无法访问其他类。私有构造函数使得它不可能为任何其他类来创建类的一个实例HTMLSalesFormatter。 public String formatSales(Sales sales). 产生HTML格式字符串中包含指定的销售信息。如下: Orders Each order in the sales information should begin with horizontal line, that is, an tag. Each order in the sales information should have the following format: Total = totalCost code: code1 quantity: quantity1 price: price1 . code: codeN quantity: quantityN price: priceN where:o quantityX is the quantity of the product. o codeX is the code of the product. o priceX is the price of the product. o totalCost is the total cost of the order. The string should end with the following HTML: 具体代码如下:import java.util.Iterator;public class HTMLSalesFormatter implements SalesFormatterpublic static HTMLSalesFormatter singletonInstance;public static HTMLSalesFormatter getSingletonInstance() if (singletonInstance !=null) return singletonInstance;else singletonInstance=new HTMLSalesFormatter();return singletonInstance;private HTMLSalesFormatter() / TODO Auto-generated constructor stubOverridepublic String formatSales(Sales sales) String result=;String htmlhead=nnOrdersn;result+=htmlhead;for(Iteratoriterator=sales.iterator();iterator.hasNext();)result+=n;Order order=iterator.next();result+=+Total = +order.getTotalCost()+n;for(Iterator itemIterator=order.iterator();itemIterator.hasNext();)OrderItem item=itemIterator.next();result+=n;result+=code: +item.getProduct().getCode()+n;result+=quantity: +item.getQuantity()+n;result+=price: +item.getProduct().getPrice()+n;result+=;result+=n;return result;(4)Class XMLSalesFormatterClass XMLSalesFormatter implements 接口SalesFormatter. 这类作为一个单,一个新的对象将不会每次使用XML格式创建的实现。静态变量 singletonInstance. 是类 XMLSalesFormatter. 的类型。含有的方法: static public XMLSalesFormatter getSingletonInstance().静态方法,得到HTMLSalesFormatter单实例类 private XMLSalesFormatter().声明私有的构造函数,这样就无法访问其他类。私有构造函数使得它不可能为任何其他类来创建类的一个实例XMLSalesFormatter。 public String formatSales(Sales sales). 产生XML格式字符串中包含指定的销售信息。如下: code1 . codeN 具体代码如下: import java.util.Iterator; public class XMLSalesFormatter implements SalesFormatter public static XMLSalesFormatter singletonInstance; public static XMLSalesFormatter getSingletonInstance() if (singletonInstance !=null) return singletonInstance; else singletonInstance=new XMLSalesFormatter(); return singletonInstance; private XMLSalesFormatter() Override public String formatSales(Sales sales) String result=; String xmlhead=n; result+=xmlhead; for (Iteratoriterator=sales.iterator(); iterator.hasNext();) Order order=iterator.next(); result+=n; for(Iterator itemIterator=order.iterator();itemIterator.hasNext();) OrderItem item=itemIterator.next(); result+=+item.getProduct().getCode()+n; result+=n; return result; (4)class gourmetCoffee中增加的代码如下:private void displaySales() /* PLACE YOUR CODE HERE */stdOut.println(salesFormatter.formatSales(sales);/* * Presents the user with a menu of options and executes the * selected task. */private void run() throws IOException int choice = getChoice();while (choice != 0) if (choice = 1) /* PLACE YOUR CODE HERE */setSalesFormatter(PlainTextSalesFormatter.getSingletonInstance();displaySales(); else if (choice = 2) /* PLACE YOUR CODE HERE */setSalesFormatter(HTMLSalesFormatter.getSingletonInstance();displaySales(); else if (choice = 3) /* PLACE YOUR CODE HERE */setSalesForma
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030智慧医疗行业技术应用现状协同效率及投资方向规划分析研究报告
- 2025-2030智慧医疗场景应用条件分析及资本运作规划研究报告
- 2025-2030智慧农业领域市场供需竞争格局投资评估现在规划深层次分析
- 2025-2030智慧农业项目分布合理性和适地性分析综合性实验田间考察测量操作记录单报告
- 硅氧烷深加工项目初步设计
- 2025-2030智慧农业服务行业市场分析与发展趋势及行业竞争格局研究报告
- 从业资格证模拟考试押运及答案解析
- 配电室安全员考试题库及答案解析
- 儿科护理专业简答题库及答案解析
- 新护士岗前考试试题与解析及答案解析
- 光伏网络安全培训
- 面砖铲除施工方案
- 2025年高考物理试卷(甘肃卷)(空白卷)
- 隐蔽工程验收实施方案
- 2025成人高考专升本《艺术概论》试题及答案
- 第八章 健美操教学设计-2025-2026学年初中体育与健康人教版八年级全一册-人教版
- 2025广东食品安全考试题库及答案
- 2025-2026学年高一上学期《新高一政策解读及学科差异化》主题班会课件
- 脏腑推拿课件
- 首检检验记录表
- 医疗质量安全 培训课件
评论
0/150
提交评论