Top Banner
2009 Pearson Education, Inc. All rights rese 1 3 Introduction to C# Applications
95

2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

Dec 30, 2015

Download

Documents

Olivia White
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

1

33

Introduction toC# Applications

Page 2: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

2

What’s in a name? That which we call a roseby any other name would smell as sweet.

– William Shakespeare

When faced with a decision, I always ask,“What would be the most fun?”

– Peggy Walker

Page 3: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

3

“Take some more tea,” the March Hare said to Alice, very earnestly. “I’ve had nothing yet,” Alice replied in an offended tone, “so I can’t take more.” “You mean you can’t take less,” said the Hatter: “it’s very easy to take more than nothing.”

– Lewis Carroll

Page 4: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

4

OBJECTIVES

In this chapter you will learn: To write simple C# applications using code rather

than visual programming. To write statements that input and output data to the

screen. To declare and use data of various types. To store and retrieve data from memory. To use arithmetic operators. To determine the order in which operators are applied. To write decision-making statements. To use relational and equality operators. To use message dialogs to display messages.

Page 5: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

5

3.1   Introduction

3.2   A Simple C# Application: Displaying a Line of Text

3.3   Creating a Simple Application in Visual C# Express

3.4   Modifying Your Simple C# Application

3.5   Formatting Text with Console.Write and Console.WriteLine

3.6   Another C# Application: Adding Integers

3.7   Memory Concepts

3.8   Arithmetic

3.9   Decision Making: Equality and Relational Operators

3.10   (Optional) Software Engineering Case Study: Examining the ATM Requirements Document

Page 6: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

6

3.1  Introduction

• Console applications input and output text in a console window, which in Windows XP and Windows Vista is known as the Command Prompt.

Page 7: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

7

1 // Fig. 3.1: Welcome1.cs

2 // Text-displaying application.

3 using System;

4

5 public class Welcome1

6 {

7 // Main method begins execution of C# application

8 public static void Main( string[] args )

9 {

10 Console.WriteLine( "Welcome to C# Programming!" );

11 } // end Main

12 } // end class Welcome1 Welcome to C# Programming!

• Figure 3.1 shows a simple application that displaysa line of text.

Outline

Welcome1.cs

Fig. 3.1 | Text-displaying application.

Class declaration for class Welcome1.

Comments improve code readability.

Page 8: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

8

3.2  A Simple C# Application: Displaying a Line of Text (Cont.)

• Programmers insert comments to document applications.

• Comments improve code readability.

• The C# compiler ignores comments, so they do not cause the computer to perform any action when the application is run.

• A comment that begins with // is called a single-line comment, because it terminates at the end of the line on which it appears.

• A // comment also can begin in the middle of a line and continue until the end of that line.

• Delimited comments begin with the delimiter /* and end with the delimiter */. All text between the delimiters is ignored by the compiler.

Page 9: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

9

3.2  A Simple C# Application: Displaying a Line of Text (Cont.)

Common Programming Error 3.1

Forgetting one of the delimiters of a delimited comment is a syntax error. The syntax of a programming language specifies the rules for creating a proper application in that language. A syntax error occurs when the compiler encounters code that violates C#’s language rules.

Page 10: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

10

3.2  A Simple C# Application: Displaying a Line of Text (Cont.)

• A using directive tells the compiler where to look for a predefined class that is used in an application.

• Predefined classes are organized under namespaces—named collections of related classes. Collectively, .NET’s namespaces are referred to as the .NET Framework Class Library.

• The System namespace contains the predefined Console class and many other useful classes.

Page 11: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

11

3.2  A Simple C# Application: Displaying a Line of Text (Cont.)

Error-Prevention Tip 3.1Forgetting to include a using directive for a namespace that contains a class used in your application typically results in a compilation error, containing a message such as “The name 'Console' does not exist in the current context.” When this occurs, check that you provided the proper using directives and that the names in the using directives are spelled correctly, including proper use of Uppercase and lowercase letters.

Page 12: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

12

3.2  A Simple C# Application: Displaying a Line of Text (Cont.)

• Programmers use blank lines and space characters to make applications easier to read.

• Together, blank lines, space characters and tab characters are known as whitespace. Whitespace is ignored by the compiler.

Page 13: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

13

3.2  A Simple C# Application: Displaying a Line of Text (Cont.)

• Keywords (sometimes called reserved words) are reserved for use by C# and are always spelled with all lowercase letters.

• Every application consists of at least one class declaration that is defined by the programmer. These are known as user-defined classes.

• The class keyword introduces a class declaration and is immediately followed by the class name.

Page 14: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

14

3.2  A Simple C# Application: Displaying a Line of Text (Cont.)

