TOPIC 6 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.

Slides:



Advertisements
Similar presentations
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Advertisements

Chapter 5 Functions.
Methods Liang, Chapter 4. What is a method? A method is a way of running an ‘encapsulated’ series of commands. System.out.println(“ Whazzup ”); JOptionPane.showMessageDialog(null,
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
Introduction to Java Programming, 4E Y. Daniel Liang.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Topic 04 Methods Programming II/A CMC2522 / CIM2561 Bavy Li.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
INF120 Basics in JAVA Programming AUBG, COS dept, Spring 2014 Lecture 08 Title: Methods, Part 2 Reference: MalikFarrell, chap 1, Liang, Ch 5.
Copyright © Curt Hill Mathematics Functions An example of static methods.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Programming Fundamentals I (COSC-1336), Lecture 5 (prepared after Chapter 5 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 5.
Chapter 5: Methods 1. Objectives To declare methods, invoke methods, and pass arguments to a method (§ ). To use method overloading and know ambiguous.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Method Abstraction You can.
Chapter 4 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Methods 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Introducing Methods.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static.
Lecture 5 Methods. Sometimes we want to perform the same sequence of operations multiple times in a program. While loops allow us to do this, they are.
1 Chapter 6 Methods. 2 Objectives F To declare methods, invoke methods, and pass arguments to a method. F To use method overloading and know ambiguous.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 4 Methods Chapter.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
1 Java Library Lecture 9 by Dr. Norazah Yusof. 2 Java Library Java has pre-defined classes that consist of the basic language classes in Java (organized.
1 Chapter 6 Methods. 2 Objectives F To declare methods, invoke methods, and pass arguments to a method. F To use method overloading and know ambiguous.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Chapter 4: Methods Method Structure Method Structure Declaring Methods Declaring Methods Calling Methods Calling Methods Passing Parameters Passing Parameters.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Lecture 5: java.lang.Math, java.lang.String and Characters Michael Hsu CSULA.
Chapter 5 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods.
Chapter 4 Mathematical Functions, Characters, and Strings 1.
Chapter 4 Mathematical Functions, Characters, and Strings
Reference: COS240 Syllabus
Chapter 4 Mathematical Functions, Characters, and Strings
Chapter 5 Methods.
Chapter 6: Methods CS1: Java Programming Colorado State University
Chapter 5 Functions DDC 2133 Programming II.
Chapter 5 Functions.
Chapter 6 Methods 1.
Chapter 3 Methods.
Chapter 5 Methods.
Chapter 6 Methods.
Chapter 5 – Part 2 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 4: Mathematical Functions, Characters, and Strings
Chapter 5 Function Basics
METHODS (FUNCTIONS) By: Lohani Adeeb khan.
Group Status Project Status.
CS2011 Introduction to Programming I Methods (II)
Chapter 6 Methods.
Chapter 5 Methods.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Chapter 5 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 4 Methods Introducing Methods Declaring Methods
Corresponds with Chapter 5
Chapter 6: Methods CS1: Java Programming Colorado State University
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

TOPIC 6 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction  The Math Class

Creating A Method

Declaring Methods public static int max(int num1, int num2) { if (num1 > num2) return num1; else return num2; }

Calling Methods F Two ways to call/invoke: (1)Method returns a value - call to method usually treated as a value - e.g. int larger = max(3, 4); System.out.println(max(3, 4)); (2)Method returns void - call to method must be a statement - e.g. System.out.println(“Welcome”);

Calling Methods F How is control passed back to the calling method?

public class TestMax { public static void main(String [ ] args) { int i = 5; int j = 2; int k = max(i, j); System.out.println(“Maximum between “ + i + “ and “ + j + “ is “ + k); } public static int max(int num1, int num2) { if (num1 > num2) return num1; else return num2; }

Passing Parameters F Formal parameters F Actual parameters F Parameter order association –Actual parameters must match the formal parameters in type, order and number. F Pass by Value -Copy of the value of the actual parameter is passed to the method. -Actual value of parameter outside method is not affected, regardless of any changes made to the formal parameter inside the method.

Overloading Methods double max(double num1, double num2) { if (num1 > num2) return num1; else return num2; }  Methods have the same name i.e. max, but different parameters. F Method signature –Method name with the parameter profiles. –Java runtime system determines which method to used based on the message signature.

The Math Class F Class constants: –PI –E F Class methods: –Trigonometric Methods –Exponent Methods –Miscellaneous

Exponent Methods  exp(double a) Returns e raised to the power of a.  log(double a) Returns the natural logarithm of a.  pow(double a, double b) Returns a raised to the power of b.  sqrt(double a) Returns the square root of a.

Miscellaneous Methods  max(a, b) and min(a, b) Returns the maximum or minimum of two parameters.  abs(a) Returns the absolute value of the parameter.  random() Returns a random double value in the range [0.0, 1.0).