Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2006 Pearson Education, Inc. All rights reserved. 1 7 7 Methods: A Deeper Look.

Similar presentations


Presentation on theme: " 2006 Pearson Education, Inc. All rights reserved. 1 7 7 Methods: A Deeper Look."— Presentation transcript:

1  2006 Pearson Education, Inc. All rights reserved. 1 7 7 Methods: A Deeper Look

2  2006 Pearson Education, Inc. All rights reserved. 2 E pluribus unum. (One composed of many.) — Virgil Form ever follows function. — Louis Henri Sullivan O! call back yesterday, bid time return. — William Shakespeare Call me Ishmael. — Herman Melville

3  2006 Pearson Education, Inc. All rights reserved. 3 Answer me in one word. — William Shakespeare When you call me that, smile! — Owen Wister There is a point at which methods devour themselves. — Frantz Fanon Life can only be understood backwards; but it must be lived forwards. — Soren Kierkegaard

4  2006 Pearson Education, Inc. All rights reserved. 4 OBJECTIVES In this chapter you will learn:  How static methods and variables are associated with an entire class rather than specific instances of the class.  How the method call/return mechanism is supported by the method call stack and activation records.  How to use random-number generation to implement game-playing applications.  To understand how the visibility of declarations is limited to specific regions of applications.  What method overloading is and how to create overloaded methods.  What recursive methods are.  The differences between passing method arguments by value and by reference.

5  2006 Pearson Education, Inc. All rights reserved. 5 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.7Argument 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

6  2006 Pearson Education, Inc. All rights reserved. 6 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

7  2006 Pearson Education, Inc. All rights reserved. 7 Good Programming Practice 7.1 Familiarize yourself with the classes and methods provided by the FCL ( msdn2.microsoft.com/en- us/library/ms229335 ). In Section 7.8, we present an overview of several common namespaces.

8  2006 Pearson Education, Inc. All rights reserved. 8 Software Engineering Observation 7.1 Don’t try to “reinvent the wheel.” When possible, reuse FCL classes and methods. This reduces application development time and avoids introducing programming errors.

9  2006 Pearson Education, Inc. All rights reserved. 9 Software Engineering Observation 7.2 To promote software reusability, every method should be limited to performing a single, well- defined task, and the name of the method should express that task effectively. Such methods make applications easier to write, debug, maintain and modify.

10  2006 Pearson Education, Inc. All rights reserved. 10 Error-Prevention Tip 7.1 A small method that performs one task is easier to test and debug than a larger method that performs many tasks.

11  2006 Pearson Education, Inc. All rights reserved. 11 Software Engineering Observation 7.3 If you cannot choose a concise name that expresses a method’s task, your method might be attempting to perform too many diverse tasks. It is usually best to break such a method into several smaller methods.

12  2006 Pearson Education, Inc. All rights reserved. 12 Fig. 7.1 | Hierarchical boss-method/worker-method relationship.

13  2006 Pearson Education, Inc. All rights reserved. 13 Fig. 7.2 | Math class methods.

14  2006 Pearson Education, Inc. All rights reserved. 14 Outline MaximumFinder.cs (1 of 2)

15  2006 Pearson Education, Inc. All rights reserved. 15 Outline MaximumFinder.cs (2 of 2)

16  2006 Pearson Education, Inc. All rights reserved. 16 Outline MaximumFinderTest. cs

17  2006 Pearson Education, Inc. All rights reserved. 17 Common Programming Error 7.1 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.

18  2006 Pearson Education, Inc. All rights reserved. 18 Software Engineering Observation 7.4 A method that has many parameters may be performing too many tasks. Consider dividing the method into smaller methods that perform the separate tasks. As a guideline, try to fit the method header on one line if possible.

19  2006 Pearson Education, Inc. All rights reserved. 19 Common Programming Error 7.2 It is a syntax error to break a string literal across multiple lines in an application. If a string does not fit on one line, split the string into several smaller string s and use concatenation to form the desired string.

20  2006 Pearson Education, Inc. All rights reserved. 20 Common Programming Error 7.3 Confusing the + operator used for string concatenation with the + operator used for addition can lead to strange results. The + operator is left-associative. 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".

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

22  2006 Pearson Education, Inc. All rights reserved. 22 Common Programming Error 7.5 Omitting the return type in a method declaration is a syntax error.

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

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

25  2006 Pearson Education, Inc. All rights reserved. 25 Common Programming Error 7.8 Forgetting to return a value from a method that should return a value is a compilation error. If a return type other than void is specified, the method must contain a return statement that returns a value consistent with the method’s return type. Returning a value from a method whose return type has been declared void is a compilation error.

26  2006 Pearson Education, Inc. All rights reserved. 26 Fig. 7.5 | Implicit conversions between simple types.

27  2006 Pearson Education, Inc. All rights reserved. 27 Common Programming Error 7.9 Converting a simple-type value to a value of another simple type may change the value if the promotion is not allowed. For example, converting a floating-point value to an integral value may introduce truncation errors (loss of the fractional part) in the result.

28  2006 Pearson Education, Inc. All rights reserved. 28 Fig. 7.6 | FCL namespaces (a subset) (Part 1 of 2).

29  2006 Pearson Education, Inc. All rights reserved. 29 Fig. 7.6 | FCL namespaces (a subset) (Part 2 of 2).

