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

String>>isPalindrome
   ^self sameAs: self reversed

Now some people used

String>>isPalindrome
   ^self = self reversed

which would not consider 'caC' as a palindrome

Some people used

String>>isPalindrome
   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

String>>isPalindrome
   | 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
   ^self
      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. 
Accessing
balance
   ^balance 

deposit: aPositiveNumber
   self adjustBalanceBy: aPositiveNumber 

do: aBlock
   history do: aBlock 

name
   ^name 

withdrawl: aPositiveNumber
   self adjustBalanceBy: aPositiveNumber negated 

Private

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

Comments

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

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

setbalance
"Set the initial balance of the account to zero"
   balance:=0.
   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