JAVA programming (09) ----- the initial application of Object design, the tortoise and the hare race

Source: Internet
Author: User

JAVA programming (09) ----- the initial application of Object design, the tortoise and the hare race

1. The common attributes and methods of turtles and rabbits are made into parent classes to avoid repeated code

Package com. lovo;/*** class: Animal * @ author Abe * attribute: name of the day on which the step is from the total distance to sleep */public class Animal {protected String name; protected int step; protected int distance; protected int sleepDay;/*** constructor * @ param name */public Animal (String name) {this. name = name;} public Animal () {}/ *** method: get the name of the walking length, days of rest, set the total distance, and set the days of rest * @ 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. Category: tortoise

Package com. lovo;/*** class: Tortoise * @ author Abe * parent class: Animal * attribute: Date of fatigue */public class Tortoise extends Animal {private int encourageDay; /*** constructor * @ param name */public Tortoise (String name) {super (name);}/*** method: Walk, Every time 2 ~ 4 meters */public void walk () {if (encourageDay = 0 & sleepDay = 0) {step = (int) (Math. random () * 3 + 2);}/*** method: 30% encouragement. You can double the distance every day for the next three days, but you can only rest for the next 4th days. */Public void encourage () {if (encourageDay = 0 & sleepDay = 0 & Math. random () & gt; = 0.7) {step = (int) (Math. random () * 3 + 2) * 2; encourageDay = 3; sleepDay = 1 ;}}/*** method: call and set incentive days ** @ return */public int getEncourageDay () {return encourageDay;} public void setEncourageDay (int enc) {this. encourageDay + = enc;}/*** method: daily Action 1. if you are not motivated to go to bed, you can see whether to launch a skill. If you are not motivated, you can move it normally. 2. if there is an incentive, the mobile phone will be motivated to go to bed if there is no incentive * @ return results of daily activities */pu Blic String everyDay () {if (this. getEncourageDay () = 0 & this. getSleepDay () = 0) {this. encourage (); if (this. getEncourageDay ()> 0) {this. setDistance (this. getStep (); return this. getName () + "initiated the skill" base force "and ran out of the game frantically" + this. getStep () + "meter !! ";} 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 () + "bottom force" continues, and it ran wildly forward. "+ this. getStep () + "meter !! ";}Else {this. setDistance (0); this. setSleepDay (-1); return this. getName () +" Meimei's sleep ~ ";}}}

3. Category: Rabbit

Package com. lovo;/*** class: Rabbit * @ author Abe * parent class: Animal */public class Rabbit extends Animal {/*** constructor */public Rabbit (String name) {super (name) ;}/ *** method: You can run 1 ~ 16, but if you have been running for more than 6 times, you have to take one day off. If you have been running for more than 11 times, you have to take two days off. */public void run () {step = (int) (Math. random () * 16 + 1); if (step> = 11) {sleepDay = 2;} else if (step> = 6) {sleepDay = 1 ;}} /*** method: run without going to bed */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 () + "meter !!! ~ It is tired and has to take a rest for two days ...... ";} Else if (this. getStep ()> = 6) {return this. getName () + "surprisingly ran out" + this. getStep () + "meter !! ~ It had to take a day off ...... ";} Else {return this. getName () +" run "+ this. getStep () +" meter! ";}} Else {this. setDistance (0); this. setSleepDay (-1); return this. getName () +" Sleep lazily ~~ ";}}}

4. start racing ~~ :)

Package com. lovo;/*** start the race and run the race first. Run the 100-meter race to win. * @ author Abe */public class Running {public static void main (String [] args) {Tortoise tor = new Tortoise (""); Rabbit rab = new Rabbit ("bunny"); for (int distance = 100, day = 1; tor. getDistance () <100 & rab. getDistance () <100; day ++) {System. out. println ("----------- day +" day ----------- "); System. out. println (tor. everyDay (); System. out. println (rab. everyDa Y (); if (tor. getDistance ()> = 100 & rab. getDistance ()> = 100) {System. out. println ("What's wrong ?! "+ Tor. getName () + rab. getName () +" at the same time rushed to the end !! Actually a draw ");} else if (tor. getDistance ()> = 100) {System. out. println (tor. getName () +" wins! ");} Else if (rab. getDistance ()> = 100) {System. out. println (rab. getName () +" wins! ");} Else {System. out. println (tor. getName () +" total run "+ tor. getDistance () +" meter! "); System. out. println (rab. getName () +" total run "+ rab. getDistance () +" meter! ");}}}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.