Good Programming Practice 3.1By convention, always begin a class name’s identifier with a capital letter and start each subsequent word in the identifier with a capital letter.

Page 15: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

15

C# Keywords and contextual keywords

abstract as base bool break

byte case catch char checked

class const continue decimal default

delegate do double else enum

event explicit extern false finally

fixed float for foreach goto

if implicit in int interface

internal is lock long namespace

new null object operator out

Fig. 3.2 | C# keywords and contextual keywords. (Part 1 of 2.)

• A class name is an identifier:– Series of characters consisting of letters, digits and underscores ( _ ).

– Cannot begin with a digit and does not contain spaces.

• The complete list of C# keywords is shown in Fig. 3.2.

3.2  A Simple C# Application: Displaying a Line of Text (Cont.)

Page 16: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

16

C# Keywords and contextual keywords

override params private protected public

readonly ref return sbyte sealed

short sizeof stackalloc static string

struct switch this throw true

try typeof uint ulong unchecked

unsafe ushort using virtual void

volatile while

Contextual Keywords

add alias ascending by descending

equals from get global group

into join let on orderby

partial remove select set value

var where yield

Fig. 3.2 | C# keywords and contextual keywords. (Part 2 of 2.)

• The contextual keywords in Fig. 3.2 can be used as identifiersoutside the contexts in which they are keywords, but for claritythis is not recommended.

3.2  A Simple C# Application: Displaying a Line of Text (Cont.)

Page 17: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

17

3.2  A Simple C# Application: Displaying a Line of Text (Cont.)

• C# is case sensitive—that is, uppercase and lowercase letters are distinct, so a1 and A1 are different (but both valid) identifiers.

Common Programming Error 3.2C# is case sensitive. Not using the proper uppercase and lowercase letters for an identifier normally causes a compilation error.

• Identifiers may also be preceded by the @ character. This indicates that a word should be interpreted as an identifier, even if it is a keyword (e.g. @int).

Page 18: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

18

3.2  A Simple C# Application: Displaying a Line of Text (Cont.)

Good Programming Practice 3.2By convention, a file that contains a single public class should have a name that is identical to the class name (plus the .cs extension) in both spelling and capitalization. Naming your files in this way makes it easier for other programmers (and you) to determine where the classes of an application are located.

Page 19: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

19

3.2  A Simple C# Application: Displaying a Line of Text (Cont.)

• A left brace, {, begins the body of every class declaration. A corresponding right brace, }, must end each class declaration.

Error-Prevention Tip 3.2Whenever you type an opening left brace, {, in your application, imeediately type the closing right brace, }, then reposition the cursor between the braces and indent to begin typing the body. This practice helps prevent errors due to missing braces.

Good Programming Practice 3.3Indent the entire body of each class declaration one “level” of indentation between the left and right braces that delimit the body of the class. This format emphasizes the class declaration’s structure and makes it easier to read. You can let the IDE format your code by selecting Edit > Advanced > Format Document.

Page 20: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

20

3.2  A Simple C# Application: Displaying a Line of Text (Cont.)

Good Programming Practice 3.4Set a convention for the indent size you prefer, then uniformly apply that convention. The Tab key may be used to create indents, but tab stops vary among text editors. We recommend using three spaces to form each level of indentation. We show how to do this in Section 3.3.

Common Programming Error 3.3It is a syntax error if braces do not occur in matching pairs.

Page 21: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

21

3.2  A Simple C# Application: Displaying a Line of Text (Cont.)

• Parentheses after an identifier indicate that it is an application building block called a method. Class declarations normally contain one or more methods.

• Method names usually follow the same casing capitalization conventions used for class names.

• For each application, one of the methods in a class must be called Main; otherwise, the application will not execute.

• Methods are able to perform tasks and return information when they complete their tasks. Keyword void indicates that this method will not return any information after it completes its task.

Page 22: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

22

3.2  A Simple C# Application: Displaying a Line of Text (Cont.)

• The body of a method declaration begins with a left brace and ends with a corresponding right brace.

Good Programming Practice 3.5As with class declarations, indent the entire body of each method declaration one “level” of indentation between the left and right Braces that define the method body. This format makes the structure of the method stand out and makes the method declaration easier to read.

Page 23: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

23

3.2  A Simple C# Application: Displaying a Line of Text (Cont.)

• Characters between double quotation marks are strings.

• Whitespace characters in strings are not ignored by the compiler.

• The Console.WriteLine method displays a line of text in the console window.

• The string in parentheses is the argument to the Console.WriteLine method.

• Method Console.WriteLine performs its task by displaying (also called outputting) its argument in the console window.

Page 24: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

24

3.2  A Simple C# Application: Displaying a Line of Text (Cont.)

