实验4接口和内部类
实验四接口和内部类
一、实验目的:熟悉JAVA 中的接口、内部类、匿名类、异常类的概念及用法。二、实验要求:
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);
System.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 部分处理危险品。
Machine
CheckBag(Goods):void
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());
}