面向对象程序设计实验报告

  • 格式:pdf
  • 大小:974.36 KB
  • 文档页数:55

课程编号:

面向对象程序设计

实验报告

姓名 学号

班级 1 指导教师

实验名称 面向对象程序设计实验报告

开设学期

开设时间 第1周——第11周

报告日期 2015.11.11

评定成绩 评定人

评定日期

实验1

一. 实验目的:

加强我自己使用继承来实现Java类的能力

二. 实验内容:

1. 首先根据要求实现超类Product类。以利于以后商品Coffee类,

CoffeeBrewer类,Orderitem类的继承。

2. 根据要求实现Coffee类,CoffeeBrewer类,Orderitem类。

3. 实现如下类图:

4.

三.实验步骤:

1.首先根据要求设计出一下的类图。

5. 不同的类的具体要求如下(都要继承超类Product类)。

(1) ClassProduct

a. 含有的变量:

code;

description;

price;

b. 含有的方法:

. public Product(String code,String

description,doubleprice)

 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_price

c. 写完具体如下:

publicclass Product {

private String code; private String description;

privatedoubleprice;

public Product(String code,String description,doubleprice) {

this.code=code; this.description=description;

this.price=price;

}

public String getCode() {

returncode; }

public String getDescription() {

returndescription;

}

publicdouble getPrice() { returnprice; }

@Override

publicboolean equals(Object a) { if (ainstanceof Product) {

if (((Product) a).code==this.getCode()) {

returntrue;

}

else { returnfalse;

}

}

returnfalse;

} @Override

public String toString() {

return (this.getCode()+"_"+this.getDescription()+"_"+this.getPrice());

}

}

(2) class Coffee

a.含有的变量:

 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_acidi

ty_body

c.写完具体如下:

publicclass 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() {

returnorigin;

}

public String getRoast() { returnroast;

}

public String getFlavor() {

returnflavor;

} public String getAroma() {

returnaroma;

}

public String getAcidity() {

returnacidity; }

public String getBody() {

returnbody;

}

@Override public String toString() {

returnthis.getCode()+"_"+this.getDescription()+"_"+this.getPrice()+"_"+thi

s.getOrigin()+"_"+this.getRoast()+"_"+this.getFlavor()+"_"+this.getAroma()+"_"+this.getAcidity()+"_"+this.getBody();

}

}

(3) class CoffeeBrewer

a.含有的变量:

 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_numberOfCups

c.写完具体如下:

publicclass CoffeeBrewer extends Product{

private String model;

private String waterSupply;

privateintnumberOfCups;

public CoffeeBrewer(String code,String description,doubleprice,String

model,String waterSupply,intnumberOfCups){

super(code, description, price);

this.model=model; this.waterSupply=waterSupply;

this.numberOfCups=numberOfCups;

}

public String getModel() { returnmodel;

}

public String getWaterSupply() {

returnwaterSupply; }

publicint getNumberOfCups() {

returnnumberOfCups;

}

public String toString() {

return

(this.getCode()+"_"+this.getDescription()+"_"+this.getPrice()+"_"+this.getModel()+"_"+this.getWaterSupply()+"_"+this.getNumberOfCups());

}

}

(4)Class OrderItem

a.含有的变量:

 product. This instance variable represents the one-way

association between OrderItem and Product. It contains a

reference to a Product object.