• A method is typically composed of one or more statements that perform the method’s task.

• Most statements end with a semicolon.

Common Programming Error 3.4Omitting the semicolon at the end of a statement is a syntax error.

Page 25: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

25

3.2  A Simple C# Application: Displaying a Line of Text (Cont.)

Error-Prevention Tip 3.3When the compiler reports a syntax error, the error may not be in the line indicated by the error message. First, check the line for which the error was reported. If that line does not contain syntax errors, check several preceding lines.

Good Programming Practice 3.6Following the closing right brace of a method body or class declaration with a comment indicating the method or class declaration to which the brace belongs improves application readability.

Page 26: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

26

3.3  Creating a Simple Application in Visual C# Express

Creating the Console Application• Select File > New Project… to display the New

Project dialog (Fig. 3.3).

• Select the Console Application template.

• In the dialog’s Name field, type Welcome1, and click OK to create the project.

Page 27: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

27

3.3  Creating a Simple Application in Visual C# Express (Cont.)

Project name

Fig. 3.3 | Creating a Console Application with the New Project dialog.

Page 28: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

28

3.3  Creating a Simple Application in Visual C# Express (Cont.)

• The IDE now contains the open console application.

• The code coloring scheme used by the IDE is called syntax-color shading and helps you visually differentiate application elements.

Page 29: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

29

3.3  Creating a Simple Application in Visual C# Express (Cont.)

Fig. 3.4 | IDE with an open console application.

Editor window

Page 30: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

30

3.3  Creating a Simple Application in Visual C# Express (Cont.)

• To have the IDE display line numbers, select Tools > Options….

– In the dialog that appears (Fig. 3.5), click the Show all settings checkbox on the lower left of the dialog.

– Expand the Text Editor node in the left pane and select All Languages. On the right, check the Line numbers checkbox.

Page 31: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

31

3.3  Creating a Simple Application in Visual C# Express (Cont.)

Fig. 3.5 | Modifying the IDE settings.

Page 32: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

32

3.3  Creating a Simple Application in Visual C# Express (Cont.)

• To set code indentation to three spaces per indent:– In the Options dialog that you opened in the previous step,

expand the C# node in the left pane and select Tabs.

– Make sure that the option Insert spaces is selected. Enter 3 for both the Tab size and Indent size fields.

– Click OK to save your settings, close the dialog and return to the editor window.

Page 33: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

33

3.3  Creating a Simple Application in Visual C# Express (Cont.)

• To rename the application file, click Program.cs in the Solution Explorer window to display its properties in the Properties window (Fig. 3.6).

• Change the File Name property to Welcome1.cs.

Page 34: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

34

3.3  Creating a Simple Application in Visual C# Express (Cont.)

Fig. 3.6 | Renaming the program file in the Properties window.

Click Program.cs todisplay its properties

Type Welcome1.cs hereto rename the fileFile Name property

Properties window

Solution Explorer

Page 35: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

35

3.3  Creating a Simple Application in Visual C# Express (Cont.)

• IntelliSense lists a class’s members, which include method names.

• As you type characters, Visual C# Express highlights the first member that matches all the characters typed, then displays a tool tip containing a description of that member.

• You can either type the complete member name, double click the member name in the member list or press the Tab key to complete the name.

• While the IntelliSense window is displayed pressing the Ctrl key makes the window transparent so you can see the code behind the window.

Page 36: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

36

3.3  Creating a Simple Application in Visual C# Express (Cont.)

Fig. 3.7 | IntelliSense feature of Visual C# Express.

Highlighted member Tool tip describeshighlighted member

Partially typed member

Member list

Page 37: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

37

3.3  Creating a Simple Application in Visual C# Express (Cont.)

