Chapter 6.5

15

Click here to load reader

Transcript of Chapter 6.5

Page 1: Chapter 6.5

LOOPING STRUCTURES

Chapter 5:

Page 2: Chapter 6.5

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( )

Page 3: Chapter 6.5

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

Page 4: Chapter 6.5

Contents

If the called method is in the same class, only the method name is needed

myMethod();

myMethodcompute

Page 5: Chapter 6.5

Learning Goals

The called method is often part of another class or object

doIt helpMe

helpMe();obj.doIt();

main

Page 6: Chapter 6.5

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?

Page 7: Chapter 6.5

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

Page 8: Chapter 6.5

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

Page 9: Chapter 6.5

Learning Goals

Write an algorithm to calculate the perimeter of a

rectangle.

Write the method in Java.

Page 10: Chapter 6.5

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

Page 11: Chapter 6.5

Learning Goals

public double calculatePerimeter()

{

double perimeter;

perimeter = 2 * (length + width);

return perimeter;

} // calculatePerimeter()

Page 12: Chapter 6.5

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);

Page 13: Chapter 6.5

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

objectsMethod calls

Page 14: Chapter 6.5

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.

Page 15: Chapter 6.5

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