© 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development  Problem-solving approach  Breaking a task down into smaller subtasks  First level.

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Starting Out with Java: From Control Structures through Objects Fourth.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
©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.
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.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Introduction to Computers and Programming Lecture 13: User defined methods Instructor: Evan Korth New York University.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Chapter Four Defining Your Own Classes continued.
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.
Chapter 6: Functions.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
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.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Chapter 6 Sub Procedures
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Programming Logic and Design Using Methods. 2 Objectives Review how to use a simple method with local variables and constants Create a method that requires.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Chapter 6 Methods Chapter 6 - Methods.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
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.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
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.
CS305j Introduction to Computing Parameters and Methods 1 Topic 8 Parameters and Methods "We're flooding people with information. We need to feed it through.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Functions + Overloading + Scope
Chapter 7 Top-Down Development
by Tony Gaddis and Godfrey Muganda
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Methods.
if-else-if Statements
Method Mark and Lyubo.
Chapter Topics Chapter 5 discusses the following main topics:
Starting Out with Programming Logic & Design
Chapter 6 Sub Procedures
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.
Chapter 6 – Methods Topics are:
Starting Out with Java: From Control Structures through Objects
Week 4 Lecture-2 Chapter 6 (Methods).
Lecture 5- Classes, Objects and Methods
Starting Out with Programming Logic & Design
Based on slides created by Bjarne Stroustrup & Tony Gaddis
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.
Standard Version of Starting Out with C++, 4th Edition
Corresponds with Chapter 5
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

© 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development  Problem-solving approach  Breaking a task down into smaller subtasks  First level of subtasks translates into the main() method  Levels of tasks below main() are developed into a series of additional methods

© 2007 Lawrenceville Press Slide 2 Chapter 7 Using Methods  Used to implement a specific task  Methods must be part of a class  A main() method defines the first level of subtasks with calls to methods that implement the subtasks  Using methods to define tasks is called procedural abstraction

© 2007 Lawrenceville Press Slide 3 Chapter 7 A Method public static void fahrenheitToCelsius() { double fTemp, cTemp; Scanner input = new Scanner(System.in); System.out.println("Enter a Fahrenheit temperature: "); fTemp = input.nextDouble(); input.close; cTemp = (double)5/(double)9 * (fTemp - 32); System.out.println("The Celsius temperature is "+cTemp); } class method access level return type method name body

© 2007 Lawrenceville Press Slide 4 Chapter 7 Method Parameters  A method can have parameters for accepting values from a calling statement  Parameters are used inside the method to perform the method's task  The data passed to a method are called arguments  The drawBar() method declaration has one parameter named length: public static void drawBar(int length)

© 2007 Lawrenceville Press Slide 5 Chapter 7 Pass by Value  In Java, arguments are passed by value  A primitive data type gives the method a copy of its value. The method does not have access to the original data.  An object gives the method a copy of its reference that points to methods for changing object data. A method can change the data stored in an object because the method has access to the object's methods.

© 2007 Lawrenceville Press Slide 6 Chapter 7 Multiple Parameters  A method can have multiple parameters  Multiple parameters must be separated by commas  The order of the arguments passed must match the order of the parameters  The modified drawBar() method declaration has two parameters: public static void drawBar(int length, String mark)

© 2007 Lawrenceville Press Slide 7 Chapter 7 Method Overloading  More than one method of the same name can be included in a class  The compiler uses the types, order, and number of parameters to determine which method to execute

© 2007 Lawrenceville Press Slide 8 Chapter 7 The return Statement  A return statement is used to send a value back to the calling statement  A return statement can return only one value  A method that returns a value must include the return type in the method declaration. For example, the cubeOf() method returns a double : public static double cubeOf(double x)  A method that returns a value is called from a statement that will make use of the returned value. For example: cube = cubeOf(num);

© 2007 Lawrenceville Press Slide 9 Chapter 7 Documenting Methods  Methods should be carefully commented so that a reader of the program understands what task the method is performing and what data, if any, will be returned by the method  Method documentation should appear just above a method  Documentation should include a brief description of the method, any preconditions, and the postcondition

© 2007 Lawrenceville Press Slide 10 Chapter 7 Preconditions and Postconditions  The precondition states what must be true at the beginning of a method for the method to work properly.  The postcondition states what must be true after the method has executed if the method has worked properly.  Preconditions and postconditions should not state facts that the compiler will verify. They should also not refer to variables or information outside the method.  The postcondition should not state how the method accomplished its task.

© 2007 Lawrenceville Press Slide 11 Chapter 7 The GradeConverter Flowchart