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

Similar documents
CS313D: ADVANCED PROGRAMMING LANGUAGE

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

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

CS111: PROGRAMMING LANGUAGE II

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

Program Fundamentals

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

Expressions and Casting

Java Primer 1: Types, Classes and Operators

Full file at

Values and Variables 1 / 30

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

Zheng-Liang Lu Java Programming 45 / 79

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

Java Fall 2018 Margaret Reid-Miller

Java Bytecode (binary file)

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

Lecture 2: Variables & Assignments

Visual C# Instructor s Manual Table of Contents

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

CMPT 125: Lecture 3 Data and Expressions

Basics of Java Programming

ECE 122 Engineering Problem Solving with Java

3. Java - Language Constructs I

COMP 110 Introduction to Programming. What did we discuss?

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Chapter 2 Elementary Programming

CSC Web Programming. Introduction to JavaScript

Lecture 1. Types, Expressions, & Variables

CSI33 Data Structures

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

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

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

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

Getting started with Java

Elementary Programming

Lecture Set 4: More About Methods and More About Operators

Lecture Set 4: More About Methods and More About Operators

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Variables and literals

Pace University. Fundamental Concepts of CS121 1

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

Course Outline. Introduction to java

Declaration and Memory

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

CS110: PROGRAMMING LANGUAGE I

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

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

Operators and Expressions

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

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

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

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

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

These are notes for the third lecture; if statements and loops.

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Lecture Set 2: Starting Java

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

Chapter 2: Data and Expressions

CIS133J. Working with Numbers in Java

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

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

CS 231 Data Structures and Algorithms, Fall 2016

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Introduction to Programming Using Java (98-388)

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

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

A Java program contains at least one class definition.

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Chapter 17. Fundamental Concepts Expressed in JavaScript

CS 11 java track: lecture 1

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

Chapter 2: Using Data

CEN 414 Java Programming

Objectives. In this chapter, you will:

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Primitive Data Types: Intro

Chapter 2: Data and Expressions

Chapter 2: Using Data

Values, Variables, Types & Arithmetic Expressions. Agenda

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

Computer Programming : C++

1.1 Your First Program

Information Science 1

Basic Operations jgrasp debugger Writing Programs & Checkstyle

1/16/12. CS 112 Introduction to Programming. A Foundation for Programming. (Spring 2012) Lecture #4: Built-in Types of Data. The Computer s View

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

Variables and data types

CS112 Lecture: Primitive Types, Operators, Strings

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Transcription:

CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today: Java basics: Compilation vs Interpretation Program structure Statements Values Variables Types Operators and Expressions Next Time: Java Statements, conditionals, and loops Reading assignments will be posted on the web site! HW 01 is posted, will begin at Lab today!

Compilation vs Interpretation Python is an example of an interpreted language; the primary workflow is to interact with the interpreter as a fancy calculator with lots of features: 2

Compilation vs Interpretation Java is an example of a language which is compiled; before running any code, your program (ending in.java) must be transformed into a lower-level form (an executable file ending in.class), and which is then passed to the interpreter, which runs the program and produces output: 3

