Exam 3: Final

Solutions and Commentary

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

Grade Distributions

Exam 3

Mean   = 9.64         X   X
Median = 9.8  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

Exams 1 to 3

Mean   = 20.90              X X
Median = 20.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______
   6 . 8 . 10. 12. 14. 16. 18. 20. 22. 24. 26. 28. 30. 32. 34. 36

Machine Problems 1-6

Mean   = 18.45                    X
Median = 18.3                     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. 18. 20. 22. 24. 26. 28. 30

Note: MP6 was more of a bonus homework, but scored as a machine problem.

Homework Assignments 1-11 (top 10 scores)

                          X
Mean   = 22.77            X
Median = 22.7             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____
   10. 12. 14. 16. 18. 20. 22. 24. 26. 28. 30

Total Scores

                          X
Mean   = 62.13            X
Median = 59.0           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______
   32. 36. 40. 44. 48. 52. 56. 60. 64. 68. 72. 76. 80. 84. 88. 92. 96
      D + +|- - C C C C + +|- - B B B B + +|- - A A A A + +

Solutions and Commentary

  1. Background: A CAD (computer aided design) system allows you to develop 3-dimensional models of parts to be manufactured on CNC (computer numerical control) machines and 3D printers. In early CAD systems, parts were typically defined by points, edges connecting points, planes bounded by edges, and the part volume is bounded by planes. In more advanced systems, a parts can be described as the sum of a part plus or minus an additional volume. For example, a hammer can be described as the sum of a cylinder (the handle) with a brick (the head), minus a small cylinder, the hole in the end of the handle so it can hang on a hook when not in use.

    Please read all 3 parts of this question before answering any parts, in order to avoid duplication between your answers, as much as you can.

    a) Identify the set of basic classes you can infer must exist in an andvanced cad system as described in the above text. (1 point)

    __point___edge___plane___volume___part_________________________________
    
    _______________________________________________________________________
    
    _______________________________________________________________________
    

    1/4 got this, 1/8 earned no credit. Twice as many missed volume and part as missed edge and plane. Classes union, difference, cylinder and brick were accepted but not required. Those who wandered away from the question with things like curve, box, sphere and cone were penalized.

    This should have been easy. The intro chapter in the text "everything is a class" and the methodology we used for both the traffic simulation and the logic simulation emphasized looking at the plain English problem desvcription and finding the classes in the text.

    b) These classes have an order, where a member of one is constructed from members of others which in turn are constructed from others. Give the classes in this order, with the top class first, and the base class(es) last. (1 point)

    __parts made from volumes defined by planes bounded by edges defined___
    
    __by points____________________________________________________________
    
    _______________________________________________________________________
    

    About as many got this as got part a. Some reversed the order. A significant number who earned little or no credit confused the idea of class hierarchy with implementation hierarchy. X is built from Y is not the same thing as X is a kind of Y.

    c) Part of the above description suggests a class hierarchy. What is the base class and what are the extensions to that base class? (1 point)

    __a part (the base class) can be a simple part defined by one volume___
    
    _________or it can be a part with an added volume______________________
    
    _________or it can be a part with a subtracted volume__________________
    

    1/15 earned full credit. a number of students gave one or two of the above for partial credit. Fully half the class mistook class hierarchy for composition or implementation hierarchy.

  2. Background: Jensen's device is an old (invented before 1962) idea for how to write a general-purpose bit of code to compute a sum. Using Jensen's device, the following bits of notation can be made to mean the same thing, where the first bit of notation is classical mathematical notation and the second is in the Algol 60 language (a language you are not expected to know):
    10
    Σ
    x = 1
    x × 2       sigma( x, 1, 10, x * 2 )

    Note that the Algol 60 call sigma() is equivalent to a static method call in Java. Here is the Algol 60 code for sigma():

    integer procedure sigma( i, low, top, fi )
        integer i, low, top, fi;
    begin
        integer s; s := 0;
        for i := low step 1 until top do s := s + fi;
        sigma := s;
    end
    
    The parts of this question have to do with translating Jensen's device into Java. First, of course, sigma() must be made into a method of a class, we'll call it class Jensen.

    a) Consider the first parameter, x is the actual parameter in the example call, i is the formal parameter in the body of Jensen.sigma(). It cannot be int, but must be Integer. Why? (1 point)

    __In Java, int variables are not objects, they are passed by value,____
    
    __Integer variables are full-fledged objects, a handle is passed.______
    
    _______________________________________________________________________
    

    1/4 got this, another 1/4 gave vague answers that might have meant this. Among those receiving no credig, about 1/6 were confused by the relation between the Algol type integer and the Java type Integer.

    b) The second and third parameters can be of type int, easy, but the 4th parameter is the star of this show. In Java, it can be a lambda expression. Give the code for a call to Jensen.sigma() using lambda expressions equivalent to the Algol 60 call given above. (1 point)

    __Jensen.sigma( x, 1, 10, ()->x * 2 );_________________________________
    
    _______________________________________________________________________
    
    _______________________________________________________________________
    

    1/6 got this. 1/6 earned no credit. 1/3 were generally confused but included the -> symbol, earning partial credit. Many were confused by the argument list for the lambda expression, either making it a function of x or worse.

    c) The lambda expression you wrote above is almost equivalent to this:

    Jensen.sigma( x, 1, 10,
            new FunctionOfX() {
                    public int fOfX() { return x * 2; }
            }
    }
    

    The 4th parameter is a new instance of some class. What class? (1 point)

    __an anonymous subclass of class or interface FunctionOfX______________
    
    _______________________________________________________________________
    
    _______________________________________________________________________
    

    Only 1 got this right. 1/4 earned no credit. 5/8 failed to note the key concept here, "anonymous subclass."

  3. Background: Java allows conditional expressions such as (a?b:c). The value of this is b if a is true, and c if a is false. For example, if all the sub-expressions are Boolean, (a?true:b) is equivalent to a||b. Note that, in both (a?true:b) and a||b, b is not evaluated if a is true.

    Suppose you were creating a class Ternary for 3-valued logic, where ternary logic variables take on one of the 3 values true, perhaps or false. You want to create a new operator, Ternary.cond(a,b,c,d) analogous to the built-in conditional operator, except that the Ternary parameter a selects between the three alternatives b, c and d.

    a) How should the parameters b, c and d be passed to Ternary.cond()? (1 point)

    __They need to be lambda expressions so that they are not evaluated____
    
    __until they are selected by the value of parameter a._________________
    
    _______________________________________________________________________
    

    Only 1/10 got this. Fully half the class gave strange answers, while 1/8 gave nonsense relating to conditional expressions involving (?:) notation, and another 1/8 were totally distracted by ternary logic and didn't notice the connection to material discused in class.

    This was disappointing. Given the importance of lambda notation in this semester's simulation framework and given the explicit mention of one parameter only being evaluated when another has a particular value, I would have hoped that more would have gotten the connection.

    b) In declaring the cond() method in class Ternary, obviously, you must give the result type (what we really want here is a generic method), you also need specify modifiers such as private, final or others. Give the specific modifiers that apply to the declaration of Ternary.cond(). (1 point)

    __public___static______________________________________________________
    
    _______________________________________________________________________
    

    1/2 got this. 1/4 earned no credit. Of those in the intermediate group, more forgot the static attribute than public. Among those making errors, a surprising 1/6 thought that the method cond() ought to be private. Nothing in the problem statement suggested this limitation, and if you are implementing a ternary logic package, it seems strange to limit use of one of the more obvious operators by the general public.

    c) If we had written our example call as a.cond(b,c,d) instead of Ternary.cond(a,b,c,d), how would your answer to part b change? (1 point)

    __The method is no longer static.______________________________________
    
    _______________________________________________________________________
    

    Again, half the class got this. The statistics were generally similar to part b, except that fully 1/4 thought the method should now be private, and 1/5 thought it should be static.

  4. Background: Most Java tutorials say that a+b where a and b are strings is described as being equivalent to a.concat(b) which returns a new string constant containing the concatenated text of the two string arguments. Now consider the expression a+b+c. Here are two ways this could be implemented:
    a.concat(b).concat(c)
    StringBuilder(a).append(b).append(c).toString()
    

    a) Java uses the second approach to implement the + operator applied to strings. Why? (1 point)

    __Concat creates a new string object each time it is called.___________
    
    __Using a StringBuilder avoids, we create only one new string._________
    
    _______________________________________________________________________
    
    _______________________________________________________________________
    

    1/2 earned full credit, while 1/5 earned no credit.

    This problem was essentialy a refinement of problem 6 on Exam 1. Apparently, 1/5 of the class did not take the time to study the posted solutions and commentary for that exam.

    b) Why do we need class StringBuilder in order to do the append operations? (Hint: The answer is hinted at by the wording in the Background section above.) (1 point)

    __Strings are not mutable.  Once a string object is created, the text__
    
    __stored in it cannot be changed.______________________________________
    
    _______________________________________________________________________
    

    3/8 got this right, 1/3 earned no credit. A common answer that earned partial credit was to say that class StringBuilder has an append() method, while class String does not. This dodges the question of why, as if String and StringBuilder were natural objects that just are as they are with no underlying reason.

  5. Background: To solve Machine Problem 5, you had to scan and process input lines of the form:
    1.5 x true
    3.7 y false
    
    In the posted solution, a private static method called scanInput(float t) processes input by:

    The only use for the parameter t was in error checking. Note that each call to scanInput() reads the second two items from one line and the first from the next. Do-it-once logic inr checkInputs reads the first time and schedules the first call to scanInput.

    a) Why the logic in CheckInputs(). Why not in the first Input gate's initializer? (1 point)

    __The input cannot be scanned until the entire circuit description_____
    
    __has been scanned.  CheckInputs is called at just this time.__________
    
    _______________________________________________________________________
    

    1/4 got this right. 2/3 earned no credit.

    b) In an alternative design, scanInput() reads the entire line and then schedules a call to outputChange for the indicated input gate. Does this make it safe to make the first call to scanInput() from the initializer for the first, Input gate? Why or why not? (1 point)

    __The answer to part a still applies.  No input changes can be_________
    
    __scanned until the entire circuit has been scanned.___________________
    
    _______________________________________________________________________
    

    1/7 got this right, 1/3 earned no credit. Many simply said no, without giving a reason or with a nonsense reason.

    c) In the alternative design, does the next call to scanInput() go? (1 point)
    __In the outputChange() method of the input gate.______________________
    
    _______________________________________________________________________
    
    _______________________________________________________________________
    

    1/3 got this right, 2/5 earned no credit. Several suggested scheduling it from Input.scan(). This is feasible but does not really answer the question, so a small penalty was imposed.

  6. Background: The posted solution to MP5 included the following code (i is the input gate handle):
    if ((i != null) && (i.outputValue != inputValue)) {
    	// something actually changes!
    	i.outputValue = inputValue;
    	i.outputChange( t, inputValue );
    }
    

    a) Why was it possible for i to be null? (Hint, think about possible errors in the input file.) (1 point)

    __If the input line was, for example, 1.0 z true, where there is_______
    
    __no input gate named z._______________________________________________
    
    _______________________________________________________________________
    

    3/5 got this right, while 1/5 earned no credit. Partial credit was mostly the result of vague answers.

    b) Why check to see that the new output value of this input differs from the old one? Is this just to avoid wasted effort, or would omitting this check cause errors elsewhere in the simulation, and if so, what errors? (1 point)

    __Output gates count changes, so they would get confused.______________
    
    __Also, not gates blindly change their output on input change._________
    
    _______________________________________________________________________
    

    1/7 got this right. 3/8 earned no credit.

  7. Background: The posted solution to MP5 contained this code:
    final boolean iv = inputValue; // kluge
    SyntaxCheck.lineEnd( sc, () -> "" + t + " " + name + iv );
    

    A question: Why the kluge? Why not write it like this?
    SyntaxCheck.lineEnd( sc, () -> "" + t + " " + name + inputValue );
    (1 point)

    __Java requires that variables in lambda expressions be effectively____
    
    __final._______________________________________________________________
    
    _______________________________________________________________________
    

    2/7 got this right, an equal number earned no credit. Half credit was given to those who mentioned iv being final without saying why this was required.

  8. A brief question: What is "do it once logic"? (1 point)
    __Logic in a context that is executed many times that ensures that_____
    
    __some piece of code is executed just once.____________________________
    
    _______________________________________________________________________
    

    3/5 got this right, only 1/8 earned no credit. Awkward or vague wording was the most common reason for partial credit.

  9. Background: If you have a method call a.b(c) and you want to rewrite the code as b(a,c), you must change the method's declaration. Assume that the original method declaration is Result b(Param cc)

    a) What changes to you need to make to the header modifiers? (modifiers are words like private and final that are prefixes on Java declarations.) (1 point)

    __The old method b() was not static.  The new one is static, it becomes
    
    __static Result b( AsClass aa, Param cc )______________________________
    
    _______________________________________________________________________
    

    1/6 got this right, almost 1/3 earned no credit. over 1/3 thought that the method needed to be changed to either public or private, while in fact, no change in its visibility is implied by the change.

    b) In the method body, you need to change this and certain variable references. Describe these changes? (1 point)

    __If f is a non-static field of class AsClass (the class of object a)__
    
    __change the reference to aa.f; also change this to aa.________________
    
    _______________________________________________________________________
    

    Almost 1/3 earned full credit, an equal number earned no credit. Partial credit was given to those who tried to say something like this without ever quite succeeding in getting the words to say it.