Farmer raising cattle

Source: Internet
Author: User

Here I saw a math in http://polaris.blog.51cto.com/1146394/399738:

A cow can have a calf every year when it is 3-10 years old. The ratio of a bull to a cow is 50%. When it is 12 years old, it is sent to a slaughterhouse for purchase.
Now there is a farmer who has a one-year-old cow. When the cow is 3 years old, it is sent to a nearby farm for breeding. How many cows may this farmer have in 40 years,
It is best to write the relevant code or answer ideas, and use object-oriented.

The following code is provided by myself. If there is any error, please correct it ~

Cow. Java:

Package test. question. niu; public class cow {int age; char sex; // F M malchar childsex; // Gender of the recently generated child public cow (INT age, char sex) {This. age = age; this. sex = sex;} public char getchildsex () {return childsex;} public void setchildsex (char childsex) {This. childsex = childsex;} public int getage () {return age;} public void setage (INT age) {This. age = age;} public int getsex () {return sex;} public void setsex (char sex) {This. sex = sex ;}}

Farmer class, farmer. Java

package test.question.niu;import java.util.ArrayList;import java.util.List;public class Farmer {List<Cow> cowList = new ArrayList<Cow>();public Farmer(){Cow cow = new Cow(1,'F');this.cowList.add(cow);}public void addCow(Cow cow){this.cowList.add(cow);}}

Test. Java

Package test. question. niu; import Java. util. arraylist; import Java. util. list;/***** a cow can have a calf every year at the age of 3 to 10. The ratio of a bull to a cow is 50%, when Niu was 12 years old, he sent it to the slaughterhouse to buy it. * Now there is a farmer who has a one-year-old cow. When the cow is 3 years old, it is sent to a nearby farm for breeding. How many cows may this farmer have in 40 years, * It is best to write related code or answer ideas using object-oriented programming. **/Public class test {public static void main (string ARGs []) {int year = 40; farmer = new farmer (); For (INT I = 1; I <= year; I ++) {// every year, every ox increases by one year. For (Int J = 0; I! = 1 & J <farmer. cowlist. size (); j ++) {farmer. cowlist. get (j ). setage (farmer. cowlist. get (j ). getage () + 1);} int size = farmer. cowlist. size (); // number of cattle (Int J = 0; j <size; j ++) {cow = farmer. cowlist. get (j); If (cow. getage ()> = 3 & cow. getage () <= 10 & cow. getsex () = 'F') {// a calf is generated only when the conditions are met. // here, each cow alternate to generate a bull or a cow, obtain the value with a probability of 50%. // you can also use (INT) math. round (math. random () to simulate probability events, char childsex = cow. getchildsex () = 'F '? 'M': 'F'; cow. setchildsex (childsex); // record the Gender cow newcow = New Cow (0, childsex) of the recently generated cattle; // The farmer, 0, just born. cowlist. add (newcow);} else if (cow. getage ()> 11) {// sell farmer if he is later than 11 years old. cowlist. remove (j) ;}} system. out. println ("owned by farmer after 40 years" + farmer. cowlist. size () + "head Ox ");}}

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.