SDSU CS 535 Object-Oriented Programming
Spring Semester, 2003
Assignment 4
    Assignment Index        
© 2003, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 20-Feb-03

Assignment 4

Due February 27

1.
a. What is the result of the evaluating the following expressions?

    'acatinthehatcomesback' tokensBasedOn: $t

   #( 2 3 1 4 5 1 7 2 4 1 9) tokensBasedOn: 1
   ‘ 21 ’ asNumber      “there are spaces before and after the 21” 
b. Write a SUnit test to assert your findings for each of the expressions in part a.

2.
a. Implement in the String class a method sumSeparatedBy: aCharacter using tokensBasedOn:. When sent to a string that contains numbers separated by aCharacter the message returns the sum of the numbers. So

‘5, 6, 4’ sumSeparatedBy: $,
returns 15. The result of the message sent to a string that does not meet this format is not defined.

b. Write an SUnit test for your method.

3.
a. What is the result of the evaluating the following expressions?

'catmat' readStream upTo: $t
'catmat' 
   readStream upTo: $t;
   upTo: $t
'catmat' readStream through: $t
'catmat' 
   readStream through: $t;
   through: $t

b. Write a SUnit test to assert your findings for each of the expressions in part a.

4. Rewrite your method sumSeparatedBy: to first convert the receiver to a stream and use stream methods to parse the string.

5. Write Smalltalk code that reads a file named ‘start’ and copies the contents to a file name ‘end’ and replaces all colons ($:) with a comma ($,). So if start contains:

cat:mat
sat:bat:
rat

end will end up with:

cat,mat
sat,bat,
rat

6. Let ‘data’ be a file that contains rows of numbers. In each row the numbers are separated by commas. Write Smalltak code that reads the file ‘data’, sums the numbers in each row, and writes out the row sums in a file called ‘result’. Each row sum in on a separate row in the file result. So if data contained:

1,2,3
2,4,2

result would contain:

6
8

7 & 8. Make the following additions to your BankAccount class from assignment 2

a. Initialize the balance to 0.0s2.
b. Modify the class so that each account also has a name. When an account is created a name is required.
c. Add the method printOn: to the class so when printString is sent to a BankAccount class at least the name and balance of the account is displayed. For example:

| account | 
account := BankAccount name: ‘Roger’.
account printString

will result in:

BankAccount(Roger, 0.00s)

d. Add a transaction history to the BankAccount. That is each time you deposit or withdrawal from the account record the amount of the transaction. You also need an undo method, which undoes the last transaction. Once a transaction is undone it is forgotten, that is it cannot be redone. You should be able to undo all transactions in the account. (Hint: OrderedCollection is your friend). For example

| account | 
account := BankAccount name: ‘Roger’.
account
   deposit: 10s2;
   deposit: 20s2;
   withdrawal: 5s2.
“account balance is now 25s2”
account undo.
“account balance is now 30s2”
account undo.
“account balance is now 10s2”
account undo.
“account balance is now 0s2”
“The result of undo at this point is undefined. We will later learn what to do in this case”
e. Write a SUnit test for your undo method.

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

    visitors since 20-Feb-03