2. Contents
Parameter : value given to method so that
it can do its task
Can has 0 or more parameter
Return value: A result that the method has
computed and returns it to the caller
Can returns 0 or 1 value
Eg. - pow(2,3)
- calculateArea()
- getBalance( )
- move( )
3. Learning Goals
A method declaration specifies the code that will
be executed when the method is invoked (called)
When a method is invoked, the flow of control
jumps to the method and executes its code
When complete, the flow returns to the place
where the method was called and continues
The invocation may or may not return a value,
depending on how the method is defined
4. Contents
If the called method is in the same class, only the
method name is needed
myMethod();
myMethodcompute
5. Learning Goals
The called method is often part of another class or
object
doIt helpMe
helpMe();obj.doIt();
main
6. Contents
What specific task will the method perform?
What input data will it need to perform its task?
What result will the method produce?
How input data are processed into result?
What algorithm will the method use?
7. Learning Goals
Method Name: calculateArea()
Task: To calculate the area of a rectangle
Data Needed (variables)
length: A variable to store the rectangle's length
width: A variable to store the rectangle's width
area: A variable to store result of calculation
Processing: area = length x width
Result to be returned: area
8. Contents
public class Rectangle // Class header
{
private double length; // Instance variables
private double width;
public Rectangle(double l, double w) // Constructor method
{
length = l;
width = w;
}
public double calculateArea() // calculate area method
{
double area;
area = length * width;
return area;
} // calculateArea()
} // Rectangle class
9. Learning Goals
Write an algorithm to calculate the perimeter of a
rectangle.
Write the method in Java.
10. Contents
Method Name: calculatePerimeter()
Task: To calculate the perimeter of a
rectangle
Data Needed (variables)
length
width
perimeter
Processing: perimeter = 2 x(length +
width)
Result to be returned: perimeter
12. Contents
Create, or instantiate, two instances of the
Rectangle class:
The objects
(instances) store
actual values.
Rectangle rectangle1 = new Rectangle(30,10);
Rectangle rectangle2 = new Rectangle(25, 20);
13. Learning Goals
We use a method call to ask each object to tell us
its area:
rectangle1 area 300
rectangle2 area 500Printed output:
System.out.println("rectangle1 area " + rectangle1.calculateArea());
System.out.println("rectangle2 area " + rectangle2.calculateArea());
References to
objects
Method calls
14. Contents
new ClassName(parameters);
Example:
new Rectangle(30, 20);
new Car("BMW 540ti", 2004);
Purpose:
To construct a new object, initialize it with
the construction parameters, and return a
reference to the constructed object.
15. Learning Goals
public class RectangleUser
{
public static void main(String argv[])
{
Rectangle rectangle1 = new Rectangle(30,10);
Rectangle rectangle2 = new Rectangle(25,20);
System.out.println("rectangle1 area " +
rectangle1.calculateArea());
System.out.println("rectangle2 area " +
rectangle2.calculateArea());
} // main()
} // RectangleUser
An application must
have a main() method
Object
Use
Object
Creation
Class
Definition