COMP 110 Introduction to Programming. What did we discuss?

Similar documents
Full file at

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

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Basics of Java Programming

ECE 122 Engineering Problem Solving with Java

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

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

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

CMPT 125: Lecture 3 Data and Expressions

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CIS 110: Introduction to Computer Programming

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

Program Fundamentals

Full file at

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

Visual C# Instructor s Manual Table of Contents

JAVA Programming Fundamentals

CS 106 Introduction to Computer Science I

3. Java - Language Constructs I

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

Getting started with Java

Operators. Lecture 3 COP 3014 Spring January 16, 2018

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

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

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

CMSC131. Introduction to your Introduction to Java. Why Java?

Java Programming Fundamentals. Visit for more.

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

Lecture Set 4: More About Methods and More About Operators

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

Lecture Set 4: More About Methods and More About Operators

JAVA OPERATORS GENERAL

BASIC ELEMENTS OF A COMPUTER PROGRAM

CIS133J. Working with Numbers in Java

C++ Programming: From Problem Analysis to Program Design, Third Edition

COMP 110 Introduction to Programming. What did we discuss?

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

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Algorithms and Programming I. Lecture#12 Spring 2015

Chapter 2 Elementary Programming

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

Chapter 2: Data and Expressions

LECTURE 3 C++ Basics Part 2

Chapter 2: Using Data

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

Declaration and Memory

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

CS Programming I: Primitives and Expressions

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++

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

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

4 Programming Fundamentals. Introduction to Programming 1 1

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

CS 112 Introduction to Programming

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

Zheng-Liang Lu Java Programming 45 / 79

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

ARG! Language Reference Manual

Java Notes. 10th ICSE. Saravanan Ganesh

Lecture 1. Types, Expressions, & Variables

Building Java Programs

Datatypes, Variables, and Operations

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

MODULE 02: BASIC COMPUTATION IN JAVA

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

Prof. Navrati Saxena TA: Rochak Sachan

Outline. Data and Operations. Data Types. Integral Types

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Values and Variables 1 / 30

Information Science 1

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

Building Java Programs

Information Science 1

1.3b Type Conversion

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

ESc101 : Fundamental of Computing

Midterms Save the Dates!

Building Java Programs

More Programming Constructs -- Introduction

Lecture 3 Tao Wang 1

4. Inputting data or messages to a function is called passing data to the function.

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

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

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

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

Chapter 6 Primitive types

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Chapter 2: Using Data

Chapter 2: Data and Expressions

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Introduction to Computer Science and Object-Oriented Programming

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

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections

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

Chapter 3: Operators, Expressions and Type Conversion

Transcription:

COMP 110 Introduction to Programming Fall 2015 Time: TR 9:30 10:45 Room: AR 121 (Hanes Art Center) Jay Aikat FB 314, aikat@cs.unc.edu Previous Class What did we discuss? COMP 110 Fall 2015 2 1

Today Announcements Still registering Labs / recitations register for them Join Piazza: piazza.com/unc/fall2015/comp110 Assignment1: due Fri, Aug 28 at 11:55 PM Check Sakai, Piazza and class webpage regularly Your first program COMP 110 Fall 2015 3 Identifiers Names of things (variables, constants, methods) in your programs Can be composed of any combination of letters, digits, underscore (_), and dollar sign ($) Cannot begin with a digit May be any length Java is case sensitive Total, total, and TOTAL are different identifiers COMP 110 Fall 2015 4 2

Illegal Identifiers COMP 110 Fall 2015 5 Questions Classify the following as legal or illegal identifiers: 1. My First Program illegal 2. my1stprogram legal 3. 1stProgram illegal 4. $money legal 5. an_identifier legal 6. Jane'sProgram illegal COMP 110 Fall 2015 6 3

Primitive Data Types What is a Data Type? Primitive data are fundamental values such as numbers and characters A set of values and the operations that can be performed on those values Operations are performed on primitive types using built in operators COMP 110 Fall 2015 7 Primitive Data Types 8 primitive data types in Java 4 represent integers byte, short, int, long 2 represent floating point numbers float, double 1 represents characters char 1 represents boolean values boolean COMP 110 Fall 2015 8 4

Primitive Data Types (Numeric Types) The difference between the various numeric primitive types is their size, and therefore the values they can store: Type byte short int long Storage 8 bits 16 bits 32 bits 64 bits Min Value -128-32,768-2,147,483,648 < -9 x 10 18 Max Value 127 32,767 2,147,483,647 > 9 x 10 18 float double 32 bits 64 bits +/- 3.4 x 10 38 with 7 significant digits +/- 1.7 x 10 308 with 15 significant digits COMP 110 Fall 2015 9 Integers Examples: 6728, 67, 0, 78, 36782 Positive integers do not have a '+' sign in front of them (but they can) No commas are used in an integer commas in Java are used to separate items in a list COMP 110 Fall 2015 10 5

