Midterms Save the Dates!

Similar documents
Midterms Save the Dates!

Midterms Save the Dates!

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

PeerWise Study. Midterms Save the Dates! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Constants Using Classes and Objects

Using Classes and Objects. Lecture 7. Midterms Save the Dates! Extra Credit Survey

Midterms Save the Dates!

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

Midterms Save the Dates!

Midterms Save the Dates!

Midterms Save the Dates!

Basics of Java Programming

Midterms Save the Dates!

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

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Expressions and Casting

Declaration and Memory

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Midterms Save the Dates!

Programming Lecture 3

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

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

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Program Fundamentals

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

CS102: Variables and Expressions

Chapter 3: Operators, Expressions and Type Conversion

CIS 110: Introduction to Computer Programming

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

LECTURE 3 C++ Basics Part 2

Full file at

Full file at

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

CMPT 125: Lecture 3 Data and Expressions

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Topic 4 Expressions and variables

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

COMP 110 Introduction to Programming. What did we discuss?

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Chapter 2: Data and Expressions

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

CS Programming I: Primitives and Expressions

EXPRESSIONS AND ASSIGNMENT CITS1001

Getting started with Java

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

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

Data types Expressions Variables Assignment. COMP1400 Week 2

CS 106 Introduction to Computer Science I

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

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

CS1 Lecture 3 Jan. 18, 2019

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

Accelerating Information Technology Innovation

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Lesson 02 Working with Data Types. MIT 31043: VISUAL PROGRAMMING By: S. Sabraz Nawaz Senior Lecturer in MIT

JAVA OPERATORS GENERAL

! This week: Chapter 6 all ( ) ! Formal parameter: in declaration of class. ! Actual parameter: passed in when method is called

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Unit 3. Operators. School of Science and Technology INTRODUCTION

Building Java Programs

Zheng-Liang Lu Java Programming 45 / 79

Object-Oriented Programming

Bits, Words, and Integers

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Visual C# Instructor s Manual Table of Contents

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Building Java Programs

Chapter 2 Working with Data Types and Operators

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

CS260 Intro to Java & Android 03.Java Language Basics

Building Java Programs

Introduction to C# Applications

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Lecture Set 4: More About Methods and More About Operators

Lecture 3 Tao Wang 1

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

(I m not printing out these notes! Take your own.)

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Lesson 02 Working with Data Types MIT 31043, Visual Programming By: S. Sabraz Nawaz

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

Programming with Java

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

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

CS112 Lecture: Working with Numbers

C: How to Program. Week /Mar/05

CS1 Lecture 3 Jan. 22, 2018

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

Chapter 2: Data and Expressions

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

Transcription:

University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Primitive Data Types Arithmetic Operators Readings Your textbook is Big Java (3rd Ed). This Week s Reading: Ch 2.1-2.5, Ch 4.1-4.2. Lecture 4 Some slides borrowed from Kurt Eiselt and Steve Wolfman. Some learning goals from Beth Simon. Labs and Tutorials Labs and tutorials start this week! You MUST be enrolled in a lab section. If the lab sections you need are full, go to ICICS Room 201 and see a Computer Science undergrad advisor! Midterms Save the Dates! Midterm #1 still being scheduled (early February) Midterm #2 is 6-7pm on March 11 (Wednesday) in Woodward IRC 2 You must notify me well in advance (I will specify deadlines as the dates approach) if you have an unavoidable conflict with a midterm. Learning Goals By the end of the next several lectures you will be able to Apply with basic competence simple programming constructs such as sequential execution, variable typing and declaration, naming, algebraic operations, operation precedence. Create programs which translate explicit English problem statements (an algorithm) into short series of sequential Java instructions. Learning Goals By the end of class today you will be able to Declare Java variables of type int. Recognize and write simple assignment statements. Trace the execution of a sequence of simple assignment statements. List the Java primitive iti types, describe what they re used for, and roughly what kinds of values you can store in them. Declare Java variables with different types. Write Java expressions for basic mathematical computations. 1

