CS151 lecture notes, Fall 1999 Week 3, Friday Reading: 4.1 through 4.7 Homework comments ----------------- Why are we writing all these methods? 1) methods are a named set of statements. The name provides implicit documentation 2) methods make it possible to hide ugly details 3) methods allow you to solve a problem once and then reuse the solution What are parameters for? 1) parameters allow you to GENERALIZE the function of a method, so that instead of solving a specific problem, it can solve a range of problems What was the point of the multadd question? 1) once we had written multadd, we had a new tool in our toolkit -- there is a general class of problems we know how to solve 2) from now on, when we address a problem, we want to see if we can identify it as an example of a class of problems we know how to solve. I sometimes call this pattern recognition: Does this specific problem belong to a general class of problems that I already know how to solve? In the examples in the homework, it was not immediately obvious that they did, since one of them required a restatement of the computation (expressing a divison as a multiplication) and the other was a degenerate example of the pattern (since it did not use the full power of the pattern). Pattern recognition is a big part of what people mean when they talk about intelligence. It may be the only thing that "intelligence" means. It is, in some ways, the hardest thing about this class. What was the point of yikes? 1) once you have written a method, you can use it to help implement new methods. In this way, programming is cumulative and hierarchical. 2) I wanted you to see the connection between an unknown value in a mathematical expression (like x) and the parameter of a method. Your definition of yikes is similar to the math expression f(x) = x^2 Although the right-hand side was more complicated. In both cases (mathematical functions and Java methods) the parameter is hypothetical. If you give me a value of x, I will give you x^2. In order to run the program, you have to provide a specific value for x. On the assignment, the choice of that value is up to you. Matching arguments and parameters --------------------------------- When you invoke a method, you have to provide arguments that match the method's parameters in type and number (and order). If you get it wrong, the error message can be confusing. Arguments get matched up in order, not by name! AND NOW, CHAPTER 4... Modulus operator ---------------- Operator that works on integers, denoted by % Yields the remainder when the operands are divided. 7%3 = 1 Checking for divisibility... if x%y = 0, then x is divisible by y. Extracting digits: x%10 yields the rightmost digit of x if x were written as a base 10 number. x%100 yields the two rightmost digits. Conditional execution --------------------- If something is true, execute a block of statements. Otherwise, don't. if (x > 0) { System.out.println ("x is positive"); } The condition (in parentheses) has to be something that is true or false. The operators in Java that compare things are > < >= <= != = There is no such thing as =< or = with a line through it These conditional operators work on ints and doubles, but NOT STRINGS! Alternative execution --------------------- if (x%2 == 0) { System.out.println ("x is even"); } else { System.out.println ("x is odd"); } Return statement ---------------- Bail out of a method before the end! public static void printLogarithm (double x) { if (x <= 0.0) { System.out.println ("Positive numbers only, please."); return; } double result = Math.log (x); System.out.println ("The log of x is " + result); } EXAM #1 WILL COVER EVERYTHING UP TO HERE Roughly, everything through 4.6