• When you type the open parenthesis character, (, after a method name, the Parameter Info window is displayed (Fig. 3.8).

• This window contains information about the method’s parameters.

Down arrow Parameter Info window

Up arrow

Fig. 3.8 | Parameter Info window.

• Up and down arrows allow you to scroll through overloaded versions of the method.

Page 38: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

38

3.3  Creating a Simple Application in Visual C# Express (Cont.)

• To save an application, select File > Save All to display the Save Project dialog (Fig. 3.9).

• In the Location textbox, specify the directory where you want to save this project.

• Select the Create directory for solution checkbox and click Save.

Fig. 3.9 | Save Project dialog.

Page 39: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

39

3.3  Creating a Simple Application in Visual C# Express (Cont.)

• To compile an application, select Build > Build Solution.

• To execute it, select Debug > Start Without Debugging (or type Ctrl + F5).

– This invokes the Main method.

• Figure 3.10 shows the results of executing this application, displayed in a console (Command Prompt) window.

Page 40: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

40

3.3  Creating a Simple Application in Visual C# Express (Cont.)

Fig. 3.10 | Executing the application shown in Fig. 3.1.

Console window

Page 41: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

41

3.3  Creating a Simple Application in Visual C# Express (Cont.)

Error-Prevention Tip 3.4When learning how to program, sometimes it is helpful to “break” a working application so you can familiarize yourself with the compiler’s syntax-error messages. Try removing a semicolon or brace from the code of Fig. 3.1, then recompiling the application to see the error messages generated by the omission.

Page 42: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

42

3.3  Creating a Simple Application in Visual C# Express (Cont.)

Running the Application from the Command

Prompt• To open the Command Prompt, select Start >

All Programs > Accessories > CommandPrompt.

Fig. 3.11 | Command Prompt window when it is initially opened.

Default prompt displays whenCommand Prompt is opened

User enters the next command here

Page 43: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

43

3.3  Creating a Simple Application in Visual C# Express (Cont.)

• To Enter the command cd (which stands for “change directory”), followed by the directory where the application’s .exe file is located

• Run the compiled application by entering the name of the .exe file.

Fig. 3.12 | Executing the application shown in Fig. 3.1 from aCommand Prompt window.

Updated prompt showingthe new current directory

Type this to change to theapplication’s directory

Application’s output Closes the Command Prompt Type this to run the Welcome1.exe application

Page 44: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

44

3.3  Creating a Simple Application in Visual C# Express (Cont.)

• As you type code, the IDE responds either by applying syntax-color highlighting or by generating a syntax error.

• A syntax error indicates a violation of Visual C#’s rules for creating correct applications.

• When a syntax error occurs, the IDE underlines the error in red and provides a description of it in the Error List window (Fig. 3.13).

Page 45: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

45

3.3  Creating a Simple Application in Visual C# Express (Cont.)

Fig. 3.13 | Syntax errors indicated by the IDE.

Intentionally omitted parenthesis character (syntax error)

Error description(s) Error List window Red underline indicates a syntax error

Page 46: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

46

3.3  Creating a Simple Application in Visual C# Express (Cont.)

Error-Prevention Tip 3.5One syntax error can lead to multiple entries in the Error List window. Each error that you address could eliminate several subsequent error messages when you recompile your application. So when you see an error you know how to fix, correct it and recompile—this may make several other errors disappear.

Page 47: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

47

• Class Welcome2, shown in Fig. 3.14, uses two statements to produce the same output as that shown in the previous example.

• Unlike WriteLine, the Console class’s Write method does not position the screen cursor at the beginning of the next line in the console window.

3.4  Modifying Your Simple C# Application

Page 48: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

48

1 // Fig. 3.14: Welcome2.cs

2 // Displaying one line of text with multiple statements.

3 using System;

4

5 public class Welcome2

6 {

7 // Main method begins execution of C# application

8 public static void Main( string[] args )

9 {

10 Console.Write( "Welcome to " );

11 Console.WriteLine( "C# Programming!" );

12 } // end Main

13 } // end class Welcome2 Welcome to C# Programming!

Fig. 3.14 | Displaying one line of text with multiple statements.

The Write method does not move the cursor to a new line after displaying its argument.

3.4  Modifying Your Simple C# Application (Cont.)

Outline

Welcome2.cs

Page 49: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

49

1 // Fig. 3.15: Welcome3.cs

2 // Displaying multiple lines with a single statement.

3 using System;

4

5 public class Welcome3

6 {

7 // Main method begins execution of C# application

8 public static void Main( string[] args )

9 {

10 Console.WriteLine( "Welcome\nto\nC#\nProgramming!" );

11 } // end Main

12 } // end class Welcome3 Welcome to C# Programming!

• A single statement can display multiple lines by using newline characters.

• Like space characters and tab characters, newline characters are whitespace characters.

• The application of Fig. 3.15 outputs four lines of text, using newline characters to indicate when to begin each new line.

Outline

Welcome3.cs

Fig. 3.15 | Displaying multiple lines with a single statement.

Page 50: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

50

3.4  Modifying Your Simple C# Application (Cont.)

• The backslash (\) is called an escape character, and is used as the first character in an escape sequence.

• The escape sequence \n represents the newline character.

• Figure 3.16 lists several common escape sequences and describes how they affect the display of characters in the console window.

Page 51: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

51

3.4  Modifying Your Simple C# Application (Cont.)

Fig. 3.16 | Some common escape sequences.

Escape sequence

Description

\n Newline. Positions the screen cursor at the beginning of the next line.

\t Horizontal tab. Moves the screen cursor to the next tab stop.

\r Carriage return. Positions the screen cursor at the beginning of the current line—does not advance the cursor to the next line. Any characters output after the carriage return overwrite the characters previously output on that line.

\\ Backslash. Used to place a backslash character in a string.

\" Double quote. Used to place a double-quote character (") in a string—e,g.,

