3and ex - georgia state university
Post on 26-Jan-2022
1 Views
Preview:
TRANSCRIPT
1
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Data Types and Expressions3 p3
David McDonald, Ph.D.Director of Emerging Technologies
C# Programming: From Problem Analysis to Program Design 2nd Edition
Chapter ObjectivesDeclare memory locations for data
Explore the relationship between classesExplore the relationship between classes, objects, and types
Use predefined data types
Use integral data types
Use floating-point types
C# Programming: From Problem Analysis to Program Design
Use floating point types
Learn about the decimal data type
2
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Chapter Objectives (continued)
Declare and manipulate strings
Work with constants
Write assignment statements using arithmetic operators
Learn about the order of operations
C# Programming: From Problem Analysis to Program Design
Learn special formatting rules for currency
Declare Boolean variables
Memory Locations for DataIdentifier√Name√R l f ti id tifi√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
C# Programming: From Problem Analysis to Program Design
• Keywords cannot be used• Use the case of the character to your
advantage • Be descriptive with meaningful names
3
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Reserved Words in C#
C# Programming: From Problem Analysis to Program Design
Reserved Words in C# (continued)• Contextual keywords
• New with C# 2.0 standards – November 20052005
• As powerful as regular keywords
• Contextual keywords have special meaning only when used in a specific context; other times they can be used as identifiersidentifiers
C# Programming: From Problem Analysis to Program Design
4
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Examples of Valid Names (Identifiers)
C# Programming: From Problem Analysis to Program Design
Examples of Invalid Names (Identifiers)
C# Programming: From Problem Analysis to Program Design
5
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
VariablesArea in computer memory where a value of a particular data type can be stored√Declare a variable√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
Types, Classes, and ObjectsType√ 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
ttype
√ Simple data type such as int, implemented as a class
C# Programming: From Problem Analysis to Program Design
6
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
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
C# Programming: From Problem Analysis to Program Design
Type, Class, and Object Examples
12
C# Programming: From Problem Analysis to Program Design
7
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Naming Conventions • Pascal case
– First letter of each word capitalizedp
– Class, method, namespace, and properties identifiers
• Camel case – Hungarian notation
C# Programming: From Problem Analysis to Program Design
– First letter of identifier lowercase; first letter of subsequent concatenated words capitalized
– Variables and objects
Predefined Data Types
• Common Type System (CTS) • Divided into two major categories
Figure 3-1 .NET common types
C# Programming: From Problem Analysis to Program Design
8
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Value and Reference Types
Figure 3-2 Memory representation for value and reference types
C# Programming: From Problem Analysis to Program Design
Value TypesFundamental or primitive data types
Figure 3-3 Value type hierarchy
C# Programming: From Problem Analysis to Program Design
9
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Value Types (continued)
C# Programming: From Problem Analysis to Program Design
Integral Data TypesPrimary difference√How much storage is needed√Whether a negative value can be stored√Whether a negative value can be stored
C# Programming: From Problem Analysis to Program Design
10
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Examples of Integral Variable Declarations
int studentCount; // number of students in the classt stude tCou t; // u be o stude ts t e c assint ageOfStudent = 20; // age - originally initialized to 20int numberOfExams; // number of examsint coursesEnrolled; // number of courses enrolled
C# Programming: From Problem Analysis to Program Design
Floating-point Types
May be in scientific notation with an exponentn.ne±P√3.2e+5 is equivalent to 320,000 √1.76e-3 is equivalent to .00176
OR in standard decimal notationDefault type is double
C# Programming: From Problem Analysis to Program Design
11
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Examples of Floating-point Declarations
double extraPerson = 3 50; // extraPerson originally setdouble extraPerson 3.50; // extraPerson originally set // to 3.50
double averageScore = 70.0; // averageScore originally set // to 70.0
double priceOfTicket; // cost of a movie ticketdouble gradePointAverage; // grade point averagefloat totalAmount = 23.57f; // note the f must be placed after
// the value for float types
Decimal Types • Monetary data items • As with the float, must attach the suffix ‘m’ or
Examples
‘M’ onto the end of a number to indicate decimal– Float attach ‘f’ or “F’
C# Programming: From Problem Analysis to Program Design
decimal endowmentAmount = 33897698.26M;decimal deficit;
12
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Boolean Variables
Based on true/false, on/off logicBoolean type in C# → boolDoes not accept integer values such as 0, 1, or -1
(this is different than most programming languages)
bool undergraduateStudent;
bool moreData = true;
Strings
• Reference type• Reference type
• Represents a string of Unicode characters
string studentName;string courseName = “CIS 3260”;
string twoLines = “Line1\nLine2”;g ;
C# Programming: From Problem Analysis to Program Design
13
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Making Data Constant
• Add the keyword const to a declaration• Value cannot be changed • Standard naming convention • Syntax
– const type identifier = expression;
t d bl TAX RATE 0 0675
25
const double TAX_RATE = 0.0675; const int SPEED = 70;const char HIGHEST_GRADE = ‘A’;
C# Programming: From Problem Analysis to Program Design
Assignment Statements
• Used to change the value of the variable– Assignment operator (=)
• Syntax• Syntaxvariable = expression;
• Expression can be:– Another variable– Compatible literal value – Mathematical equation
C ll t th d th t t tibl l– 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
14
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Examples of Assignment Statements
int numberOfMinutes, count, minIntValue;char firstInitial, yearInSchool, enterKey;
numberOfMinutes = 45;count = 0;minIntValue = -2147483648;firstInitial = ‘B’;yearInSchool = ‘1’;enterKey = ‘\n’; // newline escape character
C# Programming: From Problem Analysis to Program Design
Examples of Assignment Statements (continued)
double accountBalance, weight;decimal amountOwed, deficitValue;bool isFinished;
accountBalance = 4783.68;weight = 1.7E-3; //scientific notation may be usedamountOwed = 3000.50m; // m or M must be suffixed to
// decimal
28
deficitValue = -322888672.50M;
C# Programming: From Problem Analysis to Program Design
15
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Examples of Assignment Statements (continued)
int count = 0, newValue = 25;string aSaying fileLocation;string aSaying, fileLocation;
aSaying = “First day of the rest of your life!\n ";fileLocation = @”C:\CSharpProjects\Chapter2”;isFinished = false; // declared previously as a boolcount = newValue;
29
@ placed before a string literal signals that the characters inside the double quotation marks should be interpreted verbatim
C# Programming: From Problem Analysis to Program Design
Examples of Assignment Statements (continued)
Figure 3-5 Impact of assignment statement
C# Programming: From Problem Analysis to Program Design
16
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Arithmetic Operations • Simplest form of an assignment statement
resultVariable = operand1 operator operand2; R d bilit• Readability– Space before and after every operator
31
C# Programming: From Problem Analysis to Program Design
Basic Arithmetic Operations
Figure 3-6 Result of 67 % 3
Modulus operator with negative values√ Sign of the dividend determines the result√ -3 % 5 = -3; 5 % -3 = 2; -5 % -3 = -3;
Figure 3 6 Result of 67 % 3
C# Programming: From Problem Analysis to Program Design
17
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Basic Arithmetic Operations (continued)
• Plus (+) with string IdentifiersPlus ( ) with string Identifiers– Concatenates operand2 onto end of operand1
string result;string fullName;string firstName = “Rochelle”;string lastName = “Howard”;
fullName = firstName + “ “ + lastName;
C# Programming: From Problem Analysis to Program Design
Concatenation
Figure 3-7 String concatenation
C# Programming: From Problem Analysis to Program Design
18
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Basic Arithmetic Operations (continued)
• Increment and Decrement OperationsIncrement and Decrement Operations– Unary operator
num++; // num = num + 1;--value1; // value = value – 1;
– Preincrement/predecrement versus post
int num = 100;System.Console.WriteLine(num++); // Displays 100System.Console.WriteLine(num); // Display 101 System.Console.WriteLine(++num); // Displays 102
C# Programming: From Problem Analysis to Program Design
Basic Arithmetic Operations (continued)
Figure 3-10 Results after statement is executed
C# Programming: From Problem Analysis to Program Design
19
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Compound Operations
• Accumulation– +=
C# Programming: From Problem Analysis to Program Design
Basic Arithmetic Operations (continued)
• Order of operations– Order in which the calculations are performed
• Example– answer = 100;– answer += 50 * 3 / 25 – 4;
50 * 3 = 150150 / 25 = 6150 / 25 = 66 – 4 = 2100 + 2 = 102
C# Programming: From Problem Analysis to Program Design
20
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Order of Operations
Associativity of operators
39
√Left
√Right
C# Programming: From Problem Analysis to Program Design
Order of Operations (continued)
40
Figure 3-11 Order of execution of the operators
21
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Data Type Mismatch
Implicit type change√ Can automatically change int data type into a
d bldouble √ But cannot automatically convert from double
to int
Figure 3-12 Syntax error generated for assigning a double to an int
C# Programming: From Problem Analysis to Program Design
Mixed Expressions (continued)Explicit type change√ Cast√ (type) expression( yp ) p√ examAverage = (exam1 + exam2 + exam3) / (double)
count;
int value1 = 0,anotherNumber = 75;
double value2 = 100.99,anotherDouble = 100;
value1 = (int) value2; // value1 = 100 value2 = (double) anotherNumber; // value2 = 75.0
C# Programming: From Problem Analysis to Program Design
22
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Formatting Output• You can format data by adding dollar signs,
percent symbols, and/or commas to separate di itdigits
• You can suppress leading zeros
• You can pad a value with special characters
– Place characters to the left or right of the i ifi di isignificant digits
• Use format specifiers
C# Programming: From Problem Analysis to Program Design
Numeric Format Specifiers
44
C# Programming: From Problem Analysis to Program Design
23
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Numeric Format Specifiers (continued)
45
C# Programming: From Problem Analysis to Program Design
Custom Numeric Format Specifiers
C# Programming: From Problem Analysis to Program Design
24
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: davemcdonald@gsu.edu
Custom Numeric Format Specifiers(continued)
C# Programming: From Problem Analysis to Program Design
Formatting Output
C# Programming: From Problem Analysis to Program Design
top related