CS1S467 GUI Programming Lecture 3 Variables 2.

Slides:



Advertisements
Similar presentations
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Advertisements

Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
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.
Chapter 2: Using Data.
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.
Copyright © – Curt Hill Types What they do.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
1.2 Primitive Data Types and Variables
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
B065: PROGRAMMING Variables 2. Starter  What is wrong with the following piece of code: Option Explicit Off Dim num1 as string Dim num2 as integer num1.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
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.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Chapter 2 Variables.
Chapter 2 Basic Computation
Chapter 2: Introduction to C++
Java Coding – part 2 David Davenport Computer Eng. Dept.,
Tokens in C Keywords Identifiers Constants
Numbers in a Computer Unsigned integers Signed magnitude
Revision Lecture
Variables and Primative Types
Other Kinds of Arrays Chapter 11
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Introduction to C++
Chapter 2 Basic Computation
CS1S467 GUI Programming Lecture 2 Variables 1.
Java LESSON 1 Variables.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Variables Title slide variables.
Fundamentals of Data Representation
Data Types and Variables Part A – Introduction Numeric Data Types
Storing Negative Integers
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Introduction to Java, and DrJava
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
C++ Programming Lecture 3 C++ Basics – Part I
B065: PROGRAMMING Variables 1.
Variables & Basic Types
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Summary of what we learned yesterday
SE1H421 Procedural Programming LECTURE 3 Variables (2)
Primitive Types and Expressions
SE1H421 Procedural Programming LECTURE 2 Variables (1)
Unit 3: Variables in Java
Introduction to Java, and DrJava
B065: PROGRAMMING Variables 2.
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Variables and Constants
Programming Fundamental-1
Presentation transcript:

CS1S467 GUI Programming Lecture 3 Variables 2

Lecture Slides Lecture Slides are unsuitable for learning how to program! You just get an illusion of learning! Programming is a craft. You have to do it to learn it.

Today Recap of lab session Review of variable basics (declaration, initialisation) Type casting More on variable limits Chars Strings Constants

Review of last Lab Session (Demo on Laptop) Code layout & comments Properties of Controls Events of Controls Looking ahead to today: The strange behaviour of int and float when adding/subtracting or dividing/multiplying

Today Recap of lab session Review of variable basics (declaration, initialisation) Type casting More on variable limits Chars Strings Constants

Last Lecture: How To…. declare a variable int anIntOfMine; float aFloatOfMine; initialise a variable anIntOfMine = 5; aFloatOfMine = 5.25f; declare and initialise a variable in one line declare (&initialise) multiple variables in one line int anIntOfMine = 5; float aFloatOfMine = 5.25f; copy one variable into another one int anIntOfMine = 5, anotherInt = 3; float aFloatOfMine = 5.25f, anotherFloat; calculate with variables anIntOfMine = anotherInt; anotherFloat = aFloatOfMine; anIntOfMine = 5 * anotherInt ; anotherFloat = aFloatOfMine + 7.0f - 3.2f;

Variable Names Programmers can use any name for a variable, provided these rules are observed: the name must be one word only, no spaces helloWorld hello World always start with a letter not a number counter17 17YearsOld avoid special signs such as £ ^ & % + etc. twoAndFour two&Four Try to start with a lowercase letter, then "uppercasing" goodMorning Goodmorning

Check your knowledge now: Spot the mistakes / problems in the code: int counter = 2.57f; float hello=2.1f, you, there; there = hello + you; int mine=3; float theirs=4.5f; mine = mine + theirs; myFloat = 2.7f; 2.57 is a float not an int you not initialised Mixing different types. Could lead to problems type decl. float is missing

Check your knowledge now: Declare two int variables called noOfStudents and roomsAvailable and initialise them to 100 and 3 respectively. Figure out how many students fit into a room and store the result in a float called studsPerRoom. int noOfStudents=100, roomsAvailable= 3; float studsPerRoom; studsPerRoom = noOfStudents / roomsAvailable; What is the content of studsPerRoom now? 33.0 (not 33.3333…)

Today Recap of lab session Review of variable basics (declaration, initialisation) Type casting More on variable limits Chars Strings Constants

Converting float int Sometimes it is necessary to temporarily convert ints to floats and vice versa. This is done by a process called 'type casting' int var1; float var2 = 3.99f; var1 = (int) var2; // (int) type casting here float int: stuff after the 'dot' gets chopped of ! var1 is now 3, NOT 4 !! It doesn't get rounded.

Type Casting Can be Useful: Declare a double called penceAmount and initialise it to the value 255. Declare two ints called pounds and pence. Split penceAmount into Pounds and Pence and store the result in the respective variables. double penceAmount=255.0; int pounds, pence; pounds = (int) (penceAmount / 100.0); pence = (int) penceAmount - pounds * 100; textBox1.Text = "Pounds=" + pounds + " Pence=" + pence;

The Modulus Operator An alternative way to split a pence amount into its Pound and Pence components makes use of the modulus operator %. The modulo operator calculates the rest after division: 10%3 is 1, 20%4 is 0 The last line, however, will create an error message by the compiler double penceAmount=255.0; int pounds, pence; pounds = (int) penceAmount /100; pence = penceAmount % 100;

Why the Error Message? (in the last line: pence = penceAmount %100;) C provides implicit ("automatic", "built-in") type casts (e.g. float to int) in normal assignments. This is different from Java, C++, C# (and many other languages) who require explicit ("deliberate") type casting. Hence: float x=10.0f; int y; y=x; float x=10f; int y; y= (int) x; pence = (int) penceAmount %100;