Primitive Data Types (Characters) A char stores a single character from the Unicode character set an ordered list of characters, and each character corresponds to a unique number uses 16 bits per character, allowing for 65,536 unique characters Character literals are delimited by single quotes: 'a' 'X' '7' ' ' '$' ',' '\n' newline character (we'll discuss later) COMP 110 Fall 2015 11 Primitive Data Types (Boolean) Only two valid values true or false uses 1 bit for storage Represent any situation that has 2 states on off true false true and false are reserved words COMP 110 Fall 2015 12 6

Arithmetic Expressions Expression a combination of one or more operands and their operators Arithmetic expressions compute numeric results and make use of the arithmetic operators: Addition + Subtraction - Multiplication * Division / Remainder % If either or both operands associated with an arithmetic operator are floating point, the result is a floating point COMP 110 Fall 2015 13 Division and Remainder If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded) 14 / 3 equals? 4 8 / 12 equals? 0 The remainder, or modulus, operator (%) returns the remainder after dividing the second operand into the first (only works with integer types) 14 % 3 equals? 8 % 12 equals? 2 8 COMP 110 Fall 2015 14 7

Unary vs. Binary Operators Unary operators has only one operand example: (negative, not subtraction) 5 Binary operators has two operands example: (subtraction) 5 3 COMP 110 Fall 2015 15 Operator Precedence Determines the order in which operators are evaluated: 1. multiplication, division, and remainder 2. addition, subtraction, and string concatenation 3. arithmetic operators with the same precedence are evaluated from left to right Parentheses can be used to force the evaluation order (just like in math) COMP 110 Fall 2015 16 8

Operator Precedence (PEMDAS) Parentheses: 6 * (5 + 7) vs. 6 * 5 + 7 Exponents (powers, roots 2 5 36 1/2 ) Multiplication / Division / Mod Addition / Subtraction Left to right Which is these is correct? 30 / 5 * 3 = 6 * 3 = 18 30 / 5 * 3 = 30 / 15 = 2 this one! COMP 110 Fall 2015 17 Operator Precedence What is the order of evaluation in the following expressions? a + b + c + d + e 1 2 3 4 a + b * c - d / e 3 1 4 2 a / (b + c) - d % e 2 1 4 3 a / (b * (c + (d - e))) 4 3 2 1 COMP 110 Fall 2015 18 9

Integral Expressions All operands are integers Result is an integer Examples: 2 + 3 * 5 3 + x y/ 7 x + 2 * (y z) + 18 COMP 110 Fall 2015 19 Floating-point Expressions All operands are floating point numbers Result is a floating point Examples: 12.8 * 17.5 34.50 x * 10.5 + y 16.2 7.0 / 3.5 COMP 110 Fall 2015 20 10

Mixed Expressions Operands of different types Examples: 2 + 3.5 6 / 4 + 3.9 Integer operands yield an integer result Floating point operands yield a floating point result If both types of operands are present, the result is a floating point number implicit type coercion Precedence rules are followed COMP 110 Fall 2015 21 Type Conversion (Casting) Used to avoid implicit type coercion Syntax (datatypename) expression Expression evaluated first, then type converted to datatypename Examples: (int) (7.9 + 6.7) = 14 (int) (7.9) + (int)(6.7) = 13 COMP 110 Fall 2015 22 11

Questions 1. (5 + 4) % 6 2. (5 + 6) % 3.5 9 % 6 3 11 % 3.5 not possible 3. (double) (13) / 2 4. (double) (13 / 2) 13.0 / 2 6.5 (double) (6) 6.0 COMP 110 Fall 2015 23 Our First Program public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World"); } } COMP 110 Fall 2015 24 12

Next class (Thu, Aug 27) Binary representation Program in class: Adding two numbers Assignment1 DUE Fri, Aug 28 Reading Assignment: Chapter 1 COMP 110 Fall 2015 25 Teaching Assistants o Yenchun Chen o Junpyo Hong (JP) o Ben Newton Ben Newton Junpyo Hong (JP) COMP 110 Spring 2015 26 13

Teaching Assistants Dana Elhertani Spencer Byers Sarah White Camden Link Jeffrey Young Max Daum COMP 110 Spring 2015 27 14