I stumbled upon this mathematical problem and thought about how to solve it in an object-oriented way. The little brother took advantage of the crappy Java language and the observer model he had just learned, and thought of a small solution. Everyone is interested to see it!
The original question is as follows:
There are a pair of rabbits, from the 3rd month after the birth of a pair of rabbits each month, the rabbit long to the third month after the birth of a pair of rabbits each month, if the rabbit is not dead, ask the number of rabbits each month.
Program:
The periodic object, the control time automatically grows, the object class Clock extends observable {private static Clock Clock; public static int rabbitcount = 0;
The number of existing rabbits public static Clock getinstance () {//single case mode, everyone common one time if (Clock = null) {Clock = new Clock ();
return clock;
public void Run () {//time starts moving for (int i = 1; I <= i++) {//12 month this.setchanged (); This.notifyobservers (i);
Awakened Observer System.out.println ("object-oriented thinking: The number of" + i + "months of rabbits is:" + rabbitcount);
}} class Rubbit implements Observer {//RABBIT (Observer: observation time) int birthmoth;
Public rubbit (int birthmoth) {//initialization of the rabbit's birth time This.birthmoth = Birthmoth;
public void update (observable o, Object month) {integer now = (integer) month; if (Now-birthmoth >= 2) {//If the rabbit reaches three years old can give birth to a pair of small rabbits clock.rabbitcount = 1;//The number of rabbits increases by a pair of clock.getinstance (). addobs Erver (New Rubbit (now));
Turn the little bunny into an observer and tell it the time of its own birth}} public class Rubbitcount {//math thought public static void count () {int f1 = 1;
int F2 = 1;
int f = 0; int M= 12;
System.out.println ("Ordinary Mathematics thought: The 1th month Rabbit quantity is:" + F2);
System.out.println ("Ordinary Mathematics thought: The 2nd month Rabbit quantity is:" + F2);
for (int i = 3; I <= M; i++) {f = F2;
F2 = f1 + F2;
F1 = f;
System.out.println ("General Mathematical thought: the number of" + i + "months of rabbits is:" + F2);
} public static void Main (string[] args) {//object-oriented design Idea----------clock.getinstance (). Addobserver (New Rubbit (1));
Clock.rabbitcount + 1;
Clock.getinstance (). run ();
Math thought count (); }
}
Program results:
Object-oriented thinking: the number of rabbits in the 1th month is: 1
Object-oriented thought: the number of rabbits in the 2nd month is: 1 of the Rabbit's
idea: the number of rabbits for the 3rd month is: 2
Object-oriented thinking: the number of rabbits in the 4th month: 3
Object-oriented thinking: The number of rabbits in the 5th month is: 5
Object-oriented thinking: the number of rabbits in the 6th month is: 8 of the rabbit's target
idea: The number of rabbits for the 7th month is: The
object-oriented thinking: the number of rabbits in the 8th month: the number
of rabbits in the 9th month is :
The object-oriented thinking: the 10th month of the rabbit number is: The
object-oriented thinking: The 11th month of the rabbit number is: The
object-oriented thinking: the number of rabbits in the 12th month: 144
Ordinary Mathematics thought: the number of rabbits in the 1th month is: 1
Ordinary Mathematics thought: The 2nd month Rabbit quantity is: 1
ordinary Mathematics thought: the 3rd month Rabbit quantity is: 2
ordinary Mathematics thought: the number of rabbits of the 4th month is: 3
Ordinary Mathematics thought: the number of rabbits of the 5th month is: 5
General Mathematical thought: the number of rabbits in the 6th month is: 8
general Mathematical thought: the number of rabbits in the 7th month is:
General Mathematics thought: The number of rabbits in the 8th month is:
ordinary mathematics thought: the rabbit number of the 9th month is: The
General Mathematics thought: The number of rabbits in the 10th month is:
General Mathematics thought: The number of rabbits in the 11th month is:
General Mathematics thought: The number of rabbits in the 12th month is: 144