Therefore….. Try to get used to explicit type casting Most programming languages use it. Makes it clear that you are aware of the potential loss of precision

Today Recap of lab session Review of variable basics (declaration, initialisation) Type casting More on variable limits Chars Strings Constants

More on Variable Limits This is how the number 14 is represented in binary in a 'short‘ integer: 16 bit 1 byte 1 byte 1 1 bit 16,384 128 16 8 4 2 1 214 27 22 21 20 8 + 4 + 2 = 14

More on Variable Limits If we add 1 to the 14, the resulting 15 is stored like this: 16 bit 1 byte 1 byte 1 16,384 128 16 8 4 2 1 214 27 22 21 20 8 + 4 + 2 +1 = 15

More on Variable Limits Adding 1 to the 15 results in 16. All '1' bits flip to '0' and bit 4 flips to '1' 16 bit 1 byte 1 byte 1 1 16,384 128 16 8 4 2 1 214 27 22 21 20 16 + 0 + 0 + 0 + 0 = 16 8 + 4 + 2 +1 = 15

More on Variable Limits And this is the highest positive number that can be stored in a 'short' 16 bit 1 byte 1 byte Really? What about this one? 1 16,384 128 16 8 4 2 1 214 27 22 21 20 16,384 + 8,192 + . . . . + 2 + 1 = 32,767

More on Variable Limits What happens if we add one more?

More on Variable Limits Remember? Adding '1': All '1' bits flip to '0' and the next highest bit flips to '1' 16 bit 1 byte 1 byte 1 1 The Sign Bit 16,384 128 16 8 4 2 1 -32,768 + 0 + 0 + ……. + 0 + 0 = - 32,768 214 27 22 21 20 16,384 + 8,192 + . . . . + 2 + 1 = 32,767

More on Variable Limits Therefore -1 is represented as: 16 bit 1 byte 1 byte 1 The Sign Bit 16,384 128 16 8 4 2 1 -32,768 + 16,384 + ……. + 2 + 1 = -1 214 27 22 21 20

More on Variable Limits And -2 is : 16 bit 1 byte 1 byte 1 The Sign Bit 16,384 128 16 8 4 2 1 -32,768 + 16,384 + ……. + 2 + 0 = -2 214 27 22 21 20

The 'unsigned' Type Modifier If we declare a short like this: ushort x = 40000; 16 bit This is a normal bit now 1 byte 1 byte 1 32,768 4,096 2,048 1,024 64 4 2 1 215 212 211 210 26 22 21 20 32,768 + 4,096 + 2,048 + 1,024 + 64 = 40,000

The 'unsigned' Type Modifier Likewise: ushort x = 40000; uint y = ….; ulong z= ….; Now, what about float and double ?

Limits of 'float' & 'double' Floating point numbers are stored in two parts: mantissa and exponent 32 bit 'float' Mantissa (23 bits) Exponent (8 bits) Sign of mantissa (1 bit) From: www.atkinson.yorku.ca/~pkhaiter/Lectures1000/Lecture1000-5.ppt‎

Limits of 'float' & 'double' Example: - 0.0009876 1 -0.9876 x 10-3 Sign of Mantissa Mantissa Exponent From: www.atkinson.yorku.ca/~pkhaiter/Lectures1000/Lecture1000-5.ppt‎

Limits of 'float' & 'double' This process is complex. Details here Full knowledge not required but understanding is. Rough guide: Floating point numbers may be 100% accurate but usually they are not (you never know). The more bits (i.e. doubles), the more likely it is that the number is accurate (size matters….)

Integral vs. Floating Point Integral Types Floating Point Types e.g. short, int, uint, long,... float, double precise rounded fast to compute slow to compute "sparse" "all numbers" limited range

Today Recap of lab session Review of variable basics (declaration, initialisation) Type casting More on variable limits Chars Strings Constants

A Strange Type: char char is schizophrenic (split personality) single quotes!

The ASCII* Table *ASCII: American Standard Code for Information Interchange

The Extended ASCII* Table *ASCII: American Standard Code for Information Interchange

Some special chars Some chars have a special meaning and can control screen output:

Some special chars (cont.) Another char control characters is:

Today Recap of lab session Review of variable basics (declaration, initialisation) Type casting More on variable limits Chars Strings Constants

Strings Strings are "lots of characters together"

Using Strings

Using Strings Strings are OBJECTS in C# and have Properties and Methods. Properties are just that while Methods can do things. For example:

Today Recap of lab session Review of variable basics (declaration, initialisation) Type casting More on variable limits Chars Strings Constants

Constants Constants are variables that can't change any more once they have been initialised. Constants are declared and initialised like normal variables - but there is something else: the keyword 'const' Look! All uppercase !

Why Constants ? Imagine a CAD package that uses 'inches' to output measurements. At the beginning of the code you would write: const float MEAS_UNITS = 2.54; // 2.54 cm = 1 inch In the program 'MEAS_UNITS' is now used 400 times for calculations. 1. MEAS_UNITS can not be changed by accident in the code 2. If you want to sell the program to France where 'cm‘ is used, only 1 line needs to change.

Summary Today we have learned … about the limits of variable capacity the internal bit representation of numbers about unsigned variable types (a bit) about the mantissa & exponent format how to use chars how use strings how to declare and initialise constants

Revision This was a lot (again) ! AT HOME Revise, then program, program, program,…

END OF LECTURE 3