next up previous
Next: About this document

CS341, Autumn 1996

Name:


Smalltalk Quiz
5/14/96



(Check the last two pages of the exam for help with Smalltalk syntax.)

  1. Some methods for existing classes. (Note: in some cases these methods already appear in the Smalltalk language. Write them anyway.) (25 points total; 5 per method.)

    1. Write a method max: aNumber for class Number, that returns the maxiumum of the receiver and its argument.

    2. Write a method isBigger: aCollection for class Collection that returns true if the receiver has more elements than the argument and false otherwise.

    3. Write a method isNumeric for class Collection that returns true if and only if all elements in the collection are numbers. (Smalltalk has a method isNumber that answers whether the receiver is an instance of any numeric class.)

    4. Write a method numOccurrences: anObject for class Collection that returns the number of times the argument appears in the receiver.

    5. Write a method sumSquares for class Collection that returns the sum of the squares of all the numbers in the receiver. Signal an error if the collection contains a non-number.

  2. (35 points) Class and instance variables.

    1. (10 points) Define in words the difference between a class variable and an instance variable.

    2. (15 points) Suppose the global variable AccountList is a Collection, all members of which are instances either of class CheckingAccount or SavingsAccount. These two classes are in turn subclasses of Account.

      Suppose that instances of CheckingAccount implement the method assessServiceCharge and instances of SavingsAccount implement the method payInterest. Both of these are unary methods.

      Design a simple scheme by which every member of AccountList gets its service charge assessed or its interest paid, as appropriate. (Hint: you can define define additional instance methods for Account, CheckingAccount, SavingsAccount, and Collection.)

    3. (10 points) Demonstrate the difference between instance and class variables using some code: below you will see two definitions for a class SomeClass that are identical except one has a single instance variable and the other has a single class variable. Show some code that generates different results depending on which definition of SomeClass you use.

      Object subclass: SomeClass           ||    Object subclass: SomeClass                   
        instanceVariableNames: 'aVar'      ||      instanceVariableNames: ''              
        classVariableNames: ''             ||      classVariableNames: 'AVar'                     
                                           ||                                                      
      new: aValue                          ||    new: aValue                                  
        "A class method for SomeClass      ||      "A class method for SomeClass              
         to create an instance."           ||       to create an instance."                   
        ^self new set: aValue.             ||      ^self new set: aValue.                     
                                           ||                                                      
      access                               ||    access                                       
        "An instance method for SomeClass  ||      "An instance method for SomeClass          
         to access its variable."          ||       to access its variable."                  
        ^aVar.                             ||      ^AVar.                                     
                                           ||                                                      
      set: newValue                        ||    set: newValue                                
        "An instance method for SomeClass  ||      "An instance method for SomeClass          
         to set its variable value."       ||       to set its variable value."               
        aVar := newValue.                  ||      AVar := newValue.                          
        ^self.                             ||      ^self.
      

  3. (40 points) Defining a new class.

    A complex number has a real part and an imaginary part, which we might write where a is the real part and b is the imaginary part. The rules for adding and multiplying two complex numbers are as follows:

    Define the class ComplexNumber and define these methods:



    (Put your answers on the next page.)



    Answer to complex numbers goes here.



Here is some Smalltalk syntax that might help you out:





next up previous
Next: About this document



Dave Grove
Wed May 15 10:39:54 PDT 1996