2 Data Types and Expressions C Programming From

  • Slides: 59
Download presentation
2 Data Types and Expressions C# Programming: From Problem Analysis to Program Design th

2 Data Types and Expressions C# Programming: From Problem Analysis to Program Design th 5 Edition © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 1

Chapter Objectives • Examine how computers represent data • Declare memory locations for data

Chapter Objectives • Examine how computers represent data • Declare memory locations for data • Explore the relationship between classes, objects, and types • Use predefined data types • Use integral data types C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 2

Chapter Objectives (continued) • Use floating-point types • Learn about the decimal data type

Chapter Objectives (continued) • Use floating-point types • Learn about the decimal data type • Declare Boolean variables • Declare and manipulate strings • Work with constants C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 3

Chapter Objectives (continued) • Write assignment statements using arithmetic operators • Learn about the

Chapter Objectives (continued) • Write assignment statements using arithmetic operators • Learn about the order of operations • Learn special formatting rules for currency • Work through a programming example that illustrates the chapter’s concepts C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 4

Data Representation (continued) • Character sets – With only 8 bits, can represent 28,

Data Representation (continued) • Character sets – With only 8 bits, can represent 28, or 256, different decimal values ranging from 0 to 255; this is 256 different characters • Unicode – character set used by C# (pronounced C Sharp) – Uses 16 bits to represent characters – 216, or 65, 536 unique characters, can be represented • American Standard Code for Information Interchange (ASCII) – subset of Unicode – First 128 characters are the same C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 5

Memory Locations for Data • Identifier – Name – Rules for creating an identifier

Memory Locations for Data • Identifier – Name – Rules for creating an identifier • Combination of alphabetic characters (a-z and A-Z), numeric digits (0 -9), and the underscore • First character in the name may not be numeric • No embedded spaces – concatenate (append) words together • Keywords cannot be used • Use the case of the character to your advantage • Be descriptive with meaningful names C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 6

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 7

Reserved Words in C# (continued) • Contextual keywords • As powerful as regular keywords

Reserved Words in C# (continued) • Contextual keywords • As powerful as regular keywords • Contextual keywords have special meaning only when used in a specific context; other times they can be used as identifiers C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 8

C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not

C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 9

Naming Conventions • Pascal case – First letter of each word capitalized – Class,

Naming Conventions • Pascal case – First letter of each word capitalized – Class, method, namespace, and properties identifiers • Camel case – Hungarian notation – First letter of identifier lowercase; first letter of subsequent concatenated words capitalized – Variables and objects C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 10

Naming Conventions (continued) • Uppercase – Every character is uppercase – Constant literals and

Naming Conventions (continued) • Uppercase – Every character is uppercase – Constant literals and for identifiers that consist of two or fewer letters C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 11

Variables • Area in computer memory where a value of a particular data type

Variables • Area in computer memory where a value of a particular data type can be stored – Declare a variable – Allocate memory • Syntax – type identifier; • Compile-time initialization – Initialize a variable when it is declared • Syntax – type identifier = expression; C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 12

Types, Classes, and Objects • Type – C# has more than one type of

Types, Classes, and Objects • Type – C# has more than one type of number – int type is a whole number – Floating-point types can have a fractional portion • Types are actually implemented through classes – One-to-one correspondence between a class and a type – Simple data type such as int, implemented as a class C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 13

Types, Classes, and Objects (continued) • Instance of a class → object • A

Types, Classes, and Objects (continued) • Instance of a class → object • A class includes more than just data • Encapsulation → packaging of data and behaviors into a single or unit→class C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 14

Type, Class, and Object Examples Table 2 -7 Sample data types C# Programming: From

Type, Class, and Object Examples Table 2 -7 Sample data types C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 15

Predefined Data Types • Common Type System (CTS) • Divided into two major categories

Predefined Data Types • Common Type System (CTS) • Divided into two major categories Figure 2 -3. NET common types C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 16

Value and Reference Types Figure 2 -4 Memory representation for value and reference types

Value and Reference Types Figure 2 -4 Memory representation for value and reference types C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 17

Value Types • Fundamental or primitive data types Figure 2 -5 Value type hierarchy

Value Types • Fundamental or primitive data types Figure 2 -5 Value type hierarchy C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 18

Illustrative Program • We’ll start a project and program to illustrate the basic data

Illustrative Program • We’ll start a project and program to illustrate the basic data types in C# as we go through them C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 19

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 20

Integral Data Types • Primary difference – How much storage is needed – Whether

Integral Data Types • Primary difference – How much storage is needed – Whether a negative value can be stored • Includes number of types – – – byte & sbyte char int & uint long & ulong short & ushort C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 21

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to

© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 22

Floating-Point Types • May be in scientific notation with an exponent • n. ne±P

