JAVA程序设计(09)-----面对对象设计初级应用 龟兔赛跑

来源:互联网 发布:网络设计师是什么 编辑:程序博客网 时间:2024/05/16 14:46

1.乌龟和兔子共有属性和方法 做成父类 避免重复代码

package com.lovo;/** * 类: 动物 * @author Abe * 属性: 名字 步距 总距离 睡觉的日子 */public class Animal {protected String name;protected int step;protected int distance;protected int sleepDay;/** * 构造器 * @param name */public Animal(String name) {this.name = name;}public Animal(){}/** * 方法:获取 名字 行走长度  休息的天数 设置总距离 设置休息天数 * @return */public String getName() {return name;}public int getStep() {return step;}public int getDistance(){return distance;}public int getSleepDay() {return sleepDay;}public void setDistance(int dis) {this.distance += dis;}public void setSleepDay(int slp) {this.sleepDay += slp;}}

2.类:乌龟

package com.lovo;/** * 类 : 乌龟 * @author Abe  * 父类:Animal  * 属性:疲劳的日期 */public class Tortoise extends Animal {private int encourageDay;/** * 构造器 * @param name */public Tortoise(String name) {super(name);}/** * 方法: 走路,每次2~4米 */public void walk() {if (encourageDay == 0 && sleepDay == 0) {step = (int) (Math.random() * 3 + 2);}}/** * 方法:30%鼓励 ,之后3天每天可以走双倍的路程,但是之后的第4天只能休息。 */public void encourage() {if (encourageDay == 0 && sleepDay == 0 && Math.random() >= 0.7) {step = (int) (Math.random() * 3 + 2) * 2;encourageDay = 3;sleepDay = 1;}}/** * 方法: 调用、设置 激励天数 *  * @return */public int getEncourageDay() {return encourageDay;}public void setEncourageDay(int enc) {this.encourageDay += enc;}/** * 方法:每天的行动 1.没激励不睡觉 就看是否发动技能 没发动就普通移动 2.有激励就激励移动 没激励有睡觉就睡觉 * @return 每天活动的结果 */public String everyDay() {if (this.getEncourageDay() == 0 && this.getSleepDay() == 0) {this.encourage();if (this.getEncourageDay() > 0) {this.setDistance(this.getStep());return this.getName() + "发动了技能《底力》,疯狂的向前跑出了" + this.getStep()+ "米!!";} else {this.walk();this.setDistance(this.getStep());return this.getName() + "跑了" + this.getStep() + "米!";}} else if (this.getEncourageDay() != 0) {this.setDistance(this.getStep());this.setEncourageDay(-1);return this.getName() + "的《底力》持续中,它疯狂的向前跑出了" + this.getStep()+ "米!!";} else {this.setDistance(0);this.setSleepDay(-1);return this.getName() + "美美的睡了一觉~";}}}

3.类:兔子

package com.lovo;/** * 类:兔子 * @author Abe * 父类:Animal */public class Rabbit extends Animal {/** * 构造器 */public Rabbit(String name) {super(name);}/** * 方法:跑步 每次可以跑1~16, 但是一次跑步超过6就要休息1天,超过11就要休息2天 */public void run() {step = (int) (Math.random() * 16 + 1);if (step >= 11) {sleepDay = 2;} else if (step >= 6) {sleepDay = 1;}}/** * 方法:没睡觉就跑步 */public String everyDay(){if(this.getSleepDay() == 0 ){this.run();this.setDistance(this.getStep());if (this.getStep() >= 11) {return this.getName() + "不可思议的跑出了" + this.getStep()+ "米!!!~ 它累的不得不休息两天……";}else if(this.getStep() >= 6){return this.getName() + "惊人的跑出了" + this.getStep()+ "米!!~ 它累的不得不休息一天……";}else{return this.getName() + "跑了" + this.getStep()+ "米!";}}else{this.setDistance(0);this.setSleepDay(-1);return this.getName() + "懒洋洋的睡了一觉~~";}}}

4.开始赛跑吧~~:)

package com.lovo;/** * 开始比赛跑步 先跑完100米获胜 * @author Abe */public class Running {public static void main(String[] args) {Tortoise tor = new Tortoise("小乌龟");Rabbit rab = new Rabbit("小兔子");for(int distance = 100 , day = 1 ;tor.getDistance() < 100 && rab.getDistance() < 100 ;day++ ){System.out.println("-----------第" + day + "天-----------");System.out.println(tor.everyDay());System.out.println(rab.everyDay());if(tor.getDistance() >= 100 && rab.getDistance() >= 100){System.out.println("怎么回事?!" + tor.getName() + rab.getName() + "同时冲过了终点!! 居然是平局");}else if(tor.getDistance() >= 100){System.out.println(tor.getName() +"取得了胜利!");}else if(rab.getDistance() >= 100){System.out.println(rab.getName() +"取得了胜利!");}else{System.out.println(tor.getName() + "一共跑了"+ tor.getDistance() + "米!");System.out.println(rab.getName() + "一共跑了"+ rab.getDistance() + "米!");}}}}


0 0
原创粉丝点击