블로그 이미지
초딩입맛제주아재
하고 싶은 것만 하며 살고 싶다

calendar

1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
2006. 10. 8. 15:16 Programing/Smalltalk

1. Class Browser로 Object의 하위 클래스로 HouseKeepingBookItem 클래스를 만드세요.
    객체변수는 name value date 3개입니다.
    아무런 매쏘드도 없는 빈 껍데기 클래스입니다.

Object subclass: #HouseKeepingBookItem
instanceVariableNames: 'name value date'
classVariableNames: ''
poolDictionaries: ''
classInstanceVariableNames: ''


2. 다음을 실행하세요

testItem := HouseKeepingBookItem new.
testItem name: '스포츠신문'.

에러가 납니까? 당연합니다! HouseKeepingBookItem에 name: 이라는 매쏘드를 안만들어 줬으니까요. "Debug"버튼을 누릅니다. 디버깅 창의 왼쪽 상단 리스트에 팝업메뉴를 불러 "implement·······"메뉴를 고릅니다. 어떤 클래스에다 만들지 물어보네요. 답해줍니다.
임시 매쏘드가 만들어졌을 겁니다. 객체변수 name에 인자를 저장하는 매쏘드로 바꿔주고 accept(Ctrl+S)해줍니다.

testItem name: '스포츠신문'.를 다시 한번 해봅니다. 에러가 납니까?

name: aString

name := aString.


3.
다음의 문장들도 하나씩 실행하고 2번과 같이 매쏘드를 만들어줍니다.

testItem value: 500.
testItem date: Date today.
testItem name.
testItem value.
testItem date.

name
^name

value: anInteger
value := anInteger.

value
^value

date: aDate
date := aDate.

date
^date


4.
기존에 Workspace에 housekeepingbook에 저장되어있던 항목들(OrderedCollection type)을 전부 HouseKeepingBookItem 객체로 대체합니다.

housekeepingbook := housekeepingbook collect: [:item| ?????????? ].
??????????에 들어갈 적당한 표현식을 찾으세요.

housekeepingbook := housekeepingbookcollect: [ :each |
         HouseKeepingBookItem  new
                name: (each at: 1);
                value: (each at: 2);
                date: (each at: 3)
].


5.
숙제 1과 2의 문제들을 다 다시 작성해보세요.^^(놀라지 마시고 살짝씩만 바꿔주면 됩니다.) 기존에 OrderedCollection을 쓸 때와 비교했을 때 어떤 점이 달라졌는지 느낌 점을 말씀해주세요.

원하는 값에 바로 접근이 가능해진것 같아요...
마치 연관배열처럼 키값에 의한 access 같은...좀더 편해졌다고 할까요..


6. Class Browser로 Object의 하위 클래스로 HouseKeepingBook 클래스를 만드세요.
    객체변수는 pocketMoney items 2개 입니다.
    아무런 매쏘드도 없는 빈 껍데기 클래스입니다.

Workspace에 있는 변수들 pocketMoney와 housekeepingbook을 각각 testBook의 pocketMoney와 items에 담습니다. 2번과 같은 방법으로 하시면 됩니다.

Object subclass: #HouseKeepingBook
instanceVariableNames: 'pocketMoney items'
classVariableNames: ''
poolDictionaries: ''
classInstanceVariableNames: ''

items: aItem
items := aItem.

items
^items.

pocketMoney: aInteger
pocketMoney := aInteger.

pocketMoney
^pocketMoney.


7. HouseKeepingBook 에 sum 이라는 매쏘드를 만들어서 금액의 총합을 리턴하게 하세요. (원금을 포함하도록 하세요)

sum

^self items inject: pocketMoney into: [
:sum :item |
sum + (item value)
]


8. HouseKeepingBook 에 본인이 흑자인지 물어보는 isSurplus라는 매쏘드를 만드세요. true/false를 리턴해야 합니다. 7번 문제의 sum 매쏘드를 사용해야 합니다.

isSurplus

^(self sum > 0)


9. HouseKeepingBook 에 statusString라는 매쏘드를 만들어, 흑자면, '아싸! 흑자' 적자면 '에고... 적자'라는 문자열을 답하게 하세요. 8번 문제의 isSurplus를 사용해야 합니다.

statusString

self isSurplus ifTrue: [^'아싸!흑자'] ifFalse: [^'에고...적자']


10.
HouseKeepingBook 에 itemsOfYear: 라는 매쏘드를 만들어, 그 해의 항목들을 답하도록 하세요. 숙제 2의 답을 활용하세요.

itemsOfYear: aYear

^self items select: [:each | each date year = aYear]



11. HouseKeepingBook 에 sumOfYear: 라는 매쏘드를 만들어, 그 해 항목들의 합을 답하도록 하세요. itemsOfYear: 매쏘드를 활용하세요. (원금은 포함하지 않습니다)

sumOfYear: aYear

^(self itemsOfYear: aYear) inject: 0 into: [:sum :item | sum + (item value)]

posted by 초딩입맛제주아재