Variables – the Key to Programming. ICS-3M1 - Mr. Martens - Variables Part 1 What is a Variable? A storage location that is given a “name” You can store.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Objective: Dealing with data in C++ Agenda: Notes Essay Help.
START DEFINITIONS values (3) N1 = (8, 1,-9) i N1 average N3,2 sum N2 = 0 temp N1 Do not guess or assume any values! Follow the values of the variables.
Chapter 2 Review Questions
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.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Lecture 2: Variables and Expressions Yoni Fridman 6/29/01 6/29/01.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get.
Agenda Review Compiling Review Data Types Integer Division Composition C++ Mathematical Functions User Input Reading: , 8.11 Homework #3.
Review Blocks of code {.. A bunch of ‘statements’; } Structured programming Learning Processing: Slides by Don Smith 1.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Computer Science 101 Introduction to Programming.
Java Data Types Data types, variable declaration, and initialization.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
EXPRESSIONS AND ASSIGNMENT CITS1001. Scope of this lecture Assignment statements Expressions 2.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
CS107 References and Arrays By Chris Pable Spring 2009.
Java Data Types Assignment and Simple Arithmetic.
1 Java Basics: Data Types, Variables, and Loops “ If debugging is the process of removing software bugs, then programming must be the process of putting.
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
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,
Lesson Two: Everything You Need to Know
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Primitive Data Types 1 In PowerPoint, point at the speaker icon, then click the "Play" button.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CRE Programming Club Class 2 (Import JJZ543 and Practice Your Typing!)
GCSE Computing: Programming GCSE Programming Remembering Python.
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
1 2. Program Construction in Java. 01 Java basics.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Chapter 2 Basic Computation
Introduction to Computer Science / Procedural – 67130
Variables and Primative Types
Computer Science 3 Hobart College
Other Kinds of Arrays Chapter 11
Chapter 2 Basic Computation
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
IDENTIFIERS CSC 111.
Coding Concepts (Sub- Programs)
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Coding Concepts (Data- Types)
B065: PROGRAMMING Variables 1.
Fundamental Programming
Introduction to Primitives
Introduction to Primitives
Primitive Types and Expressions
Unit 3: Variables in Java
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Data Types and Maths Programming Guides.
Asking Questions in BYOB Scratch.
Presentation transcript:

Variables – the Key to Programming

ICS-3M1 - Mr. Martens - Variables Part 1 What is a Variable? A storage location that is given a “name” You can store different things in it while the program is running The contents may “vary”, or change, while the program is running (hence the name – variable) age

ICS-3M1 - Mr. Martens - Variables Part 1 Variables in Java You can setup (or declare) different types of variables in Java, depending on the kind of information you want to store. Here are 4 main types of variables in Java TypeSizeUsed For int4 bytes+ or – integers double8 bytes+ or – values with decimals char2 bytesa single character / letter booleantrue or false

ICS-3M1 - Mr. Martens - Variables Part 1 Variables also called: “Primitive Data Types” These variables types – int, double, char, and boolean – along with 4 other types make up Java’s Primitive Data Types Recall Java is made up entirely of separate classes, including ones you write yourself The only part of Java that is NOT classes are the primitive data types They are called “primitive”, because they can only do 1 thing: store data

ICS-3M1 - Mr. Martens - Variables Part 1 Using Variables in a Program Here is a simple console application that determines the area of a rectangle Recall that for now, all code for a console application goes inside the main method:

ICS-3M1 - Mr. Martens - Variables Part 1 Running The Program After you save it, running it may look something like this: c.readInt() pauses the program to allow the user to enter a number. There is also a readDouble()

ICS-3M1 - Mr. Martens - Variables Part 1 Fun With Variables Often times, instead of REPLACING the value stored in a variable, a programmer wants to INCREASE or DECREASE the value that is already stored there For example, a game that keeps score – you want the program to increase a persons score when they earn points, or take away points if they do something wrong

ICS-3M1 - Mr. Martens - Variables Part 1 Fun With Variables The PRO way to increase or decrease the value of a variable is to use shortcuts: xInstead ofUseTo get 10x=x+5x+=515 20x=x-12x-=128 7x=x+1x++, or x+=18 6x=x-1x--, or x-=15 14x=x*1.13x*= ** **assume x is a double, not int

ICS-3M1 - Mr. Martens - Variables Part 1 Mixing it up with int and double This can be a problem. Since “int” can only hold whole numbers you have to be careful: Even though the answer is really 2.6, z can’t handle the “.6”, so it just becomes 2.

ICS-3M1 - Mr. Martens - Variables Part 1 Mixing it up with int and double But things can get even worse. This program won’t even run – you get an error: When you multiply an int with a double you get an answer that is a double “answer” does not have enough precision to store the real answer of 25.9

ICS-3M1 - Mr. Martens - Variables Part 1 You Must “cast” the variable If you put (int) in front of “d” you are converting it (called casting) to an integer This “dumbs it down” to a level that “answer” can understand Note the answer you get is now an integer that will simply ignore the.9 decimal part of the answer

ICS-3M1 - Mr. Martens - Variables Part 1 Casting the Other Way You may want to upgrade an “int” to a double sometimes too – when you need MORE precision to make a double calculation: In this example, “d” CAN handle a decimal answer, but the best x and y can do is send back an integer as an answer, so “d” has no choice but to make the answer 2.0

ICS-3M1 - Mr. Martens - Variables Part 1 Casting the Other Way If we CONVERT x and y to “double” values, then x/y WILL produce a decimal:

ICS-3M1 - Mr. Martens - Variables Part 1 Your Turn (Part 1): Temperature Converter Create a console application that converts a temperature from Fahrenheit to Celsius. First, we will PLAN the program using a flowchart Recall the new flowchart symbol -> the input/output symbol Remember, whenever you ASK for information from a user, or TELL the user something -> you flowchart a step like that using input/output

ICS-3M1 - Mr. Martens - Variables Part 1 Temperature Converter Flowchart Start Ask user for Fahrenheit Calculate Celsius C = 5.0/9.0*(F-32) Display Celsius answer for user Stop

ICS-3M1 - Mr. Martens - Variables Part 1 Your Turn (Part 2) Use a flowchart to map out the steps in a program that calculates the average on a report card, given the four marks earned in the different subjects. When you are finished, create the application in Java. Make sure the average is displayed with decimals.