Computers, Variables and Types Engineering 1D04, Teaching Session 2.

Slides:



Advertisements
Similar presentations
C++ Basics Variables, Identifiers, Assignments, Input/Output.
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
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.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
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.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
CMT Programming Software Applications
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CS0004: Introduction to Programming Variables – Strings.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Chapter 2: Using Data.
CPS120: Introduction to Computer Science
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Arrays, Type Casting and Constants Engineering 1D04, Teaching Session 5.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Records Engineering 1D04, Teaching Session 11. © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng1 Records  So far we've seen a number of different.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
File Input and Output (I/O) Engineering 1D04, Teaching Session 7.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Applications Development
Introduction to Java Java Translation Program Structure
Copyright © – Curt Hill Types What they do.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Objects and Classes Continued Engineering 1D04, Teaching Session 10.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
CPS120: Introduction to Computer Science Variables and Constants.
Objects and Classes Engineering 1D04, Teaching Session 9.
Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Bill Tucker Austin Community College COSC 1315
A variable is a name for a value stored in memory.
Chapter 2 Variables.
Two Dimensional Arrays and Complex Conditions
Building Java Programs
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Primitive Data, Variables, Loops (Maybe)
IDENTIFIERS CSC 111.
Building Java Programs
Chapter 2 Variables.
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Primitive Types and Expressions
Unit 3: Variables in Java
Building Java Programs Chapter 2
Chapter 2 Variables.
Building Java Programs
Building Java Programs
Variables and Constants
Presentation transcript:

Computers, Variables and Types Engineering 1D04, Teaching Session 2

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng1 Typical Computer

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng2 An Abstract View of Computers

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng3 An Abstract View of Computers

Programming Languages  Algorithms are communicated to the computer using a programming language  Evolution of programming languages:  Machine language:  Assembly code: mov, and, push, add, jmp, etc  High level languages: FORTRAN, Pascal, Basic, C, C++, Java, Visual C#, Visual Basic, Haskell, Ocaml, …  Conversion of high level language to machine code is via a compiler © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng4

