Assignment 14Due Nov 3, on line
Part of
the homework for CS:2820, Fall 2020
|
Consider this little code fragment to compute the 40th Fibonacci number. The code uses class Lazy distributed with the notes for Nov. 19. The loop does only 39 iterations, yet it takes several seconds to print the result.
/* 0 */ Lazyi = ()-> 0; /* 1 */ Lazy j = ()-> 1; /* 2 */ for (int x = 1; x < 40; x++) { /* 3 */ final Lazy ii = i; /* 4 */ final Lazy jj = j; /* 5 */ Lazy k = ()-> ii.eval() + jj.eval(); /* 6 */ i = j; /* 7 */ j = k; /* 8 */ } /* 9 */ System.out.println( "= " + j.eval() );
Rewrite and submit class, Person from MP10 (and no other class) so that it does not use inner classes for scheduling new events. As the code exists in the posted solution, all of the calls to Simulator.schedule are immediately preceeded by the definition of a local inner class for the event that is being scheduled. Your job is to move these all to the outer nesting level.
Submit one file, Person.java on line. This file should contain one public class, Person plus as many other (outer) classes as you need for all the events that need scheduling, each declared as just a class, not a public class.
This job is almost mechanical, but consider the problem of making things readable. You may find that several of the new outer classes are essentially identical except for the values of the parameters to their constructors. Feel free to combine them, but also note the importance of comments to link each new outer class to the place where it is used.
Obviously, you should put all the new outer classes together and not put some before Person and some after. This may will have consequences on the header comments on the whole file. Readability is crucial.
And, of course, the changes you make should not break anything. Working through the file one inner class at a time would make excellent sense, testing after each is moved to the outer level.