# Input parameters and Output values of methods

• Parameters of a method

• A method can have:

• Zero or more input parameters

• Zero or ONE output parameter

• (It's all about information - "knowledge is power")

• Input parameters are used for:

providing information to the method (information is needed to allow the method to perform the task)

• The output parameter is used for

returning information after the method has completed the task

• The explicit input parameters of a method

• The explicit parameters are the parameters specified between brackets "(" and ")":

 ``` Rectangle R1 = new Rectangle( 1, 2, 10, 20 ); R1.translate( 1, 1 ); ```

The explicit parameters are given in red

• You can pass one or more input values to a method by providing the values as the parameters for the method when you invoke the method.

• In general, you must provide:

• The same number of parameters as specified in the method definition
• The same type of parameters as specified in the method definition

• Example: here is the description of the translate() method in Rectangle:

 void translate(int x, int y) Translates this Rectangle the indicated distance, to the right along the x coordinate axis, and downward along the y coordinate axis.

The method translate() has 2 explicit parameters and both of them are integers

Therefore, when you invoke the method translate() , you must provide 2 integer values:

 ``` R1.translate(3, 4); ```

• The implicit input parameters of a method

• The implicit parameter is the parameter specified before the method name

 ``` Rectangle R1 = new Rectangle( 1, 2, 10, 20 ); Rectangle R2 = new Rectangle( 4, 4, 10, 20 ); R1.translate( 1, 1 ); R2.translate( 1, 1 ); ```

The implicit parameters are given in red

• The following example illustrates the fact that the variable preceeding the method call is in fact a parameter

Remember what a parameter is:

A parameter alters the behavior of the method called (making it more flexible)

Consider:

 ``` Rectangle R1 = new Rectangle( 1, 2, 10, 20 ); Rectangle R2 = new Rectangle( 4, 4, 10, 20 ); int result; result = R1.getX(); ..... (1) result = R2.getX(); ..... (2) ```

1. In the statement (1), R1.getX() will return 1.
2. In the statement (2), R2.getX() will return 4.

The behavior of the method getX() is changed.

Hence, R1 is a (implicit) parameter, and so is R2

• Return value

• Methods may return a value

Methods that do not return a value has type void

• Every (non-void) return value of a method has a type (every value has a type)

• When executing a method call, the method call will be replace by the value that it returns.

• Example:

 ``` String greeting = "Hello"; int r; r = greeting.length(); ```

The call greeting.length() will return the value 5 (because "Hello" has 5 letters).

When executing r = greeting.length(), the method call greeting.length() will be replace by the value that it returns (= 5):

 ``` String greeting = "Hello"; int r; r = 5; ```

• Example:

 ``` String greeting = "Hello"; String s; s = greeting.toUpperCase(); ```

The call greeting.toUpperCase() will return the value "HELLO" (= "Hello" turned uppercase).

When executing s = greeting.toUpperCase(), the method call greeting.toUpperCase() will be replace by the value that it returns (= "HELLO"):

 ``` String greeting = "Hello"; int s; s = "HELLO"; ```

• An advanced example that uses INPUT and OUTPUT parameters: replacing a substring

• There is a replace() method in the String that is defined as follows:

 ``` public String replace(String target, String replacement) Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. ```

• Before we try to use it, we must first understand how to use it.

• Questions:

• How many parameters does the method replace() have ?

• 2

• What are the types of the parameters of the method replace() ?

• First parameter: String (from: String target)
• Second parameter: String (from: String target) --- (it happens to be the same as the type of the first parameter, but in general, they need not be the same)

• What are the type of the return value of the method replace() ?

• String

• OK, we now know what kinds of input the replace() need, and what kind of value it returns to us.

Next, we must understand what the replace() does...

For this, we read its documentation:

 ``` public String replace(String target, String replacement) Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. Example use: greeting = "Hello World" greeting.replace( "string1", "string2" ); ```

• The meaning of the documentation may not be so obvious for a first time Java student... so...

• When you read the documentation, it will help by using an example to "decipher" what it means.

• For example:         greeting.replace("string1", "string2");

• Question: try to figure out what these terms in the documentation refer to:

• What is "this string" in greeting.replace("string1", "string2"); ???

greeting

• What is "the literal target sequence" in greeting.replace("string1", "string2"); ???

"string1"

• What is "the specified literal replacement sequence" in greeting.replace("string1", "string2"); ???

"string2"

• Now we can rewrite the documentation using the example to make it more obvious:

• greeting.replace("string1", "string2");

• Replaces each substring of greeting that matches "string1" with "string2".

• Example: A graphical representation of the replace("issipp", "our") method (the original string is "Mississippi"):

• Now that we know what the method greeting.replace("string1", "string2"); does, we can try to use it.

Here is an example program:

 ``` public class String3 { public static void main(String[] args) { String greeting = "Hello World"; String myName = "Mississippi"; String result; result = greeting.replace("Hello", "Good-bye"); System.out.print("result = "); System.out.println( result ); result = myName.replace("issipp", "our"); System.out.print("result = "); System.out.println( result ); } } ```

• Notice that we pass in 2 input strings
• Notice that the method returns a string - we need to receive the output (use a variable !!!)

• DEMO Program: (the above example)