30  2006 Pearson Education, Inc. All rights reserved. 30 Good Programming Practice 7.2 The online.NET Framework documentation is easy to search and provides many details about each class. As you learn each class in this book, you should review the class in the online documentation for additional information.

31  2006 Pearson Education, Inc. All rights reserved. 31 Outline RandomIntegers.cs (1 of 2)

32  2006 Pearson Education, Inc. All rights reserved. 32 Outline RandomIntegers.cs (2 of 2)

33  2006 Pearson Education, Inc. All rights reserved. 33 Outline RollDie.cs (1 of 3)

34  2006 Pearson Education, Inc. All rights reserved. 34 Outline RollDie.cs (2 of 3)

35  2006 Pearson Education, Inc. All rights reserved. 35 Outline RollDie.cs (3 of 3)

36  2006 Pearson Education, Inc. All rights reserved. 36 Outline RandomIntegers.cs (1 of 2)

37  2006 Pearson Education, Inc. All rights reserved. 37 Outline RandomIntegers.cs (2 of 2)

38  2006 Pearson Education, Inc. All rights reserved. 38 Outline RollDie.cs (1 of 3)

39  2006 Pearson Education, Inc. All rights reserved. 39 Outline RollDie.cs (2 of 3)

40  2006 Pearson Education, Inc. All rights reserved. 40 Outline RollDie.cs (3 of 3)

41  2006 Pearson Education, Inc. All rights reserved. 41 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.

42  2006 Pearson Education, Inc. All rights reserved. 42 Outline Craps.cs (1 of 4)

43  2006 Pearson Education, Inc. All rights reserved. 43 Outline Craps.cs (2 of 4)

44  2006 Pearson Education, Inc. All rights reserved. 44 Outline Craps.cs (3 of 4)

45  2006 Pearson Education, Inc. All rights reserved. 45 Outline Craps.cs (4 of 4)

46  2006 Pearson Education, Inc. All rights reserved. 46 Outline CrapsTest.cs (1 of 2)

47  2006 Pearson Education, Inc. All rights reserved. 47 Outline CrapsTest.cs (2 of 2)

48  2006 Pearson Education, Inc. All rights reserved. 48 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.

49  2006 Pearson Education, Inc. All rights reserved. 49 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.

50  2006 Pearson Education, Inc. All rights reserved. 50 Error-Prevention Tip 7.3 Use different names for fields and local variables to help prevent subtle logic errors that occur when a method is called and a local variable of the method hides a field of the same name in the class.

51  2006 Pearson Education, Inc. All rights reserved. 51 Outline Scope.cs (1 of 2)

52  2006 Pearson Education, Inc. All rights reserved. 52 Outline Scope.cs (2 of 2)

53  2006 Pearson Education, Inc. All rights reserved. 53 Outline ScopeTest.cs

54  2006 Pearson Education, Inc. All rights reserved. 54 Outline MethodOverload.cs

55  2006 Pearson Education, Inc. All rights reserved. 55 Outline MethodOverload.cs

56  2006 Pearson Education, Inc. All rights reserved. 56 Outline MethodOverload.cs

57  2006 Pearson Education, Inc. All rights reserved. 57 Common Programming Error 7.10 Declaring overloaded methods with identical parameter lists is a compilation error regardless of whether the return types are different.

58  2006 Pearson Education, Inc. All rights reserved. 58 Fig. 7.16 | Recursive evaluation of 5!.

59  2006 Pearson Education, Inc. All rights reserved. 59 Outline FactorialTest.cs (1 of 2)

60  2006 Pearson Education, Inc. All rights reserved. 60 Outline FactorialTest.cs (2 of 2)

61  2006 Pearson Education, Inc. All rights reserved. 61 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.

62  2006 Pearson Education, Inc. All rights reserved. 62 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.

63  2006 Pearson Education, Inc. All rights reserved. 63 Software Engineering Observation 7.5 Pass-by-reference can weaken security, because the called function can corrupt the caller’s data.

64  2006 Pearson Education, Inc. All rights reserved. 64 Outline ReferenceAndOutput Parameters.cs (1 of 2)

65  2006 Pearson Education, Inc. All rights reserved. 65 Outline ReferenceAndOutput Parameters.cs (2 of 2)

66  2006 Pearson Education, Inc. All rights reserved. 66 Outline ReferenceAndOutput ParamtersTest.cs

67  2006 Pearson Education, Inc. All rights reserved. 67 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.

68  2006 Pearson Education, Inc. All rights reserved. 68 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.

69  2006 Pearson Education, Inc. All rights reserved. 69 Fig. 7.20 | Verbs and verb phrases for each class in the ATM system.

70  2006 Pearson Education, Inc. All rights reserved. 70 Fig. 7.21 | Classes in the ATM system with attributes and operations.

71  2006 Pearson Education, Inc. All rights reserved. 71 Fig. 7.22 | Class BankDatabase with operation parameters.

72  2006 Pearson Education, Inc. All rights reserved. 72 Fig. 7.23 | Class Account with operation parameters.

73  2006 Pearson Education, Inc. All rights reserved. 73 Fig. 7.24 | Class Screen with an operation parameter.

74  2006 Pearson Education, Inc. All rights reserved. 74 Fig. 7.25 | Class CashDispenser with operation parameters.


Download ppt " 2006 Pearson Education, Inc. All rights reserved. 1 7 7 Methods: A Deeper Look."

Similar presentations


Ads by Google