JAVA程序编程题Java期末复习.docx

JAVA程序编程题Java期末复习.docx

ID:59254643

大小:14.00 KB

页数:8页

时间:2020-09-08

JAVA程序编程题Java期末复习.docx_第1页
JAVA程序编程题Java期末复习.docx_第2页
JAVA程序编程题Java期末复习.docx_第3页
JAVA程序编程题Java期末复习.docx_第4页
JAVA程序编程题Java期末复习.docx_第5页
资源描述:

《JAVA程序编程题Java期末复习.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、1.编写Java应用程序,要求有3个类:CPU,PC,Test。要求:(1)CPU类有成员变量speed表示CPU的运行速度,有方法setSpeed(intm)设置运行速度,getSpeed()获取运行速度;(2)PC类有成员变量CPU,方法setCPU(CPUc)设置CPU,show()显示CPU的运行速度;(3)Test类为主类,main方法中创建一个CPU对象和一个PC对象,并调用setCPU(CPUc)设置CPU,调用show()方法显示CPU的运行速度。classCPU{intspeed;intgetS

2、peed(){returnspeed;}voidsetSpeed(intspeed){this.speed=speed;}}publicclassPC{CPUcpu;voidsetCPU(CPUcpu){this.cpu=cpu;}voidshow(){System.out.println("cpu速度:"+cpu.getSpeed());}}classTest{publicstaticvoidmain(Stringargs[]){CPUcpu=newCPU();cpu.setSpeed(2000);PCpc=n

3、ewPC();pc.setCPU(cpu);pc.show();}}2.编写Java应用程序,要求有4个类:Animal,Dog,Cat,Simulator。其中:(1)Animal为抽象类,有2个抽象方法cry()和getName(),具体动物可以给出自己的叫声及名称;(2)Dog和Cat类,为Animail的实现类;(3)Simulator类为测试类,该类包含1个main方法和1个playSound(Animalanimal)方法,在main方法中调用playSound方法模拟Dog和Cat的叫声public

4、abstractclassAnimal{publicabstractvoidcry();publicabstractStringgetAnimalName();}classDogextendsAnimal{publicvoidcry(){System.out.println("汪汪···汪汪");}publicStringgetAnimalName(){return"狗";}}classCatextendsAnimal{publicvoidcry(){System.out.println("喵喵···喵喵");}

5、publicStringgetAnimalName(){return"猫";}}classSimulator{publicvoidplaySound(Animalanimal){System.out.print("现在播放"+animal.getAnimalName()+"类的声音:");animal.cry();}publicstaticvoidmain(Stringargs[]){Simulatorsimulator=newSimulator();simulator.playSound(newDog());s

6、imulator.playSound(newCat());}}6.用类描述计算机中CPU的速度和硬盘的容量。要求Java应用程序有4个类,名字分别是PC、CPU、HardDisk和Test是主类。publicclassTest{publicstaticvoidmain(String[]args){CPUcpu=newCPU();cpu.setspeed(2200);HardDiskdisk=newHardDisk();disk.setamount(200);PCpc=newPC();pc.setCPU(cpu);

7、pc.setHardDisk(disk);pc.show();}}classCPU{privateintspeed;CPU(){}voidsetspeed(intm){this.speed=m;}intgetspeed(){returnthis.speed;}}classHardDisk{privateintamount;HardDisk(){}voidsetamount(intm){this.amount=m;}intgetamount(){returnthis.amount;}}classPC{private

8、CPUcpu;privateHardDiskHD;PC(){}voidsetCPU(CPUc){this.HD=h;}voidshow(){System.out.println("cpuspeedis”+this.cpu.getspeed()+"HardDiskamountis"+this.HD.getamount());}}补充1.编程计算8+88+888+8888--

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。