SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Similar documents
SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

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

Building Java Programs

Building Java Programs

Building Java Programs

Full file at

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

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

SSEA Computer Science: CS106A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

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

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

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

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

CIS133J. Working with Numbers in Java

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

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

Introduction to Computer Programming

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

Operators. Java operators are classified into three categories:

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Add Subtract Multiply Divide

Topic 4 Expressions and variables

by Pearson Education, Inc. All Rights Reserved. 2

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Chapter 2: Using Data

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

Chapter 3: Operators, Expressions and Type Conversion

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

CIS 110: Introduction to Computer Programming

cs1114 REVIEW of details test closed laptop period

MODULE 02: BASIC COMPUTATION IN JAVA

Lecture 2: Operations and Data Types

Introduction to TURING

CPS122 Lecture: From Python to Java

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

LECTURE 3 C++ Basics Part 2

Declaration and Memory

CS 115 Lecture 4. More Python; testing software. Neil Moore

Chapter 2, Part III Arithmetic Operators and Decision Making

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

Building Java Programs

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

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

COMP 202 Java in one week

CS 106A, Lecture 5 Booleans and Control Flow

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Operators. Lecture 3 COP 3014 Spring January 16, 2018

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

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Values, Variables, Types & Arithmetic Expressions. Agenda

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

QUIZ: What value is stored in a after this

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

Chapter 2: Overview of C++

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

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

Computer System and programming in C

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

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

Reserved Words and Identifiers

COMP 202 Java in one week

1/11/2010 Topic 2: Introduction to Programming 1 1

Lecture 4: Conditionals

Programming Lecture 3

CS102: Variables and Expressions

COMP 202. Java in one week

Building Java Programs Chapter 2

More Programming Constructs -- Introduction

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

UNIT- 3 Introduction to C++

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

CS 106 Introduction to Computer Science I

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

CSEN 102 Introduction to Computer Science

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

Lecture 3. Input, Output and Data Types

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

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

An overview about DroidBasic For Android

Chapter 4: Basic C Operators

Lecture 1. Types, Expressions, & Variables

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

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

Example 2: Simplify each of the following. Round your answer to the nearest hundredth. a

Lecture 3 Operators MIT AITI

Arithmetic and IO. 25 August 2017

CMPT 125: Lecture 3 Data and Expressions

DATA TYPES AND EXPRESSIONS

Arithmetic Expressions in C

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

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

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

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

CS112 Lecture: Primitive Types, Operators, Strings

Information Science 1

CSE 142, Summer 2014

Transcription:

SSEA Computer Science: Track A Dr. Cynthia Lee Lecturer in Computer Science Stanford

Topics for today Introduce Java programming language Assignment and type casting Expressions Operator precedence Code example: Hamilton Lyrics If we have time: Two new kinds of operators: RELATIONAL operators (>, <, ==,!=, >=, <=) LOGICAL operators (&&,,!) A new data type: boolean

Review: Assignment Assignment uses =, but it is not an algebraic equation = means, "store the value at right in the variable at left" The right side expression is evaluated first, and then its result is stored in the variable at left. What happens here? (A) x x+2 int x = 3; x = x + 2; //??? (B) x 2 (C) x 3 (D) Other or causes an error

Assignment Assignment uses = symbol that you ve seen in math class, but it is not an algebraic equation in the sense that you ve seen in math class = means, "store the value at right in the variable at left" The right side expression is evaluated first, and then its result is stored in the variable at left. What happens here? int x = 3; x = x + 2; //??? x

Compiler errors with variables A variable can't be used until it is assigned a value. int x; println(x); // Error: x has no value You may not declare the same variable twice. int x; int x; // ERROR: x already exists int y = 3; int y = 5; // Error: y already exists How can this code be fixed?

Operators and Expressions Type int

Expressions, operators expression: A value or operation that computes a value. Examples: 1 + 4 * 5 (7 + 2) * 6 / 3 The simplest expression is a literal value, like 42. A complex expression can use operators and parentheses. operator: Combines multiple values or expressions. + - * / % add, subtract/negate, multiply, divide, modulus (remainder) As a program runs, its expressions are evaluated. 1 + 1 evaluates to 2. (How would we print the output 1 + 1?)

