第四章习题
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());
}
}
6.2运行结果
因篇幅问题不能全部显示,请点此查看更多更全内容