Quiz 2

Statistics:

mean     24.583 (295.000/12)
stddev   4.804
median   23.000
midrange 21.500-29.000

1   7.16 / 10
2   4.08 / 5
3   3.50 / 5
4   5.83 / 10
 + 4-point bonus

Question 2-1

Explain why generics make more sense as a feature in statically typed languages than in dynamically typed languages.

Question 2-2

Fully parenthesize the following expression to illustrate the order in which Smalltalk will evaluate it.

   2     +     3     factorial     *     4     gcd:     12     -     4

Question 2-3

Give Smalltalk code for defining a new instance method deductPenalty in our Account class, which should deduct 10 from the balance if the current balance is below 500. Your code should use the withdraw: method already defined for the Account class.

Question 2-4

Give and describe an example of how the Composite pattern shows up in the AWT or Swing user interface libraries of Java.

Solutions

Solution 2-1

The primary benefit of generics is to allow a programmer to write code that can accomodate a range of types. This is already possible in dynamically typed languages, since code can be ambiguous about type, and this is resolved at run-time.

Solution 2-2

(( 2     +   ( 3     factorial ))  *     4 )   gcd:  ( 12     -     4 )

Solution 2-3

deductPenalty
    balance < 500 ifTrue: [ self withdraw: 10 ]

Solution 2-4

Swing has a JComponent class for representing items that lie within windows, such as buttons and text fields. One subclass of JComponent is JPanel, so it can lie within a window, and a JPanel is itself a container of JComponents. Thus JPanel is an example of the Composite design pattern.