 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.

Slides:



Advertisements
Similar presentations
Local Variables and Scope Benjamin Fein. Variable Scope A variable’s scope consists of all code blocks in which it is visible. A variable is considered.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Chapter 5 Functions.
 2006 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Introduction to Computers and Programming Introduction to Methods in Java.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Java: How to Program Methods Summary Yingcai Xiao.
Math class methods & User defined methods Math class methods Math.sqrt(4.0) Math.random() java.lang is the library/package that provides Math class methods.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
Introduction to Computers and Programming Lecture 13: User defined methods Instructor: Evan Korth New York University.
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,
 2003 Prentice Hall, Inc. All rights reserved Introduction Modules –Small pieces of a problem e.g., divide and conquer –Facilitate design, implementation,
Introduction to Methods
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
Java Program Components Java Keywords - Java has special keywords that have meaning in Java. - You have already seen a fair amount of keywords. Examples.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Part II © Copyright by Pearson Education, Inc. All Rights Reserved.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Procedural programming in Java Methods, parameters and return values.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Review for Nested loops & Math class methods & User defined methods.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
 2006 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math -Class Methods 6.4.
Methods.
Introduction Modules Small pieces of a problem ▴ e.g., divide and conquer Facilitate design, implementation, operation and maintenance of large programs.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Part III © Copyright by Pearson Education, Inc. All Rights Reserved.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Suppose we want to print out the word MISSISSIPPI in big letters.
Introduction to Modular Programming
Methods Chapter 6.
Object Oriented Systems Lecture 03 Method
Chapter 6 Methods: A Deeper Look
Chapter 6 Methods: A Deeper Look
MSIS 655 Advanced Business Applications Programming
Group Status Project Status.
CHAPTER 6 GENERAL-PURPOSE METHODS
Chapter 6 Methods.
Chapter 5 Methods.
6 Methods: A Deeper Look.
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
Presentation transcript:

 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks into self- contained units Prevent repeating code

 2005 Pearson Education, Inc. All rights reserved. 2 static Methods static method (or class method) – Applies to the class as a whole instead of a specific object of the class – the class static methods can be called without the need for an object of the class – Call a static method by using the method call: ClassName. methodName ( arguments ) – All methods of the Math class are static example: Math.sqrt( )

 2005 Pearson Education, Inc. All rights reserved. 3 Class Math is part of the java.lang package, which is implicitly imported by the compiler, so it is not necessary to import class Math to use its methods.

 2005 Pearson Education, Inc. All rights reserved. 4

