Midterms Save the Dates!

Similar documents
Midterms Save the Dates!

Midterms Save the Dates!

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

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

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

Midterms Save the Dates!

Midterms Save the Dates!

Midterms Save the Dates!

Midterms Save the Dates!

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

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

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

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

Midterms Save the Dates!

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

Expressions and Casting

Midterms Save the Dates!

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

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

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

Information Science 1

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

Declaration and Memory

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

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

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

Chapter 2: Using Data

CMPT 125: Lecture 3 Data and Expressions

! Rest of Chap 2 ! 2.3-4, ! Rest of Chap 4 ! ! Things that do not vary. ! unlike variables. ! will never change. !

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

Chapter 3: Operators, Expressions and Type Conversion

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

Lecture 3 Tao Wang 1

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

Programming Lecture 3

Operators. Lecture 3 COP 3014 Spring January 16, 2018

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

Information Science 1

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

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

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

Basics of Java Programming

LECTURE 3 C++ Basics Part 2

Full file at

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

CS Programming I: Primitives and Expressions

CIS 110: Introduction to Computer Programming

Primitive data, expressions, and variables

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

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

ECE 122 Engineering Problem Solving with Java

Building Java Programs

Operators. Java operators are classified into three categories:

CEN 414 Java Programming

CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso

EXPRESSIONS AND ASSIGNMENT CITS1001

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

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

Building Java Programs

Chapter 2. Elementary Programming

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

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

Lecture 2: Variables & Assignments

BM214E Object Oriented Programming Lecture 4

Program Fundamentals

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Unit 3. Operators. School of Science and Technology INTRODUCTION

COMP 110 Introduction to Programming. What did we discuss?

Topic 4 Expressions and variables

Chapter 2: Data and Expressions

Section we will not cover section 2.11 feel free to read it on your own

Introduction to Computer Science and Object-Oriented Programming

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

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

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

CS1 Recitation. Week 1

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

Java enum, casts, and others (Select portions of Chapters 4 & 5)

CIS133J. Working with Numbers in Java

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

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

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Operators in C. Staff Incharge: S.Sasirekha

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

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

Lecture 1. Course Overview Types & Expressions

MODULE 02: BASIC COMPUTATION IN JAVA

Survey #2. Variable Scope. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Scope Static.

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

CS102: Variables and Expressions

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

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

Survey #2. Programming Assignment 3. Final Exam. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu.

Transcription:

University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Arithmetic Operators Type Conversion Constants Readings Your textbook is Big Java (3rd Ed). This Week s Reading: Ch 2.1-2.5, Ch 4.1-4.2. Next Week: Ch 2.6-2.10, Finish Ch 4 Lecture 5 Some slides borrowed from Kurt Eiselt, Tamara Munzner, and Steve Wolfman. Some learning goals from Beth Simon. Labs and Tutorials Lab #2 is up now. It has a pre-lab! 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 is 5:30-6:30pm on February 10 (Tuesday) in Woodward IRC 2 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. PeerWise Study PeerWise is a system designed to facilitate students helping each other learn. This term s 111 sections are being studied to evaluate how effective PeerWise really is. You (Section 205) have been randomly selected as the control group : You won t use the PeerWise system. You won t be required (part of course grade) to complete some extra work on PeerWise. You have the right to opt in. You must email me by noon, January 21, to opt in. Email me your full name, student ID number, and say that you want to opt in. If you opt in, you will be given access to PeerWise, you will be required to do the extra work, and you will be marked as if you were in Section 206 (3% of mark based on PeerWise). 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. 1