Console.Write( "\"in quotes\"" );

displays

"in quotes"

Page 52: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

52

1 // Fig. 3.17: Welcome4.cs

2 // Displaying multiple lines of text with string formatting.

3 using System;

4

5 public class Welcome4

6 {

7 // Main method begins execution of C# application

8 public static void Main( string[] args )

9 {

10 Console.WriteLine( "{0}\n{1}", "Welcome to", "C# Programming!" );

11 } // end Main

12 } // end class Welcome4

Welcome to C# Programming!

• 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

Welcome4.cs

Fig. 3.17 | Displaying multiple lines of text with string formatting.

Method WriteLine’s first argument is a format string that may consist of fixed text and format items.

3.5  Formatting Text with Console.Write and Console.WriteLine

Page 53: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

53

3.5  Formatting Text with Console.Write and Console.WriteLine (Cont.)

• When a method requires multiple arguments, the arguments are separated with commas.

Good Programming Practice 3.7Place a space after each comma (,) in an argumentlist to make applications more readable.

• Large statements can be split over many lines,but there are some restrictions.

Common Programming Error 3.5Splitting a statement in the middle of an identifieror a string is a syntax error.

Page 54: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

54

3.5  Formatting Text with Console.Write and Console.WriteLine (Cont.)

• Method WriteLine’s first argument is a format string that may consist of fixed text and format items.

• Each format item is a placeholder for a value, corresponding to an additional argument to WriteLine.

– {0} is a placeholder for the first additional argument.

– {1} is a placeholder for the second, and so on.

• Format items also may include optional formatting information.

Page 55: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

55

3.6  Another C# Application: Adding Integers

• Applications remember numbers and other data in the computer’s memory and access that data through application elements called variables.

• A variable is a location in the computer’s memory where a value can be stored for use later in an application.

• A variable declaration statement (also called a declaration) specifies the name and type of a variable.

– A variable’s name enables the application to access the value of the variable in memory—the name can be any valid identifier.

– A variable’s type specifies what kind of information is stored at that location in memory.

Page 56: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

56

1 // Fig. 3.18: Addition.cs

2 // Displaying the sum of two numbers input from the keyboard.

3 using System;

4

5 public class Addition

6 {

7 // Main method begins execution of C# application

8 public static void Main( string[] args )

9 {

10 int number1; // declare first number to add

11 int number2; // declare second number to add

12 int sum; // declare sum of number1 and number2

13

14 Console.Write( "Enter first integer: " ); // prompt user

15 // read first number from user

16 number1 = Convert.ToInt32( Console.ReadLine() );

17

• Three variables declared as type int.Outline

Addition.cs

(1 of 2 )

Fig. 3.18 | Displaying the sum of two numbers input fromthe keyboard. (Part 1 of 2).

Console.ReadLine() reads the data entered by the user, and Convert.ToInt32 converts the value into an integer.

Three variables declared as type int.

The user is prompted for information.

Page 57: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

57

18 Console.Write( "Enter second integer: " ); // prompt user

19 // read second number from user

20 number2 = Convert.ToInt32( Console.ReadLine() );

21

22 sum = number1 + number2; // add numbers

23

24 Console.WriteLine( "Sum is {0}", sum ); // display sum

25 } // end Main

26 } // end class Addition

Enter first integer: 45 Enter second integer: 72 Sum is 117

Outline

Addition.cs

(2 of 2 )

Fig. 3.18 | Displaying the sum of two numbers input fromthe keyboard. (Part 2 of 2).

Page 58: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

58

3.6  Another C# Application: Adding Integers (Cont.)

• Variables of type int store integer values (whole numbers such as 7, –11, 0 and 31914).

• Types float, double and decimal specify real numbers (numbers with decimal points).

• Type char represents a single character.

• These types are called simple types. Simple-type names are keywords and must appear in all lowercase letters.

Page 59: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

59

3.6  Another C# Application: Adding Integers (Cont.)

• Variable declaration statements can be split over several lines, with the variable names separated by commas (i.e., a comma-separated list of variable names).

• Several variables of the same type may be declared in one declaration or in multiple declarations.

Good Programming Practice 3.8Declare each variable on a separate line. This format allows a comment to be easily inserted next to each declaration.

Page 60: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

60

3.6  Another C# Application: Adding Integers (Cont.)

Good Programming Practice 3.9Declare each variable on a separate line. This format allows a comment to be easily inserted next to each declaration.

Good Programming Practice 3.10By convention, variable-name identifiers begin with a lowercase letter, and every word in the name after the first word begins with a capital letter. This naming convention is known as camel casing.