Last Time We ran and experimented with a simple Java program. We saw how Java executes statements one-by-one, in order. We learned the rules for identifiers. We used identifiers to declare and name variables. Review: Variables Variable: name for location in memory where data is stored A little like variables in algebra class payrate, hoursworked, a, b, and c are all variables Variable names begin with lower case letters Java convention, not compiler/syntax requirement Variable may be name of single byte in memory or may refer to a block of many bytes More about this later Review: Data Types Java requires that we tell it what kind of data it is working with For every variable, we have to declare a data type Java language provides eight primitive data types i.e. simple, fundamental For more complicated things, can use data types created by others provided to us through the Java libraries that we invent More soon - for now, let s stay with the primitives Review: Variable Declaration variable declaration is instruction to compiler reserve block of main memory large enough to store data type specified in declaration variable name is specified by identifier syntax: typename variablename; We want a, b, and c to be integers. Here s how we do it... Assignment //***************************************** // Test3.java Author: Kurt // // Our third use of variables! //***************************************** public class Test3 { public static void main (String[] args) { int a; int b; int c; b = 3; // these c = 5; // are a = b + c; // assignment statements System.out.println ("The answer is " + a); } } Assignment statement assigns value to variable Assignment statement is identifier followed by assignment operator (=) followed by expression followed by semicolon (;) b = 3; c = 8; a = b + c; weekly_pay = pay_rate * hours_worked; Note that = is not a test for equality! 2

Java first computes value on right side Then assigns value to variable given on left side x = 4 + 7; // what s in x? Old value will be overwritten if variable was assigned before x = 2 + 1; // what s in x now? Note that = is not a test for equality! Assignment is a new concept in CS versus math. Fetch-decode-execute Dynamic. Something happens. x = x + 1; Read = as gets (short for gets assigned the value ). Here s an occasional point of confusion: Here s an occasional point of confusion: Find out! Experiments are easy to do in CS Here s an occasional point of confusion: Variable values on left of = are clobbered Variable values on right of = are unchanged copy of value assigned to a also assigned to b but that doesn t change value assigned to a Here s an occasional point of confusion: Memory locations a and b are distinct value of a at that point assigned to b changing a later does not affect previous copy 3

a: b: a: 7 b: a: 7 b: 7 a: 7 b: 7 a: 7 8 b: 7 a: 7 8 b: 7 4

Or, draw little boxes with values in them. a = 7; b = a; a=?, b=? Or, draw little boxes with values in them. a = 7; b = a; a=?, b=? a=7, b=? Questions? Or, draw little boxes with values in them. a = 7; b = a; a=?, b=? a=7, b=? a=7, b=7 Recap: Variable Declaration variable declaration is instruction to compiler reserve block of main memory large enough to store data type specified in declaration variable name is specified by identifier syntax: typename variablename; int a; Review: Data Types For every variable, we have to declare a data type Java language provides eight primitive data types i.e. simple, fundamental For more complicated things, can use data types created by others provided to us through the Java libraries that we invent More soon - today, let s stay with the primitives 5

Primitive Data Types: Non-numeric Character Type named char Java uses the Unicode character set so each char occupies 2 bytes of memory. Boolean Type named boolean Variables of type boolean have only two valid values true and false Often represents whether particular condition is true Ignore these for now. We ll see them again later. Primitive Data Types: Numbers Type Size Min Max byte 1 byte -128 127 short 2 bytes -32,768 32,767 int 4 bytes -2,147,483,648 2,147,483,647 long 8 bytes float 4 bytes double 8 bytes -9,223,372,036,854,775,808 9,223,372,036,854,775,807 approx -3.4E38 (7 sig.digits) approx 3.4E38 (7 sig.digits) approx -1.7E308 (15 sig. digits) approx 1.7E308 (15 sig. digits) Six primitives for numbers fixed size, so finite capacity integer vs. floating point Data Type Sizes Type Size Min Max int 4 bytes -2,147,483,648 2,147,483,647 double 8 bytes approx -1.7E308 (15 sig. digits) fixed size, so finite capacity Address 5802 5803 5804 5805 5806 5807 Data 10000101 10110101 11110001 00010100 approx 1.7E308 (15 sig. digits) one integer significant digits 42 4.2 42000000.000042 Floating Point Numbers Floating Point Numbers significant digits 42 = 4.2 x 10 = 4.2 x 10 1 4.2 = 4.2 x 1 = 4.2 x 10 0 42000000 = 4.2 x 10000000 = 4.2 x 10 7.000042 = 4.2 x.00001 = 4.2 x 10-5 significant digits Floating Point Numbers 42 = 4.2 x 10 = 4.2 x 10 1 4.2 = 4.2 x 1 = 4.2 x 10 0 42000000 = 4.2 x 10000000 = 4.2 x 10 7.000042 = 4.2 x.00001 = 4.2 x 10-5 only need to remember nonzero digits where to put the decimal point floats around when multiply/divide by 10 (all in binary inside computer) enormous range, but limited precision 6

