Let's create a method that adds two numbers and returns an answer. The calling method will output the two numbers and the answer to the screen.
Note: Please complete Create A Method I before doing these examples.
Start with the code created in Create A Method I:
- This should be a public method.
- It will return an integer.
- The method will be named sumNum( )
- It will accept two integers.
- The first integer will be called num1.
- The second integer will be called num2.
It doesn't matter what order you put the methods in, but by convention the main( ) method is usually put first at the top of the class. Some programmers order the methods alphabetically, others by order of importance, the most frequently changed methods go near the top of the class.
public class CreateMethod
{
public static void main(String[ ] args)
{
// DATA LAYER
String presidentName
= "John F. Kennedy";
int age = 86;
// INTERFACE
LAYER (output)
// this mixes
data in with interface
:-(
printNameAge("Abe
Lincoln", 194);
// using variables
allows separating
data and interface :-)
printNameAge(presidentName,
age);
}
public void printNameAge(String name,
int age)
{
System.out.println("Hello " +
name);
System.out.println("You
are " + age + " years old.");
}
public int sumNum(int num1, int num2)
{
}
}// end of CreateMethod
Inside the method write the code that will sum the two variables
that were declared inside the parenthesis.
Assign the answer to a
local variable named total.
You can also declare and initialize other local variables inside the method. These local variables (as well as the parameter variables in the signature line will be trashed by the garbage collector after the method is run (after the last curly bracket of the method).
public class CreateMethod
{
public static void main(String[ ] args)
{
// DATA LAYER
String presidentName = "John
F. Kennedy";
int age = 86;
// INTERFACE LAYER (output)
// this mixes data in with interface
:-(
printNameAge("Abe Lincoln",
194);
// using variables allows separating
data and interface :-)
printNameAge(presidentName, age);
}
public void printNameAge(String name,
int age)
{
System.out.println("Hello " +
name);
System.out.println("You
are " + age + " years old.");
}
public int sumNum(int num1, int num2)
{
// declare a variable to hold the
total
int total = 0;
// add the two parameters and assign
the answer to total
total = num1 + num2;
}
}// end of CreateMethod
Use the keyword "return" to return the answer back to the calling method.
The value must match the type listed on the signature line after the public/private designation. In this case the type is "int".
public class CreateMethod
{
public static void main(String[ ] args)
{
// DATA LAYER
String presidentName = "John
F. Kennedy";
int age = 86;
// INTERFACE LAYER (output)
// this mixes data in with interface
:-(
printNameAge("Abe Lincoln",
194);
// using variables allows separating
data and interface :-)
printNameAge(presidentName, age);
}
public void printNameAge(String name,
int age)
{
System.out.println("Hello " +
name);
System.out.println("You
are " + age + " years old.");
}
public int sumNum(int num1, int num2)
{
// declare
a variable to hold the total
int total = 0;
// add the
two parameters and assign the answer to total
total = num1 + num2;
// send answer back to calling method
return total;
}// all of the
local variables will die after this }
}// end of CreateMethod
public class CreateMethod
{
public static void main(String[ ] args)
{
// DATA LAYER
String presidentName = "John
F. Kennedy";
int age = 86;
int answer
=
0;
// INTERFACE LAYER (output)
printNameAge("Abe Lincoln",
194);
printNameAge(presidentName, age);
// sum two numbers then display answer
answer = sumNum(2,age);
System.out.println("The sum of 2
+ " + age + " = " + answer);
}
public void printNameAge(String name,
int age)
{
System.out.println("Hello " +
name);
System.out.println("You
are " + age + " years old.");
}
public int sumNum(int num1, int num2)
{
// declare
a variable to hold the total
int total = 0;
// add the two parameters and assign
the answer to total
total = num1 + num2;
// send answer
back to calling method
return total;
}//
all of the local variables will die after this }
}// end of CreateMethod
Java is considered a strongly-typed language. That means every object and variable must have a type and can only accept data in that type, none other.
Try passing a double or String variable into sumNum( ) to see what error message display.
Knowing Java's error messages will greatly speed your development and debug time.