Page 61: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

61

3.6  Another C# Application: Adding Integers (Cont.)

• The Console’s ReadLine method waits for the user to type a string of characters at the keyboardand press the Enter key.

• ReadLine returns the text the user entered.

• The Convert class’s ToInt32 method converts this sequence of characters into data of type int.

• ToInt32 returns the int representation of the user’s input.

Page 62: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

62

3.6  Another C# Application: Adding Integers (Cont.)

• A value can be stored in a variable using the assignment operator, =.

• Operator = is called a binary operator, because it works on two pieces of information, or operands.

• An assignment statement assigns a value to a variable.

• Everything to the right of the assignment operator, =, is always evaluated before the assignment is performed.

Good Programming Practice 3.11Place spaces on either side of a binary operator to make it stand out and make the code more readable.

Page 63: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

63

3.6  Another C# Application: Adding Integers (Cont.)

• An expression is any portion of a statement that has a value associated with it.

– The value of the expression number1 + number2 is the sum of the numbers.

– The value of the expression Console.ReadLine() is the string of characters typed by the user.

• Calculations can also be performed inside output statements.

Page 64: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

64

3.7  Memory Concepts

• Variable names actually correspond to locations in the computer’s memory.

• Every variable has a name, a type, a size and a value.

• In Fig. 3.19, the computer has placed the value 45 in the memory location corresponding to number1.

Fig. 3.19 | Memory location showing the name and value of variable number1.

Page 65: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

65

3.7  Memory Concepts (Cont.)

• In Fig. 3.20, 72 has been placed in location number2.

Fig. 3.20 | Memory locations after storing values for number1 and number2.

Page 66: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

66

3.7  Memory Concepts (Cont.)

• After sum has been calculated, memory appears as shown in Fig. 3.21.

Fig. 3.21 | Memory locations after calculating and storing the sum of number1 and number2.

Page 67: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

67

3.7  Memory Concepts (Cont.)

• Whenever a value is placed in a memory location, the value replaces the previous value in that location, and the previous value is lost.

• When a value is read from a memory location, the process is nondestructive.

Page 68: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

68

Fig. 3.22 | Arithmetic operators.

• The arithmetic operators are summarized in Fig. 3.22.

C# operation Arithmetic operator

Algebraic expression

C# expression

Addition + f + 7 f + 7

Subtraction – p – c p - c

Multiplication * b . m b * m

Division

/ or or

xx y x ÷ y

y

x / y

Remainder % r mod s v % u

• The arithmetic operators in Fig. 3.22 are binary operators.

3.8  Arithmetic

Page 69: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

69

3.8  Arithmetic (Cont.)

• Integer division yields an integer quotient—any fractional part in integer division is simply discarded without rounding.

• C# provides the remainder operator, %, which yields the remainder after division.

• The remainder operator is most commonly used with integer operands but can also be used with floats, doubles, and decimals.

• Parentheses are used to group terms in C# expressions in the same manner as in algebraic expressions.

• If an expression contains nested parentheses, the expression in the innermost set of parentheses is evaluated first.

Page 70: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

70

Fig. 3.23 | Precedence of arithmetic operators.

• Arithmetic operators are evaluated according to the rulesof operator precedence, which are generally the same as those followed in algebra (Fig. 3.23).

Operators Operations Order of evaluation (associativity)

Evaluated first

* / %

Multiplication

Division

Remainder

If there are several operators of this type, they are evaluated from left to right.

Evaluated next

+ -

Addition

Subtraction

If there are several operators of this type, they are evaluated from left to right.

3.8  Arithmetic (Cont.)

Page 71: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

71

3.8  Arithmetic (Cont.)

• The circled numbers under the statement indicate the order in which C# applies the operators.

The multiplication, remainder and division operations areevaluated first in left-to-right order (i.e., they associate fromleft to right), because they have higher precedence thanaddition and subtraction. The addition and subtractionoperations are evaluated next. These operations are alsoapplied from left to right.

Page 72: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

72

3.8  Arithmetic (Cont.)

• To develop a better understanding of the rules of operator precedence, consider the evaluation of a second-degree polynomial (y = ax2 + bx + c):

Page 73: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

73

3.8  Arithmetic (Cont.)

• As in algebra, it is acceptable to place unnecessary (redundant) parentheses in an ex pression to make the expression clearer.

• The preceding assignment statement might be paren thesized to highlight its terms as follows:

y = ( a * x * x ) + ( b * x ) + c;

Page 74: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

74

3.8  Arithmetic (Cont.)

Fig. 3.24 | Order in which a second-degree polynomial is evaluated.

Page 75: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

75

3.9  Decision Making: Equality and Relational Operators

