Methods

advertisement
Methods
Creating your own methods
Java allows you to create custom methods inside its main body.
public class Test
{
// insert your own methods right here
public static void main(String[ ] args)
{
}
}
Characteristics of a method
reserved word static
return type
static int methodname(int a, int b)
{
// body
}
All methods must have these things.
parameters
Methods do not have to have
parameters/return types
static void method1()
static int method1(int a)
static double method1()
static void method1(int a, int b, String c)
All return types must have the word return at
the end of the method.
static int method1(int a, int b)
{
int sum = a + b;
return sum;
}
This value must match return type. In this case, an integer.
In our code, a method that returns a value must be set equal to
something.
static int returnSum(int a, int b)
{
int sum = a + b;
return sum;
parameters must match
}
void main()
{
int value = returnSum(10,10);
}
Here’s a method that gives line feeds
static void skip (int n)
{
for(int k = 0; k < n; k++)
System.out.println();
}
void main()
{
skip(5);
}
Here’s a method multiplies numbers together
and returns results.
static int method(int a, int b)
{
int c = a * b;
return c;
}
void main()
{
int d = method(10,10);
}
Here’s a method that takes an array and
initializes all values to random numbers.
static void method(int[ ] a)
{
for(int j = 0; j < a.length; j++)
System.out.print(a[j] + “ “);
}
void main()
{
int[ ] array1 = new int[10];
method(array1);
}
Example 1
static void skip()
{
for(int k = 0; k < 5; k++)
System.out.println();
}
void main()
{
skip(5);
}
Example 2
static int skip(int n)
{
for(int k = 0; k < n; k++)
System.out.println();
}
void main()
{
int x = skip(5);
}
Example 3
static void skip(int n)
{
for(int k = 0; k < n; k++)
System.out.println();
return 0;
}
void main()
{
skip(5);
}
Exercise 1
Write a method called PrintWorld. PrintWorld will take one integer as a
parameter and print “Hello World” that many times.
Exercise 2
Create a method called ReturnSum that returns the sum of any two
integers. ReturnSum will take two integers as parameters, add them
together, and then return the value.
Display the value of this sum in void main.
Exercise 3
Create a method called Factors. Factors takes three integer
parameters. The first two integers are to be multiplied together
repeatedly. The third integers indicates how many times the
multiplication will occur.
Display your results.
Exercise 4
Create a method called AddArray. AddArray has no return value and
does not have any parameters. AddArray will create an array of size 10
and assign random integer values to its elements between 0-10.
Now create a method called SumArray. SumArray will return the sum
of all the elements of the array that was passed to it. SumArray takes
an integer array as its parameter.
Run AddArray and then have AddArray call SumArray and pass the
array as a parameter.
Exercise 5
Declare 3 methods. Method1 multiplies two numbers together and
returns the value.
Method2 adds two numbers together and returns the value.
Method3 divides the first number by the second number.
In void main, ask me to enter two numbers and then ask me what I
want to do with those numbers. Call the appropriate method for my
selection. This should program should keep running until I choose exit.
Download