Exam 2: MidtermSolutions and Commentary
Part of
the homework for CS:2820, Fall 2015
|
X Mean = 6.41 X X X X Median = 6.6 X X X X X X X X X X X X X X X X X X X X X X X X _____________X_X_X_X_X_X_X_X_X_X_X_X_X_X_X____ 0 . 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 . 10
X Mean = 10.99 X X X Median = 11.4 X X X X X X X X X X X X X X X X X X X X X X X X X _________X_X_X_X_X_X_X_X_X_X_X_X_X_X___X______ 0 . 2 . 4 . 6 . 8 . 10. 12. 14. 16. 18. 20
Mean = 9.05 X Median = 8.2 X X X X X X X X X X X X X X X X X X X X X X X X X X X X ___X_X_X_X_X_X_X_X_X_X_X_X_X_X_X______ 0 . 2 . 4 . 6 . 8 . 10. 12. 14. 16
X Mean = 17.50 X X X X Median = 17.9 X X X X X X X X X X X X X X X X X X X X X X X X X _________X_____X_____X_X_____X___X_X_X_X_X_X_X_X_X____ 0 . 2 . 4 . 6 . 8 . 10. 12. 14. 16. 18. 20. 22. 24
X X Mean = 36.93 X X X X Median = 37.6 X X X X X X X X X X X X X X X X X X X X _________X_____X_X_______X_X___X_X_X_X_X_X_X_X_X_X_X_X____________ 0 . 4 . 8 . 12. 16. 20. 24. 28. 32. 36. 40. 44. 48. 52. 56. 60 Grades? F D C B A
static void run() { while (!eventSet.isEmpty()) { Event e = eventSet.remove(); e.act.trigger( e.time ); } }
public void inputChange( float t, boolean v ) { Simulator.schedule( t + delay, (float time) -> this.outputChange( time, v ) ); }
a) An event e has two fields, e.time, a floating point number, and e.act, an action. In terms of objects, instances, methods and classes, what is an action? (Do not try to explain what an action does or how!) (1 point)
____ An action is an instance of a sublcass offering a trigger ________ ____ method with a float parameter that performs a specific event. ____ _______________________________________________________________________
1/10 earned full credit, 3/10 earned no credit. The shortest answer earning full credit was "an interface with a static method trigger". The most common error was to try to explain the logic simulator, despite the warning not to do so.
b) This simulator does not have a global variable time serving as a clock that everyone can inspect in order to determine the current time. How does an event-service routine learn what time it was triggered? (1 point)
____ The time is passed as a parameter when the event is triggered. ___ _______________________________________________________________________ _______________________________________________________________________
1/5 earned full credit, 3/10 earned no credit. Again, the common error was to try to explain something about the logic simulator, for example, explaining how the time is computed instead of how it is communicated to the event-service routine.
public void scan( Scanner sc, Listinputs ) // read text of gate public void setDriven( Wire w ) // tell gate that it drives wire w public int inputNumber( String in ) // get input number from input name public String inputName( int in ) // get input name from input number public void checkInputs() // check that all inputs are connected public abstract String toString() // output the textual gate description public abstract void inputChange( float t, int i, boolean v ) // simulate public void outputChange( float t, boolean v ) // simulate
a) Output gates should not be used as a source of data feeding a wire to elsewhere. We can override one of these methods in class Output to make it detect and report this error. Which method? (1 point)
____ Override setDriven() _____________________________________________ _______________________________________________________________________
This may have been the easiest problem on the exam. 6/10 earned full credit, and only a few earned no credit. The most common answer earning partial credit was to propose doing the check in outputChange(); output gates do not naturally have output change events, and if we add them, checking there will delay detecting the error until the simulation is already running, and it may not detect the error if no output change ever arives at the incorrectly wired gate.
b) If a circuit contains any output gates, then you should schedule exactly one outputValues() event (either directly or indirectly, see the previous problem for alternatives that apply). We could use "do it just once" logic in any of several of the methods of class Output to schedule the first outputValues() event. Which is the most appropriate method for this logic, and why? (Note: if you have to understand details of some other part of the program to know whether and when a method will be called, it is less appropriate.) (1 point)
____ Scan() ___________________________________________________________ _______________________________________________________________________
3/10 earned full credit, 1/10 earned no credit. Full credit was offered to those who proposed doing the work in checkInputs() because that does indeed work. The most popular answer earning partial credit, proposed by 1/5, was to do the check in outputChange(); the problem with this is that output gates do not naturally have output change events, and if you set up a logic circuit where the output never changes, you will get no output instead of a graph showing no change. 1/7 proposed doing the work in inputChange(), a better choice except that, again, if there is no change, there will be no output instead of a graph showing no change.
The reference to the previous problem was an error (the order of the problems wasn changed to make things print nicely, and the previous problem became problem 4.
c) The outputValues() method must scan a list (probably a linked list) of all of the output gates declared in the circuit in order to print them. Call this list outputList and declare it in class Output. Give an appropriate declaration and initializaiton for this variable. (1 point)
____ static List
2/5 earned full credit. The static attribute was hardest, and many forgot to say that it was a list of output gates or just a list of gates. A few decided that it should be a list of strings (or something equally off the wall).
d) What method of class Output should add new entries to the outputList discussed in the previous part? (1 point)
____ Scan() ___________________________________________________________ _______________________________________________________________________
This was another easy question; 1/2 got it right. There was hardly any partial credit. The most popular wrong answer, given by 3/10, was to do the job in outputChange(). Again, output gates have no natural output change events, and the list must be built prior to starting the simulation, so doing the job in an event service routine is too late.
e) What combination of the public, private or static attributes apply to the outputValues() method? (1 point)
____ private static ___________________________________________________ _______________________________________________________________________
1/5 earned full credit. 1/10 earned no credit. 3/10 said that the method needs to be public and 1/10 did not understand that it needs to be static.
class SyntaxCheck { public interface ByName { String s(); } public static void lineEnd( Scanner sc, ByName c ) { String s = sc.nextLine(); if (!s.isEmpty()) { Errors.warn( c.s() + " ends with '" + s + "'" ); } } }
A question: Write code to call LineEnd() from elsewhere in the code, passing it the scanner sc and whatever it takes to make c.s() above evaluate to the string "this". (1 point)
____ SyntaxCheck.lineEnd( sc, ()->"this" );____________________________ _______________________________________________________________________
1/10 got this right. and 1/5 earned no credit. An easy common error made by 1/5 of the class was to omit the SyntaxCheck prefix, needed to access this method from elsewhere in the code. 1/5 of the class had difficulty with the type prefix on the lambda expression ()-> that indicates that the lambda argument takes no parameters. 2/5 had serious difficulty with the return value of the lambda argument "this".
Note that the word "both" was accidentally left in alternatives 2 and 3. This made them a bit harder to read, but they still remain distinct enough to make a good exam question.
a) One of these is a bad idea because it depends on obscure properties of the pending event set. Which one and what property? (1 point)
_____ 1. Scheduling both at time zero. This depends on how the _______ _____ event set orders simultaneous events. ___________________________
3/5 got this right, while 1/5 earned no credit. 1/10 identified the correct alternative but gave bad reasons. Among wrong answers, method 2 was the most common, given by almost 1/5.
b) One of these imposes a computational cost on every call to outputValues() long after the header has been printed. Which one and what is the cost? (1 point)
_____ 3. outputValues() calls outputHeader() on the first call. _______ _____ This requires checking for the first call on every call. ________ _______________________________________________________________________
Another easy question, 7/10 got full credit here. 1/10 earned no credit. Partial credit was given to those who explained the cost incorrectly.