7 Methods: A Deeper Look [2014Q2-ME2008].

Slides:



Advertisements
Similar presentations
 2006 Pearson Education, Inc. All rights reserved Functions.
Advertisements

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.
7 7 Quiz Function call stack Passing values to functions Recursion.
 2006 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2006 Pearson Education, Inc. All rights reserved Arrays.
8 8 Function call stack Passing values to functions Recursion.
Chapter 7 Methods: A Deeper Look Visual C# 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
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.
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
1 Console methods Write and WriteLine also have the capability to display formatted data. Figure 3.17 shows another way to use the WriteLine method. Outline.
 2008 Pearson Education, Inc. All rights reserved Function Call Stack and Activation Records Data structure: collection of related data items.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Part II © Copyright by Pearson Education, Inc. All Rights Reserved.
4-Methods Dr. John P. Abraham Professor UTPA. Common ways of packaging code Properties Methods Classes Namespaces (related classes are grouped into a.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
 2008 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
 2006 Pearson Education, Inc. All rights reserved Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
 2009 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
 2006 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
C++ Programming Lecture 12 Functions – Part IV
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
7 Methods: A Deeper Look.
7 Methods: A Deeper Look.
7 Methods: A Deeper Look.
Functions.
Polymorphism, Interfaces & Operator Overloading
7 Methods: A Deeper Look.
Object-Oriented Programming: Inheritance
RECURSION.
5 C Functions.
Methods Chapter 6.
Programming Fundamentals Lecture #7 Functions
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
6.11 Function Call Stack and Activation Records
6 Functions.
Chapter 6 Methods: A Deeper Look
Chapter 5 - Functions Outline 5.1 Introduction
MSIS 655 Advanced Business Applications Programming
7 Arrays.
Chapter 6 Methods: A Deeper Look
Chapter 6 - Functions Outline 5.1 Introduction
MSIS 655 Advanced Business Applications Programming
6 Methods: A Deeper Look.
6 Functions.
6 Methods: A Deeper Look.
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods
6 Methods: A Deeper Look.
6 Methods: A Deeper Look.
JavaScript: Functions
6 Methods: A Deeper Look.
Fundaments of Game Design
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.
6 Functions.
Presentation transcript:

7 Methods: A Deeper Look [2014Q2-ME2008]

7.1   Introduction 7.2   Packaging Code in C# 7.3   static Methods, static Variables and Class Math 7.4   Declaring Methods with Multiple Parameters 7.5   Notes on Declaring and Using Methods 7.6   Method Call Stack and Activation Records 7.7 Argument Promotion and Casting 7.8 The Framework Class Library 7.9   Case Study: Random-Number Generation 7.9.1 Scaling and Shifting Random Numbers 7.9.2 Random-Number Repeatability for Testing and Debugging 7.10   Case Study: A Game of Chance (Introducing Enumerations) 7.11   Scope of Declarations

7.12   Method Overloading 7.13   Recursion 7.14   Passing Arguments: Pass-by-Value vs. Pass-by-Reference 7.15   (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System 7.16   Wrap-Up

Fig. 7.2 | Math class methods.

Outline MaximumFinder.cs (1 of 2)

Outline MaximumFinder.cs (2 of 2)

Outline MaximumFinderTest. cs

Fig. 7.5 | Implicit conversions between simple types.

Outline RandomIntegers.cs (1 of 2)

Outline RandomIntegers.cs (2 of 2)

Outline RollDie.cs (1 of 3)

Outline RollDie.cs (2 of 3)

Outline RollDie.cs (3 of 3)

Outline RandomIntegers.cs (1 of 2)

Outline RandomIntegers.cs (2 of 2)

Outline RollDie.cs (1 of 3)

Outline RollDie.cs (2 of 3)

Outline RollDie.cs (3 of 3)

Error-Prevention Tip 7.2 While an application is under development, create the Random object with a specific seed value to produce a repeatable sequence of random numbers each time the application executes. If a logic error occurs, fix the error and test the application again with the same seed value—this allows you to reconstruct the same sequence of random numbers that caused the error. Once the logic errors have been removed, create the Random object without using a seed value, causing the Random object to generate a new sequence of random numbers each time the application executes.

Outline Craps.cs (1 of 4)

Outline Craps.cs (2 of 4)

Outline Craps.cs (3 of 4)

Outline Craps.cs (4 of 4)

Outline CrapsTest.cs (1 of 2)

Outline CrapsTest.cs (2 of 2)

Good Programming Practice 7.3 Use only uppercase letters in the names of constants. This makes the constants stand out in an application and reminds you that enumeration constants are not variables.

Good Programming Practice 7.4 Using enumeration constants (like Status.WON, Status.LOST and Status.CONTINUE) rather than literal integer values (such as 0, 1 and 2) can make code easier to read and maintain.

Outline Scope.cs (1 of 2)

Outline Scope.cs (2 of 2)

Outline ScopeTest.cs

Outline MethodOverload.cs

Outline MethodOverload.cs

Outline MethodOverload.cs

Outline FactorialTest.cs (1 of 2)

Outline FactorialTest.cs (2 of 2)

Common Programming Error 7.11 Either omitting the base case or writing the recursion step incorrectly so that it does not converge on the base case will cause infinite recursion, eventually exhausting memory. This error is analogous to the problem of an infinite loop in an iterative (nonrecursive) solution.

Performance Tip 7.1 Pass-by-reference is good for performance reasons, because it can eliminate the pass-by-value overhead of copying large amounts of data.

Software Engineering Observation 7.5 Pass-by-reference can weaken security, because the called function can corrupt the caller’s data.

Outline ReferenceAndOutput Parameters.cs (1 of 2)

Outline ReferenceAndOutput Parameters.cs (2 of 2)

Outline ReferenceAndOutput ParamtersTest.cs

Common Programming Error 7.12 The ref and out arguments in a method call must match the parameters specified in the method declaration; otherwise, a compilation error occurs.

Software Engineering Observation 7.6 By default, C# does not allow you to choose whether to pass each argument by value or by reference. Value-types are passed by value. Objects are not passed to methods; rather, references to objects are passed to methods. The references themselves are passed by value. When a method receives a reference to an object, the method can manipulate the object directly, but the reference value cannot be changed to refer to a new object. In Section 8.8, you’ll see that references also can be passed by reference.

Fig. 7.20 | Verbs and verb phrases for each class in the ATM system.

Fig. 7.21 | Classes in the ATM system with attributes and operations.

Fig. 7.22 | Class BankDatabase with operation parameters.

Fig. 7.23 | Class Account with operation parameters.

Fig. 7.24 | Class Screen with an operation parameter.

Fig. 7.25 | Class CashDispenser with operation parameters.