Assignment 5Due Feb 24 on line
Part of
the homework for CS:2820, Spring 2021
|
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:
Epidemic.java:141: error: name has private access in Role System.out.println( p.toString() + " " + p.role.name ); ^
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.
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!
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.
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).
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.