试验4
- 格式:doc
- 大小:82.00 KB
- 文档页数:15
华北电力大学
实验报告
|
|
实验名称接口与实现接口的类
课程名称Java程序设计
|
|
专业班级:信管1301 学生姓名:王雯敏
学号:************ 成绩:
指导教师:张学斌实验日期:2015.4.20
3.实验指导
●可以把实现某一接口的类创建的对象的引用赋给该接口声明的接口变量中,那么该接口变量
就可以调用被类实现的接口方法。
●接口产生的多态就是指不同类在实现同一个接口时可能具有不同的实现方式。
实验2 货车的装载量
1.实验要求
货车要装载一批货物,货物由三种商品组成:电视、计算机和洗衣机。
卡车需要计算出整批货物的重量。
要求有一个ComputerWeight接口,该接口中有一个方法:
public double computer Weight( )
有三个实现该接口的类:Television、Computer和WashMachine。
这三个类通过实现接口computerTotalSales给出自重。
有一个Truck类,该类用ComputerWeight接口类型的数组作为成员(Truck类面向接口),那么该数组的单元就可以存放Television对象的引用、Computer对象的引用或WashMachine对象的引用。
程序能输出Truck 对象所装载的货物的总重量。
2.程序模板
CheckCarWeight.java
interface ComputerWeight {
public double computeWeight();
}
class Television implements ComputerWeight {
【代码1】//重写computeWeight()方法
}
class Computer implements ComputerWeight {
【代码2】//重写computeWeight()方法
}
class WashMachine implements ComputerWeight {
【代码3】//重写computeWeight()方法
}
class Truck {
ComputerWeight [] goods;
public static void main(String args[]) {
ComputerWeight[] goods=new ComputerWeight[650]; //650件货物
for(int i=0;i<goods.length;i++) { //简单分成三类
if(i%4==0)
goods[i]=new Television();
else if(i%4==1)
goods[i]=new Computer();
else if(i%4==2)
goods[i]=new WashMachine();
else if(i%4==3)
goods[i]=new Refrigerator();
}
Truck truck=new Truck(goods);
System.out.printf("\n货车装载的货物重量:%-8.5f kg\n",truck.getTotalWeights());
}
}
截图
实验3 小狗的状态
1.实验要求
小狗崽不同环境下可能呈现不同的状态表现,要求用接口封装小狗的状态。
具体要求如下:(1)编写一个接口DogState,该接口有一个名字为void showState()的方法。
(2)编写Dog类,该类中有一个DogState接口声明的变量state。
另外,该类有一个show()方法,在该方法中让接口state回调showState()方法。
(3)编写若干个实现DogState接口的类,负责刻画小狗的各种状态。
(4)编写主类,在主类中测试小狗的各种状态。
2.程序模板
interface DogState {
public void showState();
}
}
实验结果
【代码1】
public void showState() {
System.out.println("狂叫,并冲向去很咬敌人");
}
【代码2】
public void showState() {
System.out.println("晃动尾巴,表示欢迎");
}
【代码3】
public void showState() {
System.out.println("嬉戏");
}
3.实验扩展
用面向接口的思想编写程序,模拟水杯中的水在不同温度下可能出现的状态。
代码
interface WaterState {
public void showState();
}
class UnderZero implements WaterState {
public void showState() {
System.out.println("固态");
}
}
class Middle implements WaterState {
public void showState() {
System.out.println("液态");
}
}
class BeyoundOneHundred implements WaterState {
public void showState() {
System.out.println("气态");
}
}
class Water {
WaterState state;
public void show() {
state.showState();
}
public void setState(WaterState s) {
state = s;
}
}
public class CheckWaterState {
public static void main(String args[]) {
Water awater =new Water();
System.out.print("0度以下:");
awater.setState(new UnderZero());
awater.show();
System.out.print("0到100度:");
awater.setState(new Middle());
awater.show();
System.out.print("100度以上:");
awater.setState(new BeyoundOneHundred());
awater.show();
}
}
截图
实验4 二维图形1.实验要求
声明椭圆类Ellipse,实现Area接口,计算椭圆面积
2.程序结果
实验结果
代码
interface Area{
public double computeArea();
}
public class Ellipse implements Area{
double r1,r2;
Ellipse(double r1,double r2){
this.r1=r1;
this.r2=r2;
}
public double computeArea(){
return r1*r2*Math.PI;
}
public static void main(String[] args){
Ellipse e=new Ellipse(2,4);
System.out.print("椭圆面积为:"+puteArea());
}
}
截图
实验5 三维物体
1.实验要求
声明圆柱体类和圆锥类,继承矩形类Rectangle并实现V olume接口,计算表面积和体积2.程序结果。
实验结果
代码
interface Volume{
public double getArea();
public double getV olume();
}
class Rectangle {
double h,r;
}
class Column extends Rectangle implements Volume{
System.out.println("圆柱的体积:"+col.getVolume());
Cone con=new Cone(8.0,4.0,5.0);
System.out.println("圆锥的表面积:"+con.getArea());
System.out.println("圆锥的体积:"+con.getV olume()); }
}
截图。