IB Computer Science Unit 1 – Introduction to Java Variables.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
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.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
Lecture 2: Variables and Expressions Yoni Fridman 6/29/01 6/29/01.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Introduction to C Programming
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
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;
CHAPTER 3: CORE PROGRAMMING ELEMENTS Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
CS0007: Introduction to Computer Programming Introduction to Arrays.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Slides by Evan Gallagher Fundamental Data Types 09/09/13.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Java Data Types Data types, variable declaration, and initialization.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Primitive Variables.
Primitive Data Types. Identifiers What word does it sound like?
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables.
Primitive Variables.
A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done.
Copyright Curt Hill Variables What are they? Why do we need them?
Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.
Documentation and Style. Documentation and Comments  Programs should be self-documenting.  Use meaningful variable names.  Use indentation and white.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
The Basics Input / Output, and Primitive Data Types.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Controlling Program Flow with Decision Structures.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
JAVA Practical Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants.
1 2. Program Construction in Java. 01 Java basics.
Making Interactive Programs with Visual Basic .NET
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
Bill Tucker Austin Community College COSC 1315
3 Introduction to Classes and Objects.
Chapter 2: Input, Processing, and Output
Introduction to Python Data Types and Variables
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Variables ,Data Types and Constants
Variables ICS2O.
Unit-1 Introduction to Java
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
elementary programming
Documentation and Style
CHAPTER FOUR VARIABLES AND CONSTANTS
Chapter 2: Input, Processing, and Output
In this class, we will cover:
Primitive Types and Expressions
Chapter 2 Primitive Data Types and Operations
Variables in C Topics Naming Variables Declaring Variables
Variables and Constants
Presentation transcript:

IB Computer Science Unit 1 – Introduction to Java Variables

Creating a Variable in Java The Assignment Operator Constant Variables Variable Properties

A Variable is a named container that stores a specific piece of data Every Variable has three main properties: Name Type Contents

Variable Properties Name Every variable must have a name A variable name should be descriptive (unlike variables in Mathematics which are usually single letters) Variable names should start with lower case letters and cannot contain spaces More on variable naming conventions later…

Variable Properties Type Every variable has a specific data type Since the programmer creates every variable for a purpose, he/she should know in advance what type of variable to create Basic variable types are called Primitive Data Types In Java, the Primitive Data Types are: IntegerCharacter DoubleByte FloatLong BooleanShort

Variable Properties Content Every variable stores some data, this is commonly referred to as the variables value, or contents. However, before we can store and use the contents of a variable, we must first create the variable in our program. This is known as declaring a variable.

Creating a Variable in Java To create a variable in Java, enter the keyword for its TYPE, followed by the variable name, and then a semi-colon. For example, if we want to declare a variable of type Integer that represents the users age, we would enter: int userAge; Note: At this point, the contents or value of the variable userAge is null.

The Assignment Operator The Assignment Operator is one of the most commonly used operators in Computer Science It is represented by the mathematical equals symbol: = However, this equals symbol is used differently than in Mathematics, which uses it exclusively to indicate that two sides of an equation are the same In Computer Science, the = is used to change the contents of a variable by assigning a new value to it

The Assignment Operator We need to be aware of two things: i)Which variable are we changing ii)What value are we changing the variable to The LEFT side of the = tells us the variable being changed The RIGHT side of the = tells us the new value being assigned Note: The right side of the = can be represented in a number of ways: a raw value, another variable, an equation, etc…

The Assignment Operator For example, if we wish to assign the value of 17 to the variable we created called userAge, we would enter the following code: userAge = 17; If we wish to assign the value of userAge to be retrieved from the user, we could use the JOptionPane to get the users input: userAge = JOptionPane.showInputDialog(“AGE:”); Or we could use a second variable: int userAge; int userInput; userInput = JOptionPane.showInputDialog(“AGE:”); userAge = userInput; Any problems with this??

Constant Variables There are many instances in a program where the value of a variable never changes In these cases, it is best practice to use a special kind of variable called a Constant. For example, a variable called dblPi might be assigned the value of dblPi = When we declare a constant variable, we set its value in our code and it does not change values throughout the time the program is running To distinguish constants from regular variables, we use all capital letters to name them dbl_INTEREST_RATE is an example of a possible name for a constant variable

Constant Variables Why use constant variables at all? To avoid hard-coded “magic” numbers which makes a program difficult to trace and debug So that changes to a constant value only need to be done in one place Programming style