Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 14 Introduction to Programming Miguel A. Otaduy May 13, 2004.

Similar presentations


Presentation on theme: "COMP 14 Introduction to Programming Miguel A. Otaduy May 13, 2004."— Presentation transcript:

1 COMP 14 Introduction to Programming Miguel A. Otaduy May 13, 2004

2 Review Exercise Execution of c=2*a+b in a computer

3 Today in COMP 14 Java –special symbols –identifiers –data types –operators –expressions –Strings

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

5 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

6 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!

7 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

8 Special Symbols +-*/.;?, =

9 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

10 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

11 Illegal Identifiers

12 Questions Classify the following as legal or illegal identifiers: 1. My First Program 2. my1stProgram 3. 1stProgram 4. $money 5. an_identifier 6. Jane'sProgram illegal legal illegal legal illegal

13 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

14 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

15 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 32 bits 64 bits Min Value ,768 -2,147,483,648 < -9 x /- 3.4 x with 7 significant digits +/- 1.7 x with 15 significant digits Max Value ,767 2,147,483,647 > 9 x 10 18

16 Integers Examples: -6728, -67, 0, 78, 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

17 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)

18 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

19 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% Arithmetic Expressions

20 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? 8 / 12 equals? % 3 equals? 8 % 12 equals? 2 8 Division and Remainder

21 Unary vs. Binary Operators Unary operators –has only one operand –example: - (negative, not subtraction) -5 Binary operators –has two operands –example: - (subtraction) 5 - 3

22 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)

23 Operator Precedence What is the order of evaluation in the following expressions? a + b + c + d + e 1432 a + b * c - d / e 3241 a / (b + c) - d % e 2341 a / (b * (c + (d - e))) 4123

24 Integral Expressions All operands are integers Result is an integer Examples: * x – y / 7 x + 2 * (y – z) + 18

25 Floating-point Expressions All operands are floating-point numbers Result is a floating-point Examples: 12.8 * 17.5 – x * y / 3.5

26 Mixed Expressions Operands of different types Examples: / 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

27 Type Conversion (Casting) Used to avoid implicit type coercion Syntax (dataTypeName) expression Expression evaluated first, then type converted to dataTypeName Examples: (int) ( ) = 14 (int) (7.9) + (int)(6.7) = 13

28 Questions Evaluate These Expressions 1.(5 + 4) % 6 2.(5 + 6) % (double) (13) / 2 4.(double) (13 / 2) 9 % % 3.5 not possible (double) (6) / 2 6.5

29 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" –""

30 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" ? (count the space)

31 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"

32 Parsing Numeric Strings String to int Integer.parseInt(strExpression) Integer.parseInt("6723")6723 String to float Float.parseFloat(strExpression) Float.parseFloat("345.76") String to double Double.parseDouble(strExpression) Double.parseDouble(" ")

33 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

34 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)% Summary

35 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)

36 Tomorrow First QUIZ! Writing a whole program


Download ppt "COMP 14 Introduction to Programming Miguel A. Otaduy May 13, 2004."

Similar presentations


Ads by Google