CIS 110: Introduction to Computer Programming

Similar documents
CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Basic Operations jgrasp debugger Writing Programs & Checkstyle

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

Chapter 2: Using Data

COMP 110 Introduction to Programming. What did we discuss?

Full file at

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

Chapter 6 Primitive types

CS Programming I: Primitives and Expressions

Getting started with Java

CMPT 125: Lecture 3 Data and Expressions

Chapter 2: Data and Expressions

Introduction to Programming Using Java (98-388)

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

Building Java Programs

1007 Imperative Programming Part II

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

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

ECE 122 Engineering Problem Solving with Java

Zheng-Liang Lu Java Programming 45 / 79

Chapter 2: Data and Expressions

Information Science 1

Lecture Set 2: Starting Java

Building Java Programs

Information Science 1

Lecture Set 2: Starting Java

CS 112 Introduction to Programming

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

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

CSE 142, Summer 2015

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

JAVA OPERATORS GENERAL

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

CPS122 Lecture: From Python to Java

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Syntax of Variable Declarations. Variables Usages as Expressions. Self-assignment Statements. Assignment. Scoping and Naming Rules

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

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

CSE 142, Summer 2014

More Programming Constructs -- Introduction

Operators. Java operators are classified into three categories:

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

Chapter 2: Using Data

CS 106 Introduction to Computer Science I

Values, Variables, Types & Arithmetic Expressions. Agenda

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

Chapter 2: Using Data

Programming in C++ 5. Integral data types

Basics of Java Programming

Operators & Expressions

Operators in C. Staff Incharge: S.Sasirekha

PROGRAMMING FUNDAMENTALS

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

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Program Fundamentals

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 2

Midterms Save the Dates!

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

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

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

Visual C# Instructor s Manual Table of Contents

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

Chapter 3: Operators, Expressions and Type Conversion

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Operators in java Operator operands.

Course PJL. Arithmetic Operations

COMP 202 Java in one week

Java Fall 2018 Margaret Reid-Miller

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Computational Expression

CS112 Lecture: Primitive Types, Operators, Strings

SECTION II: LANGUAGE BASICS

Programming with Java

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

Chapter 02: Using Data

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

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

EXPRESSIONS AND ASSIGNMENT CITS1001

Prof. Navrati Saxena TA: Rochak Sachan

LECTURE 3 C++ Basics Part 2

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Primitive Data Types: Intro

Building Java Programs

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

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

Building Java Programs

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

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

A variable is a name for a location in memory A variable must be declared

Chapter 2 Working with Data Types and Operators

4 Programming Fundamentals. Introduction to Programming 1 1

An Introduction to Processing

ESc101 : Fundamental of Computing

Transcription:

CIS 110: Introduction to Computer Programming Lecture 3 Express Yourself ( 2.1) 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 1

Outline 1. Data representation and types 2. Expressions 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 2

Administrivia http://www.cis.upenn.edu/~cis110 Sign up for Piazza! New lab section: Lab 214, Th 5-6 PM. Last call for move/swap/register requests. Lab assignment #1: due at the start of lab. HW #1 out: due next Monday (online). A note on feeling lost and help! 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 3

Homework 1 Reproduce song lyrics that have a certain structure to them. Capture structure and eliminate redundancy. Only use classes, static methods, and printlns We grade on correctness and design. Correctness: Does your output match exactly with the desired output from the write-up? Design: Is your solution well-designed? 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 4

Homework Design Guidelines Does your code meet the design goals stated in the write-up? HW 1: did you capture structure and eliminate redundancy as much as reasonably possible? Does your code meet our style guidelines? Consistent indentation, naming, etc. Method comments, file-header comment. No work done directly in main. 80 characters at most per line. Standard with historical roots: 80 line terminals! Good style is like flossing! 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 5

Data Representation and Types 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 6

The Digital Realm Computers store data as sequences of bits Bits are just 0s and 1s E.g., 0101 1101 could be The integer 93 (interpreted as a binary integer) The real number 1.3*e -43 (interpreted as an IEEE 754 floating point number) The character ] (interpreted as a Unicode character) How do we know how to interpret a series of bits stored in memory? 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 7

A Type for Every Datum Types distinguish between different interpretations of data. Interpreting 0101 1101 as a int gives us the integer 93. double gives us the floating-point number 1.3*e -43. char gives us the character ]. int, double, and char are primitive types. Other primitive types: boolean, byte, float. We ll talk about boolean later, ignore the rest. 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 8

Java is a High-level Language With Java, we rarely (if ever) need to deal with data at the level of 1s and 0s. We work with ints, doubles, chars, directly. 93, 1.3*e -43, and ] instead of 0101 1101. However, data representation still influences the behavior of some operations! 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 9

Expressions 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 10

What is an Expression? An expression is a value or a set of operators that produces a value that your program can use e.g., an arithmetic calculation Operators (5.0/9.0) * (100 32) (5.0/9.0) (100 32) An expression Two expressions 5.0 9.0 100 32 A whole bunch of expressions! 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 11

Literal Expressions Literal expressions evaluate to the value they literally stand for. int 0 45-137 0xF31 double 0.15 8.1 55.0-13.2 char Q \n \ \\ boolean true false 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 12

Compound Expressions Compound expressions are formed by connecting sub-expressions with operators. e.g., the mathematical operators + - * / % 1 + 1 3 * 8 2 13 * 3 % 2 24 18 4.0 / 3.2 44 2 * 8 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 13

Division with ints and mod int arithmetic produces ints not doubles! Ex. 22/6 = 3 not 3.6666666667. Recall: 4 th grade (?) arithmetic 22/6 = a whole part 3 with a remainder of 4 (3+3+3+4 = 26) Division (/) on ints returns the whole part Mod (%) on ints returns the remainder 22%6 = 4 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 14

Precedence and Grouping Precedence is the strength with which certain operators bind to sub-expressions. e.g., 1 + 2 * 3 = 7 not 9! For arithmetic, precedence is how you learned it in grade school. *, /, and % have higher precedence or binds tighter than + and You can override precedence with parenthesis e.g., (1 + 2) * 3 = 9! 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 15

Going Between ints and doubles 22/6 = 3 but what if we want 3.6666667? Solution: the following give us what we want 22.0/6.0 22.0/6 22/6.0 The rule: if one operand is a double, the result is a double 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 16

Casts 22.0/6 = 3.6666667 but what if we want 3? Solution: casting! (int) 3.666667 = 3 Casting from int to double truncates the decimal. Syntax: (<type>) <expression> Casting is a unary operator with low precedence (int) 3.0 / 4 is equivalent to (int) (3.0 / 4) Beware, casting between int and char doesn t do what you want! e.g., (int) 3 is not equal to the number 3! 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 17

String Concatenation The concatenation operation (+) glues two Strings together. hi + bye evaluates to hibye Java kindly allows us to concatenate a String and a non-string. val: + (40/3) evaluates to val: 13. Concatenation as the same precedence as addition, so errors can arise val: + 20 3 is the same as ( val: + 20) 3. val: + 20 evaluates to the string val: 20. val: 20 3 is not a valid operation because you can t subtract a string from a number! 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 18

println Does Not Produce Values The following is invalid code! System.out.println( 5 ) + 10 Printing a value is not the same as producing a value for use in your program. Println sends off a copy of the string to your screen, never to be used by others again. An example of a side-effect in Java. 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 19