Chapter 3 Data Types and Expressions Microsoft Visual























































- Slides: 55

Chapter 3 Data Types and Expressions Microsoft Visual C#. NET: From Problem Analysis to Program Design 1

Chapter Objectives • Declare memory locations for data • Explore the relationship between classes, objects, and types • Use predefined data types • Use integral data types • Use floating-point types • Learn about decimal type • Declare Boolean variables Microsoft Visual C#. NET: From Problem Analysis to Program Design 2

Chapter Objectives (continued) • Declare and manipulate strings • Work with constants • Write assignment statements using arithmetic operators • Discover the order of operations • Work through a programming example that illustrates the chapter’s concepts • Learn special formatting rules for currency Microsoft Visual C#. NET: From Problem Analysis to Program Design 3

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 a 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 Microsoft Visual C#. NET: From Problem Analysis to Program Design 4

Reserved words in C# Microsoft Visual C#. NET: From Problem Analysis to Program Design 5

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 Microsoft Visual C#. NET: From Problem Analysis to Program Design 6

Naming Conventions (continued) • Uppercase – Every character is uppercase – Constant literals and for identifiers that consist of two or fewer letters Microsoft Visual C#. NET: From Problem Analysis to Program Design 7

Examples of Valid Names (Identifiers) Microsoft Visual C#. NET: From Problem Analysis to Program Design 8

Examples of Invalid Names (Identifiers) Microsoft Visual C#. NET: From Problem Analysis to Program Design 9

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; Microsoft Visual C#. NET: From Problem Analysis to Program Design 10

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 Microsoft Visual C#. NET: From Problem Analysis to Program Design 11

Types, Classes, and Objects • Instance of a class → object • A class includes more than just data • Encapsulation → packaging of data and behaviors into a single or unit→class Microsoft Visual C#. NET: From Problem Analysis to Program Design 12

Type, Class, and Object Examples Microsoft Visual C#. NET: From Problem Analysis to Program Design 13

Predefined Data Types • Common Type System (CTS) • Divided into two major categories Microsoft Visual C#. NET: From Problem Analysis to Program Design 14

Value and Reference Types Microsoft Visual C#. NET: From Problem Analysis to Program Design 15

Value Types • Fundamental or primitive data types Microsoft Visual C#. NET: From Problem Analysis to Program Design 16

Value Types (continued) Microsoft Visual C#. NET: From Problem Analysis to Program Design 17

Integral Data Types • Primary difference – how much storage is needed – whether a negative value can be stored Microsoft Visual C#. NET: From Problem Analysis to Program Design 18

Examples of Integral Variable Declarations int student. Count; // number of students in the class int age. Of. Student = 20; // age - originally initialized to 20 int number. Of. Exams; // number of exams int courses. Enrolled; // number of courses enrolled Microsoft Visual C#. NET: From Problem Analysis to Program Design 19

Floating-point Types • May be in scientific notation with an exponent • n. ne±P – 3. 2 e+5 is equivalent to 320000 – 1. 76 e-3 is equivalent to. 00176 • OR in standard decimal notation • Default type is double Microsoft Visual C#. NET: From Problem Analysis to Program Design 20

Examples of Floating-point Declarations double extra. Person = 3. 50; // extra. Person originally set // to 3. 50 double average. Score = 70. 0; // average. Score originally set // to 70. 0 double price. Of. Ticket; // cost of a movie ticket double grade. Point. Average; // grade point average float total. Amount = 23. 57 f; // note the f must be placed after // the value for float types Microsoft Visual C#. NET: From Problem Analysis to Program Design 21

Decimal Types • Monetary data items • As with the float, must attach the suffix ‘m’ or ‘M’ onto the end of a number to indicate decimal. – Float attach ‘f’ or “F’ decimal endowment. Amount = 33897698. 26 M; decimal deficit; Microsoft Visual C#. NET: From Problem Analysis to Program Design 22

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; Microsoft Visual C#. NET: From Problem Analysis to Program Design 23

Strings • Reference type • Represents a string of Unicode characters string student. Name; string course. Name = “Programming I”; string two. Lines = “Line 1n. Line 2”; Microsoft Visual C#. NET: From Problem Analysis to Program Design 24

Making Data Constant • • Add the keyword const to a declaration Value cannot to 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’; Microsoft Visual C#. NET: From Problem Analysis to Program Design 25

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 Microsoft Visual C#. NET: From Problem Analysis to Program Design 26

Examples of Assignment Statements int number. Of. Minutes, count, min. Int. Value; char first. Initial, year. In. School, punctuation; number. Of. Minutes = 45; count = 0; min. Int. Value = -2147483648; first. Initial = ‘B’; year. In. School = ‘ 1’; enter. Key = ‘n’; // newline escape character Microsoft Visual C#. NET: From Problem Analysis to Program Design 27

Examples of Assignment Statements (continued) double account. Balance, weight; decimal amount. Owed, deficit. Value; bool is. Finished; account. Balance = 4783. 68; weight = 1. 7 E-3; //scientific notation may be used amount. Owed = 3000. 50 m; // m or M must be suffixed to // decimal deficit. Value = -322888672. 50 M; Microsoft Visual C#. NET: From Problem Analysis to Program Design 28

