SDSU CS 683 Emerging Technologies: Embracing Change
Spring Semester, 2001
Assignment 1 Solution Part A
    Assignment Index        
© 2001, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 23-Feb-01

Assignment 1 Solutions Part A

  1. isPalindrome method

   ^self sameAs: self reversed

Now some people used

   ^self = self reversed

which would not consider 'caC' as a palindrome

Some people used

   self = self reversed
      ifTrue: [^true]
      ifFalse: [^false]

which does the same thing, but is harder to read with no benefit. Just use the shorter version.

Another solution was

   | reversed result |
    reversed := self reversed.
   result := self = reversed.
   ^ result

Using temporary variables to provide names for partial results can be a good thing. But there is not need for that in such a small and simple method.

2. BankAccount

Object subclass: #BankAccount
   instanceVariableNames: 'name balance history '
   classVariableNames: ''
   poolDictionaries: ''
   category: 'Whitney-Examples'
Class MethodsInstance Creation
name: aString
      name: aString
      balance: 0 

name: aString balance: aPositiveNumber
   ^self new
      setName: aString
      balance: aPositiveNumber 

Instance MethodsInitialize
setName: aString balance: aPositiveNumber
   name := aString.
   balance := aPositiveNumber.
   history := OrderedCollection new. 

deposit: aPositiveNumber
   self adjustBalanceBy: aPositiveNumber 

do: aBlock
   history do: aBlock 


withdrawl: aPositiveNumber
   self adjustBalanceBy: aPositiveNumber negated 


adjustBalanceBy: aNumber
   balance := balance + aNumber.
   history add: aNumber 


Some people figured out how to raise exceptions to check for invalid input. Since we have not covered exceptions I did not include it in my solution.

Some Issues

   "BankAccount currentBalance"
      show: 'Your current balance is: '; 
      show: balance; cr.

"Set the initial balance of the account to zero"
   Transcript show: 'initial balance='.
   Transcript show: balance.
Bad Coupling with Transcript

Useless comments

Copyright ©, All rights reserved.
2001 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.

    visitors since 23-Feb-01