1. Tortoise and rabbit common attributes and methods to make a parent class avoid repeating code
Package com.lovo;/** * Class: Animal * @author Abe * Property: Name step distance sleeping days */public class Animal {protected String name;protected in T step;protected int distance;protected INT sleepday;/** * constructor * @param name */public Animal (String name) {this.name = Nam e;} Public Animal () {}/** * method: Gets the number of days that the name is walking length rest set total distance set rest days * @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. Class: Turtle
Package com.lovo;/** * Class: Cuckold * @author Abe * Parent class: Animal * attribute: Fatigue Date */public Class Tortoise extends Animal {private int E ncourageday;/** * constructor * @param name */public Tortoise (String name) {super (name);} /** * Method: Walk, each time */public void Walk () {if (Encourageday = = 0 && sleepday = 0) {step = (int) (Math.random () * 3 + 2);}} /** * Method: 30% encouragement, 3 days after the next day can walk twice, but after the 4th day can only rest. */public void Encourage () {if (Encourageday = = 0 && Sleepday = = 0 && math.random () >= 0.7) {step = (int ) (Math.random () * 2 + 2) * 2;encourageday = 3;sleepday = 1;}} /** * Method: Call, set excitation days * * @return */public int getencourageday () {return encourageday;} public void Setencourageday (int enc) {this.encourageday + = enc;} /** * Method: Daily action 1. No motivation, no sleep, no start-up skills. 2. There is incentive to move without motivation, to sleep, to go to bed. * @return Results of daily activities */public String everyday () {if (this.gete Ncourageday () = = 0 && this.getsleepday () = = 0) {this.encourage (); if (This.getencourageday () > 0) {this.setdist Ance (This.getstep ()); return This.getname () + "Launched the skills of the" bottom force ", crazy forward run out of" + this.getstep () + "meters!! ";} else {this.walk (); This.setdistance (This.getstep ()); return This.getname () + "Run" + this.getstep () + "Meter!" ";}} else if (this.getencourageday ()! = 0) {this.setdistance (This.getstep ()); This.setencourageday ( -1); return this.getname () + "The bottom force" continued in, it crazy forward ran out of "+ this.getstep () +" meters!! ";} else {this.setdistance (0); This.setsleepday ( -1); return This.getname () + "A Night's Sleep ~";}}
3. Class: Rabbit
Package com.lovo;/** * Class: Rabbit * @author Abe * Parent class: Animal */public class Rabbit extends Animal {/** * constructor */public Rabbit (stri ng name) {super (name);} /** * Method: Run every time can run 1~16, but one run more than 6 will rest 1 days, more than 11 will rest 2 days */public void Run () {step = (int) (Math.random () * + 1); if (Step > = one) {sleepday = 2;} else if (step >= 6) {sleepday = 1;}} /** * Method: Run without sleep */public String everyday () {if (this.getsleepday () = = 0) {this.run (); This.setdistance (This.getstep ()); if (This.getstep () >= 11) {return this.getname () + "incredible run Out" + this.getstep () + "M!!! ~ It was tired of having to rest for two days ... ";} else if (This.getstep () >= 6) {return this.getname () + "amazing run out" + this.getstep () + "M!! ~ It was tired of having to rest a day ... ";} Else{return this.getname () + "ran" + this.getstep () + "M!";}} Else{this.setdistance (0); This.setsleepday ( -1); return This.getname () + "lazy sleep ~ ~";}}
4. Start the Race ~ ~:)
Package com.lovo;/** * Start race run finish 100 meters win * @author Abe */public class Running {public static void main (string[] args) {Tort Oise tor = new Tortoise ("Little Turtle"); Rabbit rab = new Rabbit ("Bunny"); for (int distance = n, day = 1; tor.getdistance () < && rab.getdistance () & Lt ;d ay++) {System.out.println ("-----------" + Day + "Days-----------"); System.out.println (Tor.everyday ()); System.out.println (Rab.everyday ()); if (Tor.getdistance () >= && rab.getdistance () >= 100) { System.out.println ("What's going on?! "+ tor.getname () + rab.getname () +" at the same time rushed over the end!! Actually a draw ");} else if (tor.getdistance () >=) {System.out.println (Tor.getname () + "won! ");} else if (rab.getdistance () >=) {System.out.println (Rab.getname () + "won! ");} Else{system.out.println (Tor.getname () + "altogether ran" + tor.getdistance () + "M!"); System.out.println (Rab.getname () + "altogether ran" + rab.getdistance () + "M!");}}}
Java Programming (-----) The primary application of object-oriented design turtle-Rabbit race