Primitive Data Types: Numbers Type Size Min Max byte 1 byte -128 127 short 2 bytes -32,768 32,767 int 4 bytes -2,147,483,648 2,147,483,647 long 8 bytes -9,223,372,036,854,775,808 9,223,372,036,854,775,807 float 4 bytes approx -3.4E38 (7 sig.digits) approx 3.4E38 (7 sig.digits) double 8 bytes approx -1.7E308 (15 sig. digits) approx 1.7E308 (15 sig. digits) Primary primitives are int and double Just worry about those for now Don t need to memorize exact limits, but know roughly what the limits are. int integer Data Types: Int and Double 4 bytes, about -2 billion to 2 billion double real number (double-precision floating point) 8 bytes, 15 sig figs, humongous range (Number systems briefly explained in Appendix K) Variable Declaration Examples person s age in years height of mountain to nearest meter length of bacterium in centimeters number of pets at home Recap: Assignment statement assigns value to variable Assignment statement is identifier followed by assignment operator (=) followed by expression followed by semicolon (;) b = 3; c = 8; a = b + c; weekly_pay = pay_rate * hours_worked; Note that = is not a test for equality! Recap: Assignment statement assigns value to variable Assignment statement is identifier followed by assignment operator (=) followed by expression followed by semicolon (;) b = 3; c = 8; a = b + c; weekly_pay = pay_rate * hours_worked; Note that = is not a test for equality! Expressions expression is combination of one or more operators and operands operator examples: +, *, /,... operand examples: numbers, variables,... usually performs a calculation don t have to be arithmetic but often are examples 3 7 + 2 7 + 2 * 5 (7 + 2) * 5 7

Expressions expression is combination of one or more operators and operands operator examples: +, *, /,... operand examples: numbers, variables,... usually performs a calculation don t have to be arithmetic but often are examples salary + bonus 3.14 * diameter + - * addition Arithmetic Operators Works on int, double, byte, short, long, subtraction Works on all numeric types, too multiplication Didn t have times sign on keyboard Works on all numeric types, too / More Arithmetic Operators division Integer division on integer types! Example: 13 / 5 results in 2 Just like before you learned fractions Normal division on double and float % remainder (aka mod ) Only works on integer types Example: 13 % 5 results in 3 (Aside: Operator Overloading) Hmm the same symbol / can do different things for ints and doubles: 13/5 results in 2 (the type is int) 13.0/5.0 results in 2.6 (the type is double) Similar for other operators, e.g., + 13+5 is 18 (18 is an int) 13.0+5.0 is 18.0 (18.0 is a double) 13 + 5 is 135 ( 135 is a String)! Operator Precedence What does this expression evaluate to? 7 + 2 * 5 Operator Precedence What does this expression evaluate to? 7 + 2 * 5 Multiplication has higher operator precedence than addition (just like in algebra) precedence operator operation 1 higher + - unary plus and minus 2 * / % multiply, divide, remainder 3 lower + - add, subtract 8

Operator Precedence What does this expression evaluate to? 7 + 2 * 5 Multiplication has higher operator precedence than addition (just like in algebra) precedence operator operation What about this? 7 5 3 Associativity 1 higher + - unary plus and minus 2 * / % multiply, divide, remainder 3 lower + - add, subtract Use parentheses to change precedence order or just clarify intent (7 + 2) * 5 7 + (2 * 5) Associativity What about this? 7 5 3 The result is -1 (7 5) 3, not 7 (5 3) Arithmetic operators of same precedence are left associative Matters for some operators; doesn t for others Use parentheses to be clear! (Operators and precedence in Appendix E) Shorthand: Variable Declaration and Assignment Variable declaration reserves a chunk of memory and gives it a name. Assignment sticks a value into the variable. Can do both in one statement: typename variablename; variablename = value; typename variablename = value; can declare and assign in one step Questions? 9