Assignment 3

Due Sep 10 on line

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

From here on in, each week's work will include two separate components, programming, worth half the credit, and questions, worth the other half. Since we are only giving credit for the top 10 scores, each week's work will be worth 10 points, 5 for questions, 5 for programming.

Simple multiple choice questions, 1 point each:

  1. In the road network model, the roads and intersections can be modeled by
    a) a lattice
    b) a tree
    c) a DAG (directed acyclic graph)
    d) a directed graph
    e) an undirected grap

  2. In the epidemic model we could assign people to households several ways. Which of these allows us tight control of the household size according to a pre-determined distribution while creating exactly the specified population:
    a) Set the number h of households to the population divided by the average household size, then create people, assigning each to a randomly selected household.
    b) Repeatedly pick a random household size from the size distribution and assign that many of the remaining people to it until all the people are used up.
    c) Repeatedly pick a random household size from the size distribution and create that many people for that household until the number of created people matches or exceeds the specified population. could assignin students to schools using the same algorithm we use for:
    a) determining how many students are in each household.
    b) connecting roads to intersections in the road network model.
    c) assigning people to households.
    d) assigning shoppers to stores.
    e) determining the workplace size distribution.

  3. Code in a Java static method can only access their own local variables, their own parameters, and ...
    a) instance variables of their class.
    b) private variables of their class.
    c) nothing else.
    d) final variables of their class.
    e) static variables of their class.

  4. Java class scanner allows a program to
    a) scan a stream for misspellings.
    b) pick consecutive words, numbers or other tokens from a stream.
    c) scan a stream and make arbitrary text substitutions.
    d) convert the text of a stream to canonical form.
    e) remove tabs, spaces, newlines and other delimiters from a stream.

Machine Problem 1 -- due Monday, Sept 14 Tuesday, Sept 15.

Write version one of the epidemic model. This should accept input from a text file named by the first command line parameter, where the file has the form:

pop 25 ;
house 3 , 1 ;

All it should do is read the numbers (using a scanner) and create the required number of people (25 for the example input above), then assign them to households so that the household size is uniformly distributed over the range given by the average and variation (3 plus or minus 1 for the example above).

Then, print out the entire collection of people, listing, for each person, their name and the household to which they belong, one line per person. A typical line for one person might look like this

person@896001 place@321045

And, print out the entire database of households, listing the name of the household and the names of the people that belong to it, one line per household. A typical line for one place might look like this

place@321045 person@896001 person@301579

The only rule for names is that the name should be textual and it should identify whether it is a person or place name. Every Java class has a default toString method that meets this requirement, although the strings it produces are ugly.


Your code must be in a single source file named the CLAS Linux system. This implies that the main method is in a class called Epidemic. To submit your solution, use the following Linux shell command:

~dwjones/submit SECTION

Where SECTION is the section number you are registered in, which must be one of 0A01, 0A02, 0A11 or 0A12.

Grading Criteria

If it compiles and runs on our test files, we will look at the source code.

Questions and Answers

A student asked: How do we organize our solutions, as just one source file or multiple files?

One file, named, with public class Epidemic as its main method. That class should contain a public static void main method plus any private methods needed by the main method. Other classes go up front in the same file, as demonstrated in the code discussed in lecture.

A student asked: Do we use class home or class place for the single class that will later be subdivided into subclasses for different kinds of place? Or should we use both?

Any of the above can work, but plan on eventually using both. You either pay the price up front or pay the price by having to edit code later.

A student asked: I'm having trouble figuring out how to read the input.

I advised that students try playing with scanners in little tiny programs. So I demonstrated such a playful exploration of how to read a file containing the specified data, where the name of the data file is given as a command-line argument.
import java.util.Scanner;

public class Fool {
    public static void main(String[] args) {
        try {
            final Scanner in = new Scanner( new File( args[0] ) );
   "pop" );
            int pop = in.nextInt();
   ";" );
   "house" );
            int house = in.nextInt();
   "," );
            int var = in.nextInt();
   ";" );
            System.out.println( "pop = "+pop+" house = "+house+"+/-"+var );
        } catch ( Exception e ) {
            System.out.println( "oops: "+e );

This code is awful. It fails with an ugly exception message and gives no helpful feedback about errors. If the real input gave the right bits in the wrong order, or if someone left out a comma or semicolon, they'd get no help. In the long run, you want your code to be more forgiving. It would be nice if it didn't bomb on missing punctuation, and it would be nice if it allowed the keywords in any order. Perhaps there could be default values for the numbers a person forgets to specify?

A student asked: Is it OK to interactively prompt for input? That is, the program outputs: "Enter population size" and the user types that, etc.?

No. All the discussion in lecture has pointed in one direction: The command line argument, as in the above code, gives a file name holding the name of the file describing the population. This way, you could have a file describing Upper South Amana (population very small), another file describing Lone Tree (bigger) and yet another describing North Liberty (even bigger). Once you get things working, you don't want to retype everything for each run, and when we test your code, we certainly don't want to retype all the input for each student's code.

A student asked: How do we run the program with the expected file? And does it matter what we call the test file.

Suppose you create a file called testfile containing the test data suggested in the assignment. Assuming you use as the name of your source file (you have no choice about this), you'd compile your source file with this shell commad:

And, assuming it compiles with no errors, you'd run it with this:

    java Epidemic testfile

And it doesn't matter what you call your test file, and you have no reason to know what we call our test files, because the file name is a parameter to your main program. This means you can have as many different test files as you want.

A student asked: I am having difficulty understanding what it means to assign people to homes.

Look at the road network example, where we have intersections and roads. Each road is assigned exactly one source intersection, so there is an instance variable (non-static member) of class road that is a pointer to (or a handle for) an intersection. Each road may have many outgoing intersections, so each road has a collection (a linked list in my code) of outgoing roads. The assignment of people to households can be represented identically, with a link from every person to their home and a collection of people in every home identifying members of the household.

A student asked: How do you create a specific number of Person objects Nothing I’ve looked up shows that dynamically creating a variable amount of objects is possible (even wise) in Java.

In our road network model, the buldmodel method reads the input file and creates one intersection object for every line that begins intersection. That is an example of creating a variable number of objects. In the epidemic model, the input file just gives a number of people, but your buldmodel method can use this in a for loop to manufacture that many person objects.

A student asked: What about at the end of assigning people to households, when you throw the final random number and it says create a household of 3 people but there is only one person left. Where do you put that person? Add them to an existing but underpopulated household? Create an under-populated household?

This is a genuinely interesting question because, in a strict sense, the two solutions suggested above each violate the statistical constraints. Suppose the household size is supposed to be from 2 to 6, with an average of 4. Creating a one-person household violates this rule and slightly lowers the expected average household size. Throwing that person into some already-existing household slightly raises the expected household size. In a formal sense, they are different enough that they would not be considered equivalent programs.

For our purposes, though, this kind of underspecification is very typical of real-world problems. If you model a large population, nobody will noticd the difference between the two suggested solutions. Either one will work, so the obvious answer is, do whichever is easier.