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

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

} Each object in a Java program has an identifier (name) } This includes:

Review: Using Imported Code. What About the DrawingGizmo? Review: Classes and Object Instances. DrawingGizmo pencil; pencil = new DrawingGizmo();

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

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

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

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

Expressions and Casting

Review: Classes and Object Instances. Review: Creating an Object. Using Multiple Objects. DrawingGizmo pencil; pencil = new DrawingGizmo();

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

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

Using System.out.println()

Midterms Save the Dates!

Basic Class Diagrams. Class Diagrams, cont d. Class Diagrams, cont d. Car. Car. Car. What does the minus sign here mean?

Midterms Save the Dates!

Structures and Pointers

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

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

CS112 Lecture: Primitive Types, Operators, Strings

Information Science 1

CS Kangmei Yang. Page 1

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

Converting Collections to Arrays. A Bad Approach to Array Conversion. A Better Approach to Array Conversion. public Object[] toarray();

Slide 1 Side Effects Duration: 00:00:53 Advance mode: Auto

Midterms Save the Dates!

CS 221 Lecture. Tuesday, 13 September 2011

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

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

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

QUIZ. What is wrong with this code that uses default arguments?

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Computer Science II (20082) Week 1: Review and Inheritance

EXPRESSIONS AND ASSIGNMENT CITS1001

Robots. Byron Weber Becker. chapter 6

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Organisation. Assessment

Formatting & Style Examples

CSC 120 Computer Science for the Sciences. Week 1 Lecture 2. UofT St. George January 11, 2016

Data Conversion & Scanner Class

CS111: PROGRAMMING LANGUAGE II

CS 135 Lab Assignments Week 1

ECE 122 Engineering Problem Solving with Java

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

CT 229. Java Syntax 26/09/2006 CT229

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

Lecture Set 4: More About Methods and More About Operators

Information Science 1

VARIABLES AND TYPES CITS1001

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

CIS 110: Introduction to Computer Programming

CS112 Lecture: Working with Numbers

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Announcements. 1. Forms to return today after class:

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #23 Loops: Precedence of Operators

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

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

Lab 7 1 Due Thu., 6 Apr. 2017

Lecture 2: Python Arithmetic

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

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

Full file at

Lecture 2: Variables & Assignments

Datatypes, Variables, and Operations

MODULE 02: BASIC COMPUTATION IN JAVA

I. Variables and Data Type week 3

Lesson 3: Arithmetic & Casting. Pic 10A Ricardo Salazar

CS1 Lecture 3 Jan. 18, 2019

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Slide 1 CS 170 Java Programming 1 Real Numbers Duration: 00:00:54 Advance mode: Auto

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

CS Programming I: Primitives and Expressions

6.01, Spring Semester, 2008 Assignment 3, Issued: Tuesday, February 19 1

Values, Variables, Types & Arithmetic Expressions. Agenda

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Homework #3 CS2255 Fall 2012

Lecture Set 4: More About Methods and More About Operators

Operational Semantics. One-Slide Summary. Lecture Outline

Lecture 4 CSE July 1992

CS103L FALL 2017 UNIT 1: TYPES, VARIABLES,EXPRESSIONS,C++ BASICS

LECTURE 3 C++ Basics Part 2

Lecture 1. Course Overview Types & Expressions

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

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

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

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

DATA TYPES AND EXPRESSIONS

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Variables and literals

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

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

QUIZ: Generations of computer technology. Hardware:

CS61C : Machine Structures

CS102: Variables and Expressions

CS11 Java. Fall Lecture 1

CS 101: Computer Programming and Utilization. Abhiram Ranade

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Transcription:

Class #10: Understanding Primitives and Assignments Software Design I (CS 120): M. Allen, 19 Sep. 18 Java Arithmetic } Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = 2 + 5 / 2; 3. int x = 5 / 2.0 + 2;. double x = 5 / 2.0 + 2; 5. int x = (int) 5 / 2.0 + 2; 6. int x = (int) ( 5 / 2.0 + 2 ); 7. int x = 5 / (int) 2.0 + 2; 8. int x = 5 / (int) (2.0 + 2); 9. double x = 5 / 2 + 2; ERROR.5 ERROR 1.0 Wednesday, 19 Sep. 2018 Software Design I (CS 120) 2 Java Mixed-Type Arithmetic: Tricky Cases } When we mix types of numbers in a math expression, the final result will be of widest type (unless we cast) } However, the result we get can also depend upon when the switch is made from narrow types to wider ones } This depends upon operator precedence, too } Examples: 1. 5 / 2 * 2 2. 5.0 / 2 * 2 3. 5 / 2.0 * 2. 5 / 2 * 2.0 5.0 5.0.0 Here, since division 5 / 2 is performed first, and both 5 and 2 are of type int, we use int division Conversion to double type doesn t happen until the multiplication (*) Wednesday, 19 Sep. 2018 Software Design I (CS 120) 3 } Remember: the assignment operator (=) is not the same as the equals sign used in mathematics } An assignment instruction in Java: is a command, saying that we should store the value of the expression to the target } If the target is a variable identifier, then we can change the value of what it stores this way Wednesday, 19 Sep. 2018 Software Design I (CS 120) 1

} An assignment command in Java: is processed in a fixed order: 1. Evaluate expression, and compute its value, v 2. Assign value v to target } Since this is how the computer will process the command, we must do the very same when trying to understand what it will actually do } An assignment command in Java: is processed in a fixed order: 1. Evaluate expression, and compute its value, v 2. When finished, then assign value v to target } As an example, consider the command: double num = 5 / 2; which is processed as follows: 1. Evaluate expression 5 / 2: since the values are both integers, the arithmetic uses integer division, and computes the value 2 (and not 2.5) 2. Assign value 2 to num when complete, which saves result as a double } As a result, when this is complete, num is equal to 2.0 Wednesday, 19 Sep. 2018 Software Design I (CS 120) 5 Wednesday, 19 Sep. 2018 Software Design I (CS 120) 6 } Because of the right-hand, then left-hand order that Java does things, it is possible to have the same variable occur on both sides of the = operator } In normal arithmetic, this rarely happens, as something like the following is essentially meaningless, since there is no number n for which it is true that: n = n + 3 } In Java, however, such an expression is perfectly OK, as in the following: Keeping Track of Assignments } When processing Java code (and writing it), you need to make sure to work line by line, instruction by instruction int n1 = 6 + * 2; n1: 1 int n = 7; n = n + 3; [A] Set value of n to 7 int n2 = 7 / 2; n1 = n1 + n2; n2: 3 n1: 1 + 3 17 [C] Finally, do left-hand (assignment) step. When the code is complete, the value of n is now 10 [B] Do the right-hand (evaluation) step, calculating: n + 3 7 + 3 10 n2 = n1 + n2; n2: 17 + 3 20 Note that the 3rd line changes what n1 is. Thus, when we get to the th line, we use the new value of n1 to calculate n2. Wednesday, 19 Sep. 2018 Software Design I (CS 120) 7 Wednesday, 19 Sep. 2018 Software Design I (CS 120) 8 2

