L13-Methods.ppt

Download Report

Transcript L13-Methods.ppt

King Fahd University of Petroleum & Minerals
College of Computer Science & Engineering
Information & Computer Science Department
ICS102
Lecture 13 : Methods
July 17, 2016
A Java Program
A Java Program



A Java program consists of one
or more classes
A Java class consists of one or
more methods
A Java method consists of one
or more statements
Java
classes
Java
Methods
- What is a method …

A Java application program consists of one or more classes.

Each class has one or more methods

Each method consists of one or more statements

Each method has a name.

One of the methods must be called main


When a Java application program is run, the run-time system
automatically invokes the method named main
All Java application programs start with the main method
… - What is a method …
public class class-name {
method1
method 2
method 3
…
…
method n
}
Example of a Java Program
Example of a Java Program
Class name
Main method
Class body
Instruction
Exercise

Write a program that computes the factorial (n!) of 6
then the factorial of 3, then the factorial of 10.
Exercise

Write a program that computes the factorial (n!) of 6
then the factorial of 3, then the factorial of 10.
Method
invocation
Return
type
Method
parameter
Method
definition
Return
instruction
-- Method Structure
If method doesn’t return value
Method name
public or private<static> <void or typeReturned>myMethod(<parameters>)
{
statement
Type of the return value
Variable list
statement
statement
Method body
…
…
statement
}
- Invoking a Methods



The statements inside a method body are executed when the
corresponding method is called from another method.
Calling a method is also called invoking a method
Each time the method is invoked, its corresponding body is
executed
- return Statements …

The body of a method that returns a value must also
contain one or more return statements

A return statement specifies the value returned and ends
the method invocation.
… - return Statements


A void method need not contain a return statement,
unless there is a situation that requires the method to
end before all its code is executed
Example : write a method that prints all the numbers
between 10 and 30
- Local Variables

A variable declared within a method definition is called a local
variable

All variables declared inside the method are local variables

All method parameters are local variables
- Local Variables

If two methods each have a local variable of the same name,
they are still two entirely different variables. Example:
Local variable in main method
Local variable in factorial method
Local variable in addition method
- Method Parameters …

A parameter list provides a description of the data required by a
method

It indicates the number and types of data pieces needed, the
order in which they must be given, and the local name for
these pieces as used in the method
public double myMethod(int p1, int p2, double p3)

Example: What is the parameter list of a method division that
divides two integers and returns the result (double)?
Parameters list
… - Method Parameters …


When a method is invoked, the appropriate values must be passed
to the method in the form of arguments
The number, order, and types of the arguments must exactly
match that of the parameter list
Error: type mismatch
… - Method Parameters …

Is this program correct?
Yes it is! .. Details in the next slide ..
… - Method Parameters …

If argument and parameter types do not match exactly, Java
will attempt to make an automatic type conversion


In the preceding example, the int value of argument a
would be cast to a double
A primitive argument can be automatically type cast from
any of the following types, to any of the types that
appear to its right:
byteshortintlongfloatdouble
char
… - Method Parameters …

A parameters is often thought of as a blank or placeholder
that is filled in by the value of its corresponding argument

However, a parameter is more than that: it is actually a local
variable


When a method is invoked, the value of its argument is
computed, and the corresponding parameter (i.e., local
variable) is initialized to this value
Even if the value of a formal parameter is changed within a
method (i.e., it is used as a local variable) the value of the
argument cannot be changed
Call-by-Value Example
Prints 10
Prints 10
- Method Parameters: Array Parameters


Methods can have array
parameters.
Example: Write a program
that prints the values of an
array of integers, then
increments each value of the
array by two and then prints
the new values of the same
array.
A parameter of type
array
- Details


Array arguments are always passed by reference. This means any changes
done to an array element by the invoked method, will also change the
corresponding array element in the caller method.
Example:
Public static void m1()
{ int [] A = { 1, 2, 3}; m2(A); }
public static void m2(int [] A)
{ A[1] = 5};

Method m2 changes the value of a[1] to 5; as a result the value of a[1] in
m1 will also change to 5.
- Method Overloading …




In java the same class can have methods with the same name.
Such methods are called overloaded methods.
Overloaded methods must differ in the number or type of their
arguments.
The name of a method together with the number, order, and types
of its arguments are called the method signature. No two methods
of the same class must have the same signature.
The compiler treats overloaded methods as completely different
methods. It knows which one to call by using method signatures.
- Method Overloading …
These 3 methods
have the same
name but different
signatures
The end
Important to do at home :
- read pages 206-212
Exercises


Write a method that takes as input an integer value
and returns the sum of all integers less than that
value. For example, if the input is 6, the output is
5+4+3+2+1 = 15. If the input is negative, the
output should be -1.
Write a method that takes as input an array of
integers and returns the average of the values in the
array.
Exercises


















Write a method public void printTriangleNumbers(int n) such that:
The call: printTriangleNumbers(5) prints the following on the screen:
12345
1234
123
12
1
The call: printTriangleNumbers(6) prints the following on the screen:
123456
12345
1234
123
12
1
The call: printTriangleNumbers(3) prints the following on the screen:
123
12
1