# Assignment 5

## Simple multiple choice questions, 1 point each:

1. Look at the code for the solution distributed for MP2. Suppose that class Scanner was not final. What changes would this allow to class MyScanner and its uses?
a) There would be no need to include a constructor.
b) There would be no need to declare methods like hasNext().
c) The getNext() methods would not need to use self.
d) the formal parameter to readCommunity() could be any kind of Scanner.
e) the actual parameter passed to readCommuity() could be a plain ordinary Scanner.

2. Suppose you wanted to add a method to class Person that returned true if the person was infected. This would be a parameterless method with a one line body. Which of the following would work assuming that infectionState was defined in terms of the enumeration States:
a) return infectionState > uninfected;
b) return this.infectionState > States.uninfected;
c) return infectionState.compareTo( uninfected ) > 0;
d) return this.infectionState.compareTo( States.uninfected ) > 0;
e) return (this.infectionState - States.uninfected) > 0;

3. The code for buildCommunity in the distrubuted solution creates people and then decides who to infect based on the number of people remaining who need to be infected infected and the number of people who have not yet been considered for infection pop-i. At each step, one person is considered. The probability that that person will be infected is:
a) infected / (pop-i)
b) (pop-i) / infected
c) pop / (infected-i)
d) (infected-i) / pop
e) none of the above

4. A quick scan through census data and school data reveals that the actual distribution of both household sizes and school sizes follows a log-normal distribution. Java's class Math offers exp() and log() for exponentiation and logarithms base e, and. pow(10,) and log10() for exponentiation and logarithms base 10. Assuming that rand is an instance of Random, which of the following returns a random number with a log-normal distribution?
a) log10( rand.nextGaussian() )
b) log( rand.nextGaussian() )
c) pow( 10, rand.nextDouble() )
d) exp( rand.nextGaussian() )
e) exp( rand.nextDouble() )

5. In the Bash script distributed with the notes for Lecture 9, the code does not test the exit success or failure reported by the program under test. We could record the exit status in the output file by replacing this line

with this:
if java RoadNetwork test 2> tests/out2 ; then
echo "SUCCESS" >> tests/out2
else
echo "FAILURE" >> tests/out2
fi

Where does this put the SUCCESS or FAILURE message?
a) on standard output
b) on standard error
c) at the start of tests/out2
d) at the end of tests/out2
e) somewhere else

## Machine Problem 3 -- due Monday, Sept 28 due Tuesday, Sept 29

Write version 3 of the epidemic model. This should accept input from a text file named by the first command line parameter, where the file format is as in machine-problem 2, with additional fields:

pop 25 ;
house 3.2 , 1 ;         // median and scatter of houshold sizes
infected 12 ;
employed 0.5 ;        // probability of employment
workplace 10.5, 12 ;  // median and scatter of workplace size (syntax error?)

The employed line gives the probability that an individual is employed. 1.0 would indicate a 100% probability of employment, 0.0 would indicate that nobody is employed.

The house and workplace lines give the median number of residents or employees at each such place, and the scatter of the number, where a log-normal distribution is used for the populations of the places. The definitions of median and scatter are on the Wikipedia page for log-normal distributions.

Some math: If r is a random value drawn from a normal distribution with mean 0.0 and standard deviation 1.0 (that is the distribution that nextGaussian() in class Random delivers), then σr+μ comes from a distribution with mean μ and standard deviation σ, and eσr (which is the same as eσreμ) comes from a distribution with median eμ and scatter eσ.

Directly selecting a scatter is not at all intuitive. It would be nice if we could describe a distribution with a pair [ms] where m is the median and s is something like the scatter, but s=0 creates a distribution with a sharp peak, and larger values of s widen the distribution so that when s=m the distribution spreads wide enough that 1 is just included, and as s grows greater than m the distribution spreads wider and wider. A bit of experminting shows that we get this when:

the median eμ = m
the scatter eσ = (s + m)/m

In the simulation input file, house 3.2 , 1 ; sets [ms] to [3.2, 1.0].

Of course, you will need to convert the log-normally distributed random values you get to integer values for the number of employees and household size, since no real workplace or household can have fractional members. Values from 0.0 to 1.0 should be converted to 1, values from 1.0 to 2.0 should be converted to 2, etc., noting that all values returned by the log-normal distribution are strictly positive. That is, the probability of the value 0.0 is exactly 0.0, while nonzero positive values are possible.

To do this assignment, you will need to add a subclass of Person called Employee, and subclasses of place called Home and Workplace.

A student asked: What is the output?

Remember, the purpose of your output is to debug the data structure and prove that you have actually created the necessary data. So, for the previous problem, your output was to be a list of people and a list of households (or places), where each person had a home and each home had a list of occupants.

Now, some people are just people and some are employees. All people have a home, but employees also have employers. So, the list of people now needs to mark, for each person, are they an employee or just a person, and for each place is it a home or a workplace. Homes have lists of residents, workplaces have lists of employees.

This problem is not about creating the right output format! If we continue usig the default class@number format suggested in previous assignments, it would be perfectly OK to produce output like this:

PEOPLE
Employee@15116 Home@14321 Workplace@05100
Person@31700 Home@14321
PLACES
Home14321 Person@31700 Employee@15116
Workplace@05100 Employee@15116

The above is merely a suggestion. The collection of people and places can be printed jumbled, or you can print all the employees first and all the unemployed second. The collection of places can be jumbled, or you can list them separately. What matters is that each person, of any kind, has a home, employees have a workplace, homes have residents who are any kind of person, and workplaces have residents who are only employees.

A student asked: What are we to do with the solution to MP2 you distributed?

Feel free to ignore or use it. You are free to use any solution to MP2 as a starting point for MP3. Yours, mine, or anyone else's. The only rule is, if you use anyone else's code, in part or in full, The program header must credit them as well as you. If you take my solution to MP1 and add your code to make it solve MP2, then credit me first, you second.

If the blending of code gets more complex, for example, if just one class comes from me, give me credit in the Javadoc comment for that class and give me secondary credit in the overal program heading comments. Any class that's 100% your code should have just your @author tag, while classes of mine that you modified can have both tags, and classes you didn't change just have my tag.

A student asked: Do we have to use some sort of random mechanism to assign employed/unemployed status of each person?

Yes. If a person has a 0.5 probability of being employed, then before creating each person, you should flip a coin to determine if you are creating an employee or some other kind of person. In software, this means drawing a value from a pseudorandom number stream.

A student asked: How do we use the mean and scatter values to decide how many people are in a household.

See the example code in LogNormal.java for example Java code that demonstrates how to draw a number from a log-normal distribution. This number could be used, for example, to set a household size, or to set the number of employees at a workplace.

The command line arguments on the LogNormal.java example are in the same order have the same meaning as the arguments to the household size and workplace size distributions.

Your program should work with juat about any numbers that make sense. Median household size x, spread 0.0 would make all your households the same size. A realistic one would be Median size 2, spread 1.2, but you could model some very fertile communities with Median size = 4 spread = 5. Your program should not break for median size 10, spread 10, but that would predict some mothers having really impossible numbers of children (perhaps there are lots of merged households there?). If you use the 10-10 rule for employers, you'd get a community with lots of small businesses.

A student asked: What about spaces before commas and semicolons, are these still required?

You are not required to accept "10.5," as a valid number. Your program must gracefully handle such errors. It might say that "10.5," is not a vaid number and say, in a second message, that it expected a comma, but it can't just say "oops" and output the message that came with the exception. If, by some miracle, you can handle "10.5," as being equivalent to "10.5 ," that's OK, but not expected.