《java2实用教程》(第四版)课后答案 第四章-第六章
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
《JA V A实用教程》作业2 第四章习题
4.1程序代码
(1)CPU.java
public class CPU {
int speed;
void setSpeed(int m){this.speed=m;}
int getSpeed(){return this.speed;}
}
(2)HardDisk.java
public class HardDisk {
int amount;
void setAmount(int a){this.amount=a;}
int getAmount(){return this.amount;}
}
(3)pc.java
public class PC {
CPU cpu;
HardDisk HD;
void setCPU(CPU x){cpu=x;}
void setHardDisk(HardDisk h){HD=h;}
void show(){
System.out.print("CPU的速度是:");
System.out.println(" "+this.cpu.getSpeed());
System.out.print("硬盘的大小是:");
System.out.println(" "+this.HD.getAmount()); }
}
(4)TEST.java
public class TEST {
public static void main(String[] args) { CPU c=new CPU();
c.setSpeed(2200);
HardDisk disk=new HardDisk();
disk.setAmount(200);
PC pc=new PC();
pc.setCPU(c);
pc.setHardDisk(disk);
pc.show();
}
}
4.2 运行界面
第五章习题
5.1程序代码
(1)package模拟器;
public class Animal {
void cry(){};
String getAnimalName() {
return null;
}
}
(2)package模拟器;
public class Simulator {
Animal ani;
void playSound(Animal x){
x.cry();
System.out.println("这是"+x.getAnimalName()+"的叫声!"); }
}
(3)package模拟器;
public class Dog extends Animal {
void cry(){System.out.println("汪汪汪!");};
String getAnimalName(){return ("Dog");};
}
(4)package模拟器;
public class Cat extends Animal {
void cry(){System.out.println("喵喵喵!");};
String getAnimalName(){return ("Cat");};
}
(5)package模拟器;
public class Application {
public static void main(String[] args) {
Simulator s=new Simulator();
s.playSound(new Dog());
s.playSound(new Cat());
}
}
5.2运行结果
第六章习题
6.1程序代码
(1)package模拟器;
public class Animal {
void cry(){};
String getAnimalName() {
return null;
}
}
(2)package模拟器;
public class Simulator {
Animal ani;
void playSound(Animal x){
x.cry();
System.out.println("这是"+x.getAnimalName()+"的叫声!"); }
}
(3)package模拟器;
public class Dog extends Animal {
void cry(){System.out.println("汪汪汪!");};
String getAnimalName(){return ("Dog");};
}
(4)package模拟器;
public class Cat extends Animal {
void cry(){System.out.println("喵喵喵!");};
String getAnimalName(){return ("Cat");};
}
(5)package模拟器;
public class Application {
public static void main(String[] args) {
Simulator s=new Simulator();
s.playSound(new Dog());
s.playSound(new Cat());
}
}