1.设计一个动物声音“模拟器”,希望模拟器可以模拟许多动物的叫声,要求如下:
l 编写接口Animal
Animal接口有2个抽象方法cry()和getAnimalName(),即要求实现该接口的各种具体的动物给出自己的叫声和种类名称。
l 编写模拟器类Simulator
该类有一个playSound(Animal animal)方法,该方法的参数是Animal类型。即参数animal可以调用实现Animal接口类重写的cry()方法播放具体动物的声音,调用重写的getAnimalName()方法显示动物种类的名称。
l 编写实现Animal接口的类:Dog和Cat类
在各类中通过构造方法实现对成员变量的初始化。
l 编写主类Application(用户程序)
在主类Application的main方法中至少包含如下代码。
Simulator simulator = new Simulator();
simulator.playSound(new Dog(“藏獒”));
simulator.playSound(new Cat(“加菲猫”));
interface Animal{
void cry();
String getAnimalName();
}
public class Dog implements Animal {
String name;
Dog(String n){
name=n;
}
public void cry(){
System.out.println("汪汪~汪汪~");
}
public String getAnimalName(){
return name;
}
}
public class Cat implements Animal{
String name;
Cat(String n){
name=n;
}
public void cry(){
System.out.println("喵呜~喵呜~");
}
public String getAnimalName(){
return name;
}
}
public class Simulator{
public void playSound(Animal a){
a.cry();
System.out.println("你好呀,我是:"+a.getAnimalName());
}
}
public class main_1 {
public static void main(String args[]){
Simulator simulator = new Simulator();
simulator.playSound(new Dog("藏獒"));
simulator.playSound(new Cat("加菲猫"));
}
}
2.评价成绩:体操比赛计算选手成绩的办法是去掉一个最高分和最低分后再计算平均分,而学校考察一个班级的某科目的考试情况时,是计算全班同学的平均成绩。Gymnastics类和School类都实现了接口ComputerAverage接口,但实现的算法不同。
interface ComputerAverage{
double getAverage();
}
public class School implements ComputerAverage{
double g1,g2,g3,g4,g5;
School(double g1,double g2,double g3,double g4,double g5){
this.g1=g1;
this.g2=g2;
this.g3=g3;
this.g4=g4;
this.g5=g5;
}
public double getAverage(){
return (g1+g2+g3+g4+g5)/5;
}
}
import java.util.Arrays;
public class Gymnastics implements ComputerAverage {
double res;
Gymnastics(double g1,double g2,double g3,double g4,double g5){
double a[]={g1,g2,g3,g4,g5};
//注意下面两行代码要写在构造方法里面,不然会报错
Arrays.sort(a);
res=a[1]+a[2]+a[3];
}
public double getAverage(){
return res/3;
}
}
public class main_2 {
public static void main(String args[]){
Gymnastics g=new Gymnastics(9.0,8.9,9.3,9.2,9.8);
School s=new School(90.0,89.0,93.0,92.0,98.0);
System.out.println("体操比赛平均成绩为:"+g.getAverage());
System.out.println("班级java平均成绩为:"+s.getAverage());
}
}
3.货车的装载量:货车要装载一批货物,货物由三种商品组成:电视、计算机和洗衣机。卡车需要计算出整批货物的重量。
要求有一个ComputeWeight接口,该接口中有一个方法:
public double computeWeight()
有三个实现该接口的类:TV、Computer和WashMachine。这三个类通过覆盖computeWeight()方法给出自重。
有一个Truck类,该类用ComputeWeight接口类型的数组作为数据成员,包含一个能计算货物总重量的方法。
编写测试类输出Truck对象所装载的货物的总重量。
interface ComputeWeight{
double computeWeight();
}
public class TV implements ComputeWeight{
int num;
double weight;
TV(int n,double w){
num=n;
weight=w;
}
public double computeWeight(){
return num*weight;
}
}
public class Computer implements ComputeWeight {
int num;
double weight;
Computer(int n,double w){
num=n;
weight=w;
}
public double computeWeight(){
return num*weight;
}
}
public class WashMachine implements ComputeWeight{
int num;
double weight;
WashMachine(int n,double w){
num=n;
weight=w;
}
public double computeWeight(){
return num*weight;
}
}
public class Truck{
double totw=0;
public double getWeight(ComputeWeight[] c){
for(ComputeWeight i:c){
totw+=i.computeWeight();
}
return totw;
}
}
public class main_3 {
public static void main(String args[]){
ComputeWeight[] c=new ComputeWeight[3];
c[0]=new TV(5,5.5);
c[1]=new Computer(3,4.0);
c[2]=new WashMachine(4,9.9);
Truck t = new Truck();
System.out.println("货车所载货物总重为"+t.getWeight(c)+"kg");
}
}
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.printf(“用价值%d的内部购物券买了6部手机\n”, moneyValue);
}
else if(moneyValue<20000&&moneyValue>=10000){
mobileAmount=mobileAmount-3;
System.out. printf(“用价值%d的内部购物券买了3部手机\n”, moneyValue);
}
}
}
public class NewYear{
public static void main(String args[]){
MobileShop shop=new MobileShop();
shop.setMobileAmount(30);
System.out.printf(“手机专卖店目前有%d部手机\n”, shop.getMobileAmount());
shop.purchaseMoney1.buyMobile();
shop.purchaseMoney2.buyMobile();
System.out.printf(“手机专卖店目前有%d部手机\n”, shop.getMobileAmount());
}
}
class MobileShop{
InnerPurchaseMoney purchaseMoney1; //用内部类InnerPurchaseMoney声明对象purchaseMoney1
InnerPurchaseMoney purchaseMoney2; //用内部类InnerPurchaseMoney声明对象purchaseMoney2
private int mobileAmount; //手机数量
MobileShop(){
purchaseMoney1=new InnerPurchaseMoney(20000); //创建价值为20000的purchaseMoney1
purchaseMoney2=new InnerPurchaseMoney(10000); //创建价值为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.printf("用价值%d的内部购物券买了6部手机\n", moneyValue);
}
else if(moneyValue<20000&&moneyValue>=10000){
mobileAmount=mobileAmount-3;
System.out. printf("用价值%d的内部购物券买了3部手机\n", moneyValue);
}
}
}
}
public class NewYear{
public static void main(String args[]){
MobileShop shop=new MobileShop();
shop.setMobileAmount(30);
System.out.printf("手机专卖店目前有%d部手机\n", shop.getMobileAmount());
shop.purchaseMoney1.buyMobile();
shop.purchaseMoney2.buyMobile();
System.out.printf("手机专卖店目前有%d部手机\n", 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部分处理危险品。
class DangerException extends Exception {
String message;
DangerException() {
message ="属于危险品";
}
public void toShow(){
System.out.println(message);
}
}
public class Goods {
boolean isDanger;
String name;
public void setIsDanger(boolean b){
isDanger=b;
}
public boolean isDanger(){
return isDanger;
}
public void setName(String n){
name=n;
}
public String getName(){
return name;
}
}
public class Machine{
public void CheckBag(Goods g) throws DangerException {
if(g.isDanger()){
throw new DangerException();
}else{
System.out.println(g.getName()+"不是危险品,准予通行\n");
}
}
}
public class main_5 {
public static void main(String args[]){
Machine m=new Machine();
Goods[] g=new Goods[2];
g[0]=new Goods();
g[1]=new Goods();
g[0].setName("薯片");
g[1].setName("手枪");
g[0].setIsDanger(false);
g[1].setIsDanger(true);
for(Goods i:g){
try{
m.CheckBag(i);
}catch(DangerException d){
System.out.print(i.getName());
d.toShow();
System.out.println("正在开启报警系统~");
}
}
}
}
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.printf("%f\n",a.f(3,5));
B b=(B)a;
System.out.printf("%d\n",b.g(3,5));
}
}
首先定义一个接口A 有一个方法double f
然后用B去调用A的接口,重写double f方法,计算两数相乘
同时B自身定义一个方法g,计算两数之和
首先利用多态定义一个对象,计算3*5
然后把a下转型,计算3+5
运行结果为:
15.000000
8
7.分析程序,给出运行结果
interface com{
public void speak();
}
public class E{
public static void main(String args[]){
com p=new com(){
public void speak() {
System.out.printf(“p是接口变量\n”);
}
};
p.speak();
}
}
定义一个接口com,有一个方法 void speak
测试类中定义一个com类型的对象p,同时使用匿名类,重写speak方法
然后利用p去调用speak
输出结果为:p是接口变量
8.分析程序,给出运行结果
class MyException extends Exception {
String message;
MyException(String str) {
message =str;
}
public String getMessage(){
return message;
}
}
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.printf("%d\n", a.f(12,8)+” ”);
System.out.print(a.f(120,3)+” “);
}
catch(MyException e) {
System.out.print(e.getMessage());
}
}
}
首先自定义一个异常,可以得到信息内容
抽象类A定义一个抽象方法
类B继承A,重写方法,判断如果有一个乘数超过了99就使用自定义的异常输出乘数超过99同时返回两数之积
测试类使用多态创建一个对象a,使用try catch来判断异常
输出结果为:
96
乘数超过99