Title: A Very Brief Introduction to Programming
1A (Very) Brief Introduction to Programming
2Programming with LeJOS
- LeJOS (Java programming language
specifically for Lego Robots)
- Object Oriented Programming (OOP) language
- Pieces of code others have written can easily be
reused or modified - Supports floating-point (decimal) numbers, and
arrays - Allows FULL control over the RCX brick (access
to buttons, screen, etc) - Allows multi-threading simultaneous
execution of various parts of a program
Do I have to use LeJOS? YES!
3Algorithms
- Before writing a program, develop an outline, or
algorithm of what it should do
1. The ACTIONS to execute
2. The ORDER in which these actions execute
1. Get out of bed
1. Eat breakfast
2. Take off pajamas
2. Put on suit
3. Take a shower
3. Get out of bed
4. Put on suit
4. Take a shower
5. Eat breakfast
5. Drive to work
6. Drive to work
6. Take off pajamas
4Algorithm Development
- Big picture description of problem solution in
sequential steps
- Can be used on simple problems
Example Calculate elapsed time
1. Read current time
2. Subtract initial time from current time
3. Print elapsed time
- Divide and Conquer Strategy
- Use for non-simple problems
- Continuously break down problem into smaller
pieces
- Use pseudocode or flowcharts to refine outline
- There are usually MANY possible solutions that
will work
5Pseudocode
- English-like statements (not actual programming
language)
- Normally only describes executable statements
- Cause specific actions to take place -
input, output, calculation
Example Calculate and display a persons age
Prompt user to enter date of birth
Input date of birth
Prompt user to enter todays date
Input todays date
Calculate age, store result
Display their age
6Flowchart Symbols
module name
Start
Processing Block
I/O to computer memory
Decision
end/stop/exit
End or Stop
7Pseudocode/Flowchart
Operation
Pseudocode
Flowchart Symbol
start main
main
Beginning of algorithm
calculate age
computation
age date - DOB
print age
print age
output
Is age lt18?
if age lt 18, then
decision
yes
no
stop main
End of algorithm
8Structured Programming
- Use simple control structures
- Steps performed one after another
- Two sets of steps to follow
- - one if condition is true, another if it is false
- Set of steps that are repeated as long as
condition is true
9Structured Programming
Selection
Sequence
yes
no
Repetition
branches
no
yes
loops
10Selection Statements
11The if statement (flowchart)
no
no
is condition true?
is condition true?
yes
yes
Statement 1
Statement 1
Statement 2
Statement n
12The if statement (syntax)
if(expression) statement
single statement executed if expression is true
if(expression) statement1
statement2 statement n
statements inside are executed if expression
is true
13The if statement (examples)
if (x gt 10) Sound.beep()
Causes beeping sound if x gt 10
if (x gt 10) Sound.beep()
Sound.buzz()
Causes beeping sound AND buzzing sound if x gt 10
if (x gt 10) if (y lt 5)
Sound.beep()
Causes beeping sound if x gt 10 AND y lt 5
14The if-else statement (flowchart)
yes
no
is condition true?
Statement 1
Statement 2
15The if-else statement (syntax)
if (expression) Statement1 else Statement2
Statement1 executed if expression is true.
Statement2 executed if expression is false.
if (expression) statement1
statement2 statement3 else
statementA statementB
statement1, statement2, and statement3 are
executed if expression is true
statementA and statementB are executed if
expression is false
16The if-else statement (example)
if (x 10) Sound.beep() else
Sound.buzz()
Causes beeping sound if x 10
Causes beeping sound if x is not 10
if (x 10) Sound.beep() else if (x 5)
Sound.buzz() else if (x 2)
Sound.honk() else sound.tweet()
Can combine multiple if-else statements to select
from a list of possible values
What would this flowchart look like?
17Multiple if-else statements (flowchart)
yes
no
does x 10
no
yes
BEEP
does x 5
yes
no
BUZZ
does x 2
TWEET
HONK
18Repetition Statements
19The while statement (flowchart)
no
is condition true?
Note The statements that form a loop must
MODIFY objects that are used in the condition.
Otherwise, an infinite loop is created!
yes
Execute Statement1
Execute Statement2
20The while statement (syntax)
while (condition) statement1
single statement executed while condition is true
while (condition) statement1
statement2 statement n
statements inside are executed while
condition is true
21The while statement (example)
Causes beeping sound as long as bot.isBright() is
true
while (bot.isBright() ) Sound.beep()
int x 0 while (x ! 3 )
Sound.beep() x x 1
! means not equal to
How many beeps are there?
int x 0 while (x ! 0 )
Sound.beep() x x 1
How many beeps are there?
22The do-while statement (flowchart)
Similar to while statement except condition is
tested at the END of the loop, instead of at the
beginning
Execute Statement
yes
is condition true?
will always be executed
at least once
no
23The do-while statement (syntax)
do statement1 while (condition)
Statement1 executed first time through, then
while condition is true, it is executed again
do statement1 statement2
statement n while (condition)
statements inside are executed first time
through, then while condition is true, they are
executed again
24The do-while statement (examples)
do Sound.beep() while
(bot.isBright() )
Beeps at least once, then causes beeping sound
as long as bot.isBright() is true
int x 0 do Sound.beep() x x
1 while (x ! 3 )
! means not equal to
How many beeps are there?
int x 0 do Sound.beep() x x
1 while (x !0)
How many beeps are there?
INFINITE LOOP!!!
25The for statement
- Use for loops that increment or decrement by the
same amount each - time through the loop
1. Initialize the loop-control object
2. Specify the condition that must be true to
continue the loop repetition
3. Specify the modification to the loop-control
object that follows the execution of the
statement block
26The for statement (flowchart)
initalize
test
increment/ decrement
true
statement(s)
statement(s)
27The for statement (syntax example)
for (initialization test counter increment)
statements
for (int j 1 j lt 5 j) Sound.beep()
1. Initialize the loop-control object j is an
integer, and it is assigned the value 1
2. Specify the condition that must be true to
continue the loop repetition j must be less
than 5
3. Specify the modification to the loop-control
object that follows the execution of the
statement block j is incremented by 1 ()
28Object Oriented Programming
29Types of Programming
- Object-Oriented Programming
30Unstructured Programming
Main Program
Data
- Main Program operates directly on Global Data
31Procedural Programming
- Combine returning sequences of statements into a
single place
- Program can be a sequence of procedure calls
Main Program
Data
Procedure 1
Procedure 2
Procedure 3
32Modular Programming
- Procedure of common functionality grouped
together in modules
- Each module can have its OWN data (state)
Program
Main Program
data
module 2
module 1
data data2
data data1
Procedure 3
Procedure 2
Procedure 1
33Object-Oriented Programming
- Web of interacting objects, each with its own
state (data AND operations)
- Objects interact by sending messages to one
another
- Each object has its own discrete code
- Easy to modify without affecting functionality
of other objects
34Object-Oriented Programming (Example)
1. Navigate a course by following a line
2. Locate and grab a ball
Find/Grab Ball
Follow a Line
Sense the Line
Sense the Ball
Follow the Line
Power the Claw
Power the Wheels
- Each object can be modified, without affecting
the other
- But, to accomplish overall goal, they must work
together
35Object-Oriented Programming (Classes)
- Classes define objects using methods
(functionality) and fields (data)
- classes are like engineering drawings
Class Automobile
method accelerate
method steer
36Object-Oriented Programming (Objects)
- Objects PERFORM what the class tasks describe
- realization of a class
Class Automobile
object
object
Ferrari
Model T
- many cars can be made from the same engineering
drawings
- many objects can be made from the same class
37Object-Oriented Programming (Inheritance)
- A class can inherit the functionality (methods,
data) of an existing
class, plus add additional methods and/or data
- This property of OOP is called type extensibility