COMP 14 Introduction to Programming

Slides:



Advertisements
Similar presentations
Lecture 5 Types, Expressions and Simple I/O COMP1681 / SE15 Introduction to Programming.
Advertisements

CSci 1130 Intro to Programming in Java
DATA TYPES, VARIABLES, ARITHMETIC. Variables A variable is a “named container” that holds a value. A name for a spot in the computer’s memory This value.
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Expressions, Data Conversion, and Input
Objectives You should be able to describe: Data Types
A First Book of ANSI C Fourth Edition
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
2440: 211 Interactive Web Programming Expressions & Operators.
C Tokens Identifiers Keywords Constants Operators Special symbols.
COMP 110 Introduction to Programming Mr. Joshua Stough August 29, 2007 Monday/Wednesday/Friday 3:00-4:15 Gardner Hall 307.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Input, Output, and Processing
Chapter 2: Using Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Chapter 2 Variables.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
CSCI 51 Introduction to Programming Dr. Joshua Stough January 27, 2009.
Doing math In java.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
COMP 14 Introduction to Programming Mr. Joshua Stough January 24, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Escape Sequences What if we wanted to print the quote character?
IDENTIFIERS CSC 111.
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
Fundamentals 2.
Chapter 2: Java Fundamentals
Expressions and Assignment
Primitive Types and Expressions
Presentation transcript:

COMP 14 Introduction to Programming Michele Weigle - COMP 14 - Spring 2004 COMP 14 Introduction to Programming Miguel A. Otaduy May 13, 2004

Michele Weigle - COMP 14 - Spring 2004 Review Exercise Execution of c=2*a+b in a computer

Michele Weigle - COMP 14 - Spring 2004 Today in COMP 14 Java special symbols identifiers data types operators expressions Strings

Michele Weigle - COMP 14 - Spring 2004 Reading Check-Up The rules of a language determine which instructions are valid. True or False? Hello! is an example of a legal Java identifier. If an operator has an integer and a floating-point operand, the result of the operation is a number. The expression (int) (9.2) evaluates to syntax False floating-point 9

Michele Weigle - COMP 14 - Spring 2004 Introduction Computer program: a sequence of statements whose objective is to accomplish a task Programming: process of planning and creating a program Programming language: a set of rules, symbols, and special words

Michele Weigle - COMP 14 - Spring 2004 Sample Java Program public class Hello { public static void main (String[] args) System.out.println ("Hi There!"); } Upon execution, this program would display Hi There!

Programming Languages Michele Weigle - COMP 14 - Spring 2004 Programming Languages Programming languages have rules of grammar just as English does syntax rules - which statements are legal and which are not semantic rules - determine the meaning of the instructions token - smallest individual unit of a program special symbols word symbols identifiers

Michele Weigle - COMP 14 - Spring 2004 Special Symbols + - * / . ; ? , <= != == >= the 3rd row contains two characters that together (with no space between them) are considered to be a single symbol

Word Symbols aka reserved words, or keywords Michele Weigle - COMP 14 - Spring 2004 Word Symbols aka reserved words, or keywords int float double char void public static throws return reserved words are always all lowercase each word symbol is considered to be a single symbol cannot be used for anything other than their intended purpose in a program shown in blue typewriter font in textbook full table in Appendix A

Michele Weigle - COMP 14 - Spring 2004 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

Michele Weigle - COMP 14 - Spring 2004 Illegal Identifiers

Michele Weigle - COMP 14 - Spring 2004 Questions Classify the following as legal or illegal identifiers: My First Program my1stProgram 1stProgram $money an_identifier Jane'sProgram illegal legal illegal legal legal illegal

Primitive Data Types What’s A Data Type? Michele Weigle - COMP 14 - Spring 2004 Primitive Data Types What’s A Data Type? A set of values and the operations that can be performed on those values Primitive data are fundamental values such as numbers and characters Operations are performed on primitive types using built-in operators certain types of operations can only be performed on certain types of data example: you can multiply numbers, but not words

Michele Weigle - COMP 14 - Spring 2004 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

Primitive Data Types Numeric Types Michele Weigle - COMP 14 - Spring 2004 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 float double Storage 8 bits 16 bits 32 bits 64 bits Min Value -128 -32,768 -2,147,483,648 < -9 x 1018 +/- 3.4 x 1038 with 7 significant digits +/- 1.7 x 10308 with 15 significant digits Max Value 127 32,767 2,147,483,647 > 9 x 1018 float - at most 6 or 7 places after the decimal point double - at most 15 places after the decimal point some compilers will complain if you use float, so we will use double throughout this class

Michele Weigle - COMP 14 - Spring 2004 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

Primitive Data Types Characters Michele Weigle - COMP 14 - Spring 2004 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' ordered list of characters for example, the value 65 represents uppercase 'A' 66 represents uppercase 'B' the full list of Unicode / ASCII is in Appendix C of the textbook newline character (we'll discuss later)