Fig. 3.25 | Equality and relational operators. (Part 1 of 2.)

• A condition is an expression that can be either true or false.

• Conditions can be formed using the equality operators (== and !=) and relational operators (>, <, >= and <=) summarized in Fig. 3.25.

Standard algebraic equality and relational operators

C# equality or relational operator

Sample C# condition

Meaning of C# condition

Equality operators

== x == y x is equal to y

!= x != y x is not equal to y

Page 76: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

76

3.9  Decision Making: Equality and Relational Operators (Cont.)

Standard algebraic equality and relational operators

C# equality or relational operator

Sample C# condition

Meaning of C# condition

Relational operators

> x > y x is greater than y

< x < y x is less than y

>= x >= y x is greater than or equal to y

<= x <= y x is less than or equal to y

Fig. 3.25 | Equality and relational operators. (Part 2 of 2.)

Page 77: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

77

3.9  Decision Making: Equality and Relational Operators (Cont.)

Common Programming Error 3.6Confusing the equality operator, ==, with the assignment operator, =, can cause a logic error or a syntax error. The equality operator should be read as “is equal to,” and the assignment operator should be read as “gets” or “gets the value of.” To avoid confusion, some people read the equality operator as “double equals” or “equals equals.”

Page 78: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

78

1 // Fig. 3.26: Comparison.cs

2 // Comparing integers using if statements, equality operators,

3 // and relational operators.

4 using System;

5

6 public class Comparison

7 {

8 // Main method begins execution of C# application

9 public static void Main( string[] args )

10 {

11 int number1; // declare first number to compare

12 int number2; // declare second number to compare

13

14 // prompt user and read first number

15 Console.Write( "Enter first integer: " );

16 number1 = Convert.ToInt32( Console.ReadLine() );

17

• Figure 3.26 uses six if statements to compare two integers entered by the user.

Outline

Comparison.cs

(1 of 3 )

Fig. 3.26 | Comparing integers using if statements, equality operatorsand relational operators. (Part 1 of 3).

Page 79: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

79

18 // prompt user and read second number

19 Console.Write( "Enter second integer: " );

20 number2 = Convert.ToInt32( Console.ReadLine() );

21

22 if ( number1 == number2 )

23 Console.WriteLine( "{0} == {1}", number1, number2 );

24

25 if ( number1 != number2 )

26 Console.WriteLine( "{0} != {1}", number1, number2 );

27

28 if ( number1 < number2 )

29 Console.WriteLine( "{0} < {1}", number1, number2 );

30

31 if ( number1 > number2 )

32 Console.WriteLine( "{0} > {1}", number1, number2 );

33

34 if ( number1 <= number2 )

35 Console.WriteLine( "{0} <= {1}", number1, number2 );

Compare number1 and number2 for equality.

Outline

Comparison.cs

(2 of 3 )

Fig. 3.26 | Comparing integers using if statements, equality operatorsand relational operators. (Part 2 of 3).

Page 80: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

80

36

37 if ( number1 >= number2 )

38 Console.WriteLine( "{0} >= {1}", number1, number2 );

39 } // end Main

40 } // end class Comparison Enter first integer: 42 Enter second integer: 42 42 == 42 42 <= 42 42 >= 42 Enter first integer: 1000 Enter second integer: 2000 1000 != 2000 1000 < 2000 1000 <= 2000 Enter first integer: 2000 Enter second integer: 1000 2000 != 1000 2000 > 1000 2000 >= 1000

Outline

Comparison.cs

(3 of 3 )

Fig. 3.26 | Comparing integers using if statements, equality operatorsand relational operators. (Part 3 of 3).

Page 81: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

81

3.9  Decision Making: Equality and Relational Operators (Cont.)

• If the condition in an if statement is true, the statement associated with that if statement executes.

• An if statement always begins with keyword if, followed by a condition in parentheses.

• An if statement expects one statement in its body.

Common Programming Error 3.7Forgetting the left and/or right parentheses for thecondition in an if statement is a syntax error—theparentheses are required.

Page 82: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

82

3.9  Decision Making: Equality and Relational Operators (Cont.)

Common Programming Error 3.8Reversing the operators !=, >= and <=, as in =!, => and =<, can result in syntax or logic errors.

Common Programming Error 3.9It is a syntax error if the operators ==, !=, >= and <= contain spaces between their symbols, as in = =, ! =, > = and < =, respectively.

Good Programming Practice 3.12Indent an if statement’s body to make it stand out and to enhance application readability.

Page 83: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

83

3.9  Decision Making: Equality and Relational Operators (Cont.)

Common Programming Error 3.10Placing a semicolon immediately after the right parenthesis of the condition in an if statement is normally a logic error.