Learning Goals By the end of class today you will be able to List the Java primitive 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 mathematical computations, using different operators, with correct precedence and associativity. it Explain how the same operator symbol can produce different results on different types. Describe when Java will automatically convert from one primitive type to another. Force Java to make a type conversion. Last Time We learned how to write assignment statements and trace execution through several statements. We started looking at the primitive data types in more detail. Now, let s look at the computation you can do with primitive data types 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 Review: Assignment Statements 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! 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) Six primitives for numbers fixed size, so finite capacity integer vs. floating point 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. 2

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) Recap: Assignment Statements 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 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 3

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) Operator Precedence What does this expression evaluate to? 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? 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 Operator Precedence What does this expression evaluate to? 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 Use parentheses to change precedence order or just clarify intent (7 + 2) * 5 7 + (2 * 5) What about this? 7 5 3 Associativity 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) 4

An old mystery public class Assoc int a; int b = 3; int c = 5; a = b + c; System.out.println( println("the answer is " + a); System.out.println("The answer is " + b + c); System.out.println("The answer is " + (b + c)); System.out.println(b + c + " is the answer."); System.out.println(b + (c + " is the answer.")); 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 Which of these should be legal? int shoes = 2; double socks = 1.75; double socks = 1; int shoes = 1.5; Which of these are legal? int shoes = 2; // yes double socks = 1.75; // yes double socks = 1; // yes int shoes = 1.5; // no Integers are subset of reals but reals are not subset of integers Java automatically converts int to double when needed (or smaller size to larger size) Casting Casting: force Java to convert from one type to another, even with information loss Converting from real to integer int shoes = (int) 1.5; Truncation: fractional part thrown away int shoes = (int) 1.75; int shoes = (int) 1.25; Rounding: must be done explicitly shoes = Math.round(1.99); // Feet.java Author: Tamara // What type of things can be put on feet? public class Feet int shoes = 2; int socks = (int) 1.75; System.out.println("shoes = " + shoes + " socks = " + socks); int toes = Math.round(1.99); System.out.println("toes = " + toes); What s wrong? Let s try it 5

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) Primary primitives are int and double three other integer types one other real type approx 1.7E308 (15 sig. digits) // Feet2.java Author: Tamara // What type of things can be put on feet? public class Feet2 int shoes = 2; int socks = (int) 1.75; System.out.println("shoes = " + shoes + " socks = " + socks); long toes = Math.round(1.99); System.out.println("toes = " + toes); Or Tell Java with a Type Cast // Feet2.java Author: Tamara // What type of things can be put on feet? public class Feet2 int shoes = 2; int socks = (int) 1.75; System.out.println("shoes = " + shoes + " socks = " + socks); int toes = (int) Math.round(1.99); System.out.println("toes = " + toes); Questions? Motivation for Constants Suppose we want to write a program to compute the distance in miles to various stars, for which we know the distance in lightyears. What Changes, What Doesn t? // Vroom.java Author: Tamara // Playing with constants public class Vroom double lightyears, milesaway; lightyears = 4.35; // to Alpha Centauri milesaway = lightyears * 186000 *60*60*24*365; milesaway " + milesaway); lightyears = 68; // to Aldebaran milesaway = lightyears * 186000 *60*60*24*365; milesaway " + milesaway); 6

Constants Things that do not vary unlike variables will never change Syntax: final typename variablename; final typename variablename = value; Constant names in all upper case Java convention, not compiler/syntax requirement Programming With Constants double lightyears, milesaway; final int LIGHTSPEED = 186000; final int SECONDS_PER_YEAR = 60*60*24*365; lightyears = 4.35; // to Alpha Centauri lightyears = 68; // to Aldebaran Programming With Constants double lightyears, milesaway; final int LIGHTSPEED = 186000; final int SECONDS_PER_YEAR = 60*60*24*365; final double ALPHACENT_DIST = 4.35; // to AlphaCentauri final double ALDEBARAN_DIST = 68; // to Aldebaran lightyears = ALPHACENT_DIST; lightyears = ALDEBARAN_DIST; Avoiding Magic Numbers magic numbers : numeric constants directly in code almost always bad idea! hard to understand code hard to make changes typos possible use constants instead Questions? 7