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.

Slides:



Advertisements
Similar presentations
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Advertisements

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Chapter 2: Introduction to C++.
ASP.NET Programming with C# and SQL Server First Edition
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Performing Simple Calculations with C# Svetlin Nakov Telerik Corporation
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.
Expressions, Data Conversion, and Input
Objectives You should be able to describe: Data Types
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
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,
Numeric Types, Expressions, and Output ROBERT REAVES.
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.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
JAVA Practical Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Multiple variables can be created in one declaration
Computing with C# and the .NET Framework
Operators and Expressions
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
A First Book of ANSI C Fourth Edition
Chapter 2 Variables.
Conversion Check your class notes and given examples at class.
Chapter 2: Introduction to C++.
Primitive Types and Expressions
Unit 3: Variables in Java
Presentation transcript:

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 int, double, char, string, bool 5, 1.8 x 10 4, 0x0101, abcde, T or F

Variable Declaration Statement which names a variable and reserves storage for it Data type Identifier – begins with lowercase letter Optional initial value Ending ; Assignment Operator = ‘is assigned the value of the following expression’ Initialization Assignment made when variable is declared int classPeriod = 4;

Turn to page 84 and answer Review Questions 1 – 3.

Turn to page 50, figures 2-1 and 2-2. discuss code and output Turn to page 51, figures 2-3 and 2-4 Discuss code and output Write () All output stays on the same line Format String Combination of strings and variable values into a single string

Placeholder – A pair of curly brackets Contains a number indicating the position of the variable The list follows the string First position is 0 May display multiple variables within a single call May display the same variable multiple times Console.WriteLine (“The money is ${0} exactly”, someMoney); Turn to page 52 and create the program on the bottom. Name it DisplayVariables Where do the numbers align in your output?

Alignment – Where on the line the characters are displayed Default = right Negative field value = left Field Size Length of output field Console.WriteLine (“{0, 5}”, num1); Turn to page 85 and answer Review Questions Turn to page 53 and create the program in the middle. Name it DisplayVariables2 Where do the numbers align in your output?

Integral Data Types Store whole numbers Byte, sbyte, short, ushort, int, uint, long, ulong, char Most basic is int Char – each letter has a numerical value to the computer int annualSalary = 20000;

Turn to page 75 and complete You Do It (YDI) # 1 – 8.

Floating-point Number Contains decimal positions Significant digits – mathematical accuracy of the value Float – holds as many as seven significant digits of accuracy Double – holds 15 or 16 significant digits of accuracy Decimal – greater precision than float and double; smaller range Floating-point number is double by default Place an F after the number to keep it floating-point float pocketChange = 4.87F;

C# displays floating-point numbers in the most concise way while maintaining the correct value. Double myMoney = 14.00; Will be displayed as 14 since the 00 do nothing for the value Standard numeric format strings Strings of characters Enclosed within “ “ Specify output format Format specifier Pg 57 Table 2-2 Precision specifier Specifies the number of digits to the right of the decimal moneyString = someMoney.ToString (“F”);

1. What does the 1 st WriteLine () display? 2. What does the 2 nd WriteLine () display? double someMoney = 123; string moneyString; moneyString = someMoney.ToString (“F”); Console.WriteLine (money.String); moneyString = someMoney.ToString (“F3”); Console.WriteLine (moneyString);

1. What does the 1 st WriteLine () display? 2. What does the 2 nd WriteLine () display? double someMoney = ; string conversion; conversion = someValue.ToString (“C”); Console.WriteLine (conversion); conversion = someValue.ToString (“C2”); Console.WriteLine (conversion);

Binary operators Two arguments One value on each side of the operator Operator + = add - = subtraction * = multiplication / = division % = remainder / operator Results is always an integer45/2 = 22 Remainder is dropped % operator Results is the number of digits in the remainder45/2 = 1 2 “goes into” times with a remainder of 1

PEMDAS is important. Use() to clarify operations order

counter = counter +1; = is used to assign values “Take the value of counter, add 1 to it, and assign the results to counter.” Add and assign operator += Adds the operand on the right to the operand on the left assigns the results to the operand on the left one step Subtract and assign operator -= Multiple and assign operator *= Divide and assign operator /=

Unary operators Used with only one operand Used to increase or decrease a variable by 1 Prefix increment operator ++ in front of the variable name Results calculated 1 st and then stored int someValue = 6; ++someValue; Postfix increment operator ++ at the end of the variable name Variable used 1 st and calculation is stored int someValue = 6; someValue++; Prefix decrement operator and postfix decrement operator subtracts 1 from the variable  Worsk like the increment operator

Turn to page 85 and answer Review Questions NEXT Turn to page 87 and answer Exercise #1, 3. NEXT Turn to page 76 – 77 and create YDI - Performing Arithmetic

Boolean Variable – T or F Comparison Operator Compares two items Returns a boolean value Turn to page 63, Table 2-4

Turn to page 86 and answer Review Questions NEXT Turn to page 87 and answer Exercise #2. NEXT Turn to page 77 – 78 and create YDI - Working With Boolean Variables 1,2.

Unifying Type Happens when arithmetic operands have different types C# automatically converts to a unifying type The unifying type will have the highest ‘type preference’ This is called implicit cast. Turn to page 64 – The Implicit Numeric Conversions

Turn to page 86 and answer Review Questions

char – holds any single character A number may be a character ‘ ‘ around the character char aNumValue = ‘9’; char aCharValue = ‘T’; Escape sequence – way to store nonprinting characters in the char variable. Inside ‘ ‘ Start with \ Turn to page 67, Table 2-5

string - holds a series of characters May compare string variables using == Returns a boolean value Turn to page 68, figure 2-11 and 2-12 String class Equals () Arguments within () Separate by a, Returns a boolean value Case sensitive Compare () Arguments within () Separate by a, Case sensitive Returns an integer 0 – equivalent +number – 1 st string > 2 nd string -number – 1 st string < 2 nd string Strings are compared left to right, letter by letter Turn to page 69, Figure 2-13 and 2-14.

Turn to page 86 and answer Review Questions 18, 19. NEXT Turn to page 87 and answer Exercise #4. NEXT Turn to page and create YDI - Using Escape Sequences

Constant value never changes Usually named with all UPPERCASE LETTERS const double TAX_RATE = 0.06; MUST assign a value when created

Interactive program Allows user input Console.ReadLine () myString = Console.ReadLine (); Using the string as a number Convert () Page 74 - Table 2-6

Turn to page and create YDI – Writing a Program that Accepts User Input NEXT Turn to page and create Ex. 6, 7, in the same program, 16. NEXT Go to the V drive and complete Debugging exercises a – d.