Lecture 02 Object Oriented Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Lecture 02 Object Oriented Programming

Description:

A method is a series of statement that carry out some task. ... Square root of x. ISQS6337 JAVA. Jaeki Song. Example. Car Loan Systems. Shipping Charge ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 16
Provided by: jaeki8
Learn more at: http://jsong.ba.ttu.edu
Category:

less

Transcript and Presenter's Notes

Title: Lecture 02 Object Oriented Programming


1
Lecture 05Method
2
Outlines
  • Method
  • Creating method
  • Method call
  • Passing parameters
  • Method overloading
  • Math class

3
Methods
  • A method is a series of statement that carry out
    some task.
  • Java API provides a rich collection of classes
    and methods
  • User-defined method
  • Build-in method
  • Math class method

4
Method
  • A method must include the following
  • A declaration (or header)
  • An opening curly bracket
  • A body
  • A closing curly bracket
  • The method declaration contains the following
  • Optional access modifiers
  • The return type for the method
  • The method name
  • An opening parenthesis
  • An optional list of method argument
  • A closing parenthesis

5
Creating Method
  • Structure
  • modifier returnValueType methodName (list of
    parameters)

Method header
public static int max (int num1, int
num2) ..
Method body
6
Return Type
  • Void
  • does not return a value to the calling program
  • Nonvoid
  • returns a single value to the calling program.

7
Method Name
  • A verb or a verb phrase.
  • Capitalize the first letter of the second word
  • Never mention the method name inside of the
    function definition except if the function is
    recursive.

8
Parameter Listings
  • Are variables sent to the method to perform its
    designated tasks.
  • Must determine and list the data type of each
    parameter before listing its name.

9
Method Body
  • Includes the statements that will perform the
    task
  • Must be framed with curly braces.
  • Start by defining variables (only the ones
    outside of the class and not listed within the
    parameter listing)

10
Calling a Method
  • A method is invoked by a method call
  • Specifies the method name and provides
    information (argument)
  • Two ways to call a method
  • Based on whether the method returns a value or
    not
  • Return value
  • int larger max (3,4)
  • System.out.println( max (3,4))
  • Return void
  • A call to the method must be a statement
  • System.out.println ( Welcome)
  • Use the method name as a statement

11
Example
  • Max

12
Passing Parameters
  • Pass by value
  • When you invoke a method with parameters, a copy
    of the value of the actual parameter is passed to
    the method
  • Example Swap
  • Formal parameters are changed in the example, but
    the actual parameter are not affected

13
Overloading Methods
  • Create another method with the same name, but
    different parameters
  • Example
  • Overloading the Max method

14
Commonly Used Math Class Methods
Method
Description
abs (x)
Absolute value of x
exp (x)
Exponential method ex
log (x)
Natural logarithm of x (base e)
max (x, y)
Larger value of x and y
min (x, y)
Smaller value of x and y
pow (x, y)
X raised to power y (xy)
sqrt (x)
Square root of x
15
Example
  • Car Loan Systems
  • Shipping Charge
Write a Comment
User Comments (0)
About PowerShow.com