Floating-Point Types • May be in scientific notation with an exponent • n. ne±P – 3. 2 e+5 is equivalent to 320, 000 – 1. 76 e-3 is equivalent to. 00176 • OR in standard decimal notation • Default type is double © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 23

Examples of Floating-Point Declarations double extra. Person = 3. 50; double average. Score =

Examples of Floating-Point Declarations double extra. Person = 3. 50; double average. Score = 70. 0; double price. Of. Ticket; double grade. Point. Average; float total. Amount = 23. 57 F; // extra. Person originally set to 3. 50 // average. Score originally set to 70. 0 // cost of a movie ticket // grade point average // note the F must be placed after 23. 57 C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 24

Decimal Types • Monetary data items • As with the float, must attach the

Decimal Types • Monetary data items • As with the float, must attach the suffix ‘m’ or ‘M’ • Examples decimal endowment. Amount = 33897698. 26 M; decimal deficit; C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 25

Boolean Variables • Based on true/false, on/off logic • Boolean type in C# →

Boolean Variables • Based on true/false, on/off logic • Boolean type in C# → bool • Does not accept integer values such as 0, 1, or -1 bool undergraduate. Student; bool more. Data = true; C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 26

Strings • Reference type • Represents a string of Unicode characters string student. Name;

Strings • Reference type • Represents a string of Unicode characters string student. Name; string course. Name = "Programming I"; string two. Lines = "Line 1n. Line 2"; C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 27

Making Data Constant • • Add the keyword const to a declaration Value cannot

Making Data Constant • • Add the keyword const to a declaration Value cannot be changed Standard naming convention Syntax – const type identifier = expression; const double TAX_RATE = 0. 0675; const int SPEED = 70; const char HIGHEST_GRADE = 'A'; C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 28

Assignment Statements • Used to change the value of the variable – Assignment operator

Assignment Statements • Used to change the value of the variable – Assignment operator (=) • Syntax variable = expression; • Expression can be: – – – Another variable Compatible literal value Mathematical equation Call to a method that returns a compatible value Combination of one or more items in this list C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 29

Basic Arithmetic Operations (continued) • Increment and Decrement Operations – Unary operator num++; //

Basic Arithmetic Operations (continued) • Increment and Decrement Operations – Unary operator num++; // num = num + 1; --value 1; // value = value – 1; – Preincrement/predecrement versus post int num = 100; Write. Line(num++); // Displays 100 Write. Line(num); // Display 101 Write. Line(++num); // Displays 102 C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 30

Basic Arithmetic Operations int num = 100; Write. Line(x++ + " " + ++x);

Basic Arithmetic Operations int num = 100; Write. Line(x++ + " " + ++x); // Displays 100 102 Please don’t do this!! It makes programs difficult to read! C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 31

Compound Operations • Accumulation – Variable on left side of equal symbol is used

Compound Operations • Accumulation – Variable on left side of equal symbol is used once the entire expression on right is evaluated © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 32

Order of Operations • Associatively of operators – Left – Right C# Programming: From

Order of Operations • Associatively of operators – Left – Right C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 33

Mixed Expressions • Implicit type coercion – Changes int data type into a double

Mixed Expressions • Implicit type coercion – Changes int data type into a double – No implicit conversion from double to int double answer; answer = 10 / 3; // Does not produce 3. 3333333 int value 1 = 440, another. Number = 70; double value 2 = 100. 60; value 2 = value 1; // ok here 440. 0 stored in value 2 C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 34

Mixed Expressions int value 1 = 440; double value 2 = 100. 60; value

Mixed Expressions int value 1 = 440; double value 2 = 100. 60; value 1 = value 2; // syntax error as shown in Figure 2 -14 C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 35

Mixed Expressions (continued) • Explicit type coercion – Cast – (type) expression – exam.

Mixed Expressions (continued) • Explicit type coercion – Cast – (type) expression – exam. Average = (exam 1 + exam 2 + exam 3) / (double) count; int value 1 = 0, another. Number = 75; double value 2 = 100. 99, another. Double = 100; value 1 = (int) value 2; // value 1 = 100 value 2 = (double) another. Number; // value 2 = 75. 0 C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 36

Formatting Output • You can format data by adding dollar signs, percent symbols, and/or

Formatting Output • You can format data by adding dollar signs, percent symbols, and/or commas to separate digits • You can suppress leading zeros • You can pad a value with special characters – Place characters to the left or right of the significant digits • Use format specifiers C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 37

Formatting Output (continued) C# Programming: From Problem Analysis to Program Design © 2016 Cengage

Formatting Output (continued) C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 38

Numeric Format Specifiers Table 2 -16 Standard numeric format specifiers C# Programming: From Problem

Numeric Format Specifiers Table 2 -16 Standard numeric format specifiers C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 39

Numeric Format Specifiers (continued) Table 2 -16 Standard numeric format specifiers (continued) C# Programming:

Numeric Format Specifiers (continued) Table 2 -16 Standard numeric format specifiers (continued) C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 40

