A method is like a box that takes data in, solves a problem, and usually returns a value. The standard math methods follow this pattern:
Math.sqrt (2) --> 1.414
Math.sin (30) --> -0.988 (Note: computation is in radians!)
There are times when the built-in methods of Java will not get the job done. We will often need to write customized methods that solve a problem using the basic tools of a programming language.
For example, suppose we need a program that converts gallons into liters. We could solve the problem within the main
method, as shown in Program 7 1.
Program 7-1
import chn.util.*;
public class GallonsToLiters
{
public static void main (String[] args)
{
ConsoleIO console = new ConsoleIO();
System.out.println("Enter an amount of gallons --> ");
double gallons = console.readDouble();
double liters = gallons * 3.785;
System.out.println("Amount in liters = " + liters);
}
}
This works fine, but the mathematics of the conversion is buried inside the main
method. The conversion tool is not available for general use. We are not following the software engineering principle of writing code that can be recycled in other programs.
Here is the same routine coded as a reusable method, which would allow for conversion of gallons to units other than liters:
Program7-2
import chn.util.*;
class FluidConverter
{
public double toLiters(double amount)
{
return amount * 3.785;
}
}
public class TestConverter
{
public static void main(String[] args)
{
ConsoleIO console = new ConsoleIO();
FluidConverter convert = new FluidConverter();
System.out.print("Enter an amount of gallons --> ");
double gallons = console.readDouble();
System.out.println("Amount in liters = " +
convert.toLiters(gallons));
}
}
Sample run output:
Enter an amount of gallons --> 10
Amount in liters = 37.85
Here is the sequence of events in Program 7-2.
- Execution begins in the method named
main
with the user prompt and the input of an amount of gallons.
- The
toLiters
method of the convert
object is called and the number of gallons is passed as an argument to the toLiters
method.
- Program execution moves to
toLiters
, which does the computation and returns the answer to the calling statement.
- The answer is displayed.
The general syntax of a method declaration is
modifiers return_type method_name ( parameters )
{
method_body
}
Example (from program 7-2):
public double toLiters(double gallons)
- The
modifiers
refers to a sequence of terms designating different kinds of methods. These will be discussed gradually in later lessons.
- The
return_type
refers to the type of data a method returns. The data type can be one of the predefined types (integer
, double
, char
) or a user-defined type.
method_name
is the name of the method. It must be a valid identifier. In Program 7-2, the names of the methods are main
and toLiters
.
- The
parameters
list will allow us to send values to a method. The parameter list consists of one or more type-identifier pairs (example: double amount
). The parameters in the method instantiation are called the formal parameters.
- The
method_body
contains statements to accomplish the work of the method. In the toLiters
method there is one line in the body.
The last line of the main method contains the following reference to a method: convert.toLiters(gallons)
. This causes the value represented by the variable gallons
to be sent (passed) to toLiters
, where a computation is done, and a value is returned. In the toLiters
method the value that is passed is referred to as amount
. In the next section we will talk in detail about passing values in this manner.
This idea of a method taking in a value, using the value in a computation, and returning the result of a computation is similar to the mathematical idea of a function, which is why a method that returns a value is often referred to as a function.