cse1020: introduction to computer science i
DESCRIPTION
CSE1020: Introduction to Computer Science I. Today’s Lecture. Course administrative details Material from Chapter 1 “Introduction to Programming” 19:00-22:00 = 180 minutes Tentative schedule Class = 60-70 minutes + break + 60-70 minutes. About this course…. About your instructor - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/1.jpg)
1
CSE1020:
Introductionto Computer Science I
![Page 2: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/2.jpg)
2
Today’s Lecture
• Course administrative details• Material from Chapter 1
“Introduction to Programming”
19:00-22:00 = 180 minutesTentative scheduleClass = 60-70 minutes + break + 60-70 minutes
![Page 3: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/3.jpg)
3
About this course…
• About your instructor• Course web site:
www.cs.yorku.ca/course/1020/– Review of course components
![Page 4: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/4.jpg)
4
About the text book…
Must use 2006 edition, NOT 2004 edition
![Page 5: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/5.jpg)
5
How much time per week?
Hours Activity3 Prepare for lecture (read textbook)3 Attend lecture1 Post-lecture review (review notes)2 Prepare for lab (do self-paced labs)1 Attend lab1-4 Do eCheck(s)1-3 Do chapter exercises
Total:12-17 hours per week (not including test preparation)
![Page 6: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/6.jpg)
6
Chapter 1
Introductionto Programming
![Page 7: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/7.jpg)
7
1.1 Anatomy of a Program 1.1.1 A Quick Tour1.1.2 Language Elements1.1.3 Program Execution
1.2 The Declaration Statement1.2.1 Variable Names1.2.2 The Integer Types1.2.3 Declaration and Memory1.2.4 Other Data Types1.2.5 Primitive and Non-Primitive Types
1.3 The Assignment Statement1.3.1 The int Arithmetic Operators1.3.2 Other Arithmetic Operators1.3.3 Mixed Types and Casting
Outline
![Page 8: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/8.jpg)
8
Let us take a look at a Java program. It does not matter now how the program was written; just become familiar with the terminology for describing program structures.
Note, in particular, the following four terms:
1.1.1 A Quick Tour
Imports, Class, Method, Style
![Page 9: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/9.jpg)
9
import java.lang.System;
public class Area{ public static void main(String[] args) { int width; width = 8; int height = 3; int area = width * height; System.out.println(area); }}
![Page 10: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/10.jpg)
10
import java.lang.System;
public class Area{ public static void main(String[] args) { int width; width = 8; int height = 3; int area = width * height; System.out.println(area); }}
Imports
![Page 11: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/11.jpg)
11
import java.lang.System;
public class Area{ public static void main(String[] args) { int width; width = 8; int height = 3; int area = width * height; System.out.println(area); }} Imported
Class= Delegation
![Page 12: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/12.jpg)
12
import java.lang.System;
Subpackage
ClassPackage
java
lang
util ...
![Page 13: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/13.jpg)
13
import java.lang.System;
import java.lang.*;versus
The package naming convention calls for lowercase letters.
- Note the difference:
- And as a matter of style:
![Page 14: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/14.jpg)
14
import java.lang.System;
public class Area{ public static void main(String[] args) { int width; width = 8; int height = 3; int area = width * height; System.out.println(area); }}
Class Header
Class Body, a Block
![Page 15: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/15.jpg)
15
import java.lang.*;
public class Area{ public static void main(String[] args) { int width; width = 8; int height = 3; int area = width * height; System.out.println(area); }}
Method Header
Method Body, a Block
![Page 16: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/16.jpg)
16
Class naming conventionUse title case unless an acronym, e.g. Math, UTL, StringTokenizer.
Style
Block layoutBraces must align vertically and the all statements must be left justified and indented by one tab position.
Method naming conventionUse lowercase letters but for multi-word names, capitalize the first letter of each subsequent word, e.g. main, equals, toString, isLeapYear
![Page 17: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/17.jpg)
17
Still without worrying about semantics, let us identify the elements of a program:
1.1.2 Language Elements
KeywordsIdentifiersLiteralsOperatorsSeparators
![Page 18: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/18.jpg)
18
Keywords
Identifiers
Literals
Operators
Separators
The reserved words are the keywords plus the literals: true, false, null
M ust not be a reserved word, must begin with a letter, and its character set must be: {0-9, A -Z, a-z, , _}
Recognized by the presence of a number, 'character', "characters", or one of: true, false, null
The separators are: . , ; … ( ) [ ] { }
The character set of operators: = > < ! ~ ? : & | + - * / ^ %
![Page 19: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/19.jpg)
19
abstract assert
boolean break byte
case catch char class const continue
default do double
else enum extends
final finally float for
goto
if implements import instanceof int interface
long
native new
package private protected public
return
short static strictfp super switch synchronized
this throw throws transient try
void volatile
while
Keywords
![Page 20: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/20.jpg)
20
Identify the language elements in the following program...
Keywords, Identifiers, Literals, Operators, Separators
Example
![Page 21: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/21.jpg)
21
import java.lang.System;
public class Area{ public static void main(String[] args) { int width; width = 8; int height = 3; int area = width * height; System.out.println(area); }}
Keywords, Identifiers, Literals, Operators, Separators
![Page 22: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/22.jpg)
22
import java.lang.System;
public class Area{ public static void main(String[] args) { int width; width = 8; int height = 3; int area = width * height; System.out.println(area); }}
Keywords, Identifiers, Literals, Operators, Separators
![Page 23: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/23.jpg)
23
import java.lang.System;
public class Area{ public static void main(String[] args) { int width; width = 8; int height = 3; int area = width * height; System.out.println(area); }}
Keywords, Identifiers, Literals, Operators, Separators
![Page 24: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/24.jpg)
24
1.1.3 Program Execution
compile -time errors
COMPILE
read source file
Java to bytecode
Area.java Area.class EDIT
save the file
create or edit
RUN
VM
read one instruction
bytecode to native
CPU
fetch
execute
Area.class
![Page 25: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/25.jpg)
25
1.2 The Declaration Statement
type name;
The name of a primitive or non-primitive type, e.g. int, double…
An identifier to be associated with a memory block
A separator
The scope of the variable = the enclosing block of the declaration. The variable is not known outside its scope.
![Page 26: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/26.jpg)
26
Rules and guidelines for the name:• Must be an identifier
• Must not be in the scope of another variable with the same name
• A good name is indicative of the content that will be stored in the variable
• As a matter of style, use lowercase letters, but for multi-word names, capitalize the first letter of each subsequent word
1.2.1 Variable Names
![Page 27: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/27.jpg)
27
A type is a range of values and a set of operations on these values.
The range of the int type consists of all whole numbers between -2 and +2 billions (approx). int supports the four arithmetic operations plus the remainder.
The long type is very similar to int except its range is much bigger, +/-1019
An integer literal has an int type unless suffixed by L (l), in which case it is long.
1.2.2 The Integer Types
![Page 28: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/28.jpg)
28
•Logical versus Physical
•Address versus Content
•Bytes, KB, MB, GB, TB, …
•The Memory Block
0
1
.
24
25
26
27
28
29
30
31
.
•1-byte block at address 24
•1-byte block at address 25
•2-byte block at address 26
•4-byte block at address 28
1.2.3 Declaration and Memory
![Page 29: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/29.jpg)
29
0
1
.
23
24
25
26
27
28
29
30
.
What happens when we write:
int width;width
Note that no initialization is involved; only an association of a name with an address.
1. A block big enough to hold an int is allocated, e.g. a 4B block at 24
2. Its address is associated with the variable name, e.g. 24 with width
![Page 30: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/30.jpg)
30
Real
Integer
Use for integer data, e.g.
count. 100% exact
Use for real data, e.g. amount.
Inherently inaccurate
1.2.4 Other Data Types
![Page 31: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/31.jpg)
31
Numeric Types
Real
Integer
float
double
4
8
±1038 SD=7
±10308 SD=15
int
long
4
8
±2G exact
±2E exact
Integer literals are int by default unless suffixed with L
Real literals are recognized thru a decimal point or an exponent. They are double by default unless suffixed with F
![Page 32: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/32.jpg)
32
•Stores the result on a condition
•Has only two possible values
•true and false are reserved words
•Boolean variables are not integersNote: Boolean literals are the easiest to recognize!
The Type
boolean
![Page 33: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/33.jpg)
33
The Character Type•A letter, digit, or symbol
•Digits versus Numbers
•Store the code, not the typeface
•The case of English: ASCII
•char is thus an (unsigned) integer type
•Unicode has 64K codes
char
Character literals are recognized by single quotes surrounding one character, e.g. 'A'
![Page 34: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/34.jpg)
34
Code Character
0 . . .
32 space . . .
48-57 '0'-'9' . . .
65-90 'A'-'Z' . . .
97-122 'a'-'z' . . .
65535
Escape Meaning
\uxxxx The character whose code is
(hex) xxxx
\' Single quote
\" Double quote
\\ Backslash
\n New line
\r Carriage return
\f Form Feed
\t Tab
\b Backspace
More on Characters
![Page 35: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/35.jpg)
35
Non-Primitive
Primitive
number
character
boolean
class
interface
array
1.2.5 Primitive & Non-Primitive
![Page 36: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/36.jpg)
36
PRIMITIVE TYPES
Type Size
(bytes) Approximate Range
min max S.D.
byte 1 -128 +127 ?
short 2 -32,768 +32,767 ?
int 4 -2_109 +2_109 ?
S I G N E D long 8 -9_1018 +9_1018 ?
I N T E G E R
UNSIGNED char 2 0 65,535 ?
SINGLE float 4 +3.4_1038 +3.4_1038 7
N U M B E R R
E A L DOUBLE double 8 -1.7_10308 +1.7_10308 15
BOOLEAN boolean 1 true/false N/A
Java’s Primitive Type
N/A
N/A
N/A
N/A
N/A
N/A
![Page 37: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/37.jpg)
37
name = value;
- Pre-declared and in-scope- Its type can hold RHS- Its content will be overwritten
- a Literal- a Name, or- an Expression
A separator
An operator
1.3 The Assignment Statement
![Page 38: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/38.jpg)
38
1.3.1 The int Arithmetic Operators
Precedence Operator Kind Syntax Operation
+ infix x + y add y to x -5 _
- infix x - y subtract y from x
* infix x * y multiply x by y
/ infix x / y divide x by y - 4 _
% infix x % y remainder of x / y
+ prefix +x identity
- prefix - x negate x
++ prefix ++x x = x + 1; result = x - 2 _
- - prefix - - x x = x - 1; result = x
++ postfix x++ result = x; x = x + 1 - 1 _
- - postfix x- - result = x; x = x - 1
![Page 39: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/39.jpg)
39
Examplesdouble price;
price = 17.25;
int quantity = 25;
boolean isValid = false;
double cost;
cost = price;
double extended;
extended = quantity * price;
RHS is a variable
Can combine declaration with assignment.
RHS is an expression
![Page 40: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/40.jpg)
40
Example
5 + (4 - 3) / 5 - 2 * 3 % 4
![Page 41: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/41.jpg)
41
Example
5 + (4 - 3) / 5 - 2 * 3 % 4
= 5 + 1 / 5 - 2 * 3 % 4
![Page 42: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/42.jpg)
42
Example
5 + (4 - 3) / 5 - 2 * 3 % 4
= 5 + 1 / 5 - 2 * 3 % 4
![Page 43: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/43.jpg)
43
Example
5 + (4 - 3) / 5 - 2 * 3 % 4
= 5 + 1 / 5 - 2 * 3 % 4
= 5 + 0 - 2 * 3 % 4
![Page 44: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/44.jpg)
44
Example
5 + (4 - 3) / 5 - 2 * 3 % 4
= 5 + 1 / 5 - 2 * 3 % 4
= 5 + 0 - 2 * 3 % 4
![Page 45: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/45.jpg)
45
Example
5 + (4 - 3) / 5 - 2 * 3 % 4
= 5 + 1 / 5 - 2 * 3 % 4
= 5 + 0 - 2 * 3 % 4
= 5 + 0 - 6 % 4
![Page 46: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/46.jpg)
46
Example
5 + (4 - 3) / 5 - 2 * 3 % 4
= 5 + 1 / 5 - 2 * 3 % 4
= 5 + 0 - 2 * 3 % 4
= 5 + 0 - 6 % 4
![Page 47: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/47.jpg)
47
Example
5 + (4 - 3) / 5 - 2 * 3 % 4
= 5 + 1 / 5 - 2 * 3 % 4
= 5 + 0 - 2 * 3 % 4
= 5 + 0 - 6 % 4
= 5 + 0 - 2
![Page 48: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/48.jpg)
48
Example
5 + (4 - 3) / 5 - 2 * 3 % 4
= 5 + 1 / 5 - 2 * 3 % 4
= 5 + 0 - 2 * 3 % 4
= 5 + 0 - 6 % 4
= 5 + 0 - 2
![Page 49: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/49.jpg)
49
Example
5 + (4 - 3) / 5 - 2 * 3 % 4
= 5 + 1 / 5 - 2 * 3 % 4
= 5 + 0 - 2 * 3 % 4
= 5 + 0 - 6 % 4
= 5 + 0 - 2
= 5 - 2
![Page 50: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/50.jpg)
50
Example
5 + (4 - 3) / 5 - 2 * 3 % 4
= 5 + 1 / 5 - 2 * 3 % 4
= 5 + 0 - 2 * 3 % 4
= 5 + 0 - 6 % 4
= 5 + 0 - 2
= 5 - 2
= 3
![Page 51: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/51.jpg)
51
• The int operators satisfy closure thru circular wrapping
• The / int operator always rounds toward 0 and leads to an exception if the divisor is zero
• The sign of % is the same as that of the dividend
• The real operators satisfy closure by adding Infinity and NaN. Hence, dividing by zero does not lead to exceptions
• (a * b) / c is not the same as a * (b / c) for any type
• (a + b) – c is not the same as a + (b – c) for real types
1.3.2 Other Arithmetic OperatorsEach of long, float, and double come with 11 operators with the same symbols as int; i.e. the symbols are overloaded. Note:
![Page 52: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/52.jpg)
52
• Promotion (aka widening conversion) is done automatically when needed
• May lead to loss of precision but the order of magnitude is preserved
• Demotion is not done automatically. Can be done manually thru a cast
• Casting is risky…avoid it.
1.3.3 Mixed Types and Casting
![Page 53: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/53.jpg)
53
Demotion
char
float
long
int
byte short
double
Promotion
![Page 54: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/54.jpg)
54
Note:
• The cast operator has a precedence that is higher than * but less than ++
• The = operator has the lowest precedence of all operators
• There are shorthand operators to combine assignment with an operator:
x op= y is shorthand for x = x op y
Ex: x +=1 is like x = x + 1 or x++
![Page 55: CSE1020: Introduction to Computer Science I](https://reader035.vdocuments.mx/reader035/viewer/2022081516/56814a47550346895db764ba/html5/thumbnails/55.jpg)
55
Example
int iVar = 15;
long lVar = 2;
float fVar = 7.6f - iVar / lVar;
double dVar = 1L / lVar + fVar / lVar;
int result = 100 * dVar;
Fix, if need be, and output resultThe answer may surprise you!