Assignment 5

Due Feb 24 on line

Part of the homework for CS:2820, Spring 2021
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 MP2. Suppose that we changed the name field of class Role to be private.
    a) We would also have to change the constructor.
    b) We would also have to change the findRole method.
    c) We would also have to change the populateRoles method.
    d) We would have to change one of the methods in class Person. — correct
    e) No other changes to the code would be required.

    It was possible to solve this without understanding any code by simply taking the distributed solution to MP2, changing the declaration to private and compiling it. That produces this error message, and no others: error: name has private access in Role
    	    System.out.println( p.toString() + " " + );

    Line 141 of the file is in class Person, so the answer must be d).

    This kind of experiment is useful, but arriving at the answer by inspecting the code teaches you more about what is going on. I recommend this kind of experiment as a way to test your understanding.

  2. Suppose you wanted to make the name and fraction fields for each Role final.
    a) This would require no change to the rest of the code.
    b) Using class MyScanner from lecture 12 would simplify this. — correct
    c) This is not feasible.
    d) This could be done for name but not fraction.
    e) This could be done for fraction but not name.

    Again, you can do the experiment! Doing so leads to lots of error messages warning that both name and fraction were referenced in contexts where they might not have been initialized. So, we rule out answer a). To get farther, you have to find out why the fields couldn't be final.

    The problem is, for each of the variables, there is an if statement where one branch changes the variable and the other does not. The other branch is a call to Errors.fatal, but the Java compiler does not understand that this never returns, so it thinks this branch should also set the variables. From this, we can conclude that d) and e) are wrong, since the problem is the same for both variables.

    All it takes to fix the error is to make nonsense assignments to the variables after the calls to Errors.fatal in the if statements, so c) is wrong.

    What about b)? Class MyScanner moves the if statement inside the call to getNext or getNextFloat. As a result, the calling code makes exactly one assignment to each of the variables in question, making it trivial to declare them as final. So, b) is correct!

  3. To solve Machine Problem 3, each Person object needs:
    a) a Role field
    b) a Place field
    c) some kind of Collection<Place> field
    d) a) and c) — correct
    e) b) and c)
    Each person needs a Role field because when we print out the
    list of people, we need to list each person's role.
    Each person can be associated with more than one place, so b) is not helpful
    but c) seems necessary.  That makes d) correct.

  4. In drawing numbers from the log-normal distribution, the code in computed sigma separately from the final draw from the distribution. In your solution to MP3,
    a) this would be unnecessary.
    b) you could compute sigma just once for each kind of place.
    c) sigma is the standard deviation of the normal distribution.
    d) Each kind of place can have a different sigma.
    e) All of the above. — correct

    You do not need a variable called sigma because you could always write this code for each drawing from the distribution:

    lognormal = Math.exp( Math.log( (scatter + median) / median )
                        * rand.nextGaussian() )
              * median;

    Admittedly, the expression got long and ugly, but it demonstrates that there was no need to compute sigma separately. So, a) true.

    b) is true, since the values on which sigma depends are properties of the category of place and nothing else. This also makes d) true.

    c) is true, I looked it up on Wikipedia to check this.

    Therefore, the answer is e).

  5. The sample solution for MP2 has only one kind of error report, a fatal error. This made testing annoying because ...
    a) we need a separate test file to test the handling of each error. — correct
    b) the program exits with the same exit code no matter what.
    c) it vastly increased the number conditions we had to test.
    d) it prevented automated testing.
    e) all of the above

    It took a separate test file to elicit each error message, so a) is true.

    The program exits with exit(1) for errors and with an implicit exit(0) for successful termination by returning from the main method. This makes b) false.

    The number of conditions that need testing is not increased by making errors fatal. In fact, making them non-fatal may somewhat increase what needs testing. For example, default values substituted for incorrect inputs should not cause additional errors. So, c) is false.

    We demonstrated an automated test script, so d) must be false, and this and other false answers make e) wrong.