当前位置:文档之家› 实验4 接口和内部类

实验4 接口和内部类

实验4 接口和内部类
实验4 接口和内部类

实验四接口和内部类

一、实验目的:熟悉JA V A中的接口、内部类、匿名类、异常类的概念及用法。

二、实验要求:

1.设计一个动物声音“模拟器”,希望模拟器可以模拟许多动物的叫声,要求如下:

●编写接口Animal

Animal接口有2个抽象方法cry()和getAnimalName(),即要求实现该接口的各种具体的动物给出自己的叫声和种类名称。

●编写模拟器类Simulator

该类有一个playSound(Animal animal)方法,该方法的参数是Animal类型。即参数animal 可以调用实现Animal接口类重写的cry()方法播放具体动物的声音,调用重写的getAnimalName()方法显示动物种类的名称。

●编写实现Animal接口的类:Dog和Cat类

各类的UML图如下所示:

在各类中通过构造方法实现对成员变量的初始化。

●编写主类Application(用户程序)

在主类Application的main方法中至少包含如下代码。

Simulator simulator = new Simulator();

simulator.playSound(new Dog(“藏獒”));

simulator.playSound(new Cat(“加菲猫”));

2.评价成绩:体操比赛计算选手成绩的办法是去掉一个最高分和最低分后再计算平均分,而学校考察一个班级的某科目的考试情况时,是计算全班同学的平均成绩。Gymnastics类和School类都实现了接口ComputerAverage接口,但实现的算法不同。

编写Java程序实现以上功能。

3.货车的装载量:货车要装载一批货物,货物由三种商品组成:电视、计算机和洗衣机。卡车需要计算出整批货物的重量。

要求有一个ComputeGoodsWeight接口,该接口中有一个方法:

public double computeWeight()

有三个实现该接口的类:Television、Computer和WashMachine。这三个类通过实现接口的computeWeight()方法给出自重。

有一个Truck类,该类用ComputeGoodsWeight接口类型的数组作为成员(Truck类面向接口),那么该数组的单元就可以存放Television对象的引用、Computer对象的引用或WashMachine对象的引用。程序能输出Truck对象所装载的货物的总重量。

4.手机专卖店为了促销自己的产品,决定发行内部购物券,但其他商场不能发行该购物券。编写一个MobileShop类(模拟手机专卖店),该类中有一个名字为InnerPurchaseMoney 的内部类(模拟内部购物券)。

程序模板:请按模板要求,将代码替换为Java程序代码。

NewYear.java

class MobileShop{

代码1 //用内部类InnerPurchaseMoney声明对象purchaseMoney1

代码2 //用内部类InnerPurchaseMoney声明对象purchaseMoney2

private int mobileAmount; //手机数量

MobileShop(){

代码3 //创建价值为20000的purchaseMoney1

代码4 //创建价值为10000的purchaseMoney2

}

void setMobileAmount(int m){

mobileAmount=m;

}

int getMobileAmount(){

return mobileAmount;

}

class InnerPurchaseMoney{

int moneyValue;

InnerPurchaseMoney(int m){

moneyValue=m;

}

void buyMobile(){

if(moneyValue>=20000){

mobileAmount=mobileAmount-6;

System.out.println(“用价值”+moneyValue+”的内部购物券买了3部手机”);

}

else if(moneyValue<20000&&moneyValue>=10000){

mobileAmount=mobileAmount-3;

System.out. println(“用价值”+moneyValue+”的内部购物券买了3部手机”);

}

}

}

public class NewYear{

public static void main(String args[]){

MobileShop shop=new MobileShop();

shop.setMobileAmount(30);

Sys tem.out.println(“手机专卖店目前有”+shop.getMobileAmount()+”部手机”);

shop.purchaseMoney1.buyMobile();

shop.purchaseMoney2.buyMobile();

System.out.println(“手机专卖店目前有”+shop.getMobileAmount()+”部手机”);

}

}

5.车站检查危险品的设备,如果发现危险品会发出警告。编程模拟设备发现危险品。

编写一个Exception的子类DangerException,该子类可以创建异常对象,该异常对象调用toShow()方法输出“属于危险品”。

编写一个Machine类,该类的方法checkBag(Goods goods)当发现参数goods是危险品时(goods的isDanger属性是true)将抛出DangerException异常。

程序在主类的main()方法中的try-catch语句的try部分让Machine类的实例调用checkBag(Good goods)方法,如果发现危险品就在try-catch语句的catch部分处理危险品。

6.分析程序,给出运行结果

interface A{

double f(double x,double y);

}

class B implements A {

public double f(double x,double y){

return x*y;

}

int g(int a,int b){

return a+b;

}}

public class E{

public static void main(String args[]){

A a=new B();

System.out.println(a.f(3,5));

B b=(B)a;

System.out.println(b.g(3,5));

}

}

7.分析程序,给出运行结果

interface com{

public void speak();

}

public class E{

public static void main(String args[]){

com p=new com(){

public void speak() {

System.out.println(“p是接口变量”);

}

};

p.speak();

}

}

8.分析程序,给出运行结果

class MyException extends Exception {

String message;

MyException(String str) {

message =str;

}

}

abstract class A {

abstract int f( int x, int y) throws MyException;

}

class B extends A {

int f(int x, int y) throws MyException {

if(x>99 || y>99)

throw new MyException(“乘数超过99”);

return x*y;

}

}

public class E {

public static void main(String args[]) {

A a;

a=new B();

try {

System.out.println(a.f(12,8)+””);

System.out.print(a.f(120,3)+”“);

}

catch(MyException e) {

System.out.print(e.getmessage());

}

}

}

相关主题
文本预览
相关文档 最新文档