Methods CSCI 1301 What is a method? A method is a collection of statements that performs a specific task. Pre-Defined methods: available in Java library.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Starting Out with Java: From Control Structures through Objects Fourth.
Introduction to C++ Functions Chapter 6 Sections 6.1 – 6.3 Computer II.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
1 Fall 2009ACS-1903 Methods – Ch 5 A design technique referred to as stepwise refinement (or divide and conquer, or functional decomposition) is used to.
Chapter 6: User-Defined Functions I
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter 6: User-Defined Functions I
Copyright © 2012 Pearson Education, Inc. Chapter 6 Modularizing Your Code with Methods.
11 Chapter 5 METHODS. 22 INTRODUCTION TO METHODS A method is a named block of statements that performs a specific task. Other languages use the terms.
Introduction to Methods
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Chapter 06 (Part I) Functions and an Introduction to Recursion.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Procedural programming in Java Methods, parameters and return values.
Methods Chapter Why Write Methods? Methods are commonly used to break a problem down into small manageable pieces. This is called divide and conquer.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 5 : Methods. Why Write Methods?  Methods are commonly used to break a problem down into small manageable pieces. This is called divide and conquer.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Programming Fundamentals Enumerations and Functions.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Introduction to Methods ISYS 350. Methods Methods can be used to break a complex program into small, manageable pieces – This approach is known as divide.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Chapter 5 Methods. 2 Contents 1. Introduction to Methods 2. Passing Arguments to a Method 3. More about Local Variables 4. Returning a Value from a Method.
Functions + Overloading + Scope
Chapter 6: User-Defined Functions I
Examples of Classes & Objects
by Tony Gaddis and Godfrey Muganda
Object Oriented Systems Lecture 03 Method
CSCI 161: Introduction to Programming Function
Methods.
Chapter Topics Chapter 5 discusses the following main topics:
Starting Out with Java: From Control Structures through Objects
Starting Out with Java: From Control Structures through Objects
Chapter 4 void Functions
Classes & Objects: Examples
6 Chapter Functions.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Chapter 6: User-Defined Functions I
Chapter 6 – Methods Topics are:
Starting Out with Java: From Control Structures through Objects
Lecture 5- Classes, Objects and Methods
Functions Imran Rashid CTO at ManiWeber Technologies.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Corresponds with Chapter 5
Presentation transcript:

Methods CSCI 1301

What is a method? A method is a collection of statements that performs a specific task. Pre-Defined methods: available in Java library –System.out.println –Integer.parseInt –Math.pow User Defined methods: you create your own –main is a method you have created in every program

Divide and Conquer Methods break a complex program into small manageable pieces. Write several small methods which each solve a specific part of the problem.

public class BigProblem { public static void main (String [] args) { } public class DividedProblem { public static void main (String [] args) { } public static void printAnimals() { System.out.println ("Part 3: Animals"); System.out.println ("Dog"); System.out.println ("Cat"); System.out.println ("Bird"); } printAnimals(); public static void printNumbers() { System.out.println ("Part 2: Numbers"); System.out.println ("1"); System.out.println ("2"); System.out.println ("3"); } printNumbers(); public static void printLetters() { System.out.println ("Part 1: Letters"); System.out.println ("A"); System.out.println ("B"); System.out.println ("C"); } printLetters(); System.out.println ("Part 1: Letters"); System.out.println ("A"); System.out.println ("B"); System.out.println ("C"); System.out.println ("Part 2: Numbers"); System.out.println ("1"); System.out.println ("2"); System.out.println ("3"); System.out.println ("Part 3: Animals"); System.out.println ("Dog"); System.out.println ("Cat"); System.out.println ("Bird"); Using Methods requires: 1. Defining a method 2. Calling a method method definitions call statements

Save Time by Reusing Code Methods enable code reuse. Once a method has been defined it can be called repeatedly.

public class CodeReuse { public static void main (String [] args) { double x, y, z; x = Math.pow(5, 2) + Math.pow(6,2); y = Math.pow (3, 2) * 5; z = (9.0/5.0) * Math.pow (3, 3); display(x); display(y); display(z); } public static void display(double a) { System.out.println(" "); System.out.println ("Result: "+ a ); System.out.println(" "); System.out.println(); } Math is a class in the Java library. It contains several methods including pow. powpow(double a, double b) - Returns the value of the first argument raised to the power of the second argument. Predefined methodUser Defined method The display method is called 3 times Result: Result: Result: Output of Program

Defining Methods A method definition consists of a header and a body –header: contains name of method and lists parameters –body: statements enclosed within a set of curly braces public static void main (String [] args) { System.out.println(“Hello World!”); } header body method nameparameters

Parts of a Method Header Method modifiers – public and static are used to make a method available for use by other classes yet belong to the class in which it is defined. Return type – void means the method does not return a value. Otherwise specify the type of data the method returns (e.g., int, double … etc.) Method name –use a descriptive name. Use the same naming restrictions as variable names. Parameters enclose within parentheses allow data to be passed to the method. If the method does not receive any arguments, the parentheses are empty. public static double sum (double a, double b) public static void displayMessage( ) public static void showPay (int hours, double rate) NOTICE: Do NOT use a semicolon to end a method header

More about parameters Values that are passed into a method are called arguments, and the variables that receive those values are called parameters. Math.pow(5, 2) public static double pow (double b, double x) { //code to compute and return b x  5 2 } arguments parameters

More about parameters The parameter list defines temporary variables that will hold the values passed to the method. These variables will be destroyed when the method ends. The order of parameters must match the order of the arguments –Math.pow(5, 2) computes 5 2 but Math.pow(2, 5) computes 2 5 Math.pow(5, 2) public static double pow (double b, double x) { //code to compute and return b x  5 2 } arguments parameters

Writing the Method Body Include the statements to execute within the curly braces. { System.out.print(“Hello”); System.out.println(“ World!”); } public static void displayMessage( )

Writing the Method Body Create local variables, if necessary, to temporarily hold data. Like parameters, these local variables will be destroyed when the method ends. { double pay; //local variable pay = hours * rate; System.out.println(“ Pay: $” + pay); } public static void showPay (int hours, double rate)

Writing the Method Body If there is a return type, use a return statement to return a value of the same data type. { double total; //local variable total = a + b; return total; } public static double sum (double a, double b)

Calling Methods Parentheses are still need, even if the method does not have parameters public static void displayMessage( ) { System.out.print(“Hello”); System.out.println(“ World!”); } public static void main (String [] args ) { System.out.println(“Goodbye”); } call statement displayMessage(); method definition

Calling Methods Do not include data types when passing arguments public static void showPay (int hours, double rate) { double pay; //local variable pay = hours * rate; System.out.println(“ Pay: $” + pay); } public static void main (String [] args) { int hoursWorked = 40; double payRate = 16.50; } showPay(hoursWorked, payRate); showPay(int hoursWorked, double payRate); valid call statement ERROR!!

Calling Methods User defined methods can call other methods The value returned by a method can be used within a programming statement. public static double sample (double a, double b) { double total; //local variable total = + return total; } displayMessage(); sum(a,b)Math.pow(a,b);

Terminology Review Values that are sent into a method. a)arguments b)modifiers c)return type d)parameters

Terminology Review Only a copy of an argument’s value is passed into a parameter value. a)pass by reference b)pass by value c)mutator method d)void method

Terminology Review A special variable that holds a value being passed into a method. a)argument b)modifier c)return type d)parameter

Terminology Review A variable declared inside a method and is not accessible to statements outside the method. a)argument b)local variable c)lifetime variable d)final variable