Sunteți pe pagina 1din 21

Method

1
Objectives
 Able to differentiate types of method
 Able to differentiate between method with
parameter and method without parameter
 Able to declare method header
 Able to create calling statement

2
Type of method :
1. Void Method

• Defined as non returnable method. It will not return


anything to the calling statement. It will have void
keyword in the method header

Eg: public void calculate( )


{
// statements
}

3
Example of void Method
Method declaration:
class Vehicle
{
public void displayMessage( ) //void method header
{
System.out.println(“Hello, welcome to Vehicle class”);
}
}

Hello, welcome to Vehicle class


Invocation/Calling statement:

class VehicleTest
{
public static void main (String[ ] args)
{
Vechicle myVehicle = new Vehicle( );
myVehicle.displayMessage( ); //invocation or method call

}
}
4
Type of method :
2. Non-Void Method

• Defined as returnable method. It will return a value


to the calling statement. Consist of one return
statement. It will have the data types keyword in
the method header.

Eg: public int calculate ( )


{
// statements
return 23;
}
5
Example of Non-void Method
Method declaration:

class Vehicle
{
public int displayMessage( ) //non-void method header
{
System.out.println(“Hello, welcome to Vehicle class”);
return 123;
}
}
Hello, welcome to Vehicle class
Invocation/Calling statement: The output is 123
class VehicleTest
{
public static void main (String[ ] args)
{
Vechicle myVehicle = new Vehicle( );
int result = myVehicle.displayMessage( ); //method invocation
System.out.println(“ The output is ”+ result);
}
} 6
::Check point::
 Create a method header and an invocation
statement for the following:
1. In Employee class, create public method named
CalSalary that will not return anything to the calling
statement.
2. In Transportation class, create a public method
named AirTransport, that will return an integer data
type value to the calling statement.
3. In Festival class, create a public method named
EidFitri that will return a string data type value to the
calling statement.

7
::Check point 1::
 Create a method header and an invocation statement for the following:
 In Employee class, create public method named CalSalary that will not
return anything to the calling statement.

Answer:

class Employee
{
public void calculateSalary( )
{
}
}

class EmployeeTest
{
public static void main ( String[ ] args)
{
Employee myEmployee = new Employee( );
myEmployee.calculateSalary( );
}
8
}
::Check point 2::
 Create a method header and an invocation statement for the following:
 In Transportation class, create a public method named AirTransport, that will return an integer data type
value to the calling statement.

Answer:

class Transportation
{
public int airTransport( )
{
return 947;
}
}

class TransportationTest
{
public static void main(String[ ] args)
{
Transportation myTransport = new Transportation( );
int result = myTransport. airTransport( );
System.out.println (“The output is ” + result);
} 9
}
::Check point 3::
 Create a method header and an invocation statement for the following:
 In Festival class, create a public method named EidFitri that will return a string data type value
to the calling statement.

Answer:

class Festival
{
public String eidFitri( )
{
return “Rendang”;
}
}

class FestivalTest
{
public static void main (String[ ] args)
{
Festival myFest= new Festival ( );
String raya = myFest.eidFitri( );
System.out.println(“The message is ” + raya);
}
} 10
Argument and parameter
 Allmethods, void or non-void methods can
have parameter.

 Argument is a variable or value that is


passed to the method header

 Parameter is a variable or value that is


received from the calling statement.
11
Method without parameter
:: Example ::

class Employee
{
public void CalSalary ( ) No receiving parameter
{
//…statements…
}
}

class EmployeeTest
{
public static void main (String[ ] args)
{

Employee myEmployee = new Employee( );


myEmployee.CalSalary ( ); No argument passed

}
}
12
Method with parameter
:: Example ::

class Employee
{
public void CalSalary (int basic) 1 receiving parameter
{ as integer
//…statements…
}
}

class EmployeeTest
{
public static void main(String[ ] args)
{
Employee myEmployee = new Employee( );
myEmployee.CalSalary ( 4000); 1 integer argument passed
}
}
13
Method with parameter
:: Example ::

class Employee
{
public void CalSalary (int basic, double rate) 2 receiving parameters
{ integer and double
//…statements…
}
}

class EmployeeTest
{
public static void main (String[ ] args)
{
Employee myEmployee = new Employee( );
myEmployee.CalSalary( 4000, 1.7); 2 arguments passed
integer and double
}
}

 NOTE: Argument is passed by location and with correspondence data type to the
receiving parameter 14
Method with parameter
:: Example ::

class Employee
{
public void CalSalary (int Basic, double Rate) 2 parameters received
{ integer and double
//…statements…
}
}

class EmployeeTest
{
public static void main (String[ ] args)
{
Employee myEmployee = new Employee( );
int Pay = 4000;
double PayRate= 1.7;
myEmployee.CalSalary (Pay, PayRate); 2 arguments passed
integer and double
}
}
15
:: Check Point ::
 Writethe suitable invocation statement called
by object named myWork for the following
method header:

public void doWork(double amount,double charge)

public void doWork(int num,String name,double charge)

public void doWork(String name)

public void doWork(char grade,double amount)


16
:: Check Point ::
 Write the suitable method header for the following
invocation/calling statement:
total =109;

grade=‘A’;

myObj.Compute ( total, 45.90);

myObj.Move (789, total, grade);

myObj.Finale (“Hakim”, grade, 99.89, total);

myObj.Computer (“Compaq”, 4999.99, grade); 17


::Example::
Non-void method without parameter
import java.util.*;
class Number
{
public int checkNumber( )
{
int num, output;
Scanner sc = new Scanner(System.in);
System.out.println(“Enter an integer : ”);
num = sc.nextInt();
if (num >0)
output=1;
else
output= -1;
return output;
}
}

class NumberTest
{
public static void main (String[ ]args)
{
Number myNum = new Number();
int result = myNum.checkNumber();
System.out.println(“ The answer is ” + result);
} 18
}
::Example::
Non-void method with parameter
import java.util.*;
class NumberTest
{
public static void main (String[ ] args)
{
Scanner s = new Scanner(System.in);
Number myNum = new Number();
int num;
System.out.println(“Enter an integer”);
num = s.nextInt();
int result = myNum.checkNumber (num);
System.out.println(“ The answer is ” + result);
}
}

class Number
{
public int checkNumber(int num)
{
int output;
if (num >0)
output=1;
else
output= -1;
return output;
} 19
::Check Point::
 Create a class ShortCourse, containing a public
method, updatedFee() that will accept 3
parameters, courseTitle, courseId and courseFee.
It shall calculate and return the new total fees of
specific courses based on the following discount
rates shown below. Display new fees in main().

courseId discount
ISB10103 22%
INB11303 15%
Others 10% 20
Parameter Passing: Key Points
1. Arguments are passed to a method by using the pass-by-
value scheme.
2. Arguments are matched to the parameters from left to right.
The data type of an argument must be assignment-
compatible with the data type of the matching parameter.
3. The number of arguments in the method call must match the
number of parameters in the method definition.
4. Parameters and arguments do not have to have the same
name.
5. Local copies, which are distinct from arguments,are created
even if the parameters and arguments share the same
name.
6. Parameters are input to a method, and they are local to the
method.Changes made to the parameters will not affect the
value of corresponding arguments.
21

S-ar putea să vă placă și