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

Slides:



Advertisements
Similar presentations
Data Types and Expressions
Advertisements

Chapter 2: Basic Elements of C++
C# Programming: From Problem Analysis to Program Design1 3 Data Types and Expressions C# Programming: From Problem Analysis to Program Design 2 nd Edition.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
C# Programming: From Problem Analysis to Program Design1 Data Types and Expressions C# Programming: From Problem Analysis to Program Design 3rd Edition.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
JavaScript, Third Edition
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
Java Chapter 2 Creating Classes. Class Defines structure of an object or set of objects; Includes variables (data) and methods (actions) which determine.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Chapter 2: Using Data.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
COMP Primitive and Class Types Yi Hong May 14, 2015.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Data Types and Expressions
Chapter 2 Variables.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Data Types, Identifiers, and Expressions
Java Programming: From Problem Analysis to Program Design, 4e
Data Types and Expressions
C# Programming: From Problem Analysis to Program Design
Data Types and Expressions
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
Data Types and Expressions
Data Types and Expressions
Chapter 2: Introduction to C++.
Data Types and Expressions
Primitive Types and Expressions
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Variables.
Data Types and Expressions
Presentation transcript:

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

2 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 Design3 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 Design4 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 Design5 Reserved words in C#

Microsoft Visual C#.NET: From Problem Analysis to Program Design6 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 Design7 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 Design8 Examples of Valid Names (Identifiers)

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

Microsoft Visual C#.NET: From Problem Analysis to Program Design10 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 Design11 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 Design12 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 Design13 Type, Class, and Object Examples

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

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

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

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

Microsoft Visual C#.NET: From Problem Analysis to Program Design18 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 Design19 Examples of Integral Variable Declarations int studentCount; // number of students in the class int ageOfStudent = 20; // age - originally initialized to 20 int numberOfExams; // number of exams int coursesEnrolled; // number of courses enrolled

Microsoft Visual C#.NET: From Problem Analysis to Program Design20 Floating-point Types May be in scientific notation with an exponent n.ne±P –3.2e+5 is equivalent to –1.76e-3 is equivalent to OR in standard decimal notation Default type is double

Microsoft Visual C#.NET: From Problem Analysis to Program Design21 Examples of Floating-point Declarations double 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 ticket double gradePointAverage; // grade point average float totalAmount = 23.57f; // note the f must be placed after // the value for float types

Microsoft Visual C#.NET: From Problem Analysis to Program Design22 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 endowmentAmount = M; decimal deficit;

Microsoft Visual C#.NET: From Problem Analysis to Program Design23 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 undergraduateStudent; bool moreData = true;

Microsoft Visual C#.NET: From Problem Analysis to Program Design24 Strings Reference type Represents a string of Unicode characters string studentName; string courseName = “Programming I”; string twoLines = “Line1\nLine2”;

Microsoft Visual C#.NET: From Problem Analysis to Program Design25 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 = ; const int SPEED = 70; const char HIGHEST_GRADE = ‘A’;

Microsoft Visual C#.NET: From Problem Analysis to Program Design26 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 Design27 Examples of Assignment Statements int numberOfMinutes, count, minIntValue; char firstInitial, yearInSchool, punctuation; numberOfMinutes = 45; count = 0; minIntValue = ; firstInitial = ‘B’; yearInSchool = ‘1’; enterKey = ‘\n’;// newline escape character

Microsoft Visual C#.NET: From Problem Analysis to Program Design28 Examples of Assignment Statements ( continued ) double accountBalance, weight; decimal amountOwed, deficitValue; bool isFinished; accountBalance = ; weight = 1.7E-3; //scientific notation may be used amountOwed = m; // m or M must be suffixed to // decimal deficitValue = M;

Microsoft Visual C#.NET: From Problem Analysis to Program Design29 Examples of Assignment Statements ( continued ) int count = 0, newValue = 25; string aSaying, fileLocation; aSaying = “First day of the rest of your life!\n "; fileLocation isFinished = false;// declared previously as a bool count = 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 Design30 Examples of Assignment Statements ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design31 Arithmetic Operations Simplest form of an assignment statement resultVariable = operand1 operator operand2; Readability –Space before and after every operator

Microsoft Visual C#.NET: From Problem Analysis to Program Design32 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 Design33 Basic Arithmetic Operations ( continued ) Plus (+) with string Identifiers –concatenates operand2 onto end of operand1 string result; string fullName; string firstName = “Rochelle”; string lastName = “Howard”; fullName = firstName + “ “ + lastName;

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

Microsoft Visual C#.NET: From Problem Analysis to Program Design35 Basic Arithmetic Operations ( continued ) Increment 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 100 System.Console.WriteLine(num); // Display 101 System.Console.WriteLine(++num); // Displays 102

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

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

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

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

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

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

Microsoft Visual C#.NET: From Problem Analysis to Program Design42 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 Design43 Mixed Expressions ( continued ) Explicit type coercion –Cast –(type) expression –examAverage = (exam1 + exam2 + exam3) / (double) count; int value1 = 0, anotherNumber = 75; double value2 = , anotherDouble = 100; value1 = (int) value2; // value1 = 100 value2 = (double) anotherNumber; // value2 = 75.0

Microsoft Visual C#.NET: From Problem Analysis to Program Design44 Programming Example - CarpetCalculator

Microsoft Visual C#.NET: From Problem Analysis to Program Design45 Data Needs for the CarpetCalculator

Microsoft Visual C#.NET: From Problem Analysis to Program Design46 Non-changing Definitions for the CarpetCalculator

Microsoft Visual C#.NET: From Problem Analysis to Program Design47 CarpetCalculator Example

Microsoft Visual C#.NET: From Problem Analysis to Program Design48 Algorithm for CarpetCalculator Example

Microsoft Visual C#.NET: From Problem Analysis to Program Design49 Algorithm for the CarpetCalculator Example ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design50 CarpetCalculator Example ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design51 /* CarpetCalculator.csAuthor:Doyle */ using System; namespace CarpetExample { class CarpetCalculator { 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 roomLengthFeet = 12, roomLengthInches = 2, roomWidthFeet = 14, roomWidthInches = 7; double roomLength, roomWidth, carpetPrice, numOfSquareFeet, numOfSquareYards, totalCost;

Microsoft Visual C#.NET: From Problem Analysis to Program Design52 roomLength = roomLengthFeet + roomLengthInches / INCHES_PER_FOOT; roomWidth = roomWidthFeet + roomWidthInches / INCHES_PER_FOOT; numOfSquareFeet = roomLength * roomWidth; numOfSquareYards = numOfSquareFeet / SQ_FT_PER_SQ_YARD; carpetPrice = 27.95; totalCost = numOfSquareYards * carpetPrice; Console.Out.WriteLine("The cost of " + BEST_CARPET + " is {0:C}", totalCost); Console.Out.WriteLine( ); carpetPrice = 15.95; totalCost = numOfSquareYards * carpetPrice; Console.Out.WriteLine("The cost of " + ECONOMY_CARPET + " is " + "{0:C}",totalCost); Console.Read(); } } }

Microsoft Visual C#.NET: From Problem Analysis to Program Design53 CarpetCalculator Example ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design54 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 Design55 Chapter Summary ( continued ) Constants Assignment statements –Order of operations