Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.

Slides:



Advertisements
Similar presentations
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Starting Out with Java: From Control Structures through Objects Fourth.
Advertisements

5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Copyright © 2012 Pearson Education, Inc. Chapter 6 Modularizing Your Code with Methods.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Chapter 41 General Procedures Sub Procedures, Part I Sub Procedures, Part II Function Procedures.
Methods & Activation Record. Recap: what’s a method?
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
11 Chapter 5 METHODS CONT’D. 22 MORE ON PASSING ARGUMENTS TO A METHOD Passing an Object Reference as an Argument to a Method Objects are passed by reference.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 6: Functions Starting Out with C++ Early Objects
Chapter 5, Methods Java How to Program, Late Objects Version, 10/e
Methods Chapter Why Write Methods? Methods are commonly used to break a problem down into small manageable pieces. This is called divide and conquer.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
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.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Eighth Edition.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 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.
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.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
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.
Expressions Methods if else Statements Loops Potpourri.
Classes - Intermediate
Methods.
Method Examples CS 139 Algorithm Development 10/06/2008.
Exam Review 10/01/2014 Happy October. The Exam  Will be in Canvas  Two parts  Part A is recall – closed book, closed notes ◦Quizzes, in class activity.
Starting Out with Java: From Control Structures through Objects 5 th edition By Tony Gaddis Source Code: Chapter 5.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
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.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
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.
by Tony Gaddis and Godfrey Muganda
if-else-if Statements
Building Java Programs
Chapter Topics Chapter 5 discusses the following main topics:
Starting Out with Java: From Control Structures through Objects
Conditional Loops.
Starting Out with Java: From Control Structures through Objects
Chapter 6: Functions Starting Out with C++ Early Objects Ninth Edition
6 Chapter Functions.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
CS2011 Introduction to Programming I Methods (I)
class PrintOnetoTen { public static void main(String args[]) {
Chapter 6 – Methods Topics are:
Starting Out with Java: From Control Structures through Objects
Lecture 5- Classes, Objects and Methods
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.
Names of variables, functions, classes
Starting Out with Java: From Control Structures through Objects
Happy October Exam Review 10/01/2014.
Presentation transcript:

Chapter 5 : Methods Part 2

Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned from a method, back to the statement that called it. int num = Integer.parseInt(“700”);  The string “700” is passed into the parseInt method.  The int value 700 is returned from the method and stored into the num variable.

Defining a Value-Returning Method public static int sum(int num1, int num2) { int result; result = num1 + num2; return result; } Return type This expression must be of the same data type as the return type The return statement causes the method to end execution and it returns a value back to the statement that called the method.

Calling a Value-Returning Method total = sum(value1, value2); public static int sum(int num1, int num2) { int result; result = num1 + num2; return result; }

ValueReturn.java public class ValueReturn { public static void main(String[] args) { int total, value1 = 20, value2 = 40; total = sum(value1, value2); System.out.println("The sum of " + value1 + " and " + value2 + " is " + total); } public static int sum(int num1, int num2) { int result; // result is a local variable result = num1 + num2; return result; }

Returning a booleanValue  Frequently, we need to write methods to test arguments for validity and return true or false public static boolean isValid(int number) { boolean status; If(number >= 1 && number <= 100) status = true; else status = false; Return status; }

Returning a booleanValue Calling code: int value = 20; If(isValid(value)) System.out.println(“The value is within range”); else System.out.println(“The value is out of range”);

Returning a Reference to a String Object customername = fullName(“John”, “Martin”); public static String fullName(String first, String last) { String name; name = first + “ “ + last; return name; } address “John Martin” Local variable name holds the reference to the object. The return statement sends a copy of the reference back to the call statement and it is stored in customername.

ReturnString.java public class ReturnString { public static void main(String[] args) { String customerName; customerName = fullName("John", "Martin"); System.out.println(customerName); } public static String fullName(String first, String last) { String name; name = first + " " + last; return name; }

Problem Solving with Methods  A large, complex problem can be solved a piece at a time by methods.  The process of breaking a problem down into smaller pieces is called functional decomposition.  If a method calls another method that has a throws clause in its header, then the calling method should have the same throws clause.