大数据预科班作业8(一二套)
(第一套)
1. (static 属性)有如下代码
class MyClass{
static int a;
int b;
}
public class TestMain{
public static void main(String args[]){
MyClass mc1 = new MyClass();
MyClass mc2 = new MyClass();
mc1.a = 100;
mc1.b = 200;
mc2.a = 300;
mc2.b = 400;
System.out.println(mc1.a);
System.out.println(mc1.b);
System.out.println(mc2.a);
System.out.println(mc2.b);
}
}
请输出结果:
300
200
300
400
2. (静态成员)有如下代码
class MyClass {
int a;
static int b;
void fa(){}
static void fb(){}
public void m1(){
System.out.println(a); //1
System.out.println(b); //2
fa(); //3
fb(); //4
}
public static void m2(){
System.out.println(a); //5
System.out.println(b); //6
fa(); //7
fb(); //8
}
}
请问哪些行会编译出错?eg
A. //1
B. //2
C. //3
D. //4
E. //5
F. //6
G. //7
H. //8
E
G
3. (静态属性)有如下代码
class MyClass {
static int count = 0;
public MyClass(){
count++;
System.out.println(count);
}
}
public class TestMain{
public static void main(String args[]){
MyClass mc1 = new MyClass();
MyClass mc2 = new MyClass();
MyClass mc3 = new MyClass();
}
}
请写出该程序运行时输出的结果。
1
2
3
4. *(静态初始化代码块)有如下代码
class MyClass{
static int i = 10;
static {
i = 20;
System.out.println("In Static");
}
public MyClass(){
System.out.println("MyClass()");
}
public MyClass(int i){
System.out.println("MyClass(int)");
this.i = i;
}
}
public class TestMain{
public static void main(String args[]){
MyClass mc1 = new MyClass();
System.out.println(mc1.i);
MyClass mc2 = new MyClass(10);
System.out.println(mc2.i);
}
}
请写出该程序运行的结果
In Static
MyClass()
20
MyClass(int)
10
注:静态代码块执行一次
5. (静态方法)有以下代码
class Super{
public static void m1(){
System.out.println("m1 in Super");
}
public void m2(){
System.out.println("m2 in Super");
}
}
class Sub extends Super{
public static void m1(){
System.out.println("m1 in Sub");
}
public void m2(){
System.out.println("m2 in Sub");
}
}
public class TestMain{
public static void main(String args[]){
Super sup = new Sub();
sup.m1();
sup.m2();
Sub sub = (Sub) sup;
sub.m1();
sub.m2();
}
}
写出这个程序的运行结果。
m1 in Super
m2 in Sub
m1 in Sub
m2 in Sub
注:静态方法不能重写
6. (static)*以下哪些论述是正确的
A. 静态方法中不能调用非静态方法t
B. 非静态方法中不能调用静态方法f
C. 静态方法不能被覆盖 t
D. 静态方法能够用类名直接调用 t
E. 可以在不产生任何一个对象的情况下调用静态方法t
F. 静态方法里可以使用 this f
A
C
D
E
7. (final 属性的初始化)*有如下代码
1. class MyClass{
2. final int value;
3. public MyClass(){}
4. public MyClass(int value){
5. this.value = value;
6. }
7. }
8. public class TestMain{
9. public static void main(String args[]){
10. MyClass mc = new MyClass(10);
11. System.out.println(mc.value);
12. }
13. }
选择正确答案:
A. 编译通过,输出 10
B. 编译不通过,把第 2 行改为 final int value = 10;
C. 编译不通过,把第 3 行改为 public MyClass(){ value = 10; }
C
8. (final 变量)*有如下代码
class MyClass {
public void printValue(final int value){
System.out.println(value);
}
public void changeValue(int value){
value = value * 2;
System.out.println(value);
}
}
public class TestMain{
public static void main(String args[]){
MyClass mc = new MyClass();
int value = 5;
final int fvalue = 10;
mc.printValue(value); //1
mc.printValue(fvalue); //2
mc.changeValue(value); //3
mc.changeValue(fvalue);//4
}
}
选择正确答案
A. 编译通过
B. //1 出错
C. //2 出错
D. //3 出错
E. //4 出错
A
9. (final 修饰引用)*有如下代码
class MyValue{
int value;
}
public class TestFinal{
public static void main(String args[]){
final MyValue mv = new MyValue();
mv.value = 100;
//1
System.out.println(mv.value);
}
}
下面说法正确的是:c
A. 编译不通过
B. 编译通过。在//1 处加上:mv.value = 200; 则编译不通过
C. 编译通过。如果在//1 处加上:mv = new MyValue(); 则编译不通过。
C
注:引用数据类型只是地址不能改变,内容可以改变
10. (final 方法,方法覆盖)有如下代码
class Super{
public final void m1(){
System.out.println("m1() in Super");
}
public void m1(int i){
System.out.println("m1(int) in Super");
}
}
class Sub extends Super{
public void m1(int i){
System.out.println("m1(int) in Sub");
}
public void m1(double d){
System.out.println("m1(double) in Sub");
}
}
public class TestMain{
public static void main(String args[]){
Sub s = new Sub();
s.m1();
s.m1(10);
s.m1(1.5);
}
}
以上程序是否能编译通过?如果可以,输出运行的结果;如果不可以,应该怎样
修改?
可以编译通过(亲测)
11. (abstract,方法覆盖)*有以下代码
abstract class MyAbstractClass{
public abstract void m1(); //1
abstract protected void m2(){} //2
}
class MySubClass extends MyAbstractClass{
void m1(){} //3
protected void m2(){} //4
}
问:这段代码哪些地方有错误?
A. //1
B. //2
C. //3
D. //4
B抽象函数没有{}
C一大原则(子类复写方法访问权限>=父类对应方法权限)
12. (abstract)关于 abstract,以下选项正确的是:
A. abstract 类中可以没有 abstract 方法
B. abstract 类的子类也可以是 abstract 类
C. abstract 类不能创建对象,但可以声明引用
D. abstract 方法不能有方法体
A
B
C
D
13. (修饰符综合)*下列方法声明中正确的是:df
A. abstract final void m() f
B. public void final m() f
C. static abstract void m() f
D. private final void m() t
E. private abstract void m() f
F. public static final void m()t
答案:D F
A中abstract 与final冲突
B中void 和final位置放反
C中static与abstract冲突
E中private与abstract冲突
注:(修饰符可互换) 返回值类型 方法名()
14. (abstract)把 Chp6 中的 Shape 类改为抽象类,并把其中的求周长和求面积的方法改为抽象方法。
package chp7;
abstract class Shape{
abstract public double girth();
abstract public double area();
}
class Circle extends Shape{
private double radius;
private double pi = 3.1415926;
public Circle(double radius) {
this.radius = radius;
}
public double area() {
return pi * radius * radius;
}
public double girth() {
return 2 * pi * radius;
}
}
class Rect extends Shape{
private double a;
private double b;
public Rect(){}
public Rect(double a, double b) {
this.a = a;
this.b = b;
}
public double area() {
return a*b;
}
public double girth() {
return 2*(a+b);
}
}
class Square extends Rect{
private double a;
public Square(double a) {
super();
this.a = a;
}
public double area() {
return a*a;
}
public double girth() {
return 4*a;
}
}
15. (abstract)*把 Chp6 中的 Role 类改为抽象类,并把其中的 attack 方法改为抽象方法。
package chp7; abstract class Role{ private String name; //构造方法 public Role(){} public Role(String name) { this.name = name; } // set/get方法 public String getName() { return name; } public void setName(String name) { this.name = name; } //业务方法 abstract public int attack(); } class Magicer extends Role{ private int level = 1; public Magicer(){} public Magicer(String name, int level) { super(name); if (level > 10 || level < 1){ System.out.println("level error!"); return; } this.level = level; } public int getLevel() { return level; } public void setLevel(int level) { if (level<1 || level > 10){ System.out.println("level error!"); return; } this.level = level; } public int attack(){ return level * 5; } } class Soldier extends Role{ private int hurt; public Soldier(){} public Soldier(String name, int hurt) { super(name); this.hurt = hurt; } public int getHurt() { return hurt; } public void setHurt(int hurt) { this.hurt = hurt; } public int attack(){ return hurt; } } class Team{ private Role[] teams = new Role[6]; private int index = 0; public Team(){} public void addMember(Role role){ if (index == 6){ System.out.println("组队已满!"); return; } teams[index] = role; index++; } public int attackSum(){ int sum = 0; for(int i = 0; i<index; pre="" <="" }="" totalattack);="" +="teams[i].attack();" system.out.println(?总攻击伤害为?="" totalattack="team.attackSum();" int="" team.addmember(s2);="" team.addmember(s1);="" team.addmember(m2);="" team.addmember(m1);="" team();="" team="new" 40);="" soldier(?jim?,="" s2="new" soldier="" 20);="" soldier(?john?,="" s1="new" 10);="" magicer(?jerry?,="" m2="new" magicer="" 5);="" magicer(?tom?,="" m1="new" {="" args)="" main(string[]="" void="" static="" public="" testrole="" sum;="" return="" sum="" i++){="">
16. (static)*设计一个类 MyClass,为 MyClass 增加一个 count 属性,用来统计总共创建了多少个对象。
package chp7; public class MyClass { private int count = 0; public MyClass(){ count ++; } public int getCount(){ return count; } }17. (static, final)**修改 Chp6 中的 Account 类,增加两个功能
a) Id 属性做成 final 的 b) 自动分配 id 的功能。当创建多个 Account 对象时,要求 a) Id 自动分配,每个 Account 对象的 id 都不重复; b)自动分配的 id 从 100001 开始。 例如: Account a1 = new Account(); Account a2 = new Account(); System.out.println(a1.getId()); //输出 100001 System.out.println(a2.getId()); //输出 100002
package chp7; class Account{ private final long id; private double balance; private String password; private static long sid = 100000; public Account(){ sid++; id = sid; } public long getId() { return id; } /*注意:要去掉setId方法 public void setId(long id) { this.id = id; }*/ public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public String getPassword() { return null; } public void setPassword(String password) { if (password.length() != 6) return; this.password = password; } } public class TestAccount{ public static void main(String args[]){ Account a1 = new Account(); Account a2 = new Account(); System.out.println(a1.getId()); System.out.println(a2.getId()); } }18. (静态初始化代码块,对象创建的过程)***有以下代码
class ClassA{ static { System.out.println("In ClassA Static"); } public ClassA(){ System.out.println("ClassA()"); } } class ClassB{ static { System.out.println("In ClassB Static"); } public ClassB(){ System.out.println("ClassB()"); } } class ClassC extends ClassB{ static{ System.out.println("In ClassC Static"); } public ClassC(){ System.out.println("ClassC()"); } } class MyClass { static ClassA ca = new ClassA(); ClassC cc = new ClassC(); static{ System.out.println("In MyClass Static"); } public MyClass(){ System.out.println("MyClass()"); } } public class TestMain{ public static void main(String args[]){ MyClass mc1 = new MyClass(); MyClass mc2 = new MyClass(); System.out.println(mc1.cc == mc2.cc); System.out.println(mc1.ca == mc2.ca); } } 写出这个程序运行的结果。
In ClassA Static ClassA() In MyClass Static In ClassB Static In ClassC Static ClassB() ClassC() MyClass() ClassB() ClassC() MyClass() false true(第二套)
1. 代码改错:
interface IA{ void m1(); int a = 100; } class MyClass implements IA{ public void m1(){} } public class TestInterface{ public static void main(String args[]){ IA ia = new MyClass(); ia.m1(); System.out.println(IA.a); } }
//没错 interface IA { void m1(); int a = 100; } class MyClass implements IA { public void m1() { } } public class DE1 { public static void main(String args[]) { IA ia = new MyClass(); ia.m1(); System.out.println(IA.a); } }
2. 代码填空:
interface IA{ void m1(); void m2(); } abstarct class MyClassA implements IA{ public void m1(){} } class MyClassB extends MyClassA{ public void m2(){} }
interface IA { void m1(); void m2(); } abstract class MyClassA implements IA {//修改abstarct->abstract public void m1() { } } class MyClassB extends MyClassA { public void m2() { } } public class DE1 { }
3. 有如下代码:
interface IA{ void ma(); } interface IB extends IA{ void mb(); } interface IC{ void mc(); } interface ID extends IB, IC{ void md(); }
1) 如果有一个类 ClassE 实现 ID 接口,如果不希望 ClassE 是抽象的,则需要实现哪些方法?
void md(); void ma(); void mb(); void mc();2) 把下面的代码补充完整
public class TestClassE{ public static void main(String args[]){ IC ic = new ClassE(); //调用 ma 方法 ______(IA)ic.ma();________ //调用 mb 方法 ______(IB)ic.mb();________ //调用 mc 方法 ______ic.mc();________ //调用 md 方法 ______(ID)ic.md();________ } }
如上:该强转强转
3) 写出下面代码的输出结果
public class TestClassE{ public static void main(String args[]){ IC ic = new ClassE(); System.out.println(ic instanceof IA); System.out.println(ic instanceof IB); System.out.println(ic instanceof IC); System.out.println(ic instanceof ID); System.out.println(ic instanceof ClassE); } }
true true true true4. 把上一章的 Shape 类由抽象类改为接口。
将class改为interface; 将方法的{}去掉;5. *有如下代码:
interface IA{ void ma(); } interface IB{ void mb(); } class MySuper implements IA{ public void ma(){} } class MySub extends MySuper implements IB{ public void mb(){} } public class TestMain{ public static void main(String args[]){ MySuper ms = new MySub(); System.out.println(ms instanceof IA); System.out.println(ms instanceof IB); System.out.println(ms instanceof MySuper); System.out.println(ms instanceof MySub); } }
问:该程序输出结果是什么?
true true true true6. *关于接口和抽象类,下列说法正确的是:
A. 抽象类可以有构造方法,接口没有构造方法 B. 抽象类可以有属性,接口没有属性 C. 抽象类可以有非抽象方法,接口中都是抽象方法 D. 抽象类和接口都不能创建对象 E. 一个类最多可以继承一个抽象类,但是可以实现多个接口
A C D E7. *写出下面代码的输出结果:
interface IA{ void m1(); } class IAImpl1 implements IA{ public void m1(){ System.out.println(“impl1”); } } class IAImpl2 implements IA{ public void m1(){ System.out.println(“impl2”); } } class MyClass{ private IA ia; public MyClass(IA ia){ this.ia = ia; } public void setIa(IA ia){ this.ia = ia; } public void myMethod(){ ia.m1(); } } public class TestMyClass{ public static void main(String args[]){ IA ia1 = new IAImpl1(); MyClass mc = new MyClass(ia1); mc.myMethod(); IA ia2 = new IAImpl2(); mc.setIa(ia2); mc.myMethod(); } }
impl1 impl28. *写出下面代码的输出结果
interface Light{ void shine(); } class RedLight implements Light{ public void shine(){ System.out.println(“Red Light shine in Red”); } } class YellowLight implements Light{ public void shine(){ System.out.println(“Yellow Light shine in Yellow”); } } class GreenLight implements Light{ public void shine(){ System.out.println(“Green Light shine in Green”); } } class Lamp{ private Light light; public void setLight(Light light){ this.light = light; } public void on(){ light.shine(); } } public class TestLamp{ public static void main(String args[]){ Light[] ls = new Light[3]; ls[0] = new RedLight(); ls[1] = new YellowLight(); ls[2] = new GreenLight(); Lamp lamp = new Lamp(); for (int i = 0; i<ls.length; i++){ lamp.setLight(ls[i]); lamp.on(); } } }
Red Light shine in Red Yellow Light shine in Yellow Green Light shine in Green9. *写出下面代码执行的结果
interface JavaTeacher{ void teach(); } class TeacherA implements JavaTeacher{ public void teach(){ System.out.println(“TeacherA teach Java”); } } class TeacherB implements JavaTeacher{ public void teach(){ System.out.println(“TeacherB teach Java”); } } class School{ public static JavaTeacher getTeacher(int i){ if (i == 0) return new TeacherA(); else return new TeacherB(); } } public class TestSchool{ public static void main(String args[]){ JavaTeacher jt = School.getTeacher(0); jt.teach(); jt = School.getTeacher(10); jt.teach(); } }
TeacherA teach Java TeacherB teach Java10. *代码填空
abstract class Animal{ public abstract void eat(); } interface Pet{ void play(); } class Dog extends Animal implements Pet{ public void eat(){ System.out.println(“Dog eat Bones”); } public void play(){ System.out.println(“Play with Dog”); } } class Cat extends Animal implements Pet{ public void eat(){ System.out.println(“Cat eat fish”); } public void play(){ System.out.println(“Play with Cat”); } } class Wolf extends Animal{ public void eat(){ System.out.println(“Wolf eat meat”); } } public class TestMain{ public static void main(String args[]){ Animal as[] = new Animal[3]; as[0] = new Dog(); as[1] = new Cat(); as[2] = new Wolf(); //调用 as 数组中所有动物的 eat 方法 //1 //调用 as 数组中所有宠物的 play 方法 //2 } } //1 处应该填入的代码为:
for (int i = 0; i<as.length; pre="" <="" }="" i++){="" as[i].eat();="">//2 处应该填入的代码为:
for (int i = 0; i<as.length; pre="" <="" }="" i++){="" p.play();="" as[i];="" p="(Pet)" pet="" pet){="" instanceof="" (as[i]="" if="">
11. *定义一个接口 MediaPlayer,表示家庭影院的一个设备。MediaPlayer 中包含 play(),stop(), open()三个方法,分别表示播放、停止和开仓功能。MediaPlayer 有三个实现类,分别为:DVDPlayer,表示 DVD 播放器;CDPlayer,表示 CD 播放器;TapePlayer,表示录音机(播放磁带)。类图如下:
创建一个遥控器 Controller 类。该遥控器有三个控制通道,可以分别控制三个 设备。部分代码如下: class Controller{ private MediaPlayer[] players; public Controller(){ //构造函数中初始化 players 数组 } //对相应的设备调用 play 方法 public void play(int i){ players[i].play(); } } 要求: 1) 完成 MediaPlayer 接口及其子类的代码。 2) 把 Controller 补充完整,完善其构造函数,并为其增加 stop(int i)和 open(int i)方法
package chp8; interface MediaPlayer{ void play(); void stop(); void open(); } class DVDPlayer implements MediaPlayer{ public void open() { System.out.println("DVDPlayer Open"); } public void play() { System.out.println("DVD play"); } public void stop() { System.out.println("DVD stop"); } } class CDPlayer implements MediaPlayer{ public void open() { System.out.println("CDPlayer Open"); } public void play() { System.out.println("CD play"); } public void stop() { System.out.println("CD stop"); } } class TapePlayer implements MediaPlayer{ public void open() { System.out.println("Tape Open"); } public void play() { System.out.println("Tape play"); } public void stop() { System.out.println("Tape stop"); } } class Controller{ private MediaPlayer[] players; public Controller(){ players = new MediaPlayer[3]; players[0] = new DVDPlayer(); players[1] = new CDPlayer(); players[2] = new TapePlayer(); } public void play(int i){ this.players[i].play(); } public void stop(int i){ this.players[i].stop(); } public void open(int i){ this.players[i].open(); } }12. *定义一个 Performer 接口,表示一个演员,接口中定义 perform 方法,表示表演。为这个接口提供若干实现类:Singer,表示歌手;Dancer,表示舞蹈演员;Player,表示演奏者。类图如下:
定义一个 Program 类,表示一个节目。每一个节目需要多个演员配合,因此每 一个 Program 类中都包含一个属性:Performer 数组,表示表演这个节目的所 需要的演员。 给出 Program 的部分代码: class Program { private Performer[] ps; public Program(){ ps = new Performer[3]; ps[0] = new Singer(); ps[1] = new Dancer(); ps[2] = new Player(); } } 在现有代码基础上,为 Program 增加一个 show 方法,在这个方法中,调用所有 表演这个节目的所有 Performer 的 perform 方法。 Program 类图如下:
package chp8; interface Performer{ void perform(); } class Singer implements Performer{ public void perform() { System.out.println("Singer sing a song"); } } class Dancer implements Performer{ public void perform() { System.out.println("Dancer have a dance"); } } class Player implements Performer{ public void perform() { System.out.println("Player play music"); } } class Program { private Performer[] ps; public Program(){ ps = new Performer[3]; ps[0] = new Singer(); ps[1] = new Dancer(); ps[2] = new Player(); } public void show(){ for(int i = 0; i<ps.length; pre="" <="" }="" {="" args)="" main(string[]="" void="" static="" public="" i++){="" p="new" p.show();="" program();="" program="" testprogram="" ps[i].perform();="">
13. *在原有的雇员练习上修改代码
公 司 会 给 SalariedEmployee 每 月 另 外 发 放 2000 元 加 班 费 , 给 BasePlusSalesEmployee 发放 1000 元加班费 改写原有代码,加入以上的逻辑 并写一个方法,打印出本月公司总共发放了多少加班费
package chp8; public class TestEmployee { public static void main(String[] args) { Employee[] es = new Employee[4]; es[0] = new SalariedEmployee("John", 5, 5000); es[1] = new HourlyEmployee("Tom", 10, 25, 170); es[2] = new SalesEmployee("Lucy", 7, 200000, 0.03); es[3] = new BasePlusSalesEmployee("James", 8, 1000000, 0.02, 5000); //ͳ¼Æ¼Ó°à·Ñ double sum = 0; for(int i=0; i160) result=160*this.salaryPerHour+(hours-160)*this.salaryPerHour*1.5; else result=this.hours*this.salaryPerHour; return result+super.getSalary(month); } } class SalesEmployee extends Employee{ private double sales; private double rate; public SalesEmployee(String name, int birthMonth, double sales, double rate) { super(name, birthMonth); this.sales = sales; this.rate = rate; } public double getSalary(int month) { return this.sales*this.rate+super.getSalary(month); } } class BasePlusSalesEmployee extends SalesEmployee implements Overtime{ private double basedSalary; public BasePlusSalesEmployee(String name, int birthMonth, double sales, double rate, double basedSalary) { super(name, birthMonth, sales, rate); this.basedSalary = basedSalary; } public double getSalary(int month) { return this.basedSalary+super.getSalary(month); } public double getOvertimePay() { return 1000; } }14. (强制类型转换)**有如下代码
interface IA { void m1(); public void m2(); public abstract void m3(); } abstract class Super{} class Sub1 extends Super{} class Sub2 extends Super{} public class TestInterface{ public static void main(String args[]){ Super sup = new Sub1(); Sub1 sub1 = (Sub1)sup; //1 } } 在//1 处可以编译(不考虑运行时是否会产生异常)通过的代码为:acd A. Sub2 sub2 = (Sub2) sup; B. Sub2 sub2 = (Sub2) sub1; C. IA ia = (IA) sup; D. IA ia = (IA) sub1;
A C D B中Sub1和Sub2没啥关系15. **有下列代码:
interface ServiceInterface{ void doService1(); void doService2(); void doService3(); } abstract class AbstractService implements ServiceInterface{ public void doService1(){} public void doService2(){} public void doService3(){} } 需要一个实 现 ServiceInterface 接 口的类 MyService,第 一种方式 可以让 MyService 实现 ServiceInterface 接口,即:class MyService implements ServiceInterface 第二种方式可以让 MyService 继承 AbstractService 类,即 class MyService extends AbstractService 请问:这两种方式有什么区别?AbstractService 类有什么作用?
1.实现接口的必须实现其中抽象方法 2.继承类的话不需要在实现方法,可以重写
16. **写出下面代码的运行结果
interface IA{ void ma(IB ib); } interface IB{ void mb(); } class IAImpl implements IA{ public void ma(IB ib){ System.out.println(“ma in IAImpl”); ib.mb(); } } class IBImpl implements IB{ private IA ia; public void setIa(IA ia){ this.ia = ia; } public void mb(){ System.out.println(“mb in IBImpl”); } public void method(){ ia.ma(this); } } public class TestMain{ public static void main(String args[]){ IA ia = new IAImpl(); IBImpl ib = new IBImpl(); ib.setIa(ia); ib.method(); } }
ma in IAImpl mb in IBImpl17. **在之前的游戏角色 Role 程序上进行修改。
1) 创建 Role 接口,包含两个方法: a) int attack(); 表示攻击,返回值表示对敌人的伤害 b) void practise(); 表示练习。练习之后对敌人的伤害会增加。 2) 创建 NamedRole 类,该类为一个抽象类,实现了 Role 接口,并有两个属性: name 和 age, 表示角色的名字和年龄。 3) 增加 MagicStick 接口。该接口表示法师使用的法杖。接口中包含一个方法, 方法为: int fire() 4) 为 MagicStick 类增加两个实现类,分别为 GreenStick 和 BlackStick。其 中,对于这两个类的 fire 方法: a) GreenStick 平时返回 1,夏天(6~8 月)使用时返回 2 b) BlackStic 奇数月返回 1,偶数月返回 2 5) 修改 Magicer 类 a) 为法师类增加 MagicStick 类的属性 stick,表示法师使用的法杖。 b) 让其继承自 NamedRole 类,并实现 attack 和 practise 功能。其中 i. attack 返回值为法师的魔法等级(level) *每一级的固定伤害(5) ii. practise()方法: 1. 当法师的 stick 属性为 null 时,调用 practise 则 level++ 2. 当法师的 stick 不为 null 时,调用 practise 方法时,法师的 等级 level 满足: level = level + 1 + stick.fire();即:法师的 等级增加为 1+stick 属性的 fire 方法的返回值 6) 增加 Weapon 接口,表示战士使用的武器。Weapon 接口中定义了两个方法: void setSoldier(Soldier s); 该方法表示设置武器的使用者 int fire(); 该方法的返回值表示战士使用该武器时,对敌人的伤害值 7) 为 Weapon 增加两个实现了,一个为 Bolo,表示大刀,一个为 Pike,表示 长矛。 对这两个实现类的描述如下: Bolo : 当 soldier 的年龄大于等于 18 岁时,fire 方法返回 100 当 soldier 年龄小于 18 岁时,fire 方法返回 50 Pike:Pike 类有一个属性:name,表示长矛的名字。 当长矛的名字和战士的名字一致时,fire 方法返回 1000; 当长矛的名字和战士的名字不一致时,fire 方法返回 25 8) 修改 Soldier 类 a) 为 Soldier 类增加一个 Weapon 属性,表示战士的武器 b) 让其继承自 NamedRole 类,并实现 attack 和 practise 功能。其中 i. Soldier 的 attack 返回值为战士的 hurt 值与武器的 fire 方法返 回值的和,即 总攻击输出 = 战士的徒手伤害值 + 武器的伤害值 ii. practise()方法:每调用一次则战士的 hurt 值+10 9) 编写相应的测试代码。 要求:两个人一组合作完成。一个人负责把一个整数 n 拆分成两个整数的和, 另一个人负责 写一个函数,判断某一个整数 a 是否是质数
package chp8; import java.util.Date; interface Role{ int attack(); void practise(); } interface MagicStick{ int fire(); } class GreenStick implements MagicStick{ public int fire(){ Date d = new Date(); if (d.getMonth() >=6 && d.getMonth() <= 8){ return 2; }else return 1; } } class BlackStick implements MagicStick{ public int fire(){ Date d = new Date(); if (d.getMonth() % 2 == 0){ return 2; }else return 1; } } interface Weapon{ int fire(); void setSoldier(Soldier s); } class Bolo implements Weapon{ private Soldier s; public void setSoldier(Soldier s){ this.s = s; } public int fire(){ if (s.getAge()>=18) return 100; else return 50; } } class Pike implements Weapon{ private Soldier s; private String name; public Pike(){} public Pike(String name){this.name = name;} public int fire() { if (s.getName().equals(name)){ return 1000; }else { return 25; } } public void setSoldier(Soldier s) { this.s = s; } } abstract class NamedRole implements Role{ private String name; private int age; public NamedRole(){} public NamedRole(String name, int age) { this.name = name; this.age = age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class Magicer extends NamedRole{ private int level = 1; private MagicStick stick; public Magicer(){} public Magicer(String name, int age, int level) { super(name, age); if (level > 10 || level < 1){ System.out.println("level error!"); return; } this.level = level; } public int getLevel() { return level; } public void setLevel(int level) { if (level<1 || level > 10){ System.out.println("level error!"); return; } this.level = level; } public MagicStick getStick() { return stick; } public void setStick(MagicStick stick) { this.stick = stick; } public int attack(){ return level * 5; } public void practise() { level ++; if (stick != null){ level += stick.fire(); } } } class Soldier extends NamedRole{ private int hurt; private Weapon weapon; public Soldier(){} public Soldier(String name,int age, int hurt) { super(name, age); this.hurt = hurt; } public Weapon getWeapon() { return weapon; } public void setWeapon(Weapon weapon) { this.weapon = weapon; } public int getHurt() { return hurt; } public void setHurt(int hurt) { this.hurt = hurt; } public int attack(){ int result = hurt; if (weapon != null){ result += weapon.fire(); } return result; } public void practise() { hurt += 10; } } public class TestRole { public static void main(String[] args) { MagicStick ms1 = new GreenStick(); MagicStick ms2 = new BlackStick(); Magicer m1 = new Magicer("John", 20, 5); Magicer m2 = new Magicer("Tom", 30, 8); m1.setStick(ms1); m2.setStick(ms2); System.out.println(m1.attack()); System.out.println(m2.attack()); m1.practise(); m2.practise(); System.out.println(m1.attack()); System.out.println(m2.attack()); Weapon w1 = new Bolo(); Weapon w2 = new Pike("Jerry"); Soldier s1 = new Soldier("Tom", 19, 100); Soldier s2 = new Soldier("Jerry", 25, 150); //×¢Ò⣬¼ÈÒªµ÷ÓÃSoldierµÄsetWeapon·½·¨£¬Ò²Òªµ÷ÓÃWeaponµÄsetSoldier·½·¨ s1.setWeapon(w1); w1.setSoldier(s1); s2.setWeapon(w2); w2.setSoldier(s2); System.out.println(s1.attack()); System.out.println(s2.attack()); s1.practise(); s2.practise(); System.out.println(s1.attack()); System.out.println(s2.attack()); } }