Integer division and remainder When we divide integers, the quotient an integer 14 / 4 is 3, not 3.5. (Java ALWAYS rounds down.) 3 4 52 4 ) 14 10 ) 45 27 ) 1425 12 40 135 2 5 75 54 21 To access the remainder portion, use % operator: 14 % 4 is 2 45 % 10 is 5 1425 / 27 is...? (your turn)

Operators and Expressions Type double

Real numbers: double Examples: 6.022, -42.0, 2.143e17 Placing.0 or. after an integer makes it a double. The operators + - * / % () all still work with double. / produces an exact* answer: 15.0 / 2.0 is 7.5 Precedence is the same: () before * / % before + - * to a certain number of decimal places/significant digits

Quirks of real numbers Some double values print poorly (too many digits). double result = 1.0 / 3.0; println(result); // 0.3333333333333 The computer represents double in an imprecise way Because only a finite amount of space in the box println(0.1 + 0.2); Instead of 0.3, the output is 0.30000000000000004

Type casting type cast: A conversion from one type to another. To promote an int into a double to get exact division from / To truncate a double from a real number to an integer Syntax: (type) expression Examples: double result = (double) 19 / 5; // 3.8 int result2 = (int) result; // 3 int x = (int) (10.0 * 10.0 * 10.0); // 1000

More about type casting Type casting has high precedence and only casts the term immediately next to it. double x = (double) 1 + 1 / 2; // 1.0 double y = 1 + (double) 1 / 2; // 1.5 You can use parentheses to force evaluation order. double average = (double) (a + b + c) / 3; A conversion to double can be achieved in other ways. Common trick: Multiplying by 1.0 double average = 1.0 * (a + b + c) / 3;

Example: Hamilton Lyrics Generator Good style

Relational and Logical Operators The operators we learned previously were primarily arithmetic operators

Relational operators Operator Meaning Example Value == equals 1 + 1 == 2 true!= does not equal 3.2!= 2.5 true < less than 10 < 5 false > greater than 10 > 5 true <= less than or equal to 126 <= 100 false >= greater than or equal to 5.0 >= 5.0 true if statements and loops use logical tests. for (int i = 0; i < 10; i++) {... if (age >= 40) {... These are boolean expressions. Boolean is a logical data type.

New data type: boolean Only two possible values: true and false Actually write true and false (without the quotes) in your code: Named after George Boole, a pioneer in the area of formal logic I m assuming not actually an angry person, just had resting angry face

Misuse of if What's wrong with the following code? int percent = readint("what percentage did you earn? "); if (percent >= 90) { println("you got an A!"); } if (percent >= 80) { println("you got a B!"); } if (percent >= 70) { println("you got a C!"); } if (percent >= 60) { println("you got a D!"); } if (percent < 60) { println("you got an NP!"); }...

Solution: if/else-if Ends with else: exactly 1 path taken. Ends with if: 0-1 paths taken. if (test) { statements; } else if (test) { statements; } else if (test) { statements; } Example: if (place == 1) { println("gold medal!!"); } else if (place == 2) { println("silver medal!"); } else if (place == 3) { println("bronze medal."); }

Unnecessary if The following code is unnecessarily verbose and redundant: if (x < 0) { println("x is negative"); } else if (x >= 0) { println("x is non-negative"); } The second test is unnecessary and can be removed: if (x < 0) { println("x is negative"); } else { println("x is non-negative"); }

Logical operators Tests can be combined using logical operators: Operator Description Example Result && and (2 == 3) && (-1 < 5) false or (2 == 3) (-1 < 5) true! not!(2 == 3) true "Truth tables" for each, used with logical tests p and q: p q p && q p q true true true true true false false true false true false true false false false false p!p true false false true

Or / in code Or in code means one or the other or both In English, sometimes we use or to include the both case, and sometimes we don t mean it to include the both case: Inclusive or includes the both case: You will be in great shape if you go running or biking daily of course you will also be in great shape if you do both! Exclusive or excludes the both case: Your lunch special comes with soup or salad. usually restaurants mean you have to choose only one or other. p q p && q p q true true true true true false false true false true false true false false false false