next up previous contents
Next: Other information about methods Up: 6 Multiple methods Previous: Basics of using another   Contents

Passing information between methods

What if you want the behaviour of a method to vary depending on a value of a certain variable in your main method? It is possible to pass several values to method, and receive up to one value back. You may for example have several numbers you would like some calculation performed on. You could pass these values to a method and then receive the result back.

Let's start with how to give, or pass, a value to a method. This is done by giving the method an argument, which goes between the round brackets () after the method name. Look at the following example, an explanation follows.

/**
 * Purpose: Example of program with two methods. 
 *          The main method uses the printSquare method.
 *          The printSquare takes an double argument and prints the 
 *          value of the argument squared to the screen.
 */

public class ArgExample
{
    // method to print square of a value to screen
    public static void printSquare(double y)
    {
        System.out.println(y*y);
        return;
    }

    // main method
    public static void main(String[] argv)
    {
        double x = 5.0;

        printSquare(3.0);  // call to print the square of 3.0
        printSquare(x);    // call to print the square of x
    }
}

The method printSquare is declared as public static void printSquare(double y). Unlike before, the brackets that follow the method name now contain a variable declaration, in this case for a variable y which is of type double. The variable y exists within this method and can be used here for calculations etc..

Because the variable declaration for y is in the round brackets, whenever the method printSquare is called the method expects a double value to be in the round brackets in the call statement. So the call for printSquare may look like:

printSquare(3.0);

In the printSquare method y then takes this value, for the case above 3.0, so the value 9.0 is printed to the screen.

Within the main method a variable x is declared and given a value. When the call printSquare(x) is used, y takes whatever value x has in the main method at the time, in this case 5.0, so 25.0 is printed to the screen.

It is possible for a method to have several arguments. These should be separated by commas, e.g.

public static void calculate(double x, double y, int i, short n)
{
    //method code goes here
    return;
}

then to call the method simply enter values, or variables, of the expected type:

calculate(3.0, result, 2, num) // where result and num are
                              // double and short variables respectively

Exercise 6.1


(6 marks)



The purpose of this exercise is to write your own class with multiple methods.



So far all the additional methods you have seen have been declared as void and have not passed back, or returned, any values to the method that called them. This is the meaning of void, that the calling method should not expect any result to be passed back to it from the method being called. However methods can also be declared as double, int, boolean, String, or any other variable type, in which case the code for the method must return a value of that type. This is probably easiest to understand from an example.

import java.io.*;
public class NonVoidMethodExample
{
    public static double function1(double x)
    {
        /* write some mathematical function of x here, e.g. x + 3x^2 */
        return (x + 3*x*x);
    }

    // and then to call the above method

    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(
                              new InputStreamReader(System.in));
        System.out.print("Enter a value of x at which to evaluate f(x) ");
        System.out.print("or return to end: ");
        // read input until an empty line is returned
        String nextLine;
        while (( nextLine = br.readLine()) != null)
        {       
            if (nextLine.equals(""))
            {
                break;
            }
            double x = Double.parseDouble(nextLine); // JDK 1.3.1
            System.out.println("f(x) at x=" + x + " is " + function1(x));
            System.out.print("Enter another value of x, or return to end: ");
        }
    }
}

The above example defines a method called function1 which has the return type double and also takes a double argument. The method simply performs a calculation using the argument and returns the result.

The use of this method is demonstrated in the main method. In this example, the user is prompted to enter a number, then there is a loop which takes each entered number, uses the function1 method to calculate another number, and prints them. The program ends when the user enters nothing.

Notice that the call to a void method exists on its own on a line. In contrast, in the example above, there is a value returned from the method, so this needs to be used somehow in a calculation or stored in a variable.


next up previous contents
Next: Other information about methods Up: 6 Multiple methods Previous: Basics of using another   Contents
RHUL Dept. of Physics