Professional Documents
Culture Documents
2XB3 - Week - 02 - ProgrammingModel - ST
2XB3 - Week - 02 - ProgrammingModel - ST
Lecture 2
Programming Model for Implementing Algorithms
(Static Methods)
Reza Samavi
objects
arrays
15
Built-in Data Types
17 add, subtract,
int integers 12345 multiply, divide
double
floating-point 3.1415 add, subtract,
numbers 6.022e23 multiply, divide
true
boolean truth values false and, or, not
16
Basic Definitions
17
Integer Operations
1234 = 12*99 + 46
18
Type Conversion
19
Data type Summary
21
A Foundation for Programming
objects
arrays
22
If Statement
23
If Statement Examples
24
While Loop
statement 2
while (boolean expression) {
statement 1;
statement 2; loop body
true
} boolean expression statement 1
false
25
While Loop Challenge
26
For Loops
true
boolean expression statement 1
false
27
Loop Examples
29
Control Flow Summary
Control flow.
■ Sequence of statements that are actually executed in a program.
■ Conditionals and loops: enable us to choreograph the control flow.
30
A Foundation for Programming
objects
arrays
31
Arrays
32
Many Variables of the Same Type
33
Many Variables of the Same Type
// easy alternative
double[] a = new double[10];
…
a[4] = 3.0;
… declares, creates,
a[8] = 8.0; and initializes
… [stay tuned for
double x = a[4] + a[8]; details]
34
Arrays in Java
36
Arrays in Java
Compact alternative.
■Declare, create, and initialize in one statement.
■Default initialization: all numbers automatically set to zero.
37
A Foundation for Programming
objects
build bigger programs
functions and modules and reuse code
arrays
38
Functions
x
y
z
f f (x, y, z)
Functions (Static Methods)
Java function.
■ Takes zero or more input arguments.
■ Returns one output value.
■ Side effects (e.g., output to standard draw). more general than
mathematical functions
Applications.
■Scientists use mathematical functions to calculate formulas.
■Programmers use functions to build modular programs.
■You use functions for both.
Examples.
■ Built-in functions: Math.random(), Math.abs(), Integer.parseInt().
■ Our I/O libraries: StdIn.readInt(), StdDraw.line(), StdAudio.play().
■ User-defined functions: main().
40
Anatomy of a Java Function
input output
2.0 f(x) = Öx 1.414213…
41
Flow of Control
Key point. Functions provide a new way to control the flow of execution.
Key point. Functions provide a new way to control the flow of execution.
43
Scope
Scope (of a name). The code that can refer to that name.
Ex. A variable's scope is code following the declaration in the block.
50
Java Function for f(x)
library or user-defined
51
Building Functions
Process.
■ Step 1: identify a useful feature.
■ Step 2: implement it.
■ Step 3: use it.
52
Libraries
53
Using a Library
public class RandomPoints {
public static void main(String args[]) {
int N = Integer.parseInt(args[0]);
for (int i = 0; i < N; i++) {
double x = StdRandom.gaussian(0.5, 0.2);
double y = StdRandom.gaussian(0.5, 0.2);
StdDraw.point(x, y);
}
} use library name
} to invoke method
% javac RandomPoints.java
% java RandomPoints 10000