Java Basic Program Structure Java programs are organized as classes (more on these next week!) stored in files with the suffix.java, and with code written inside methods delimited by curly braces; each program must have a method called main, which contains the code that will be executed when you run your program: SampleProgram.java public class SampleProgram { Class name is same as file name public static void main(string[] args) { Curly braces enclose classes and methods Class // Here is code that is executed when // you run your program. Method } } 4

Java Comments Java has comments, exactly like Python, but with a different syntax: Python: Java: and # /*. */ and // 5

Java Statements In Python, we compute by evaluating expressions, which yield a value, which is then printed out. In Java, we compute by executing statements, which have an effect on the state of the program: they assign a value to a variable, or print a value out, or otherwise change something in the system. Python: Java: Assignment Statements Note: All Java statements end in semicolon. 6

Java Statements It is often useful to understand the effect of a sequence of assignment statements by tracing the values of the variables, which change after each statement. The collection of all values is the state of the program:

Java Values and Types A Data Type (or just Type) is a collection of values and associated operations. Java is a Strongly-Typed language supporting many different types: String hi there 8

Java Values and Types However, in CS 112 we will only use the following types: 9

Java Values and Types Literal values are similar to Python: int 4-5 double 3.4-2.34e10 char a \n \t // single quotes for chars boolean true false // note lower case String hi there // must use double quotes Note that String is capitalized 10

Java Values and Types Python is weakly typed : values have types but variables do not; variables are just names for any value you want and can be reused for any values; the only errors occur when variables have not yet been assigned values: 11

Java Values and Types Java is strongly-typed in that All variables must be declared with a type before being used and can only be used for that type of value: 12

Java Values and Types During compilation, the types are checked and errors will be reported with line numbers and terse explanations: 13

Java Values and Types This might seem unduly rigid, but the philosophy of strongly-typed languages is that specifying types makes programmers more careful about variables, and bugs and errors can be found during compilation, not when the program is running. Values can be converted from one type to another implicitly or explicitly: Widening Conversions (implicit): Narrow types (less information) Wider types (more information) Example: int double double x; x = 4; // 4 is widened to 4.0 and then assigned No error! 14

Java Values and Types This might seem unduly rigid, but the philosophy of strongly-typed languages is that specifying types makes programmers more careful about variables, and bugs and errors can be found during compilation, not when the program is running. Values can be converted from one type to another implicitly or explicitly: Widening Conversions (implicit): Narrow types (less information) Wider types (more information) Example: int double double x; x = 4; // 4 is widened to 4.0 and then assigned Example 2: char int int x; x = A ; // a is converted to its Unicode // value 65 and assigned to x 15

Java Values and Types This might seem unduly rigid, but the philosophy of strongly-typed languages is that specifying types makes programmers more careful about variables, and bugs and errors can be found during compilation, not when the program is running. Values can be converted from one type to another implicitly or explicitly: Narrowing Conversions (you must specify a cast or else get an error): Wider types (more information) Narrower types (less information) Example: double int int x; x = 4.5; Error! 16

Java Values and Types This might seem unduly rigid, but the philosophy of strongly-typed languages is that specifying types makes programmers more careful about variables, and bugs and errors can be found during compilation, not when the program is running. Values can be converted from one type to another implicitly or explicitly: Narrowing Conversions (you must specify or else get an error): Wider types (more information) Narrower types (less information) Example: double int int x; x = 4.5; Error! Cast Must explicitly tell Java to truncate the double value to an integer: int x; x = (int) 4.5; // x gets 4 17

Java Values and Types double int char (int) (char) Narrowing String boolean Widening 18

Java Values and Types Digression: You probably think this is a purely academic matter, and making a type conversion mistake will only lose you a few points on the midterm. Think again: In 1996, the Adriade 6 rocket exploded after takeoff because of a bad type conversion in its control code: 19

Java Operators The operators are almost exactly the same as in Python: Same: + addition += - subtraction -= * multiplication *= % modulus %= == equals!= not equal < less <= less or equal > greater >= greater or equal 20

Java Operators When Java evaluates an overloaded operator, it automatically performs widening conversions as necessary to make the operands fit the operator: Example: + is overloaded it works for two ints or two doubles. All the arithmetic operators in java are overloaded for int and double. Widening Conversion: 4 + 2.3 4.0 + 2.3 => 6.3 Result is the wider type! 21

Java Operators Division is overloaded, but behaves differently for ints and doubles.. Python: two different division operators: / floating-point division /= // integer division Java: division operator is overloaded : / returns an int if both operands are ints, otherwise returns double: 5 / 2 => 2 5.0 / 2 => 2.5 5.0 / 2.0 => 2.5 5 / (double) 2 => 2.5 22

Java Operators The boolean operators in Java look different (although they work exactly the same): Python: Java: not! and && or Note that in both languages, and and or are lazy: (false && X) => false (without evaluating X) (true X) => true (without evaluating X) Example: ( (4 < 6) && ( 5 >= 5) ) => true // both < and >= are evaluated ( (7 < 6) && ( 5 >= 5) ) => false // only < needs to be evaluated 23

Java Operators There is NO exponentiation operator in Java: Python: x ** 2 x squared Java: have to use explicit math functions: Math.pow(x,2) => returns double You will become familiar with the explicit Math functions in HW 01. 24

Java Operators Finally, Java has several useful increment and decrement operators which Python lacks; these can be used as statements OR expressions: Statements: ++x; x++ ; // same as x = x + 1 or x += 1 --x; x-- ; // same as x = x 1 or x -= 1 Expressions: ++x has the value AFTER adding 1 x++ has the value BEFORE adding 1 x y z int x = 4; 4 undef undef int y = ++x; 5 5 undef int z = x++ ; 6 5 5 25

Java Character Data Type The char data type is useful when we manipulate Strings (which are simply sequnces of chars. Here are the most useful methods in the Character library: 26

Java Operators The String data type is necessary for manipulating textual data and for input and output: Note that + is overloaded: it can be used for plus (int, double) or concatenate (Strings). 27

Java String Data Type 28

Java String Data Type Punchline: ( str.compareto( str2 ) R 0 ) is equivalent to ( str R str2 ) where R is one of ==,!=, <, >, <=, >= 29

Java String Data Type 30