Recently learned Python, understand, but really do not want to write Python code.
I think, Java is my profession and strength, why should I leave it, I am not interested in
I do professional in my own field, other people's field let others do it
First skills, then other words
The algorithm problem of heifer, my mind has always been inflexible, the algorithm is even more, but it doesn't hinder my liking
Cattle cows on the farm.
Every year, a baby heifer.
Cows five-year-old cows
How many cows are on the farm several years?
This is the topic of primary school students, if listed, to find out the law, I believe that the children should be now. But my IQ is not as high as that of the children.
Analysis:
1, the farm has an old cow, can give birth to cows, older than 5 years old, will give birth to a cow every year
2, born heifer, more than 5 years old, can grow into a cow, raw cow
The code is as follows:
Package Util;public class Cows {private int age;//cow's age public cows () {super ();//TODO auto-generated constructor Stub}publi C Cows (int age) {super (); this.age = age;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Conditions for the production of cows: older than 5 years old, return 1 heifer, if less than 5 years old, then return to empty public cows producecow () {if (age>5) {return new cows (1);} return null;}}
Package util;/** * 1. Analysis: 1.1 The farm began with a cow with fertility (older than 5 years), a heifer will be born every year, and the heifer will grow up to be a heifer 5 years from an object-oriented perspective, the ability of a heifer to be a cow object, and we can define it as an internal method; The farm houses cows and calves, and we can The field maintains a variable-length container (set) that stores all cows; over time, the age of the cows is changing and the number of cows on the farm is changing, and we can define in the farm a way to change the farm attributes (number of cows) and cow attributes (age) over time. 1.2 Objects: Farm and Cow (attribute: Age) 1.3 The relationship of the object, the farm and the cow is a combination of the relationship 2. Convert object to Class 2.1 cow object (age and Birth Heifer method Producecow) */import java.util.ArrayList; Import Java.util.list;public class Farm {private static list<cows> cows;//initialization of a new farm, initializing a fertile cow (older than 5 years old) static {cows=new arraylist<cows> (); Cows cow = new cows (5); Cows.add (cow);} Gets the year of the Cow object collection public static list<cows> getcows (int i=0;i<year;i++) {///New Year change, when the number of cows in cattle farm is counted, Re-build a temporary cow circle, circle the existing cows and newborn cows list<cows> tempcows = new arraylist<cows> (); for (cows cow1:cows) {// Traverse the existing cow ring Tempcows.add (COW1);//Add the original cow to the new cow ring Cow1.setage (cow1.getage () +1);//original Cow Age +1cows newcow=cow1.producecow ();// The original cow to judge their own age, if the birth period, automatically generate a calf, not to 5 years old, return to Nullif (newcow!=null) {tempcows.add (Newcow);//Add calf to new cow ring}}cows = tempcows;// Drive all cows in the temporary cow ring into the milkNiu Quan, the original dairy circle will be more and more System.out.printf ("Number of cows in the year%d:%d%n", i+1,cows.size ()); tempcows=null;//remove the temporary cow ring}return cows;}}
java-Fun Algorithm