5 Memory “boxes” Abstract View of Memory

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng6 Introduction to Variables  When we create algorithms that solve problems, we typically model the problem we are solving - we create an abstraction of the real problem and then use properties of that abstract model to get a solution that (hopefully) solves the original problem.  We often create mathematical variables to represent physical entities.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng7 Mathematical Variables velocity of car is v starts at velocity v 0 t represents time acceleration of car is a t v = v 0 + at so, at this point in time, what is the car’s velocity?

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng8 Introduction to Program Variables  Computer programs implement algorithms and we need to calculate values that represent physical entities or simply data items. We therefore need to manipulate and store data.  A program variable is a named memory location that can be used to store data.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng9 myVariable myString Birthday 65 Introduction to Program Variables

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng10 Introduction to Program Variables  Variables can have different forms depending on the type of data you wish to store in them.  Int variables store integer numbers.  Float variables (double) store real numbers.  Char variables store a single character.  String variables store text (multiple characters).  Bool variables store true or false.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng11 int myInteger; double myDouble; Introduction to Program Variables  Variables must be declared before they can be used.  Declaring a variable assigns a name to a previously unused memory location. program segments will be shown in these “page” templates

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng12 int myInteger; double myDouble; myInteger Introduction to Program Variables

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng13 myInteger int myInteger; double myDouble; myDouble Introduction to Program Variables

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng14 int myInteger, myOtherInteger, myThirdInt; double myDouble; Introduction to Program Variables  Multiple variables of the same type can be declared at the same time by separating them with a comma.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng15  Variables can be assigned a value with the assignment (=) operator. myInteger = 5; myDouble = 4.5; myInteger = 25; Variable Constant / Literal Introduction to Program Variables

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng16 myInteger myDouble myInteger = 5; myDouble = 4.5; myInteger = 25; sequence 5 Introduction to Program Variables

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng17 myInteger myDouble sequence 5 myInteger = 5; myDouble = 4.5; myInteger = 25; 4.5 Introduction to Program Variables

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng18 myInteger myDouble sequence 4.5 myInteger = 5; myDouble = 4.5; myInteger = 25; 25 The value at any location can change from time to time. Introduction to Program Variables

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng19 Introduction to Program Variables  Remember the definition of a variable:  A named memory location used to store data.  The assignment operator (=) can be thought of as a left arrow, placing a value into a memory location.  myInteger  5; (put 5 in the myInteger box)  But we don’t have  on our keyboard, so we use the equals sign instead.  myInteger = 5;

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng20 Introduction to Program Variables  In our example, myInteger holds different values at different times.  Sequence is important here!  The current contents of a variable is what was most recently assigned to it.  Assignment always occurs from right to left  myInteger = 5; Storing 5 into “myInteger” makes sense.  5 = myInteger; Storing “myInteger” into the value of 5 makes no sense.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng21 myInteger = myOtherInteger; What does this do? Introduction to Program Variables

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng22 myInteger = myOtherInteger; Introduction to Program Variables myInteger 123 myOtherInteger

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng23 myInteger = 20; Introduction to Program Variables  When not being assigned to, a variable stands in for the value that it contains.  In this case, if myOtherInteger contained 20 then this statement would be equivalent to the following: myInteger = myOtherInteger;

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng24 myInteger = myInteger + 5; What does this do? Introduction to Program Variables

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng25 Introduction to Program Variables  This adds 5 to the current contents of myInteger and stores the result back into myInteger.  If myInteger used to contain 20, it now contains 25.  Repeating this statement will result in repeatedly adding 5 to myInteger. myInteger = myInteger + 5;

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng26 double a, b, c, d, e, f, g; a = 4; b = 25; c = 30; d = 60; e = 10; f = b; f = f + c; f = f + d; f = f + e; g = f / a; What does this program do? Example Program 1

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng27 Example Program 1  It’s not intuitively obvious what this program does.  It can be improved easily by giving the variables useful names.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng28 double b, c, d, e, sum, average; int numTerms; numTerms = 4; b = 25; c = 30; d = 60; e = 10; sum = b; sum = sum + c; sum = sum + d; sum = sum + e; average = sum / numTerms; Example Program 1

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng29 Program Variables  There are a number of variable naming conventions.  In 1D04, the following convention will be adopted:  All first words are lowercase.  Subsequent words have their first letter capitalized. fred, theVariable, bobLovesMe

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng30 Program Variables  Variables cannot start with a number.  Extremely short or exceptionally long variable names can be hard to work with. Use an appropriate length.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng31 Program Variables  There is still room for improvement in the code that calculates the average. What can you do to make it “better”?

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng32 Program Variables  There is still room for improvement in the code that calculates the average. In this case, we can simply add the values of b, c, d and e together without doing it sequentially.  While we do this we need to bear in mind that:  Rules for the mathematical order of operations apply to operations on variables.  We should use brackets anywhere our intention might be unclear.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng33 double b, c, d, e, average; int numTerms; b = 25; c = 30; d = 60; e = 10; numTerms = 4; average = (b+c+d+e) / numTerms; “Better” Version

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng34 Program Variables  Lastly, variables can be initialized when they are declared.  Does that mean those variables will behave like constants?  No  Their values can still be changed later in the program.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng35 double b = 25, c = 30, d = 60, e = 10, average; int numTerms = 4; average = (b+c+d+e) / numTerms; Initialized Version

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng36 Program Variables  Notice the use of quotes in characters and strings.  The type of box that a literal is given is defined by its quotes.  Single quotes get a character box. ‘x’ ‘#’  Double quotes get a string box. “x”  Double quotes are not single quotes typed twice.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng37 char name1, name2, name3; string s1, s2; name1 = 'B'; name2 = 'abc'; name3 = "abc"; s1 = "B"; s2 = "abc";  Examples: 66 name1name2 works fine s1 s2 box not big enough name3 box not the right kind works fine Program Variables

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng38  Booleans can contain only the values true or false.  They will become more useful as the course progresses. Program Variables

