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.

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
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.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
CMT Programming Software Applications
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
1 Fundamental Data Types. 2 Outline  Primitive Data Types  Variable declaration  Numbers and Constants  Arithmetic Operators  Arithmetic Operator.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
Primitive Data Types byte, short, int, long float, double char boolean Are all primitive data types. Primitive data types always start with a small letter.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Java Variables and Expressions CSC160 Professor Pepper (presentation adapted from Dr. Siegfried)
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Introduction to Computer Programming CSC171 – 001 Getting Started – Chapters 1 & 2 Professor Pepper (presentation adapted from Dr. Siegfried)
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
String Escape Sequences
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
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.
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
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.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
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.
Primitive Variables.
Copyright Curt Hill Variables What are they? Why do we need them?
Chapter 2 Variables.
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.
CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
A Sample Program #include using namespace std; int main(void) { cout
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 2 Variables.
Chapter 2 Basic Computation
Elementary Programming
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
IDENTIFIERS CSC 111.
Chapter 2 Edited by JJ Shepherd
Chapter 2 Variables.
Fundamentals 2.
Expressions and Assignment
elementary programming
Introduction to Primitives
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Introduction to Primitives
In this class, we will cover:
Primitive Types and Expressions
Unit 3: Variables in Java
Names of variables, functions, classes
Chapter 2 Variables.
Presentation transcript:

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 in x –monopoly square – holds different monopoly pieces

Data types type: A category or set of data values. –Examples: integer, real number, string. Internally, the computer stores all data as 0s and 1s. –examples: "hi"

Java's primitive types primitive types: Java's built-in simple data types for numbers, text characters, and logic. –Java has eight primitive types. –Types that are not primitive are called object types. Four primitive types we will use: NameDescriptionExamples –int integers (whole numbers) 42, -3, 0, –double real numbers 3.14, -0.25, 9.4e3 –char single text characters 'a', 'X', '?', '\n' –boolean logical values true, false

Types typekindmemoryrange byteinteger1 byte-128 to 127 shortinteger2 bytes to intinteger4 bytes to longinteger8 bytes to floatfloating point 4 bytes± x to ± x doublefloating point 8 bytes± x to ± x charsingle character 2 bytesall Unicode characters booleantrue or false1 bit

Create a variable create a variable –In Java: ; example: int myIntVar; –declare with correct type: int, double, char, boolean, (not capitalized) –declare Object variables with types such as : String, Scanner, Random, (capitalized) You try: –Create a program that creates the following variables: int myQuantity double myDollars boolean answer char oneLetter String myName

Change a variable change variable contents: –In Java: = ; example: myIntVar = 5 ;. –Equal sign means set equal to –Left side gets put into right side You try – set those variables = to something myQuantity = 1 myDollars = 3.3 answer = true oneLetter = ‘a’ myName = “pepper” Now, print those variables with: –System.out.println(myQuantity + myDollars + answer + oneLetter + myName) What happens when you put single or double quotes where there were none, or take away the quotes?

Use variables in a real program On Paper: Average Pay attention to your steps

Writing the Average program Specification – write a program which can find the average of three numbers. Let’s list the steps that our program must perform to do this: Add up these values Divide the sum by the number of values Print the result Each of these steps will be a different statement.

Flow chart Done in Gliffy Show how

Writing the Average program Add up these values Divide the sum by the number of values Print the result sum = ; an assignment statement

Assignment Statements Assignment statements take the form: variable = expression Memory location where the value is stored Combination of constants and variables

Expressions Expressions combine values using one of several operations. The operations being used is indicated by the operator: +Addition -Subtraction *Multiplication /Division Examples: * value x / y

Writing the Average program 1 sum = ; Divide the sum by the number of values Print the result average = sum / 3; Names that describe what the values represent

Writing the Average program 2 sum = average = sum / 3; Print the result System.out.println(″The average is ″ + average); The output method variable name

Writing the Average program 3 public static void main(String[] args) { sum = ; average = sum / 3; System.out.println("The average is " + average); } We still need to add a declare our variables. This tells the computer what they are.

Writing the Average program 4 public class Average3 { public static void main(String[] args) { int sum, average; sum = ; average = sum / 3; System.out.println("The average is " + average); } Tells the computer that sum and average are integers

Writing the Average program 5 public class Average3a { public static void main(String[] args) { int sum; int average; sum = ; average = sum / 3; System.out.println("The average is " + average); } We could also write this as two separate declarations.

You try Add 1 to the average and then print it again. average = average + 1

Variables and Identifiers Variables have names – we call these names identifiers. Identifiers identify various elements of a program (so far the only such element are the variables. Some identifiers are standard (such as System )

Identifier Rules An identifier must begin with a letter or an underscore _ Java is case sensitive upper case (capital) or lower case letters are considered different characters. Average, average and AVERAGE are three different identifiers. Numbers can also appear after the first character. Identifiers can be as long as you want but names that are too long usually are too cumbersome. Identifiers cannot be reserved words (special words like int, main, etc.)

Spelling Conventions Name constants Variables start lower case Classes uppercase Word boundaries upper case (numberOfPods)

Some Illegal Identifiers timeAndAHalf & is not allowed time&ahalf fourTimesFive * is not allowed four*five times2 or twoTimes Cannot begin with a number 2times myAge Blanks are not allowed my age Suggested Identifier ReasonIllegal Identifier

Assignment int number1 = 33; double number2; number2 = number1; byte  short  int  long  float  double char

Dividing int / int  int (even if you assign it to a double) float / int  float int / float  float Solution: Cast it ans = n / (double) m

Math Operators & PEMDAS + add - subtract * multiply - division % remainder Example: base + (rate * hours)

Fancy Math variable = variable op (expression) count = count + 1 count = count + (6 / 2 * a + 3) variable op = expression count += 1 count += (6 / 2 * a + 3) Example: int count = 1; count += 2; The value of count is now 3

More Fancy Math Increment ++ Decrment – ++n adds 1 before executing n++ adds 1 after executing Example:

Constants Constant doesn’t change Why use a variable if  massive changes later  show meaning  avoid Hard coding public static final int MAX_PEOPLE = 20; Capitalize by convention only

Summary A variable holds one value Variables have certain types Literals of different types are entered differently (i.e. quotes for String) The system keeps variable values until you change them. Constants are a special kind of variable – no changing