CS151 lecture notes, Fall 1999 Week 6, Friday QUIZ 8 For Monday: Read Chapter 7!!! Accumulators ------------ Very often the goal of a loop is to accumulate an answer: 1) add up all the numbers between m and n 2) find the product of all the numbers between 1 and n (name that function!) 3) lots of mathematical series that are expressed as the sum of a series of terms. The first thing we need is a loop that goes from a lower bound to an upper bound. public static void traverse (int m, int n) { int i = m; while (i <= n) { System.out.println (i); i = i+1; } } Then all we need to add is an accumulator to add up all the numbers between m and n: public static int itSum (int m, int n) { int i = m; int total = 0; while (i <= n) { total = total + i; i = i+1; } return total; } You might want to read Section 7.7 Debugging techniques -------------------- On the homework and the quiz, I have suggested complementary debugging techniques for recursive and iterative methods: recursive: print the parameters at the beginning of the method result: automatic stack diagram! iterative: print the loop variables at the beginning of each iteration result: iteration table Internal variables ------------------ If you declare a variable inside an if or while statement, it only exists within that statement! public static int itSum (int m, int n) { int i = m; while (i <= n) { System.out.println (i); int total = total + i; i = i+1; } return total; // ERROR: total does not exist outside the loop } The character type ------------------ Yes, it's time for yet another type: char. A char variable can contain exactly one character. A char value looks like a String value, except it uses single quotes instead of double, and it can contain only one character. char fred = 'c'; if (fred == 'c') { System.out.println (fred); } comparsion operators work on chars (arithmetic, too) println is overloaded to work with chars EVIL JAVA TYPE MISTAKE: if you add two characters, you get an int! Program development ------------------- So far I have not said much about program development, but the book has talked about two approaches: 1) incremental development 2) encapsulation and generalization Let's talk about them... Incremental development ----------------------- 1) start with a working program 2) if you don't have a working program, don't even think about adding anything to it 3) add as few lines as you can at a time, such that the result is testable (faster machines this year, frequent compilation is more appealing) 4) define intermediate steps along the way and test them (in other words, use scaffolding) Encapsulation and generalization -------------------------------- See chapter 6. 1) write code that works in main 2) think about how to split it up into methods 3) move the code into a method 4) think about how to generalize it (look at all the constants and see what seems useful) Alternate version 1) find yourself writing a whole bunch of similar methods 2) "factor out" the common part into another method 3) generalize