Good Programming Practice 3.13Place no more than one statement per line in an application. This format enhances readability.

Good Programming Practice 3.14A lengthy statement can be spread over several lines. If a single statement must be split across lines, choose breaking points that make sense, such as after a comma in a comma- separated list, or after an operator in a lengthy expression. If a statement is split across two or more lines, indent all subsequent lines until the end of the statement.

Page 84: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

84

3.9  Decision Making: Equality and Relational Operators (Cont.)

Fig. 3.27 | Precedence and associativity of operations discussed.

• Figure 3.27 shows the precedence of the operators introduced in this chapter from top to bottom in decreasing order of precedence.

Operators Associativity Type

* / % left to right multiplicative

+ - left to right additive

< <= > >= left to right relational

== != left to right equality

= right to left assignment

Page 85: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

85

3.9  Decision Making: Equality and Relational Operators (Cont.)

Good Programming Practice 3.15Refer to the operator precedence chart (the complete chart is in Appendix A) when writing expressions containing many operators. Confirm that the operations in the expression are performed in the order you expect. If you’re uncertain about the order of evaluation in a complex expression, use parentheses to force the order, as you would do in algebraic expressions. Observe that some operators, such as assignment, =, associate from right to left rather than from left to right.

Page 86: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

86

3.10  Examining the ATM Requirements Document

• A requirements document specifies the overall purpose of a system and what it must do.

• A small local bank intends to install a new ATM (Fig. 3.28).

Fig. 3.28 | Automated teller machine user interface.

Screen

Keypad

Deposit Slot

Cash Dispenser

Page 87: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

87

3.10  Examining the ATM Requirements Document (Cont.)

• The ATM should have the following functionality.– The screen prompts the user to enter an account number.

– The user enters a five-digit account number, using the keypad.

– For authentication purposes, the screen prompts the user to enter the PIN.

– The user enters a five-digit PIN, using the keypad.

– If the user enters a valid account number and the correct PIN for that account, the screen displays the main menu.

Page 88: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

88

3.10  Examining the ATM Requirements Document (Cont.)

• The main menu (Fig. 3.29) displays a numbered option for each of the three types of transactions, and the option to exit the system.

Fig. 3.29 | ATM main menu.

Page 89: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

89

3.10  Examining the ATM Requirements Document (Cont.)• The user enters 2 to make a withdrawal (Fig. 3.30).

• If the user enters 1, the screen displays the user’s account balance from the bank’s database.

Fig. 3.30 | ATM withdrawal menu.

Page 90: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

90

3.10  Examining the ATM Requirements Document (Cont.)

• The user enters 3 to make a deposit:

1. The screen prompts the user to enter a deposit amount.

2. The screen displays a message telling the user to insert a deposit envelope into the deposit slot.

3. If the deposit slot receives a deposit envelope, the ATM credits the deposit amount to the user’s account balance in the bank’s database.

Page 91: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

91

3.10  Examining the ATM Requirements Document (Cont.)

• Requirements gathering might include interviews with potential users of the system and specialists in fields related to the system.

• The software life cycle specifies the stages through which software evolves from the time it is conceived to the time it is retired from use.

– Waterfall models perform each stage once in succession.

– Iterative models may repeat one or more stages several times throughout a product’s life cycle.

Page 92: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

92

3.10  Examining the ATM Requirements Document (Cont.)

• To capture what a proposed system should do, developers often employ a technique known as use case modeling.

• Use cases represent different capabilities that the system provides to its clients.

• The simplified ATM system we build in this case study requires only the first three use cases (Fig. 3.31).

Page 93: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

93

3.10  Examining the ATM Requirements Document (Cont.)

Use Case Diagrams• A use case diagram models the interactions between a

system’s clients and the system.

• Figure 3.31 shows the use case diagram for our ATM system.

Fig. 3.31 | Use case diagram for the ATM system from the user’s perspective.

Page 94: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

94

3.10  Examining the ATM Requirements Document (Cont.)

• The stick figure represents an actor, which defines the roles that an external entity—such as a person or another system—plays when interacting with the system.

Page 95: 2009 Pearson Education, Inc. All rights reserved. 1 3 3 Introduction to C# Applications.

2009 Pearson Education, Inc. All rights reserved.

95

3.10  Examining the ATM Requirements Document (Cont.)

• A design specification should specify how the system should be constructed to satisfy the system requirements.

• A system is a set of components that interact to solve a problem.

• For example, to perform the ATM system’s designated tasks, our ATM system has a user interface (Fig. 3.28), contains software that executes financial transactions and interacts with a database of bank-account information.

– System structure describes the system’s objects and their interrelationships.

– System behavior describes how the system changes as its objects interact with one another.