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

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

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.
Class 7.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
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.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
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.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
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
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.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
Objectives You should be able to describe: Data Types
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
CPS120: Introduction to Computer Science
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CSC 107 – Programming For Science. The Week’s Goal.
Primitive Variables.
Chapter 2 Variables.
COMP Primitive and Class Types Yi Hong May 14, 2015.
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.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
© A+ Computer Science - A reference variable stores the memory address of an object. Monster fred = new Monster(); Monster sally.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
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.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2 Variables.
Chapter 2 Basic Computation
BASIC ELEMENTS OF A COMPUTER PROGRAM
Building Java Programs
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ITEC113 Algorithms and Programming Techniques
Object Oriented Programming
Multiple variables can be created in one declaration
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
IDENTIFIERS CSC 111.
Building Java Programs Chapter 2
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Building Java Programs
Chapter 2 Variables.
Chapter 2: Java Fundamentals
Building Java Programs Chapter 2
Expressions and Assignment
Building Java Programs
Introduction to Primitives
Introduction to Primitives
Primitive Types and Expressions
Building Java Programs Chapter 2
Chapter 2 Variables.
Presentation transcript:

Introduction to Computing Concepts Note Set 7

Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic

Memory in a computer Computer uses a few levels of memory to hold data that it is using in processing RAM

Data Collection of raw facts or figures Perform some processing on data in order to obtain information ▫ Example Data: Test Grades ▫ Example Information: Class Average, Standard Deviation, etc. Data (and info.) usually has an intrinsic Data Type ▫ Numerical Data (test grades)? String Data (names)? 99 Computer Science Bob, Sam, Jane

Data in Java Need some way to store data/info while program is running/processing For this, we use variables ▫ Allow storage a piece of data  Will become “larger” or “smarter” as the semester progresses ▫ Each has a particular data type

Data Types To efficiently use the computer’s memory, each variable has a data type ▫ Behind the scenes, tells the compiler and JVM how to use memory to store a pieces of data  E.g. A number is stored differently than an integer Java is a Strongly Typed Language ▫ The languages enforces rules as to what you can do with the data in variables ▫ E.g. Won’t necessarily let you do this? “Bob” + 27 Doesn’t necessarily make sense to add a string and number…

2 Categories of Data Types Primitive Data Types ▫ Holds a single data item such as integer, character, or true/false value Reference Data Types ▫ Data type whose value is actually a memory address

Primitive Data Types Data TypeDescriptionExample Values int Stores positive and negative whole numbers in the range of 2 31 to long Stores positive and negative whole numbers in the range of approx -9*10 18 to 9* *10 10 short Stores positive and negative whole numbers in the range from to float stores numbers with up to 6 or 7 decimals (Single Precision) double Stores numbers with up to 14 or 15 decimal places (Double Precision) boolean Stores data in only one of two states: True or False true, false byte Stores positive and negative whole numbers in the range -128 to char stores any one of the 65,436 single characters from the Unicode character set ‘a’ ‘;’ ‘M’ ‘.’

Declaring Variables declaration statement ▫ Line of code that identifies, or declares, the type and names the identifier or variable. General Form: SomeDataType SomeIdentifierName; int grade; long atomsInBody; double Average; float price;

Declaring Variables – Identifier Names Rules for names of Identifiers 1.must start with letter, underscore or dollar sign ($) 2.subsequent characters can be letters, underscores, dollar signs, or digits (0 – 9) 3.Any Length (but be reasonable) 4.Name should be meaningful (no variables named x, y, z) 5.Remember – Java is Case Sensitive 6.Can’t declare 2 variables with the same name in the same method 7.Can’t have a variable that is a reserved word or the same name as a method General Form: SomeDataType SomeIdentifierName; Where have we seen these rules before?

In Code public class TestVariables { public static void main (String [] args) { int grade1 = 100; int grade2, grade3; float average; //Other stuff here } Notice that you can declare the variable and provide an initial value all in one statement.

check point Declare a variable to hold your GPA Declare a variable to hold your age (in years)

Assignment Statements Used to store a new value in a variable uses the assignment operator (=) userAge = 21; boolean flag newUserAge = userAge; sum = grade1 + grade2 + grade; float average = sum / 3.0;

Assignment Statements = is right associative ▫ Means always stores what’s on the right side in the variable on the left side ▫ Will evaluate expression on right first (step 1), then perform the assignment int grade1 = 98; int grade2 = 100; int sum = grade1 + grade2; Step 1 Evaluates to 198 Step 1 Evaluates to 198 Step 2 Stores 198 in sum

Variables in Output Can use a variable in an output statement Can concatenate a string literal and variable with plus sign userAge = 21; System.out.println(userAge); System.out.println(“Age is “ + userAge);

Important Arithmetic Operators in Java OperationSymbolExampleResult Addition+3+47 Subtraction-4-31 Multiplication*4 * 312 Division/5.0/ Integer Division /5/22 Modular Division %20 % 3 2 (only the remainder is stored) Cast (dataType) literal or identifier (int) (truncates decimal)

Important Arithmetic Operators in Java OperationSymbolExampleResult Addition+3+47 Subtraction-4-3 Multiplication*4 * 312 Division/5.0/ Integer Division/5/22 Modular Division%20 % 3 2 (only the remainder is stored) Cast (dataType) literal or identifier (int) (truncates decimal) Might Be New Math Ideas

check point What is the result of? ▫ 3 / 2 ▫ 2 / 3 ▫ 5 % 4 ▫ 4 % 5

Arithmetic Operators The order of operator precedence is a predetermined order that defines the sequence in which operators are evaluated in an expression Addition, subtraction, multiplication, and division can manipulate any numeric data type When Java performs math on mixed data types, the result is always the larger data type Casts allow programmers to force a conversion from one primitive type to another

Numeric Expressions Any Expression that can be evaluated to a number Can include operators, literal values, variables, method calls Only primitive data types may participate in numeric expressions ▫ Any method calls we use in a numeric expression will return a primitive data type. A literal value and a variable (or 2 variables) must be separated by an arithmetic operator

What if multiple operators in 1 expression? Follow the order of Precedence ▫ Unless parentheses dictate otherwise, evaluate expressions in the following order  Multiplication and/or Division  Integer Division  Modular Division  Addition and/or subtraction ▫ When multiple operations of the same kind are present, Java performs the left to right 18 / 3 – * 2

Let’s Evaluate 18 / 3 – * 2 6 – * 2 6 –

Parentheses in Expressions Change the order of operations when found in an expression, the part inside the parentheses is evaluated first. Then the rest of the expression is evalutated If they are nested, then the inner-most set of parentheses is evaluated first

Let’s Evaluate 18 / (3 – 2) + 4 * 2 18 / * *

check point – Your Turn! 18 / 3 * 2 + (3 * (2 + 5))