Values, Variables, Types & Arithmetic Expressions. Agenda

Similar documents
Introduction to Programming EC-105. Lecture 2

Fundamentals of Programming CS-110. Lecture 2

Making Decisions. Agenda

CS313D: ADVANCED PROGRAMMING LANGUAGE

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Full file at

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Introduction to Programming

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Basics of Java Programming

COMP Primitive and Class Types. Yi Hong May 14, 2015

Visual C# Instructor s Manual Table of Contents

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

LECTURE 3 C++ Basics Part 2

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Datatypes, Variables, and Operations

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Chapter 2: Using Data

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Chapter 2 Elementary Programming

Programming Lecture 3

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Chapter 2: Using Data

Program Fundamentals

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Operators. Lecture 3 COP 3014 Spring January 16, 2018

CIS 110: Introduction to Computer Programming

Java Primer 1: Types, Classes and Operators

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Zheng-Liang Lu Java Programming 45 / 79

Programming with Java

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Basics of Java Programming variables, assignment, and input

1/16/12. CS 112 Introduction to Programming. A Foundation for Programming. (Spring 2012) Lecture #4: Built-in Types of Data. The Computer s View

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

3. Java - Language Constructs I

Full file at

Reserved Words and Identifiers

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Chapter 2 Exercises and Answers

Programming in C++ 5. Integral data types

BASIC ELEMENTS OF A COMPUTER PROGRAM

The Design of C: A Rational Reconstruction"

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

ECE 122 Engineering Problem Solving with Java

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

CS111: PROGRAMMING LANGUAGE II

Building Java Programs

CS110: PROGRAMMING LANGUAGE I

CS112 Lecture: Primitive Types, Operators, Strings

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Ex: If you use a program to record sales, you will want to remember data:

Declaration and Memory

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Python Numbers. Learning Outcomes 9/19/2012. CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 Discussion Sections 02-08, 16, 17

Building Java Programs

Lecture 1. Types, Expressions, & Variables

MODULE 02: BASIC COMPUTATION IN JAVA

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Computer System and programming in C

JAVA OPERATORS GENERAL

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

The Design of C: A Rational Reconstruction

ESc101 : Fundamental of Computing

Work relative to other classes

Chapter 2 Elementary Programming. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

SECTION II: LANGUAGE BASICS

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

CEN 414 Java Programming

Lecture 3. More About C

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Number Representation & Conversion

An Introduction to Processing

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

COMP-202: Foundations of Programming. Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Introduction to Programming Using Java (98-388)

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Chapter 2: Data and Expressions

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Chapter 2: Data and Expressions

Fall 2017 CISC124 9/16/2017

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

Motivations. Chapter 2: Elementary Programming 8/24/18. Introducing Programming with an Example. Trace a Program Execution. Trace a Program Execution

Building Java Programs

VARIABLES AND TYPES CITS1001

Information Science 1

The Design of C: A Rational Reconstruction" Jennifer Rexford!

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

COMP 110 Introduction to Programming. What did we discuss?

CSI33 Data Structures

Fundamental of Programming (C)

Chapter 2: Data and Expressions

Transcription:

Values, Variables, Types & Arithmetic Expressions Lecture 2 Object-Oriented Programming Agenda Inside of a Computer Value Variable Data Types in Java Literals Identifiers Type conversions Manipulating Variables Constants Reserved Words Manipulating Values Expression Operator Precedence and Associativity Type Conversions Objects Readings Lecture 2 Object-Oriented Programming 2 1

Lecture 2 Object-Oriented Programming 3 Inside of a Computer Lecture 2 Object-Oriented Programming 4 2

Simplistic View of a Computer Lecture 2 Object-Oriented Programming 5 Very Simplistic View of a Computer CPU Location 0 Location 1 Location 2 Location 3 Location 4 Location 5 Each location is 1 byte of memory 1 byte = 8 bits Each bit is an electric impulse carrying 1 or 0. This simplistic view is enough to explain the basic concepts of programming to students Lecture 2 Object-Oriented Programming 6 3

Value The only task a computer can do is arithmetic e.g. multiplying, dividing, subtracting, etc. Therefore, everything in the computer is represented as a value Numbers, letters, characters, etc are all represented as values Values could change depending on their nature. For example the temperature today is different from the temperature yesterday The number of cars inside Lahore is different then the number of cars Islamabad. Lecture 2 Object-Oriented Programming 7 Variable To store a value inside a computer a variable is used. A variable is a space in the memory to store a value. This space is reserved until the variable is required. Lecture 2 Object-Oriented Programming 8 4

What Makes a Variable Variable has three important characteristics: Type How much memory do a variable need. This information is determined by a type. Name How to differentiate a variable with another variable of the same type. Name refers to the memory location assigned to this variable. Value What is the value? The actual value contained by a variable. Lecture 2 Object-Oriented Programming 9 An Example of a Variable Type of the variable is integer (written as int in Java) int temperature = 35 A name of the variable An initial value of the variable Lecture 2 Object-Oriented Programming 10 5

int temperature = 35 Example of a Variable (Memory View) Locations 0 3 are collectively called as temperature 100011 is the binary equivalent of 35 00000000 Location 0 00000000 Location 1 00000000 Location 2 00100011 Location 3 Location 4 Location 5 Lecture 2 Object-Oriented Programming 11 Changing the Value of Variable Lets change the value of temperature. temperature = 45902 Locations 0 3 are collectively called as temperature 1011001101001110 is the binary equivalent of 45902 00000000 Location 0 00000000 Location 1 10110011 Location 2 01001110 Location 3 Location 4 Location 5 Lecture 2 Object-Oriented Programming 12 6