5 public class JavaMathFunctions { public static void main(String[] args) { System.out.println("The sum of 2 and 3 = " + 5); System.out.println("7 + 8 = " + (7 + 8)); System.out.println(" "+Math.ceil(10.7)); System.out.println(" "+Math.floor(10.7)); System.out.println(" "+Math.ceil(-10.7)); System.out.println(" "+Math.floor(-10.7)); System.out.println(" "+Math.ceil(-10.4)); System.out.println(" "+Math.floor(-10.3)); System.out.println(" "+Math.pow(2,3)); System.out.println(" "+Math.sqrt(10.7)); System.out.println(" "+Math.max(3,5)); System.out.println(" "+Math.min(3,5)); }

 2005 Pearson Education, Inc. All rights reserved. 6 static Methods, static Fields and Class Math (Cont.) Method main main is declared static so it can be invoked without creating an object of the class containing main

 2005 Pearson Education, Inc. All rights reserved. 7 Declaring Methods with Multiple Parameters Multiple parameters can be declared by specifying a comma-separated list. – Arguments passed in a method call must be consistent with the number, types and order of the parameters Sometimes called formal parameters

 2005 Pearson Education, Inc. All rights reserved. 8

9

10

 2005 Pearson Education, Inc. All rights reserved. 11 Common Programming Error Declaring method parameters of the same type as float x, y instead of float x, float y is a syntax error-a type is required for each parameter in the parameter list.

 2005 Pearson Education, Inc. All rights reserved. 12 Declaring Methods with Multiple Parameters (Cont.) Reusing method Math.max – The expression Math.max( x, Math.max( y, z ) ) determines the maximum of y and z, and then determines the maximum of x and that value String concatenation – Using the + operator with two String s concatenates them into a new String – Using the + operator with a String and a value of another data type concatenates the String with a String representation of the other value

 2005 Pearson Education, Inc. All rights reserved. 13 Common Programming Error It is a syntax error to break a String literal across multiple lines in a program. If a String does not fit on one line, split the String into several smaller Strings and use concatenation to form the desired String.

 2005 Pearson Education, Inc. All rights reserved. 14 Common Programming Error Confusing the + operator used for string concatenation with the + operator used for addition can lead to strange results. Java evaluates the operands of an operator from left to right. For example, if integer variable y has the value 5, the expression "y + 2 = " + y + 2 results in the string "y + 2 = 52", not "y + 2 = 7", because first the value of y (5) is concatenated with the string "y + 2 = ", then the value 2 is concatenated with the new larger string "y + 2 = 5". The expression "y + 2 = " + (y + 2) produces the desired result "y + 2 = 7".

 2005 Pearson Education, Inc. All rights reserved. 15 Notes on Declaring and Using Methods Three ways to call a method: – Use a method name by itself to call another method of the same class – Use a variable containing a reference to an object, followed by a dot (. ) and the method name to call a method of the referenced object – Use the class name and a dot (. ) to call a static method of a class static methods cannot call non- static methods of the same class directly

 2005 Pearson Education, Inc. All rights reserved. 16 Notes on Declaring and Using Methods (Cont.) Three ways to return control to the calling statement: – If method does not return a result: Program flow reaches the method-ending right brace or Program executes the statement return; – If method does return a result: Program executes the statement return expression ; – expression is first evaluated and then its value is returned to the caller

 2005 Pearson Education, Inc. All rights reserved. 17 Common Programming Error Declaring a method outside the body of a class declaration or inside the body of another method is a syntax error.

 2005 Pearson Education, Inc. All rights reserved. 18 Common Programming Error Omitting the return-value-type in a method declaration is a syntax error.

 2005 Pearson Education, Inc. All rights reserved. 19 Common Programming Error Placing a semicolon after the right parenthesis enclosing the parameter list of a method declaration is a syntax error.

 2005 Pearson Education, Inc. All rights reserved. 20 Common Programming Error Redeclaring a method parameter as a local variable in the method’s body is a compilation error.

 2005 Pearson Education, Inc. All rights reserved. 21 Common Programming Error Forgetting to return a value from a method that should return a value is a compilation error. If a return value type other than void is specified, the method must contain a return statement that returns a value consistent with the method’s return-value-type..

 2005 Pearson Education, Inc. All rights reserved. 22 Random-Number Generation Random-number generation – static method random from class Math – class Random from package java.util

 2005 Pearson Education, Inc. All rights reserved. 23

 2005 Pearson Education, Inc. All rights reserved. 24 Two different sets of results containing integers in the range 1-6

 2005 Pearson Education, Inc. All rights reserved. 25 Scope of Declarations Basic scope rules – Scope of a parameter declaration is the body of the method in which appears – Scope of a local-variable declaration is from the point of declaration to the end of that block – Scope of a method or field of a class is the entire body of the class

 2005 Pearson Education, Inc. All rights reserved. 26 Scope of Declarations (Cont.) Shadowing – A field is shadowed (or hidden) if a local variable or parameter has the same name as the field This lasts until the local variable or parameter goes out of scope

 2005 Pearson Education, Inc. All rights reserved. 27 Common Programming Error A compilation error occurs when a local variable is declared more than once in a method.

 2005 Pearson Education, Inc. All rights reserved. 28

 2005 Pearson Education, Inc. All rights reserved. 29

 2005 Pearson Education, Inc. All rights reserved. 30