实验4接口和内部类

上传人:ji****n 文档编号:45115884 上传时间:2018-06-15 格式:DOC 页数:5 大小:49KB
返回 下载 相关 举报
实验4接口和内部类_第1页
第1页 / 共5页
实验4接口和内部类_第2页
第2页 / 共5页
实验4接口和内部类_第3页
第3页 / 共5页
实验4接口和内部类_第4页
第4页 / 共5页
实验4接口和内部类_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《实验4接口和内部类》由会员分享,可在线阅读,更多相关《实验4接口和内部类(5页珍藏版)》请在金锄头文库上搜索。

1、实验四实验四 接口和内部类接口和内部类一、实验目的:熟悉 JAVA 中的接口、内部类、匿名类、异常类的概念及用法。 二、实验要求: 1.设计一个动物声音设计一个动物声音“模拟器模拟器” ,希望模拟器可以模拟许多动物的叫声,要求如下:,希望模拟器可以模拟许多动物的叫声,要求如下: 编写接口 Animal Animal 接口有 2 个抽象方法 cry()和 getAnimalName(),即要求实现该接口的各种具体 的动物给出自己的叫声和种类名称。 编写模拟器类 Simulator 该类有一个 playSound(Animal animal)方法,该方法的参数是 Animal 类型。即参数 ani

2、mal 可以调用实现 Animal 接口类重写的 cry()方法播放具体动物的声音,调用重写的 getAnimalName()方法显示动物种类的名称。 编写实现 Animal 接口的类:Dog 和 Cat 类 各类的 UML 图如下所示:在各类中通过构造方法实现对成员变量的初始化。 编写主类 Application(用户程序) 在主类 Application 的 main 方法中至少包含如下代码。 Simulator simulator = new Simulator(); simulator.playSound(new Dog(“藏獒”); simulator.playSound(new C

3、at(“加菲猫”);2评价成绩评价成绩:体操比赛计算选手成绩的办法是去掉一个最高分和最低分后再计算平均分, 而学校考察一个班级的某科目的考试情况时,是计算全班同学的平均成绩。Gymnastics 类 和 School 类都实现了接口 ComputerAverage 接口,但实现的算法不同。 编写 Java 程序实现以上功能。SimulatorplaySound(Animal):voidAnimalcry():void getAnimalName():StringDogString name;cry():void getAnimalName():StringCatString name;cry(

4、):void getAnimalName():String3货车的装载量货车的装载量:货车要装载一批货物,货物由三种商品组成:电视、计算机和洗衣机。 卡车需要计算出整批货物的重量。 要求有一个 ComputeGoodsWeight 接口,该接口中有一个方法: public double computeWeight() 有三个实现该接口的类:Television、Computer 和 WashMachine。这三个类通过实现接 口的 computeWeight()方法给出自重。 有一个 Truck 类,该类用 ComputeGoodsWeight 接口类型的数组作为成员(Truck 类面 向接口

5、) ,那么该数组的单元就可以存放 Television 对象的引用、Computer 对象的引用或 WashMachine 对象的引用。程序能输出 Truck 对象所装载的货物的总重量。4手机专卖店为了促销自己的产品,决定发行内部购物券,但其他商场不能发行该购物券。手机专卖店为了促销自己的产品,决定发行内部购物券,但其他商场不能发行该购物券。 编写一个编写一个 MobileShop 类(模拟手机专卖店)类(模拟手机专卖店) ,该类中有一个名字为,该类中有一个名字为 InnerPurchaseMoney 的内部类(模拟内部购物券)的内部类(模拟内部购物券) 。 程序模板:请按模板要求,将代码替换

6、为 Java 程序代码。NewYear.java class MobileShop代码 1 /用内部类 InnerPurchaseMoney 声明对象 purchaseMoney1 代码 2 /用内部类 InnerPurchaseMoney 声明对象 purchaseMoney2private int mobileAmount; /手机数量MobileShop() 代码 3 /创建价值为 20000 的 purchaseMoney1 代码 4 /创建价值为 10000 的 purchaseMoney2void setMobileAmount(int m) mobileAmount=m; int

7、 getMobileAmount() return mobileAmount; class InnerPurchaseMoneyint moneyValue;InnerPurchaseMoney(int m) moneyValue=m;void buyMobile() if(moneyValue=20000)mobileAmount=mobileAmount-6;System.out.println(“用价值”+moneyValue+”的内部购物券买了 3 部手机”); else if(moneyValue=10000)mobileAmount=mobileAmount-3;System.ou

8、t. println(“用价值”+moneyValue+”的内部购物券买了 3 部手机”); public class NewYearpublic static void main(String args) MobileShop shop=new MobileShop(); shop.setMobileAmount(30); System.out.println(“手机专卖店目前有”+shop.getMobileAmount()+”部手机”); shop.purchaseMoney1.buyMobile(); shop.purchaseMoney2.buyMobile(); System.ou

9、t.println(“手机专卖店目前有”+shop.getMobileAmount()+”部手机”); 5.车站检查危险品的设备,如果发现危险品会发出警告。编程模拟设备发现危险品。车站检查危险品的设备,如果发现危险品会发出警告。编程模拟设备发现危险品。 编写一个 Exception 的子类 DangerException,该子类可以创建异常对象,该异常对象 调用 toShow()方法输出“属于危险品”。 编写一个 Machine 类,该类的方法 checkBag(Goods goods)当发现参数 goods 是危险品 时(goods 的 isDanger 属性是 true)将抛出 Dange

10、rException 异常。 程序在主类的 main()方法中的 try-catch 语句的 try 部分让 Machine 类的实例调用 checkBag(Good goods)方法,如果发现危险品就在 try-catch 语句的 catch 部分处理危险品。Goodsboolean isDanger; String name;setIsDanger(boolean):void isDanger():boolean setName(String):void getName():StringDangerExceptionString message;DangerException() toSh

11、ow():voidMachineCheckBag(Goods):void6分析程序,给出运行结果分析程序,给出运行结果 interface Adouble 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 Epublic static void main(String args)A a=new B();System.out.println(a.f(3,5);B b=(B)a;Syste

12、m.out.println(b.g(3,5); 7分析程序,给出运行结果分析程序,给出运行结果 interface compublic void speak(); public class Epublic 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

13、(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(x99 | y99) 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();

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号