Section 3 2 Methods that return values Method















- Slides: 15

Section 3. 2 Methods that return values Method name Description abs(value) absolute value Java's Math class ceil(value) Constant Description E 2. 7182818. . . PI 3. 1415926. . . rounds up cos(value) cosine, in radians floor(value) rounds down log(value) logarithm, base e log 10(value) logarithm, base 10 max(value 1, value 2) larger of two values min(value 1, value 2) smaller of two values pow(base, exponent) base to the exponent power random() random double between 0 and 1 range [0. 0, 1. 0) round(value) nearest whole number sin(value) sine, in radians sqrt(value) square root 1

Methods that return values • return: To send a value out as the result of a method, which can be used in an expression. – A return is like the opposite of a parameter: • Parameters pass information in from the caller to the method. • Return values pass information out from a method to its caller. Math. abs -42 42 main 2. 71 3 Math. round – The Math methods do not print results to the console. Instead, each method evaluates to produce (or return) a numeric 2 result, which can be used in an expression.

• Math method call syntax: Math. <method name> ( <parameter(s)> ) • Examples: double square. Root = Math. sqrt(121. 0); System. out. println(square. Root); // 11. 0 int absolute. Value = Math. abs(-50); System. out. println(absolute. Value); // 50 System. out. println(Math. min(3, 7)+2); //5 – Notice that the preceding calls are used in expressions; they can be printed, stored into a variable, etc. 3

• Evaluate the following expressions: – Math. abs(-1. 23) – Math. pow(3, 2) – Math. pow(10, -2) – Math. sqrt(121. 0) - Math. sqrt(256. 0) – Math. round(Math. PI) + Math. round(Math. E) – Math. ceil(6. 022) + Math. floor(15. 9994) – Math. abs(Math. min(-3, -5)) • Math. max and Math. min can be used to bound numbers. Consider an int variable named age. – What statement would replace negative ages with 0? age = (age + Math. abs(age)) / 2; – What statement would cap the maximum age to 40? age = Math. min(age, 40); 4

5

6

7

• Syntax for writing your own methods that return a value: public static <type> <name> ( < parameter(s)> ) { < statement(s)> ; } – Returning a value from a method: return <expression> ; • Example: // Returns the slope of the line between the given points. public static double slope(int x 1, int y 1, int x 2, int y 2) { double dy = y 2 - y 1; double dx = x 2 - x 1; return dy / dx ; } • Example call: double my. Slope = slope(3, 9, 12, 22); System. out. println("The slope = " + my. Slope); Output: The slope = 1. 44444444 8

Return examples // Converts Fahrenheit to Celsius. public static double f. To. C(double degrees. F) { double degrees. C = 5. 0 / 9. 0 * (degrees. F - 32); return degrees. C; } // Computes length of triangle hypotenuse given its side lengths. public static double hypotenuse(int a, int b) { double c = Math. sqrt(a * a + b * b); return c; } // Rounds the given number to two decimal places. // Example: round(2. 71828183) returns 2. 72. public static double round 2(double value) { } double result return result = value * 100; = Math. round(result); = result / 100; result; // upscale the number // round to nearest integer // downscale the number 9

Return examples shortened // Converts Fahrenheit to Celsius. public static double f. To. C(double degrees. F) { return 5. 0 / 9. 0 * (degrees. F - 32); } /* Computes length of triangle hypotenuse given its side lengths. */ public static double hypotenuse(int a, int b) { return Math. sqrt(a * a + b * b); } // Rounds the given number to two decimal places. // Example: round(2. 71828183) returns 2. 72. public static double round 2(double value) { return Math. round(value * 100) / 100; } 10

Return questions • Write a method named area that accepts a circle's radius as a parameter and returns its area. – You may wish to use the constant Math. PI in your solution. • Write a method named distance. From. Origin that accepts x and y coordinates as parameters and returns the distance between that (x, y) point and the origin. • Write a method named attendance that accepts a number of lectures attended by a student, and returns how many points a student receives for attendance. The student receives 2 points for each of the first 5 lectures attended and 1 point for each subsequent lecture. 11

How to comment methods with parameters • If your method accepts parameters and/or returns a value, write a brief description of what the parameters are used for and what kind of value will be returned. – In your comments, you can also write your assumptions about the values of the parameters. – You may wish to give examples of what values your method returns for various input parameter values. – Example: // This method returns the factorial of the given integer n. // The factorial is the product of all integers up to that number. // Assumes that the parameter value is non-negative. // Example: factorial(5) returns 1 * 2 * 3 * 4 * 5 = 120. public static int factorial(int n) {. . . } 12

Method Overloading • You can write methods so that both of the following work: draw. Box(8, 10); and draw. Box(); public static void draw. Box (int x, int y) { { // draws a box x places wide and y places tall. …} public static void draw. Box () { { // draws a box of default size, say 5 by 5 draw. Box(5, 5); } • These methods differ in their Signatures, the number and/or types of their parameters. The compiler looks for the definition of a method named draw. Box that has 2 integer parameters in the first case and no parameters in the second. As far as the compiler is concerned these are completely different methods. 13

Method Overloading: Having more than one method with the same name, but different parameters. public static void main(String[] args) { draw. Block(); draw. Block('C'); draw. Block('A'); draw. Block('T'); draw. Block(); } public static void draw. Block() { System. out. println("+-+"); System. out. println("| |"); System. out. println("+-+"); } public static void draw. Block(char c) { System. out. println("+-+"); System. out. println("|" + c + "|"); System. out. println("+-+"); } +-+ | | +-+ |C| +-+ |A| +-+ |T| +-+ | | +-+ 14

More precisely: A Method Signature is the name of a method along with the number of its parameters and the types of those parameters. Method Overloading occurs when more than one method have the same name. They must have different signatures! Example: Definitions of methods within System. out. println(String s) System. out. println(boolean b) System. out. println(char c) System. out. println(int i) System. out. println(double d). . . 15