Examples of Assignment Statements (continued) int count = 0, new. Value = 25; string a. Saying, file. Location; a. Saying = “First day of the rest of your life!n "; file. Location = @”C: CSharp. ProjectsChapter 2”; is. Finished = false; // declared previously as a bool count = new. Value; @ placed before a string literal signal that the characters inside the double quotation marks should be interpreted verbatim. Microsoft Visual C#. NET: From Problem Analysis to Program Design 29

Examples of Assignment Statements (continued) Microsoft Visual C#. NET: From Problem Analysis to Program Design 30

Arithmetic Operations • Simplest form of an assignment statement result. Variable = operand 1 operator operand 2; • Readability – Space before and after every operator Microsoft Visual C#. NET: From Problem Analysis to Program Design 31

Basic Arithmetic Operations • Modulus operator with negative values – sign of the dividend determines the result – -3 % 5 = -3; 5 % -3 = 2; -5 % -3 = -3; Microsoft Visual C#. NET: From Problem Analysis to Program Design 32

Basic Arithmetic Operations (continued) • Plus (+) with string Identifiers – concatenates operand 2 onto end of operand 1 string result; string full. Name; string first. Name = “Rochelle”; string last. Name = “Howard”; full. Name = first. Name + “ “ + last. Name; Microsoft Visual C#. NET: From Problem Analysis to Program Design 33

Concatenation Microsoft Visual C#. NET: From Problem Analysis to Program Design 34

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; System. Console. Write. Line(num++); // Displays 100 System. Console. Write. Line(num); // Display 101 System. Console. Write. Line(++num); // Displays 102 Microsoft Visual C#. NET: From Problem Analysis to Program Design 35

Basic Arithmetic Operations (continued) int num = 100; System. Console. Write. Line(x++ + “ “ + ++x); // Displays 100 102 Microsoft Visual C#. NET: From Problem Analysis to Program Design 36

Basic Arithmetic Operations (continued) Microsoft Visual C#. NET: From Problem Analysis to Program Design 37

Compound Operations • Accumulation – += Microsoft Visual C#. NET: From Problem Analysis to Program Design 38

Basic Arithmetic Operations (continued) answer = 100; answer += 50 * 3 / 25 – 4; 50 * 3 = 150 / 25 = 6 6– 4=2 100 + 2 = 102 • Order of operations – Order in which the calculations are performed Microsoft Visual C#. NET: From Problem Analysis to Program Design 39

Order of Operations • Associativity of operators – Left – Right Microsoft Visual C#. NET: From Problem Analysis to Program Design 40

Order of Operations (continued) Microsoft Visual C#. NET: From Problem Analysis to Program Design 41

Mixed Expressions • Implicit type coercion: – Changes int data type into a double – No implicit conversion from double to int Microsoft Visual C#. NET: From Problem Analysis to Program Design 42

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 Microsoft Visual C#. NET: From Problem Analysis to Program Design 43

Programming Example Carpet. Calculator Microsoft Visual C#. NET: From Problem Analysis to Program Design 44

Data Needs for the Carpet. Calculator Microsoft Visual C#. NET: From Problem Analysis to Program Design 45

Non-changing Definitions for the Carpet. Calculator Microsoft Visual C#. NET: From Problem Analysis to Program Design 46

Carpet. Calculator Example Microsoft Visual C#. NET: From Problem Analysis to Program Design 47

Algorithm for Carpet. Calculator Example Microsoft Visual C#. NET: From Problem Analysis to Program Design 48

Algorithm for the Carpet. Calculator Example (continued) Microsoft Visual C#. NET: From Problem Analysis to Program Design 49

Carpet. Calculator Example (continued) Microsoft Visual C#. NET: From Problem Analysis to Program Design 50

/* Carpet. Calculator. cs Author: Doyle */ using System; namespace Carpet. Example { 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; double room. Length, room. Width, carpet. Price, num. Of. Square. Feet, num. Of. Square. Yards, total. Cost; Microsoft Visual C#. NET: From Problem Analysis to Program Design 51

} room. Length = room. Length. Feet + room. Length. Inches / INCHES_PER_FOOT; room. Width = room. Width. Feet + 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; Console. Out. Write. Line("The cost of " + BEST_CARPET + " is {0: C}", total. Cost); Console. Out. Write. Line( ); carpet. Price = 15. 95; total. Cost = num. Of. Square. Yards * carpet. Price; Console. Out. Write. Line("The cost of " + ECONOMY_CARPET + " is " + "{0: C}", total. Cost); Console. Read(); } } Microsoft Visual C#. NET: From Problem Analysis to Program Design 52

Carpet. Calculator Example (continued) Microsoft Visual C#. NET: From Problem Analysis to Program Design 53

Chapter Summary • Memory locations for data • Relationship between classes, objects, and types • Predefined data types – Integral data types – Floating-point types – Decimal type – Boolean variables – Strings Microsoft Visual C#. NET: From Problem Analysis to Program Design 54

Chapter Summary (continued) • Constants • Assignment statements – Order of operations Microsoft Visual C#. NET: From Problem Analysis to Program Design 55