Com S 342 --- Principles of Programming Languages HOMEWORK 2: FORMULATING ABSTRACTIONS WITH HIGHER-ORDER PROCEDURES (File $Date: 1999/09/14 22:02:09 $) Due: problems 1-3, September 16, 1999; 4-6, September 30, 1999. (Note: on September 30 you may have some other problems from homework 3 due as well.) In this homework you learn how to use procedures to abstract patterns of computation, or what the book calls general methods. You'll also start to learn about classes in Java. The section headings below give the readings related to the problems. WHAT TO HAND IN For code hand in *both* your printout of the code and a transcript of testing. Be sure the code has your name and section information in a comment at the top as described in homework 0. at the top. Although you may want to write your code out by hand as practice for taking the tests, and then type it in, please don't turn in handwritten code, unless the problem specifically states otherwise. *No* credit will be given for programming problems unless you also hand in a transcript that includes test output. For many problems, we will provide test harnesses; for such problems you must run our tests. For some problems, you will have to provide your own tests. For this homework, the directory $PUB/homework/hw2.tst contains test harnesses for the coding problem. (Recall that $PUB means /home/course/cs342/public, see homework 0.) The way to use the test harnesses was described in homework 1. For problems that don't require code, you can write the answer by hand. or you can hand in a printout. SICP section 1.3 1. (5 points) [tail-recursive sum] Do exercise 1.30 in the book in Scheme. You can test your code using (test-hw2 "sum") from within scheme342. Be sure to hand in a script of your testing with your code. (Note: we did this in Java in class.) 2. [accumulate] (a) (20 points) Do exercise 1.32(a) in the book in Scheme, using a recursive process. You can test your code using (test-hw2 "accumulate") from within scheme342. (Hint, you may want to solve exercise 1.31 first.) (b) (20 points) Do exercise 1.32(a) in Java, using an iterative process generated by a while loop.) Write your code in a public class "Accumulate" with a public static method named "accumulate". You can test this part with $PUB/homework/hw2.tst/AccumulateTest.java, which you should copy into the directory where your code is. (See problem 5(b) on homework 1.) Note: the code for this problem uses various classes and interfaces in the library, for your code should look as follows in outline. import lib.*; public class Accumulate { public static double accumulate(Monoid op, Series ser, double a, double b) { // ... your code goes here ... } } See the files $PUB/lib/Monoid.java and $PUB/lib/Series.java for the meaning of these two interfaces and their operations. 3. [substitution model of procedures] (5 points) Do exercise 1.34 in the book. Think about the answer first, and then confirm your intuition using the Scheme interpreter. 4. [traced fixed-point] (10 points) Do exercise 1.36 in Scheme. You are on your own for testing this, but be sure to hand in both the printout of your testing and your code. 5. [continued fractions] a. (15 points) Do exercise 1.37(a) in the book using Scheme. Test your code as described in the book, and hand in a transcript of your testing as well as your code. b. (20 points) Do exercise 1.37(a) in the book using Java. Decide on the appropriate translation of this into Java; you get maximum points for making the most Java-like solution. since the interface will be determined by you, we leave it up to use do testing for this part. Hand in a transcript of your testing as well as your code. 6. [approximation to e] (10 points) Do exercise 1.38 in either Scheme or Java (your choice). Hand in a transcript of your testing as well as your code. 7. (extra credit) If you have done all of the above, you can do extra credit in SICP sections 1.3.1-1.3.3 by selecting any of the exercises in those sections not listed above. These will be worth 15 points if done in Scheme, or 25 points if done in Java. Be sure to hand in both your code and test output. Extra credit problems should be handed in to your TA, and separately from the other problems.