Custom Numeric Format Specifiers Table 2 -17 Custom numeric format specifiers C# Programming: From

Custom Numeric Format Specifiers Table 2 -17 Custom numeric format specifiers C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 41

Custom Numeric Format Specifiers (continued) Table 2 -17 Custom numeric format specifiers (continued) C#

Custom Numeric Format Specifiers (continued) Table 2 -17 Custom numeric format specifiers (continued) C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 42

Width Specifier • Useful when you want to control the alignment of items on

Width Specifier • Useful when you want to control the alignment of items on multiple lines • Alignment component goes after the index ordinal followed by a comma (before the colon) – If alignment number is less than actual size, it is ignored – If alignment number is greater, pads with white space • Negative alignment component places spaces on right Write. Line("{0, 10: F 0}{1, 8: C}", 47, 14); 47 $14. 00 //Right justifies values C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 43

Programming Example – Carpet. Calculator © 2016 Cengage Learning®. May not be scanned, copied

Programming Example – Carpet. Calculator © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 44

C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not

C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 45

C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not

C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 46

Carpet. Calculator Example C# Programming: From Problem Analysis to Program Design © 2016 Cengage

Carpet. Calculator Example C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 47

Algorithm for Carpet. Calculator Example Figure 2 -17 Carpet. Calculator flowchart C# Programming: From

Algorithm for Carpet. Calculator Example Figure 2 -17 Carpet. Calculator flowchart C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 48

Algorithm for the Carpet. Calculator Example (continued) Figure 2 -18 Structured English for the

Algorithm for the Carpet. Calculator Example (continued) Figure 2 -18 Structured English for the Carpet. Calculator example C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 49

Carpet. Calculator Example (continued) Figure 2 -19 Class diagram for the Carpet. Calculator example

Carpet. Calculator Example (continued) Figure 2 -19 Class diagram for the Carpet. Calculator example C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 50

Carpet. Calculator Example (continued) Figure 2 -20 Revised class diagram without methods C# Programming:

Carpet. Calculator Example (continued) Figure 2 -20 Revised class diagram without methods C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 51

/* Carpet. Calculator. cs Author: Doyle using System; using static System. Console; */ namespace

/* Carpet. Calculator. cs Author: Doyle using System; using static System. Console; */ namespace Carpet. Calculator { class Carpet. Calculator { static void Main( ) { const int SQ_FT_PER_SQ_YARD = 9; const int INCHES_PER_FOOT = 12; const string BEST_CARPET = "Berber"; const string ECONOMY_CARPET = "Pile"; int room. Length. Feet = 12, room. Length. Inches = 2, room. Width. Feet = 14, room. Width. Inches = 7; C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 52

double room. Length, room. Width, carpet. Price, num. Of. Square. Feet, num. Of. Square.

double room. Length, room. Width, carpet. Price, num. Of. Square. Feet, num. Of. Square. Yards, total. Cost; room. Length = room. Length. Feet + (double) room. Length. Inches / INCHES_PER_FOOT; room. Width = room. Width. Feet + (double) room. Width. Inches / INCHES_PER_FOOT; num. Of. Square. Feet = room. Length * room. Width; num. Of. Square. Yards = num. Of. Square. Feet / SQ_FT_PER_SQ_YARD; carpet. Price = 27. 95; total. Cost = num. Of. Square. Yards * carpet. Price; C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 53

Write. Line("The cost of " + BEST_CARPET + " is {0: C}", total. Cost);

Write. Line("The cost of " + BEST_CARPET + " is {0: C}", total. Cost); Write. Line( ); carpet. Price = 15. 95; total. Cost = num. Of. Square. Yards * carpet. Price; Write. Line("The cost of " + ECONOMY_CARPET + " is " + "{0: C}", total. Cost); Read. Key( ); } } } C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 54

Carpet. Calculator Example (continued) C# Programming: From Problem Analysis to Program Design © 2016

Carpet. Calculator Example (continued) C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 55

Coding Standards • Naming conventions – Identifiers • Spacing conventions • Declaration conventions C#

Coding Standards • Naming conventions – Identifiers • Spacing conventions • Declaration conventions C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 56

Chapter Summary • Memory representation of data • Bits versus bytes • Number system

Chapter Summary • Memory representation of data • Bits versus bytes • Number system – Binary number system • Character sets – Unicode C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 57

Chapter Summary (continued) • Memory locations for data • Relationship between classes, objects, and

Chapter Summary (continued) • Memory locations for data • Relationship between classes, objects, and types • Predefined data types – Integral, Floating-point, Decimal, Boolean and String variables C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 58

Chapter Summary (continued) • Constants • Assignment statements – Order of operations • Formatting

Chapter Summary (continued) • Constants • Assignment statements – Order of operations • Formatting output C# Programming: From Problem Analysis to Program Design © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part 59