Assignment 6

Due Oct 2 on line

Part of the homework for CS:2820, Fall 2020
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

Simple multiple choice questions, 1 point each:

  1. Look at the code for the solution distributed for MP3. In class Place there is a constructor. Why is it needed?
    a) Because subclass constructors have no access to name.
    b) Because the main program calls it to construct each new Place.
    c) Because allPlaces is static.
    d) Because allPlaces is final.
    e) Because name is final.

  2. Look at the code for the solution distributed for MP3. When you run the code, employees output their home, infection state, and workplace in that order. This is because ...
    a) the code of Employee.toString puts them in that order.
    b) the code of Person.toString puts them in that order.
    c) of the way super.toString is called from in Person.
    d) of the way super.toString is called from in Employee.
    e) of the way Epidemic.writeCommunity formats its output.

  3. A Java variable that is protected can also be:
    a) public
    b) private
    c) a local variable of a method
    d) final and static
    e) an index variable of a for loop

  4. The solution to MP3 distributed in class does not ue a factory to create people. Instead, the outer loop in buildCommunity selects the particular subclass to create. As we add more types of people, for example, Student, buildCommunity will get more and more complex. Suppose we tried to isolate buildCommunity from this problem by adding a Person factory somewhere. Where would be the best place for this factory?
    a) As a static method of a new class by itself
    b) As a static method of class Person
    c) As a static method of class Employee
    d) As a static method of class Epidemic
    e) As a non-static (regular) method of class Person

  5. The code for buildCommunity distributed in the posted solution to MP3 creates a community with an unrealistic correlation:
    a) Employees of the same employer never live together.
    b) Employees of the same employer always live together.
    c) Employees of the same employer frequently live together.
    d) Employees of different employers never live together.
    e) None of the above.

Machine Problem 4 -- due Monday, Oct 5

Write version 4 of the epidemic model. This should fix the problem with spaces before commas and semicolons, so the following is now just as legal and has the same meaning as in the code suggested for MP3:

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 example community discription that worked for MP3 (with spaces before all the punctuation) should still work.

In addition, eliminate the unrealistic correlation discussed in problem 5 above. The example code distritubed for MP3 created this problem by the way it created new homes and workplaces in parallel. There are several ways to break this correlation, and unfortunately, the changes required to do so are not confined to class Epidemic.

A student asked: Is it OK to just set the delimiter used to divide the input into strings to include not only space, tab and newline, but also comma and semicolon?

Suppose the input looked like this?

house,3.2;,1;;,;,;;,;// median and scatter of houshold sizes

Would your program accept this? The solution distributed to the previous machine problem, and everything discussed in class, required that the comma and semicolon be present. So suppose you get this input:

house 3.2  1         // median and scatter of houshold sizes

Would your program still output an error message complaining about a missing comma and missing semicolon? Also, the previous solution accepts this:

house   // household description
   3.2, // median
   1;   // scatter

Would that input still work? The assignment was only to remove the requirement for a space before the punctuation, not to eliminate other things that the previous solution accepted, nor to cease checking for missing punctuation.

A student asked: How do we submit.

Exactly the same submission mechanism as was used for the previous assignment.

A student asked: If we just use two loops, one to create people and link them to their households, and another to link employees to workplaces, does that break the correlation?

No, you have to somehow process the people in a different order in one loop than in the other. Shuffling the list, drawing people at random, or some similar strategy will do this.

A student asked: Can we assume that the scatter is an integer?

No. The math for log-normal distributions makes it clear this is a real number, and in Java, that means a float or a double. Note that the the patterns you use to match floats and doubles are the same.

A student asked: Do we really need to use class MyScanner at all?

No, there is and never was a need for class MyScanner. The only purpose of this class was to hide the ugly details of making scanners deal with the input. This statement applies to data abstrction in general. The fact that no program ever needs to use data abstraction does not mean that data abstraction is bad or that it is a bad idea to use it. This is a class on object-oriented programming. Effective use of data abstraction, classe and objects is a central subject in this class.