Shortcut Assignment Operators for Integers } Along with the basic arithmetic and assignment operators, int and double arithmetic can use some shortcuts to simplify our coding } Each of these does some arithmetic operation, and then does the assignment of the newly computed value to the associated identifier Shortcut Assignments Description Notation Example Equivalent* Subtract and assign -= x -= 3; x = x - 3; Add and assign += x += 3; x = x + 3; Multiply and assign *= x *= 3; x = x * 3; Divide and assign /= x /= 3; x = x / 3; Remainder and assign %= x %= 3; x = x % 3; Add one and assign ++ x++; x = x + 1; Subtract one and assign -- x--; x = x - 1; Using Shortcut Assignments int x = 10; x++; 11 x += 1; 12 x *= 2; 2 x -= 1; 23 x--; 22 x /= 5; (* See slides 11 12 for more detail.) Wednesday, 19 Sep. 2018 Software Design I (CS 120) 9 Wednesday, 19 Sep. 2018 Software Design I (CS 120) 10 One Complication: Mixed Arithmetic } When dealing with a single type, it is OK to think of these complex assignment operators as simply abbreviations (as in the table on slide 9) } However, these operators actually have a cast operation built into them } Each time we run one, it does its computation and then casts the result to guarantee it is the right type } That is, cast will be to the type of the identifier on the left-hand side } Thus, when we see something like this: x *= 2; } This is really an abbreviation for: Here, the cast makes no difference to the result, but it is still technically performed. x = (int)( x * 2 ); // produces value 6 One Complication: Mixed Arithmetic } Due to the built-in cast operation for each complex assignment operator, we can mix types when using them } Just need to remember that the cast happens at the end, just before the assignment operation itself } Thus, something like this is completely legal in Java: int x = 2; x *=.5; } This is allowed, since it is actually an abbreviation for: int x = 2; x = (int)( x *.5 ); // produces value 9 Wednesday, 19 Sep. 2018 Software Design I (CS 120) 11 Wednesday, 19 Sep. 2018 Software Design I (CS 120) 12 3

} Basic Java code operations happen one by one, line by line, in the order given by the programmer } Until we actually reach some instruction, none of what it does is visible to our program } Thus, any variables we want to use have to be declared and instantiated before we use them } Assignments work like other sequential code } Once an assignment is given to some identifier, all uses of that identifier after that point will use the assigned value } Nothing before the assignment is affected } The newly assigned value is used until another assignment occurs } Example: what are the values of x and y after the following runs? Oval o = new Oval( 10, 10, size, size ); int size = 300; This code will not compile. The variable size must be declared and set to the value we want before we can use it in the Oval() constructor. y = 2; x is 5; y is 5 (x changed) x is 5; y is 2 (y changed) Wednesday, 19 Sep. 2018 Software Design I (CS 120) 13 Wednesday, 19 Sep. 2018 Software Design I (CS 120) 1 } Once an assignment is given to some identifier, all uses of that identifier after that point will use the assigned value } Nothing before the assignment is affected } The newly assigned value is used until another assignment occurs } Example: what are the values of x and y after the following runs? y = x; x is 5; y is 5 x is 5; y is 5 (no swap!) Wednesday, 19 Sep. 2018 Software Design I (CS 120) 15 } Due to the sequential nature of Java, we sometimes have to get creative } As seen on the previous slide, if we want to swap two values, simply using two variable identifiers won t work } We need an extra variable to store temporary information if we are going to swap values of two variables int temp = x; y = temp; ; temp is 3 x is 5; y is 5; temp is 3 x is 5; y is 3; temp is 3 (swap!) Wednesday, 19 Sep. 2018 Software Design I (CS 120) 16

This Week } Meetings this week: } Tuesday Thursday: regular classroom } Friday: in the CS Lab (16 Wing) } Homework 02: due Friday, 21 September, 5:00 PM on D2L } Reading assignment: End of chapter 3 (3.10 3.18) } Next Tuesday, 25 September, by start of class } Quiz 02: Lectures 5 7 (to today) } Next Tuesday, 25 September, end of class } Office Hours: : Wing 210 } Monday/Wednesday/Friday, 12:00 PM 1:00 PM } Tuesday/Thursday, 1:30 PM 3:00 PM Wednesday, 19 Sep. 2018 Software Design I (CS 120) 17 5