You are on page 1of 9

Methods in Java Return Values Methods can be divided into two groups commands and queries.

s. In many programming languages, these are known as procedures (commands) and functions (queries). A command is a method that per orms a task and then returns control to the point where they were called. A query is a method that is used to calculate or determine some value, and that value is then returned to the point where the method was called. !"ample # $he ollowing method calculates values o the unction whose de ining equation is
2 f x = x 3 if x 0 x if x 0

public static double f (double x) { double y; if (x < 0) { y = x * x; } else { y = x * x * x; } return y; } $wo notes about the di erences in this method as compared to previous methods% #. $he keyword void has been replaced by the keyword double. &revious methods returned nothing (void), while this one returns a double. '. $he method contains the statement return y; rather than simply ending. $his indicates that the method should inish at this point, and send the value y back to the point where the method was called. $he return statement does not need to be at the end o the method. In act, there can be more than one return statement (i appropriate logic is used). (or e"ample, an alternative to the above code might be public static double f (double x) { double y; if (x < 0) { return x * x; } else { return x * x * x; } ) *e have already used e"amples o this type o method rom the Math class, such as Math.s rt or Math.rando!.

#+. ,ov. '-#-

&age # o +

Methods in Java Return Values Boolean Methods $hrough the use o boolean methods, we can greatly improve the clarity o our code through the use o queries that return either true or alse. Many boolean e"pressions can be quite complicated, and to package that logic into a clearly identi ied method greatly improves the readability o code. (or e"ample, the code ragment if ("# <= a$e %% a$e <="&) could be replaced by if (is'een(a$e)) where is'een is a boolean method. public static boolean is'een (int n) { if ("# <= n %% n <= "&) { return true; } else { return false; } } A more condensed version o the method is also possible, while the clarity o the method name is preserved. public static boolean is'een (int n) { return "# <= n %% n <= "&; }

#+. ,ov. '-#-

&age ' o +

Methods in Java Return Values Exercises #. a) *rite a method rando!(u!ber with no parameters that returns a random int value between # and .. b) /opy and modi y your method rando!(u!ber to have one int parameter, hi$h, that returns a random integer value between # and hi$h. c) /opy and modi y your method random,umber to have two int parameters, !in and !ax, that returns a random integer value between !in and !ax. d) /opy and modi y your method random,umber to have three int parameters, !in !ax, and step, that returns a random integer value between !in and !ax in increments o step. (or e"ample, a call to rando!(u!ber("0) *+) +) could return the values "0, "+, *0, or *+. '. /onsider this method then answer the questions that ollow. public static int !ystery ( double a) double b) { int value = 0; if (a < b) { value = ,"; } if (a - b) { value = "; } return value; } a) *hat is the identi ier o the method0 b) *hat are its parameters0 c) *hat type o value is returned by the method0 d) 1ewrite the method using a nested if structure. e) 1ewrite the method using multiple return statements. 2. Assuming that the method f has been de ined as it was in !"ample #, state, with reasons, which o the ollowing statements is invalid. a) .yste!.out.println(f(,/)); b) double x = f(,/); c) double x = .yste!.out.println(f(,/)); d) double x = ,/; 3. 4. .. f(x); *rite a method lar$est that returns the value o the largest o its three double parameters. *rite a method $cd that returns the value o the greatest common divisor o its two int parameters. *rite a boolean method is0ivisible with two int parameters. $he method should return true i and only i the irst parameter value is e"actly divisible by the second.

#+. ,ov. '-#-

&age 2 o +

Methods in Java Return Values 5. 6. *rite a boolean method is1ri!e that returns true i and only i its int parameter is a prime number. *rite a boolean method is2etter that returns true i and only i its single char parameter is a letter o the alphabet (either upper or lower case).

#+. ,ov. '-#-

&age 3 o +

Methods in Java Return Values Solutions #. a) *rite a method rando!(u!ber with no parameters that returns a random int value between # and .. public static int genRandNumber() { int number; number = (int)(6 * Math.random() ) + 1; return number; b) /opy and modi y your method rando!(u!ber to have one int parameter, hi$h, that returns a random integer value between # and hi$h. public static int genRandNumber(int high) { int number; number = (int)(high * Math.random() ) + 1; return number; c) /opy and modi y your method random,umber to have two int parameters, !in and !ax, that returns a random integer value between !in and !ax. public static int genRandNumber(int min! int ma") { int number; number = (int)((ma" # min + 1) * Math.random() ) + min; return number; d) /opy and modi y your method random,umber to have three int parameters, !in !ax, and step, that returns a random integer value between !in and !ax in increments o step. (or e"ample, a call to rando!(u!ber("0) *+) +) could return the values "0, "+, *0, or *+. public static int genRandNumber(int min! int ma"! int step) { $$ determine ho% man& possible choices %e ha'e int num(hoices = ((ma" # min) $ step) + 1; int number; number = step*((int)(num(hoices * Math.random() )) + min; return number;

#+. ,ov. '-#-

&age 4 o +

Methods in Java Return Values '. /onsider this method then answer the questions that ollow. public static int !ystery ( double a) double b) { int value = 0; if (a < b) { value = ,"; } if (a - b) { value = "; } return value; } a) *hat is the identi ier o the method0 The identifier, or name, of the method is m&ster&. b) *hat are its parameters0 The parameters are a and b, which are oth of t!pe double. c) *hat type o value is returned by the method0 The method returns an inte"er #int$ value. d) 1ewrite the method using a nested if structure. public static int m&ster& ( double a! double b) { int 'alue = ); i* (a + b) { 'alue = #1; else { i* (a , b) { 'alue = 1; return 'alue;

#+. ,ov. '-#-

&age . o +

Methods in Java Return Values e) 1ewrite the method using multiple return statements. public static int m&ster& ( double a! double b) { i* (a + b) { return #1; else { i* (a , b) { return 1; else { return );

2.

Assuming that the method f has been de ined as it was in !"ample #, state, with reasons, which o the ollowing statements is invalid. a) .yste!.out.println(f(,/)); This is valid. b) double x = f(,/); This is valid. c) double x = .yste!.out.println(f(,/)); This is invalid, since the println statement does not return a value to e stored in x. d) double x = ,/; This is valid. f(x);

3.

*rite a method lar$est that returns the value o the largest o its three double parameters. public static double largest(double a! double b! double c) { double largest; i* (a , b -- a , c) largest = a; else i* (b , c) largest = b; else largest = c; return largest;

#+. ,ov. '-#-

&age 5 o +

Methods in Java Return Values 4. *rite a method $cd that returns the value o the greatest common divisor o its two int parameters. %ote& This solution uses a simple approach, ut it is not particularl! efficient. public static int gcd(int a! int b) { $$ the greatest common di'isor is the largest number $$ that %ill di'ide e'enl& into both numbers (i.e.! $$ there is no remainder) # use modulo *or remainder int gcd = ); *or (int i = 1; i += a; i++) { i* (a . i == ) -- b . i == )) { gcd = i; return gcd; .. *rite a boolean method is0ivisible with two int parameters. $he method should return true i and only i the irst parameter value is e"actly divisible by the second. public static boolean is/i'isible(int a! int b) { i* (a . b == )) { return true; else { return *alse;

#+. ,ov. '-#-

&age 6 o +

Methods in Java Return Values 5. *rite a boolean method is1ri!e that returns true i and only i its int parameter is a prime number. public static boolean is0rime(int number) { $$ a prime number is di'isible onl& b& 1 and itsel* $$ don1t need to test these numbers *or (int i = 2; i + number; i++) { $$ use the is/i'isible method *rom pre'ious e"ercise i* (is/i'isible(number! i)) { $$ a single di'isible number means %e1re done $$ so return a *alse right a%a& return *alse; $$ i* %e made it here! no di'isible 'alues %ere *ound $$ so the number must be prime return true; 6. *rite a boolean method is2etter that returns true i and only i its single char parameter is a letter o the alphabet (either upper or lower case). public static boolean is3etter(char letter) { i* ((141 += letter -- letter += 151) 66 (1a1 += letter -- letter += 171)) { return true; else { return *alse;

#+. ,ov. '-#-

&age + o +

You might also like