$24.99
lab 11
Objectives
• Workwithobjects
Activities
1.Extendthebankaccountexampleintheslideswiththefollowingupgradeandnewmethods: • withdraw –modifythismethodsoanegativebalanceisshowninsteadofaprintedmessage. • accrue –add1yearofsimpleinteresttothebalance. Atfirstassumeaninterestrateof1% • setrate –changetheinterestratetothegivenargument. Youmaystartwiththefollowingcodewhichiscopiedfromthelectureslides: (define (new-account initial-balance) (let ((balance initial-balance)) (define (deposit f) (begin (set! balance (+ balance f)) balance)) (define (withdraw f) (if ( f balance) "Insufficient␣funds" (begin (set! balance (- balance f)) balance))) (define (bal-inq) balance) (lambda (method) (cond ((eq? method ’deposit) deposit) ((eq? method ’withdraw) withdraw) ((eq? method ’balance-inquire) bal-inq)))))
2. Create two bank account objects and demonstrate that the balance and rate can be set and modified independently. 3. For this question, we will use the heap-supporting functions as seen in the lecture slides as building blocks for this assignment to build a new heap implementation, i.e., using objects. Specifically, we will create a priority queue object. A priority queue is a modified queue, such that when one requests the next element off the front ofthequeue,thehighest-priorityelementisretrievedfirst. Theobjectiveistoconstructapriorityqueueobjectwhich: 1. maintainsthepriorityqueueasaheap, 2. usesageneric(firstorder)orderrelation, 3. exposesmethods empty?, insert, extract-min,and size.
1
Thus,theobjectconstructorforthepriorityqueueshouldtakeoneargument(theorderrelation),andreturna dispatcheryielding4methods. Hint: Ofcourseitispossibletoimplement size byactuallytraversingtheheapeverytimeitiscalled,butyou candosomethingsmarterbymaintainingaprivatevariable q-size,whichisdestructivelyupdatedeverytime somethingisinsertedorextractedfromtheheap.