Type of a Variable Among other advantages a type binds the memory to a variable name. The type int is of 4 bytes in Java. Therefore, it can hold maximum of 2,147,483,647 value. It can also hold values in negative down to -2,147,483,648. Lecture 2 Object-Oriented Programming 13 Variable for Real Numbers int cannot hold a real value. Therefore, a type double is used to hold real values. Double takes 8 bytes of memory instead of 4 bytes of a double. Out of the 8 bytes in a double 4 bytes are used to hold the value before the decimal point and 4 bytes for the value after the decimal point. Lecture 2 Object-Oriented Programming 14 7

Relative Comparison of int and double int numpeople = 2; Reserves 32 bits (4 bytes) and sets the value stored in that space to 2. The name numpeople is associated with this space. double bill = 32.45; Reserves 64 bits (8 bytes) and sets the value stored in that space to 32.45. The name bill is associated with this space. Lecture 2 Object-Oriented Programming 15 Other Types in Java Lecture 2 Object-Oriented Programming 16 8

Identifier Used to denote names of variables, methods and classes. Sequence of characters (digits, letters, _, $) can t start with a digit. Case sensitive Select legal identifiers. Sum_, 48my, $$_10, all/clear, get-lost, my48 Lecture 2 Object-Oriented Programming 17 Literals Integer literals 2000, 0, -7 (int) 3000, 3000L (long) Decimal numbers (8) Octal (020) Hexadecimal (0x90) Floating point literals 9.8f, 9.8F (float) 56.67, 56.67d (double) Lecture 2 Object-Oriented Programming 18 9

Literals (continued) Boolean literals true / false Character literals A, \u0041 String literals Hello Lecture 2 Object-Oriented Programming 19 Type Conversion Java can perform conversion automatically int value can be assigned to long. Depends upon type compatibility Not all type conversions implicitly allowed. Cant assign a long value to int. Solution Casting Lecture 2 Object-Oriented Programming 20 10

Type Conversion (cont.) Widening conversion Narrow data types are converted into broad data type with out loss of information Both types are compatible. Numeric types are not compatible with Boolean and char Destination type is larger than source type. Example byte int int long Lecture 2 Object-Oriented Programming 21 Type Conversion (cont.) Narrowing conversion Broader data type is converted into narrower data type with loss of information Process is called casting (explicit type conversion) Target variable = (Target-type) Source variable byte b; int a=50; b=(byte)a; Truncation?????? Type conversion in expressions (f*b) + (i/c) (d*s)????????? Lecture 2 Object-Oriented Programming 22 11

Manipulating Variables Assignment Statement In Mathematics the value x = x + 1 is not possible why? In Java x = x +1 is possible because = is an assignment operator and not an equality operator. Assignment operator means that the contents of the right hand side is transferred to the memory location of the left hand side. Lecture 2 Object-Oriented Programming 23 Assignment Statement x = 5671 5671 is written at the memory location reserved for x Lecture 2 Object-Oriented Programming 24 12

Constants Constants are values which cannot be modified e.g. the value of Pi To declare a constant in Java, we write a keyword final before the variable type. final double pi = 3.14; Lecture 2 Object-Oriented Programming 25 Reserved Words Some names cannot be declared as variable names because they are reserved words in Java Lecture 2 Object-Oriented Programming 26 13

Manipulating Values Mathematical Operators Common mathematical operators are available in Java for manipulating values e.g. addition(+), subtraction(-), multiplication(*), division(/), and modulus (%). Java has many other operators also which we will study in due course. Lecture 2 Object-Oriented Programming 27 What is the Result of this Expression? What is the result of this arithmetic expression a) 7 b) 0.5 c) 13.0 d) 4 6 + 2 * 3 / 6 Lecture 2 Object-Oriented Programming 28 14

Arithmetic Expression Evaluation To evaluate an arithmetic expression two concepts needs to be understood Operator Precedence Operator precedence controls the order in which operations are performed Operator Associativity The associativity of an operator specifies the order in which operations of the same precedence are performed Lecture 2 Object-Oriented Programming 29 Operator Precedence and Associativity Operators Precedence and Associativity for Java is following 1. *, /, % Do all multiplications, divisions and remainders from left to right. 2. +, - Do additions and subtractions from left to right. Lecture 2 Object-Oriented Programming 30 15

Evaluating an Expression 6 + 2 * 3 / 6 Three operators are in this expression. However, * and / both have the same precedence and + has lower precedence then these two. * and / will be evaluated first but both have the same precedence level. Therefore, operator associativity will be used here to determine the first to get evaluated i.e. left to right. The right most sub expression will be evaluated followed by the next right one and so on. Lecture 2 Object-Oriented Programming 31 Primitive Data Types So far the variable types that we have studied are primitive data types. Primitive data types only have a memory space for storing values. However, Object-Oriented Programming is special because OOP has more variables then just primitive data types. Lecture 2 Object-Oriented Programming 32 16

Objects Objects are one step ahead of primitive data types. They contain values and operations both. e.g. A String object has a value as well as operations that could be performed on that value e.g. operations to find its size, operation to compare its size with another String etc. Lecture 2 Object-Oriented Programming 33 String Object In Java we could use string data type. A String in Java is declared as: String city = Lahore ; Thought Question Why the S in String is capitalized? Lecture 2 Object-Oriented Programming 34 17

Readings Book Name: Object-oriented Programming in JavaTM Textbook Author: Richard L. Halterman Content: Chapter 2 and 3 Lecture 2 Object-Oriented Programming 35 Acknowledgements While preparing this course I have greatly benefited from the material developed by the following people: Andy Van Dam (Brown University) Mark Sheldon (Wellesley College) Robert Sedgewick and Kevin Wayne (Princeton University) Mark Guzdial and Barbara Ericsson (Georgia Tech) Richard Halterman (Southern Adventist University) Lecture 2 Object-Oriented Programming 36 18