1 7 Methods A Deeper Look 2009 Pearson

  • Slides: 113
Download presentation
1 7 Methods: A Deeper Look 2009 Pearson Education, Inc. All rights reserved.

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

2 Form ever follows function. – Louis Henri Sullivan E pluribus unum. (One composed

2 Form ever follows function. – Louis Henri Sullivan E pluribus unum. (One composed of many. ) – Virgil O! call back yesterday, bid time return. – William Shakespeare Call me Ishmael. – Herman Melville When you call me that, smile! – Owen Wister 2009 Pearson Education, Inc. All rights reserved.

3 Answer me in one word. – William Shakespeare There is a point at

3 Answer me in one word. – William Shakespeare 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 2009 Pearson Education, Inc. All rights reserved.

4 OBJECTIVES In this chapter you will learn: § How static methods and variables

4 OBJECTIVES In this chapter you will learn: § How static methods and variables are associated with a 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. § How the visibility of declarations is limited to specific regions of applications. 2009 Pearson Education, Inc. All rights reserved.

5 OBJECTIVES § What method overloading is and how to create overloaded methods. §

5 OBJECTIVES § 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. 2009 Pearson Education, Inc. All rights reserved.

6 7. 1 Introduction 7. 2 Packaging Code in C# 7. 3 static Methods,

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

7 7. 10 Case Study: A Game of Chance (Introducing Enumerations) 7. 11 Scope

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

8 7. 1 Introduction • The best way to develop and maintain a large

8 7. 1 Introduction • The best way to develop and maintain a large application is to construct it from small, simple pieces. • This technique is called divide and conquer. 2009 Pearson Education, Inc. All rights reserved.

9 7. 2 Packaging Code in C# • Common ways of packaging code are

9 7. 2 Packaging Code in C# • Common ways of packaging code are properties, methods, classes and namespaces. • The Framework Class Library provides many predefined classes that contain methods for performing common tasks. Good Programming Practice 7. 1 Familiarize yourself with the classes and methods provided by the Framework Class Library. Software Engineering Observation 7. 1 Don’t try to “reinvent the wheel. ” When possible, reuse Framework Class Library classes and methods. 2009 Pearson Education, Inc. All rights reserved.

10 7. 2 Packaging Code in C# (Cont. ) • Methods allow you to

10 7. 2 Packaging Code in C# (Cont. ) • Methods allow you to modularize an application by separating its tasks into reusable units. • Methods that you write are sometimes referred to as user defined methods. – The “divide-and-conquer” approach makes application development more manageable by constructing applications from small, simple pieces. – Software reusability—existing methods can be used as building blocks to create new applications. – Avoid repeating code. • Dividing an application into meaningful methods makes the application easier to debug and maintain. 2009 Pearson Education, Inc. All rights reserved.

11 7. 2 Packaging Code in C# (Cont. ) Software Engineering Observation 7. 2

11 7. 2 Packaging Code in C# (Cont. ) 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. 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. 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. 2009 Pearson Education, Inc. All rights reserved.

12 7. 2 Packaging Code in C# (Cont. ) • The code that calls

12 7. 2 Packaging Code in C# (Cont. ) • The code that calls a method is known as the client code. • An analogy to the method-call-and-return structure is the hierarchical form of management (Figure 7. 1). Fig. 7. 1 | Hierarchical boss-method/worker-method relationship. (Part 1 of 2. ) 2009 Pearson Education, Inc. All rights reserved.

13 7. 2 Packaging Code in C# (Cont. ) – The boss method does

13 7. 2 Packaging Code in C# (Cont. ) – The boss method does not know how the worker method performs its designated tasks. – The worker may also call other worker methods. • This “hiding” of implementation details promotes good software engineering. 2009 Pearson Education, Inc. All rights reserved.

7. 3 static Methods, static Variables and Class Math 14 • A method that

7. 3 static Methods, static Variables and Class Math 14 • A method that applies to the class in which it is declared as a whole is known as a static method. • To declare a method as static, place the keyword static before the return type in the method’s declaration. • You call any static method by specifying the name of the class in which the method is declared, followed by the member access (. ) operator and the method name. 2009 Pearson Education, Inc. All rights reserved.

7. 3 static Methods, static Variables and Class Math (Cont. ) 15 • Class

7. 3 static Methods, static Variables and Class Math (Cont. ) 15 • Class Math provides a collection of static methods that enable you to perform common mathematical calculations. • You do not need to create a Math object before calling method Sqrt. • Method arguments may be constants, variables or expressions. 2009 Pearson Education, Inc. All rights reserved.

16 7. 3 static Methods, static Variables and Class Math (Cont. ) • Figure

16 7. 3 static Methods, static Variables and Class Math (Cont. ) • Figure 7. 2 summarizes several Math class methods. In the figure, x and y are of type double. Fig. 7. 2 | Math class methods. (Part 1 of 3. ) 2009 Pearson Education, Inc. All rights reserved.

17 7. 3 static Methods, static Variables and Class Math (Cont. ) Fig. 7.

17 7. 3 static Methods, static Variables and Class Math (Cont. ) Fig. 7. 2 | Math class methods. (Part 2 of 3. ) 2009 Pearson Education, Inc. All rights reserved.

18 7. 3 static Methods, static Variables and Class Math (Cont. ) Fig. 7.

18 7. 3 static Methods, static Variables and Class Math (Cont. ) Fig. 7. 2 | Math class methods. (Part 3 of 3. ) 2009 Pearson Education, Inc. All rights reserved.

7. 3 static Methods, static Variables and Class Math (Cont. ) 19 • Class

7. 3 static Methods, static Variables and Class Math (Cont. ) 19 • Class Math also declares two static constants that represent commonly used mathematical values: Math. PI and Math. E. • These constants are declared in class Math as public and const. – public allows other programmers to use these variables in their own classes. – Keyword const prevents its value from being changed after the constant is declared. 2009 Pearson Education, Inc. All rights reserved.

7. 3 static Methods, static Variables and Class Math (Cont. ) 20 Common Programming

7. 3 static Methods, static Variables and Class Math (Cont. ) 20 Common Programming Error 7. 1 Every constant declared in a class, but not inside a method of the class is implicitly static, so it is a syntax error to declare such a constant with keyword static explicitly. • Because these constants are static, you can access them via the class name Math and the member access (. ) operator, just like class Math’s methods. • When objects of a class containing static variables are created, all the objects of that class share one copy of the class’s static variables. • Together the static variables and instance variables represent the fields of a class. 2009 Pearson Education, Inc. All rights reserved.

7. 3 static Methods, static Variables and Class Math (Cont. ) 21 Why Is

7. 3 static Methods, static Variables and Class Math (Cont. ) 21 Why Is Method Main Declared static? • The Main method is sometimes called the application’s entry point. • Declaring Main as static allows the execution environment to invoke Main without creating an instance of the class. • When you execute your application from the command line, you type the application name, followed by command line arguments that specify a list of strings separated by spaces. • The execution environment will pass these arguments to the Main method of your application. 2009 Pearson Education, Inc. All rights reserved.

7. 3 static Methods, static Variables and Class Math (Cont. ) 22 Additional Comments

7. 3 static Methods, static Variables and Class Math (Cont. ) 22 Additional Comments about Method Main • Applications that do not take command-line arguments may omit the string[] args parameter. • The public keyword may be omitted. • You can declare Main with return type int (instead of void) to enable Main to return an error code with the return statement. • You can declare only one Main method in each class. 2009 Pearson Education, Inc. All rights reserved.

7. 3 static Methods, static Variables and Class Math (Cont. ) 23 • You

7. 3 static Methods, static Variables and Class Math (Cont. ) 23 • You can place a Main method in every class you declare. • However, you need to indicate the application’s entry point. • Do this by clicking the menu Project > [Project. Name] Properties. . . and selecting the class containing the Main method that should be the entry point from the Startup object list box. 2009 Pearson Education, Inc. All rights reserved.

Outline 24 • A Maximum. Finder class is presented in Fig. 7. 3. Maximum.

Outline 24 • A Maximum. Finder class is presented in Fig. 7. 3. Maximum. Finder. cs ( 1 of 3 ) Prompt the user to enter three double values and read them from the user. Fig. 7. 3 | User-defined method Maximum. (Part 1 of 3. ) 2009 Pearson Education, Inc. All rights reserved.

Outline 25 Maximum. Finder. cs ( 2 of 3 ) Call method Maximum to

Outline 25 Maximum. Finder. cs ( 2 of 3 ) Call method Maximum to determine the largest of the three double values passed as arguments to the method. The method’s name is Maximum and that the method requires three double parameters to accomplish its task Fig. 7. 3 | User-defined method Maximum. (Part 2 of 3. ) 2009 Pearson Education, Inc. All rights reserved.

Outline 26 Maximum. Finder. cs ( 3 of 3 ) Fig. 7. 3 |

Outline 26 Maximum. Finder. cs ( 3 of 3 ) Fig. 7. 3 | User-defined method Maximum. (Part 3 of 3. ) 2009 Pearson Education, Inc. All rights reserved.

Outline • Class Maximum. Finder. Test (Fig. 7. 4) contains the application’s entry point.

Outline • Class Maximum. Finder. Test (Fig. 7. 4) contains the application’s entry point. 27 Maximum. Finder Test. cs (1 of 2 ) Create an object of class Maximum. Finder Calls the object’s Determine Maximum method to produce the application’s output Fig. 7. 4 | Application to test class Maximum. Finder. (Part 1 of 2. ) 2009 Pearson Education, Inc. All rights reserved.

Outline 28 Maximum. Finder Test. cs (2 of 2 ) Fig. 7. 4 |

Outline 28 Maximum. Finder Test. cs (2 of 2 ) Fig. 7. 4 | Application to test class Maximum. Finder. (Part 2 of 2. ) 2009 Pearson Education, Inc. All rights reserved.

7. 4 Declaring Methods with Multiple Parameters (Cont. ) 29 • When a method

7. 4 Declaring Methods with Multiple Parameters (Cont. ) 29 • When a method has more than one parameter, the parameters are specified as a comma-separated list. • There must be one argument in the method call for each parameter (sometimes called a formal parameter) in the method declaration. • Each argument must be consistent with the type of the corresponding parameter. • When program control returns from a method, that method’s parameters are no longer accessible in memory. • Methods can return at most one value. 2009 Pearson Education, Inc. All rights reserved.

7. 4 Declaring Methods with Multiple Parameters (Cont. ) 30 • Variables should be

7. 4 Declaring Methods with Multiple Parameters (Cont. ) 30 • Variables should be declared as fields of a class only if they are required for use in more than one method or if the application needs to save their values between calls to the class’s methods. Common Programming Error 7. 2 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. 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. 2009 Pearson Education, Inc. All rights reserved.

7. 4 Declaring Methods with Multiple Parameters (Cont. ) 31 Implementing Method Maximum by

7. 4 Declaring Methods with Multiple Parameters (Cont. ) 31 Implementing Method Maximum by Reusing Method Math. Max • The entire body of our maximum method could also be implemented with nested calls to Math. Max, as follows: return Math. Max( x, Math. Max( y, z ) ); • Before any method can be called, all its arguments must be evaluated to determine their values. • Math. Max( y, z ) is evaluated first, then the result is passed as the second argument to the other call to Math. Max 2009 Pearson Education, Inc. All rights reserved.

7. 4 Declaring Methods with Multiple Parameters (Cont. ) 32 Assembling Strings with String

7. 4 Declaring Methods with Multiple Parameters (Cont. ) 32 Assembling Strings with String Concatenation • string concatenation allows you to combine strings using operator +. • When one of the + operator’s operands is a string, the other is implicitly converted to a string, then the two are concatenated. • If a bool is concatenated with a string, the bool is converted to the string "True" or "False". 2009 Pearson Education, Inc. All rights reserved.

7. 4 Declaring Methods with Multiple Parameters (Cont. ) 33 • All objects have

7. 4 Declaring Methods with Multiple Parameters (Cont. ) 33 • All objects have a To. String method that returns a string representation of the object. • When an object is concatenated with a string, the object’s To. String method is implicitly called to obtain the string representation of the object. • A large string literal in a program can be broken into several smaller strings and placed them on multiple lines for readability, and reassembled using string concatenation or string 2009 Pearson Education, Inc. All rights reserved.

7. 4 Declaring Methods with Multiple Parameters (Cont. ) 34 Common Programming Error 7.

7. 4 Declaring Methods with Multiple Parameters (Cont. ) 34 Common Programming Error 7. 3 If a string does not fit on one line, split the string into several smaller strings and use concatenation to form the desired string. C# also provides so called verbatim string literals, which are preceeded by the @ character. Such literals can be split over multiple lines and the characters in the literal are processed exactly as they appear in the literal. Common Programming Error 7. 4 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". 2009 Pearson Education, Inc. All rights reserved.

7. 5 Notes on Declaring and Using Methods 35 • To access the class’s

7. 5 Notes on Declaring and Using Methods 35 • To access the class’s non-static members, a static method must use a reference to an object of the class. • There are three ways to return control to the statement that calls a method. – Reaching the end of the method. – A return statement without a value. – A return statement with a value. 2009 Pearson Education, Inc. All rights reserved.

7. 5 Notes on Declaring and Using Methods (Cont. ) 36 Common Programming Error

7. 5 Notes on Declaring and Using Methods (Cont. ) 36 Common Programming Error 7. 5 Declaring a method outside the body of a class declaration or inside the body of another method is a syntax error. Common Programming Error 7. 6 Omitting the return type in a method declaration is a syntax error. Common Programming Error 7. 7 Redeclaring a method parameter as a local variable in the method’s body is a compilation er ror. Common Programming Error 7. 8 Forgetting to return a value from a method that should return one is a compilation error. If a return type other than void is specified, the method must contain a return statement in each possible execution path. 2009 Pearson Education, Inc. All rights reserved.

7. 6 Method-Call Stack and Activation Records 37 • A stack is a last

7. 6 Method-Call Stack and Activation Records 37 • A stack is a last in, first out (LIFO) data structure. – Elements are added by pushing them onto the top of the stack. – Elements are removed by popping them off the top of the stack. • When an application calls a method, the return address of the calling method is pushed onto the program execution stack. 2009 Pearson Education, Inc. All rights reserved.

7. 6 Method-Call Stack and Activation Records (Cont. ) 38 • The program-execution stack

7. 6 Method-Call Stack and Activation Records (Cont. ) 38 • The program-execution stack also stores local variables. This data is known as the activation record or stack frame of the method call. – When a method call is made, its activation record is pushed onto the program-execution stack. – When the method call is popped off the stack, the local variables are no longer known to the application. • If so many method calls occur that the stack runs out of memory, an error known as a stack overflow occurs. 2009 Pearson Education, Inc. All rights reserved.

39 7. 7 Argument Promotion and Casting • Argument promotion is the implicit conversion

39 7. 7 Argument Promotion and Casting • Argument promotion is the implicit conversion of an argument’s value to the type that the method expects to receive. • These conversions generate compile errors if they don’t follow C#’s promotion rules; these specify which conversions can be performed without losing data. – An int can be converted to a double without changing its value. – A double cannot be converted to an int without loss of data. – Converting large integer types to small integer types (e. g. , long to int) can also result in changed values. • The types of the original values remain unchanged. 2009 Pearson Education, Inc. All rights reserved.

40 7. 7 Argument Promotion and Casting (Cont. ) • Figure 7. 5 lists

40 7. 7 Argument Promotion and Casting (Cont. ) • Figure 7. 5 lists the simple types alphabetically and the types to which each can be promoted. Fig. 7. 5 | Implicit conversions between simple types. (Part 1 of 2. ) 2009 Pearson Education, Inc. All rights reserved.

41 7. 7 Argument Promotion and Casting (Cont. ) Fig. 7. 5 | Implicit

41 7. 7 Argument Promotion and Casting (Cont. ) Fig. 7. 5 | Implicit conversions between simple types. (Part 2 of 2. ) 2009 Pearson Education, Inc. All rights reserved.

7. 7 Argument Promotion and Casting (Cont. ) 42 • All simple types can

7. 7 Argument Promotion and Casting (Cont. ) 42 • All simple types can also be implicitly converted to type object. • In cases where information may be lost, you must use a cast operator to explicitly force the conversion. 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. 2009 Pearson Education, Inc. All rights reserved.

43 7. 8 The. NET Framework Class Library • Predefined classes are grouped into

43 7. 8 The. NET Framework Class Library • Predefined classes are grouped into categories of related classes called namespaces. • Together, these namespaces are referred to as the. NET Framework Class Library. 2009 Pearson Education, Inc. All rights reserved.

44 7. 8 The. NET Framework Class Library (Cont. ) • Some key Framework

44 7. 8 The. NET Framework Class Library (Cont. ) • Some key Framework Class Library namespaces are described in Fig. 7. 6 | Framework Class Library namespaces (a subset). (Part 1 of 2. ) 2009 Pearson Education, Inc. All rights reserved.

45 7. 8 The. NET Framework Class Library (Cont. ) Fig. 7. 6 |

45 7. 8 The. NET Framework Class Library (Cont. ) Fig. 7. 6 | Framework Class Library namespaces (a subset). (Part 2 of 2. ) 2009 Pearson Education, Inc. All rights reserved.

46 7. 8 The. NET Framework Class Library (Cont. ) Good Programming Practice 7.

46 7. 8 The. NET Framework Class Library (Cont. ) 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. 2009 Pearson Education, Inc. All rights reserved.

7. 9 Case Study: Random-Number Generation 47 • Objects of class Random can produce

7. 9 Case Study: Random-Number Generation 47 • Objects of class Random can produce random byte, int and double values. • Method Next of class Random generates a random int value. • The values returned by Next are actually pseudorandom numbers—a sequence of values produced by a complex mathematical calculation. • The calculation uses the current time of day to seed the random-number generator. 2009 Pearson Education, Inc. All rights reserved.

7. 9 Case Study: Random-Number Generation (Cont. ) 48 • If you supply the

7. 9 Case Study: Random-Number Generation (Cont. ) 48 • If you supply the Next method with an argument— called the scaling factor—it returns a value from 0 up to, but not including, the argument’s value. • You can also shift the range of numbers produced by adding a shifting value to the number returned by the Next method. • Finally, if you provide Next with two int arguments, it returns a value from the first argument’s value up to, but not including, the second argument’s value. 2009 Pearson Education, Inc. All rights reserved.

Outline 49 Rolling a Six-Sided Die • Figure 7. 7 shows two sample outputs

Outline 49 Rolling a Six-Sided Die • Figure 7. 7 shows two sample outputs of an application that simulates 20 rolls of a six-sided die and displays each roll’s value. Random. Integers. cs (1 of 2 ) Create the Random object random. Numbers to produce random values. Fig. 7. 7 | Shifted and scaled random integers. (Part 1 of 2. ) 2009 Pearson Education, Inc. All rights reserved.

Outline 50 Random. Integers. cs (2 of 2 ) Call Next with two arguments.

Outline 50 Random. Integers. cs (2 of 2 ) Call Next with two arguments. Fig. 7. 7 | Shifted and scaled random integers. (Part 2 of 2. ) 2009 Pearson Education, Inc. All rights reserved.

Outline 51 Rolling a Six-Sided Die 6000 Times • The application in Fig. 7.

Outline 51 Rolling a Six-Sided Die 6000 Times • The application in Fig. 7. 8 simulates 6000 rolls of a die. Each integer from 1 to 6 should appear approximately 1000 times. Roll. Die. cs (1 of 4 ) Fig. 7. 8 | Roll a six-sided die 6000 times. (Part 1 of 4. ) 2009 Pearson Education, Inc. All rights reserved.

Outline 52 Roll. Die. cs (2 of 4 ) Fig. 7. 8 | Roll

Outline 52 Roll. Die. cs (2 of 4 ) Fig. 7. 8 | Roll a six-sided die 6000 times. (Part 2 of 4. ) 2009 Pearson Education, Inc. All rights reserved.

Outline 53 Roll. Die. cs (3 of 4 ) Fig. 7. 8 | Roll

Outline 53 Roll. Die. cs (3 of 4 ) Fig. 7. 8 | Roll a six-sided die 6000 times. (Part 3 of 4. ) 2009 Pearson Education, Inc. All rights reserved.

Outline 54 Roll. Die. cs (4 of 4 ) Fig. 7. 8 | Roll

Outline 54 Roll. Die. cs (4 of 4 ) Fig. 7. 8 | Roll a six-sided die 6000 times. (Part 4 of 4. ) • The values produced by method Next enable the application to realistically simulate rolling a six-sided die. 2009 Pearson Education, Inc. All rights reserved.

7. 9 Case Study: Random-Number Generation (Cont. ) 55 7. 9. 1 Scaling and

7. 9 Case Study: Random-Number Generation (Cont. ) 55 7. 9. 1 Scaling and Shifting Random Numbers • Given two arguments, the next method allows scaling and shifting as follows: number = random. Numbers. Next( shifting. Value, shifting. Value + scaling. Factor ); – shifting. Value specifies the first number in the desired range of consecutive integers. – scaling. Factor specifies how many numbers are in the range. 2009 Pearson Education, Inc. All rights reserved.

7. 9 Case Study: Random-Number Generation (Cont. ) 56 • To choose integers at

7. 9 Case Study: Random-Number Generation (Cont. ) 56 • To choose integers at random from sets of values other than ranges of consecutive integers, it is simpler to use the version of the Next method that takes only one argument: number = shifting. Value + difference. Between. Values * random. Numbers. Next( scaling. Factor ); – shifting. Value specifies the first number in the desired range of values. – difference. Between. Values represents the difference between consecutive numbers in the sequence. – scaling. Factor specifies how many numbers are in the range. 2009 Pearson Education, Inc. All rights reserved.

7. 9 Case Study: Random-Number Generation (Cont. ) 57 7. 9. 2 Random Number

7. 9 Case Study: Random-Number Generation (Cont. ) 57 7. 9. 2 Random Number Repeatability for Testing and Debugging • The calculation that produces the pseudorandom numbers uses the time of day as a seed value to change the sequence’s starting point. • You can pass a seed value to the Random object’s constructor. • Given the same seed value, the Random object will produce the same sequence of random numbers. 2009 Pearson Education, Inc. All rights reserved.

7. 9 Case Study: Random-Number Generation (Cont. ) 58 Error-Prevention Tip 7. 2 While

7. 9 Case Study: Random-Number Generation (Cont. ) 58 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 2009 Pearson Education, Inc. All rights reserved.

7. 10 Case Study: A Game of Chance (Introducing Enumerations) 59 • The rules

7. 10 Case Study: A Game of Chance (Introducing Enumerations) 59 • The rules of the dice game craps are as follows: You roll two dice. Each die has six faces, which contain one, two, three, four, five and six spots, respectively. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the sum is 7 or 11 on the first throw, you win. If the sum is 2, 3 or 12 on the first throw (called “craps”), you lose (i. e. , “the house” wins). If the sum is 4, 5, 6, 8, 9 or 10 on the first throw, that sum becomes your “point. ” To win, you must continue rolling the dice until you “make your point” (i. e. , roll that same point value). You lose by rolling a 7 before making your point. 2009 Pearson Education, Inc. All rights reserved.

Outline • The declaration of class Craps is shown in Fig. 7. 9. 60

Outline • The declaration of class Craps is shown in Fig. 7. 9. 60 Craps. cs (1 of 4 ) A user-defined type called an enumeration declares a set of constants represented by identifiers, and is introduced by the keyword enum and a type name. Fig. 7. 9 | Craps class simulates the dice game craps. (Part 1 of 4. ) 2009 Pearson Education, Inc. All rights reserved.

Outline Sums of the dice that would result in a win or loss on

Outline Sums of the dice that would result in a win or loss on the first roll are declared in an enumeration. 61 Craps. cs (2 of 4 ) Initialization is not strictly necessary because it is assigned a value in every branch of the switch statement. Must be initialized to 0 because it is not assigned a value in every branch of the switch statement. Call method Roll. Dice for the first roll of the game. Fig. 7. 9 | Craps class simulates the dice game craps. (Part 2 of 4. ) 2009 Pearson Education, Inc. All rights reserved.

Outline 62 Craps. cs (3 of 4 ) Call method Roll. Dice for subsequent

Outline 62 Craps. cs (3 of 4 ) Call method Roll. Dice for subsequent rolls. Fig. 7. 9 | Craps class simulates the dice game craps. (Part 3 of 4. ) 2009 Pearson Education, Inc. All rights reserved.

Outline 63 Craps. cs (4 of 4 ) Declare method Roll. Dice to roll

Outline 63 Craps. cs (4 of 4 ) Declare method Roll. Dice to roll the dice and compute and display their sum. Fig. 7. 9 | Craps class simulates the dice game craps. (Part 4 of 4. ) 2009 Pearson Education, Inc. All rights reserved.

7. 10 Case Study: A Game of Chance (Introducing Enumerations) (Cont. ) 64 •

7. 10 Case Study: A Game of Chance (Introducing Enumerations) (Cont. ) 64 • A user-defined type called an enumeration declares a set of constants represented by identifiers, and is introduced by the keyword enum and a type name. • As with a class, braces ({ and }) delimit the body of an enum declaration. Inside the braces is a commaseparated list of enumeration constants. • The enum constant names must be unique, but the value associated with each constant need not be. 2009 Pearson Education, Inc. All rights reserved.

7. 10 Case Study: A Game of Chance (Introducing Enumerations) (Cont. ) 65 Good

7. 10 Case Study: A Game of Chance (Introducing Enumerations) (Cont. ) 65 Good Programming Practice 7. 3 Use only uppercase letters in the names of constants. This makes the con stants stand out in an application and reminds you that enumeration constants are not variables. • Variables of an enumeration type should be assigned only the values declared in the enumeration. 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. 2009 Pearson Education, Inc. All rights reserved.

7. 10 Case Study: A Game of Chance (Introducing Enumerations) (Cont. ) 66 •

7. 10 Case Study: A Game of Chance (Introducing Enumerations) (Cont. ) 66 • When an enum is declared, each constant in the enum declaration is a constant value of type int. • If you do not assign a value to an identifier in the enum declaration, the compiler will do so. – If the first enum constant is unassigned, the compiler gives it the value 0. – If any other enum constant is unassigned, the compiler gives it a value equal to one more than the value of the preceding enum constant. 2009 Pearson Education, Inc. All rights reserved.

7. 10 Case Study: A Game of Chance (Introducing Enumerations) (Cont. ) 67 •

7. 10 Case Study: A Game of Chance (Introducing Enumerations) (Cont. ) 67 • You can declare an enum’s underlying type to be byte, short, ushort, int, uint, long or ulong by writing private enum My. Enum : type. Name { Constant 1, Constant 2, . . . } – type. Name represents one of the integral simple types. • To compare a simple integral type value to the underlying value of an enumeration constant, you must use a cast operator. 2009 Pearson Education, Inc. All rights reserved.

Outline • The Main method is in class Craps. Test (Fig. 7. 10). 68

Outline • The Main method is in class Craps. Test (Fig. 7. 10). 68 Craps. Test. cs (1 of 2 ) Create an object of class Craps. Call the game object’s Play method to start the game. Fig. 7. 10 | Application to test class Craps. (Part 1 of 2. ) 2009 Pearson Education, Inc. All rights reserved.

Outline 69 Craps. Test. cs (2 of 2 ) Fig. 7. 10 | Application

Outline 69 Craps. Test. cs (2 of 2 ) Fig. 7. 10 | Application to test class Craps. (Part 2 of 2. ) 2009 Pearson Education, Inc. All rights reserved.

70 7. 11 Scope of Declarations • The scope of a declaration is the

70 7. 11 Scope of Declarations • The scope of a declaration is the portion of the application that can refer to the declared entity by its unqualified name. • The basic scope rules are as follows: – The scope of a parameter declaration is the body of the method in which the declaration appears. – The scope of a local-variable declaration is from the point at which the declaration appears to the end of the block containing the declaration. – The scope of a non-static method, property or field of a class is the entire body of the class. • If a local variable or parameter in a method has the same name as a field, the field is hidden until the block terminates. 2009 Pearson Education, Inc. All rights reserved.

71 7. 11 Scope of Declarations (Cont. ) Error-Prevention Tip 7. 3 Use different

71 7. 11 Scope of Declarations (Cont. ) 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. 2009 Pearson Education, Inc. All rights reserved.

Outline • Class Scope (Fig. 7. 11) demonstrates scoping issues with fields and local

Outline • Class Scope (Fig. 7. 11) demonstrates scoping issues with fields and local variables. 72 Scope. cs (1 of 3 ) Local variable x hides instance variable x (declared in line 8) in method Begin. Fig. 7. 11 | Scope class demonstrates instance- and local-variable scopes. (Part 1 of 3. ) 2009 Pearson Education, Inc. All rights reserved.

Outline 73 Scope. cs (2 of 3 ) Fig. 7. 11 | Scope class

Outline 73 Scope. cs (2 of 3 ) Fig. 7. 11 | Scope class demonstrates instance- and local-variable scopes. (Part 2 of 3. ) 2009 Pearson Education, Inc. All rights reserved.

Outline 74 Scope. cs (3 of 3 ) Local variable x is declared within

Outline 74 Scope. cs (3 of 3 ) Local variable x is declared within Use. Local. Variable and goes out of scope when the method returns. Because no local variable x is declared in Use. Instance. Variable, instance variable x (line 8) of the class is used. Fig. 7. 11 | Scope class demonstrates instance- and local-variable scopes. (Part 3 of 3. ) 2009 Pearson Education, Inc. All rights reserved.

Outline • A class that tests the Scope class is shown in Fig. 7.

Outline • A class that tests the Scope class is shown in Fig. 7. 12 75 Scope. Test. cs ( 1 of 2 ) Fig. 7. 12 | Application to test class Scope. (Part 1 of 2. ) 2009 Pearson Education, Inc. All rights reserved.

Outline 76 Scope. Test. cs ( 2 of 2 ) Fig. 7. 12 |

Outline 76 Scope. Test. cs ( 2 of 2 ) Fig. 7. 12 | Application to test class Scope. (Part 2 of 2. ) 2009 Pearson Education, Inc. All rights reserved.

77 7. 12 Method Overloading • Methods of the same name can be declared

77 7. 12 Method Overloading • Methods of the same name can be declared in the same class, or overloaded, as long as they have different sets of parameters. • When an overloaded method is called, the C# compiler selects the appropriate method by examining the number, types and order of the arguments in the call. • Method overloading is used to create several methods with the same name that perform the same tasks, but on different types or numbers of arguments. 2009 Pearson Education, Inc. All rights reserved.

Outline 78 Declaring Overloaded Methods Class Method. Overload (Fig. 7. 13) includes two overloaded

Outline 78 Declaring Overloaded Methods Class Method. Overload (Fig. 7. 13) includes two overloaded versions of a method called Square. Method. Overload. Cs ( 1 of 2 ) Overloaded version of the method that operates on an integer. Overloaded version of the method that operates on a double. Fig. 7. 13 | Overloaded method declarations. (Part 1 of 2. ) 2009 Pearson Education, Inc. All rights reserved.

Outline 79 Method. Overload. Cs ( 2 of 2 ) Overloaded version of the

Outline 79 Method. Overload. Cs ( 2 of 2 ) Overloaded version of the method that operates on a double. Fig. 7. 13 | Overloaded method declarations. (Part 2 of 2. ) 2009 Pearson Education, Inc. All rights reserved.

Outline • Class Method. Overload. Test (Fig. 7. 14) tests class Method. Overload. 80

Outline • Class Method. Overload. Test (Fig. 7. 14) tests class Method. Overload. 80 Method. Overload Test. cs Fig. 7. 14 | Application to test class Method. Overload. 2009 Pearson Education, Inc. All rights reserved.

Outline 81 Distinguishing Between Overloaded Methods • The compiler distinguishes overloaded methods by Method.

Outline 81 Distinguishing Between Overloaded Methods • The compiler distinguishes overloaded methods by Method. Overload. cs ( 1 of 3 ) their signature—a combination of the method’s name and the number, types and order of its parameters. Return Types of Overloaded Methods • Method calls cannot be distinguished by return type. • The application in Fig. 7. 15 illustrates the compiler errors generated when two methods have the same signature but different return types. 2009 Pearson Education, Inc. All rights reserved.

Outline 82 Method. Overload. cs ( 2 of 3 ) Fig. 7. 15 |

Outline 82 Method. Overload. cs ( 2 of 3 ) Fig. 7. 15 | Overloaded methods with identical signatures cause compilation errors, even if return types are different. 2009 Pearson Education, Inc. All rights reserved.

Outline • Overloaded methods can have the same or different return types if the

Outline • Overloaded methods can have the same or different return types if the methods have different parameter lists. • Overloaded methods need not have the same number of parameters. 83 Method. Overload. cs ( 3 of 3 ) Common Programming Error 7. 10 Declaring overloaded methods with identical parameter lists is a compilation error regardless of whether the return types are different. 2009 Pearson Education, Inc. All rights reserved.

84 7. 13 Recursion • A recursive method is a method that calls itself.

84 7. 13 Recursion • A recursive method is a method that calls itself. • A recursive method is capable of solving only the base case(s). • Each method call divides the problem into two conceptual pieces: a piece that the method knows how to do and a recursive call, or recursion step that solves a smaller problem. • A sequence of returns ensues until the original method call returns the result to the caller. 2009 Pearson Education, Inc. All rights reserved.

85 7. 13 Recursion (Cont. ) Recursive Factorial Calculations • A recursive declaration of

85 7. 13 Recursion (Cont. ) Recursive Factorial Calculations • A recursive declaration of the factorial method is arrived at by observing the following relationship: • n! = n. (n – 1)! • Figure 7. 16(b) shows the values returned from each recursive call in 5! to its caller until the value is calculated and returned. 2009 Pearson Education, Inc. All rights reserved.

86 7. 13 Recursion (Cont. ) Fig. 7. 16 | Recursive evaluation of 5!.

86 7. 13 Recursion (Cont. ) Fig. 7. 16 | Recursive evaluation of 5!. 2009 Pearson Education, Inc. All rights reserved.

Outline • Figure 7. 17 uses recursion to calculate and display the factorials of

Outline • Figure 7. 17 uses recursion to calculate and display the factorials of the integers from 0 to 10. 87 Factorial. Test. cs ( 1 of 2 ) Fig. 7. 17 | Recursive Factorial method. (Part 1 of 2. ) 2009 Pearson Education, Inc. All rights reserved.

Outline 88 Factorial. Test. cs ( 2 of 2 ) First, test to determine

Outline 88 Factorial. Test. cs ( 2 of 2 ) First, test to determine whether the terminating condition is true. The recursive call solves a slightly simpler problem than the original calculation. Fig. 7. 17 | Recursive Factorial method. (Part 2 of 2. ) 2009 Pearson Education, Inc. All rights reserved.

89 7. 13 Recursion (Cont. ) • First, test to determine whether the terminating

89 7. 13 Recursion (Cont. ) • First, test to determine whether the terminating condition is true. • The recursive call solves a slightly simpler problem than the original calculation. 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. 2009 Pearson Education, Inc. All rights reserved.

90 7. 14 Passing Arguments: Pass-by-Value vs. Pass-by-Reference • Two ways to pass arguments

90 7. 14 Passing Arguments: Pass-by-Value vs. Pass-by-Reference • Two ways to pass arguments to functions in many programming languages are pass by value and pass by reference. • When an argument is passed by value (the default in C#), a copy of its value is made and passed to the called function. • When an argument is passed by reference, the caller gives the method the ability to access and modify the caller’s original variable. 2009 Pearson Education, Inc. All rights reserved.

91 7. 14 Passing Arguments: Pass-by-Value vs. Pass-by-Reference (Cont. ) Performance Tip 7. 1

91 7. 14 Passing Arguments: Pass-by-Value vs. Pass-by-Reference (Cont. ) 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. 2009 Pearson Education, Inc. All rights reserved.

92 7. 14 Passing Arguments: Pass-by-Value vs. Pass-by-Reference (Cont. ) • To pass an

92 7. 14 Passing Arguments: Pass-by-Value vs. Pass-by-Reference (Cont. ) • To pass an object by reference into a method, simply provide as an argument in the method call the variable that refers to the object. • In the method body, the parameter will refer to the original object in memory, so the called method can access the original object directly. • Passing a value-type variable to a method passes a copy of the value. 2009 Pearson Education, Inc. All rights reserved.

93 7. 14 Passing Arguments: Pass-by-Value vs. Pass-by-Reference (Cont. ) • Passing a reference-type

93 7. 14 Passing Arguments: Pass-by-Value vs. Pass-by-Reference (Cont. ) • Passing a reference-type variable passes the method a copy of the actual reference that refers to the object. – The reference itself is passed by value, but the method can still use the reference it receives to modify the original object in memory. • A return statement returns a copy of the value stored in a value-type variable or a copy of the reference stored in a reference-type variable. • In effect, objects are always passed by reference. 2009 Pearson Education, Inc. All rights reserved.

94 7. 14 Passing Arguments: Pass-by-Value vs. Pass-by-Reference (Cont. ) • Applying the ref

94 7. 14 Passing Arguments: Pass-by-Value vs. Pass-by-Reference (Cont. ) • Applying the ref keyword to a parameter declaration allows you to pass a variable to a method by reference • The ref keyword is used for variables that already have been initialized in the calling method. • Preceding a parameter with keyword out creates an output parameter. • This indicates to the compiler that the argument will be passed by reference and that the called method will assign a value to it. • A method can return multiple output parameters. 2009 Pearson Education, Inc. All rights reserved.

Outline • Class Reference. And. Output. Parameters (Fig. 7. 18) contains three methods that

Outline • Class Reference. And. Output. Parameters (Fig. 7. 18) contains three methods that calculate the square of an integer. 95 Reference. And. Outp ut. Parameters. cs ( 1 of 4 ) Fig. 7. 18 | Reference, output and value parameters. (Part 1 of 4. ) 2009 Pearson Education, Inc. All rights reserved.

Outline 96 Reference. And. Outp ut. Parameters. cs ( 2 of 4 ) When

Outline 96 Reference. And. Outp ut. Parameters. cs ( 2 of 4 ) When you pass a variable to a method with a reference parameter, you must precede the argument with the same keyword (ref or out) that was used to declare the reference parameter. Fig. 7. 18 | Reference, output and value parameters. (Part 2 of 4. ) 2009 Pearson Education, Inc. All rights reserved.

Outline 97 Reference. And. Outp ut. Parameters. cs ( 3 of 4 ) Modify

Outline 97 Reference. And. Outp ut. Parameters. cs ( 3 of 4 ) Modify caller’s x. Fig. 7. 18 | Reference, output and value parameters. (Part 3 of 4. ) 2009 Pearson Education, Inc. All rights reserved.

Outline 98 Reference. And. Outp ut. Parameters. cs ( 4 of 4 ) Assign

Outline 98 Reference. And. Outp ut. Parameters. cs ( 4 of 4 ) Assign a value to caller’s uninitialized x. Doesn’t modify any caller variable. Fig. 7. 18 | Reference, output and value parameters. (Part 4 of 4. ) 2009 Pearson Education, Inc. All rights reserved.

99 7. 14 Passing Arguments: Pass-by-Value vs. Pass-by-Reference (Cont. ) • When you pass

99 7. 14 Passing Arguments: Pass-by-Value vs. Pass-by-Reference (Cont. ) • When you pass a variable to a method with a reference parameter, you must precede the argument with the same keyword (ref or out) that was used to declare the reference parameter. 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. 2009 Pearson Education, Inc. All rights reserved.

100 7. 14 Passing Arguments: Pass-by-Value vs. Pass-by-Reference (Cont. ) Software Engineering Observation 7.

100 7. 14 Passing Arguments: Pass-by-Value vs. Pass-by-Reference (Cont. ) 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. 2009 Pearson Education, Inc. All rights reserved.

Outline 101 • Class Reference. And. Output. Parameters. Test tests the Reference. And. Output.

Outline 101 • Class Reference. And. Output. Parameters. Test tests the Reference. And. Output. Parameters class. Reference. And Output. Paramters Test. cs Fig. 7. 19 | Application to test class Reference. And. Output. Parameters. 2009 Pearson Education, Inc. All rights reserved.

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System 102 • An operation is a service that objects of a class provide to clients of the class. • The verbs and verb phrases in Fig. 7. 20 help us determine the operations of our classes. Fig. 7. 20| Verbs and verb phrases for each class in the ATM system. (Part 1 of 2. ) 2009 Pearson Education, Inc. All rights reserved.

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System (Cont. ) 103 Fig. 7. 20| Verbs and verb phrases for each class in the ATM system. (Part 2 of 2. ) 2009 Pearson Education, Inc. All rights reserved.

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System (Cont. ) 104 Modeling Operations • We place operations in the third compartment of the three transaction classes in the updated class diagram of Fig. 7. 21. 2009 Pearson Education, Inc. All rights reserved.

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System (Cont. ) 105 Fig. 7. 21 | Classes in the ATM system with attributes and operations. 2009 Pearson Education, Inc. All rights reserved.

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System (Cont. ) 106 • UML operations are implemented as methods in C#. • The UML represents operations by listing the operation name, followed by a comma-separated list of parameters in parentheses, a colon and the return type. • Each parameter in the comma-separated parameter list consists of a parameter name, followed by a colon and the parameter type: 2009 Pearson Education, Inc. All rights reserved.

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System (Cont. ) Operations of Class Bank. Database and Class 107 Account • Class Bank. Database needs an operation that provides an authentication service to the ATM. • We place the operation Authenticate. User in the third compartment of class Bank. Database. • We add a Validate. PIN operation to class Account. • Note that we specify a return type of bool for the Authenticate Userand Validate. PIN operations. • The database acts as an intermediary between the ATM and the account data, preventing unauthorized access. 2009 Pearson Education, Inc. All rights reserved.

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System (Cont. ) 108 • Class Properties are placed in the second compartment of a class diagram. • To indicate a read-only property in the UML, we follow the property’s type with “{read. Only}. ” • We modeled Get. Available. Balance and Get. Total. Balance as operations in class Bank. Database so that we could specify parameters to which the ATM can pass arguments. • We assign Credit and Debit operations to classes Bank. Database and Account. – Crediting an account adds an amount only to the Account’s total balance. – Debiting an account, subtracts the amount from both the total and available balances. 2009 Pearson Education, Inc. All rights reserved.

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System (Cont. ) 109 Operations of Class Screen • All visual output occurs through the screen of the ATM. • We simply create one operation that can display any message specified by a parameter. Operations of Class Keypad • Class Keypad should perform a Get. Input operation. Operations of Class Cash. Dispenser and Class Deposit. Slot • We create operations Dispense. Cash and Is. Sufficient. Cash. Available for class Cash. Dispenser. 2009 Pearson Education, Inc. All rights reserved.

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System (Cont. ) 110 Identifying and Modeling Operation Parameters • We identify an operation’s parameters by examining what data the operation requires to perform its assigned task. • The class diagram in Fig. 7. 22 models class Bank. Database. Fig. 7. 22 | Class Bank. Database with operation parameters. 2009 Pearson Education, Inc. All rights reserved.

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System (Cont. ) 111 • The class diagram in Fig. 7. 23 models the parameters of class Account’s operations. Fig. 7. 23 | Class Account with operation parameters. 2009 Pearson Education, Inc. All rights reserved.

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System (Cont. ) 112 • Figure 7. 24 models class Screen with a parameter for operation Display Message. Fig. 7. 24 | Class Screen with an operation parameter. 2009 Pearson Education, Inc. All rights reserved.

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System

7. 15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System (Cont. ) 113 • The class diagram in Fig. 7. 25 specifies that operation Dispense. Cash of class Cash. Dispenser takes decimal parameter amount to indicate the amount of cash (in dollars) to be dispensed. Fig. 7. 25 | Class Cash. Dispenser with operation parameters. • Operation Is. Sufficient. Cash. Available also takes decimal parameter amount to indicate the amount of cash in question. 2009 Pearson Education, Inc. All rights reserved.