Methods, Continued
One more time (with feeling). The multiplier method will now be created in steps as the printOneLine method was previously.
Create the header. It needs a return type which is an integer in this case. It needs an identifier which is multiplier, and it needs parameters so that it can take in the two values to be multiplied. From the time being, we will leave the static modifier in for our discussions but note that it will not be used much once your programs move over into independent classes. To be clear, as long as you are writing one-file programs where the methods are all written in the same file as the main method, and called within the main method, you will need to place static between public (or private) and int (or other data types). But in most Java programming, that will not be happening much.
public static int multiplier( int value1, int value2 )
Create the body. This is simple and easy.
public static int multiplier( int value1, int value2 )
{
}
Design the method. Write out the method's operations in pseudocode.
public static int multiplier( int value1, int value2 )
{
// initialize method/variables
// conduct the multiplication
// return the result
}
Implement the method. Finally, fill in the necessary code. Note there are no methods used inside this method but that will sometimes occur in other methods.
public static int multiplier( int value1, int value2 )
{
// initialize method/variables
int result;
// conduct the multiplication
result = value1 * value2;
// return the result
return result;
}
Once again, there is not much to doing this, but you will need to practice at it. One more thing that you need to know related to this process is what kind of parameters are used in what locations. When you are formally declaring your method header or you are showing the method implementation, you are formally announcing to the compiler what the parameters, the method identifier, the return types, and the scope are going to be. For this reason, these are called formal parameters. However, when you actually use the method, you do not use any data types in the parameter list; you only use the program variables for the multiplication process; these are called actual parameters. It will be important for you to know when to place data types in your parameter lists and when you don't, so you need to familiarize yourself with these terms.
A brief program structure is shown next that exemplifies these terms, and the correct use of parameters. Only the code critical to the creation and use of the method are shown. The Console IO Class code is not shown.
// package name
// class name
// class constants
// class (member) variables
// Main Program
public static void main( String[] args )
{
// initialize program/variables
int oneValue, otherValue, product;
// assignment process, for variables
oneValue = 5;
otherValue = 7:
// multiplication process
// - the method uses ACTUAL parameters
// which DO NOT SHOW data types
product = multiplier( oneValue, otherValue );
// output process
// some output here
// end program
// display program end
}
// method Implementations
// the method implementation has FORMAL parameters which DO SHOW data types
// the formal parameters (identifiers) don't have to be the same
// as the actual parameters
// the multiplier and main answer variables also don't need to be the same
public static int multiplier( int firstVal, int secondVal )
{
// initialize method/variables
int answer;
// multiply values
answer = firstVal * secondVal;
// return the result
return answer;
}
The structure and overview has been presented here. Watch this video to see the complete program developed along with the method.