CS115 lecture notes, Spring 1999 Week 3, Friday Reading: 4.1 through 4.7 Quiz 4: arguments and parameters More than one parameter ----------------------- So far, all the methods we have looked at have taken either zero or one parameters, so we have not had a chance to see a parameter list. public static void printTime (int hour, int minute, int second) { System.out.println (hour + ":" + minute + ":" + second); } The list of parameters is similar to a variable declaration, in the sense that the types come before the names. One difference is that you can create multiple variables with the same type: int x, y, z; But you have to give the types of all the parameters separately. int hour, int minute, int second Of course, not all the parameters have to have the same type: public static void gebrozzle (String day, int date, double month) { 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! 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