Primitive Data Types Booleans Michele Weigle - COMP 14 - Spring 2004 Primitive Data Types Booleans 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 We'll see a lot more of booleans and boolean expressions later on in the semester

Arithmetic Expressions Michele Weigle - COMP 14 - Spring 2004 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: If either or both operands associated with an arithmetic operator are floating point, the result is a floating point Addition + Subtraction - Multiplication * Division / Remainder %

Division and Remainder Michele Weigle - COMP 14 - Spring 2004 Division and Remainder If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded) The remainder, or modulus, operator (%) returns the remainder after dividing the second operand into the first (only works with integer types) 14 / 3 equals? 4 8 / 12 equals? 14 % 3 equals? 2 8 % 12 equals? 8

Unary vs. Binary Operators Michele Weigle - COMP 14 - Spring 2004 Unary vs. Binary Operators Unary operators has only one operand example: - (negative, not subtraction) -5 Binary operators has two operands example: - (subtraction) 5 - 3

Michele Weigle - COMP 14 - Spring 2004 Operator Precedence Determines the order in which operators are evaluated: multiplication, division, and remainder addition, subtraction, and string concatenation 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)

Michele Weigle - COMP 14 - Spring 2004 Operator Precedence What is the order of evaluation in the following expressions? a + b + c + d + e a + b * c - d / e 1 2 3 4 3 1 4 2 a / (b + c) - d % e 2 1 4 3 a / (b * (c + (d - e))) 4 3 2 1

Michele Weigle - COMP 14 - Spring 2004 Integral Expressions All operands are integers Result is an integer Examples: 2 + 3 * 5 3 + x – y / 7 x + 2 * (y – z) + 18 in these expressions, x, y, and z are some unknown numbers.

Floating-point Expressions Michele Weigle - COMP 14 - Spring 2004 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 in these expressions, x and y are some unknown numbers.

Michele Weigle - COMP 14 - Spring 2004 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 2.0 + 3.5 1 + 3.9 1.0 + 3.9 if you have both integer and floating -point operands, the integer will be converted to a floating-point number and the operation will take place -- the result will be floating-point

Type Conversion (Casting) Michele Weigle - COMP 14 - Spring 2004 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 casting a floating-point number to an integer truncates everything after the decimal point (int) 14.6 7 + 6 the textbook has very nice examples of mixed expressions and casting on pgs. 35-37 -- make sure you take a look at them and understand the answers

Questions Evaluate These Expressions Michele Weigle - COMP 14 - Spring 2004 Questions Evaluate These Expressions (5 + 4) % 6 (5 + 6) % 3.5 (double) (13) / 2 (double) (13 / 2) 9 % 6 3 11 % 3.5 not possible 13.0 / 2 6.5 (double) (6) 6.0

Michele Weigle - COMP 14 - Spring 2004 The class String String sequence of zero or more characters enclosed in double quotation marks null or empty strings have no characters numeric strings consist of integers or decimal numbers length is the number of characters in a string The class String is used to manipulate strings Examples: "Hello World" "1234" "45.67" "" note the null, or empty, string

Michele Weigle - COMP 14 - Spring 2004 Strings Every character has a position in the string (starting with 0) "Hello World" The length of the string is the number of characters in it what's the length of "Hello World"? 0123456789... 11 (count the space)

Parsing Numeric Strings Michele Weigle - COMP 14 - Spring 2004 Parsing Numeric Strings In Java, input from the user comes in the form of a string we need to know how to get the number values out of the string Numeric String a string with only integers or decimal numbers "6723", "-823", "345.76"

Parsing Numeric Strings Michele Weigle - COMP 14 - Spring 2004 Parsing Numeric Strings String to int Integer.parseInt(strExpression) Integer.parseInt("6723") 6723 String to float Float.parseFloat(strExpression) Float.parseFloat("345.76") 345.76 String to double Double.parseDouble(strExpression) Double.parseDouble("1234.56") 1234.56 the capital letters in Integer, Float, and Double are important -- Java is case-sensitive!

Michele Weigle - COMP 14 - Spring 2004 Summary Identifiers can be composed of any combination of letters, digits, underscore (_), and dollar sign ($) cannot begin with a digit Java is case-sensitive Data Types main integer type: int main floating-point type: double others: char, boolean

Michele Weigle - COMP 14 - Spring 2004 Summary Arithmetic Operators: If one of the operands is floating-point, the result is a floating-point Can only use % with two integer operands Casting (int) (54.9) - truncates the floating-point number (double) (23) - adds a .0 to the integer addition + subtraction - multiplication * division / remainder (mod) %

Michele Weigle - COMP 14 - Spring 2004 To do Homework 1 due tonight Homework 2 due tomorrow night Algorithm design Theory questions First program (after tomorrow’s class) Read ch. 2 (pp. 37-end)

Michele Weigle - COMP 14 - Spring 2004 Tomorrow First QUIZ! Writing a whole program