Types  The type of a variable provides very important information to the programmer and to the compiler  The number of students in a tutorial is an integer, not a real number or double – you cannot have students in a class!  Some operations are only valid over certain types  Type conversions when mixing types © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng39

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng40  Remember “Click Me” from lab 1? void …(…) { textBox1.Text = "You Clicked 'Click Me'"; } Type Conversion You Clicked 'Click Me'

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng41 void …(…) { int myInteger = 10; textBox1.Text = myInteger; } Type Conversion  What happens when we run the method with an integer?

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng42 Type Conversion

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng43 Type Conversion  The variables are in different size/kind of boxes.  We need to do an explicit conversion from integer to string.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng44 void …(…) { int myInteger = 10; textBox1.Text = Convert.ToString(myInteger); } Example Program 2

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng45 Type Conversion  The Convert methods are found in the System library.  In order to use them as demonstrated in this tutorial the following line must be at the beginning of your source file:  using System;  Visual Studio does this by default for you.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng46 Example Program 3  It’s quite natural to want a numerical type along with a descriptive string in a textbox.  How do we do that?

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng47 private void ClickMe_Click(object sender, EventArgs e) { int sum; sum = ; textBox1.Text = "The Answer to Life, The Universe, and Everything is ??"; } We want the value of sum! Example Program 3

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng48 private void ClickMe_Click(object sender, EventArgs e) { int sum; sum = ; textBox1.Text = "The Answer to Life, The Universe, and Everything is sum"; } Example Program 3

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng49 private void button1_Click(object sender, EventArgs e){ int sum; sum = ; textBox1.Text = "The Answer to Life, The Universe, and Everything is sum"; } Example Program 3 Click Me

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng50 Example Program 3  We need to convert sum into a string and concatenate it with “The Answer to …”.  Convert.ToString(intValue) produces a string consisting of characters representing the value of intValue.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng51 private void ClickMe_Click(object sender, EventArgs e) { int sum; sum = ; textBox1.Text = "The Answer to Life, The Universe, and Everything is " + Convert.ToString(sum); } Example Program 3

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng52 private void button1_Click(object sender, EventArgs e){ int sum; sum = ; textBox1.Text = "The Answer to Life, The Universe, and Everything is " + Convert.ToString(sum); } Example Program 3 Click Me

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng53 Example Program 3  Actually, this is a special case! We did not need to convert the integer to a string in this particular case.  Why? Because the concatenation operator (+) implicitly converts numerical values to strings if necessary.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng54 private void ClickMe_Click(object sender, EventArgs e) { int sum; sum = ; textBox1.Text = "The Answer to Life, The Universe, and Everything is "+ sum; } Example Program 3

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng55 Concatenation  Multiple things can be concatenated at once.  All must be either strings or implicitly convertible to strings by the concatenation operator.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng56 string myVeryLongString; int myAge = 22; string myBirthday = "January 3, 1984"; myVeryLongString = " I was born on " + myBirthday + ". I am now " + myAge; Example Program 4

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng57 string myVeryLongString; int myAge = 22; string myBirthday = "January 3, 1984"; myVeryLongString = " I was born on " + myBirthday + ". I am now " + myAge; String Variable of type string String Integer which is implicitly convertible to a string. Example Program 4

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng58 Inputting Data  Suppose we want to change the inputs used to calculate the answer to Life, the Universe, and Everything.  We could read two numbers from two textboxes, add them, and then write the answer to a textbox. Click Me

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng59 private void ClickMe_Click(object sender, EventArgs e) { int sum; sum = inputBox1.Text + inputBox2.Text; outputBox.Text = "The Answer to Life, The Universe, and Everything is " + sum; } Example Program 5

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng60 Inputting Data

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng61 Inputting Data  Textboxes deal only with Strings.  We need our inputs to be integers.  An explicit conversion is required.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng62 private void ClickMe_Click(object sender, EventArgs e) { int sum; sum = Convert.ToInt32(inputBox1.Text) + Convert.ToInt32(inputBox2.Text); outputBox.Text = "The Answer to Life, The Universe, and Everything is " + sum; } Example Program 6

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng63 private void ClickMe_Click(object sender, EventArgs e) { int sum; sum = Convert.ToInt32(inputBox1.Text) + Convert.ToInt32(inputBox2.Text); outputBox.Text = "The Answer to Life, The Universe, and Everything is " + sum; } Example Program 6 Click Me

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng64 Points to Ponder  Program variables  Variable typing  Type conversions