1 Basic Programming Concepts
1 Basic Programming Concepts
ITP3914 – Programming
Machine : c aann
a n
ss andd c
Language di
tteedi oouu y..
e r:yy
VVer rrorrsseeas a s iill y
Program a k ee eerro
mak
0011000100001000
m
nntt
e nddee
i ne ddeeppen
M aacchhine
M
1000001001010101
9
Assembly
AssemblyLanguage
Language––English-like
English-like
abbreviations
abbreviationsto
torepresent
represent computer
computer
operations.
operations. More
Morehuman-readable.
human-readable.
Solution
Solution
High
High level
level programming
programming languages
languages
are
are provided
provided to
to close
close the
the gap
gap between
between
human
human programmers
programmers and and computers.
computers.
LD
LD R0,
R0,#05
#05
ADD R0, $1234
Assembly
AssemblyLanguage
Language
ADD
SUB
SUB
R0, $1234
R0,
R0,#22
#22
LD
LD $2345,
$2345,R0
R0
0110001111000010
0110001111000010
Machine
MachineLanguage
Language 0001000111100011
0001000111100011
0011000100001000
0011000100001000
Easier and Easier! No more excuse saying that this module is difficult!
• Dinosaurs
• Cobol – for business data processing; still in use in banks
• Fortran – for scientific computation
• Middle Aged
• Pascal – a sophisticated but overly complicated HLL
• C – a widely used HLL for general computing
• Youngsters
• C++ – an enhancement over C with object-oriented features
• Java – an OO language with networking and lots other features
• C# - developed by Microsoft in competition with Java
Java
(C) VTC, Prepared by [email protected] 17
Your First Java Program
/*
/*
**The
TheHelloWorld
HelloWorldApplication
Application
*/
*/
public
publicclass
classHelloWorld
HelloWorld{{
public
publicstatic
staticvoid
voidmain(String
main(String[][]argv)
argv)
{{
System.out.println("Hello
System.out.println("HelloWorld!");
World!");
}}
}}
Edit
Compile
Execute
Collectively, known
as Java APIs
Application
Programming
Interfaces
Eclipse
(C) VTC, Prepared by [email protected] 27
Part 4 – Some more details on Java programs
/*
* The HelloWorld Application Multi-line Comment
*/
radius Assignment
radius==3;
3;
3
System.out.println("Radius
System.out.println("Radius==""++radius);
radius); radius area
area
area==radius
radius**radius
radius**3.14;
Expression
3.14;
System.out.println("Area 3 28.26
System.out.println("Area==""++area);
area);
}} radius area
}}
You
Youcan
canalso
alsoprint
printthe
thevalue
value
stored in a variable.
stored in a variable.
(C) VTC, Prepared by [email protected] 33
Does it work???
public
publicclass
classCircle
Circle{{
public
publicstatic
staticvoid
voidmain(String
main(String[][]args)
args)
{{
radius
radius==3;
3;
int
int radius;
radius;
System.out.println("Radius
System.out.println("Radius==""++radius);
Strangely, students just
radius);
System.out.println("Area
System.out.println("Area==""++area);
area);put all the things into a
program and HOPE they
double
double area;
area;
area
work correctly together.
area==radius
radius**radius
radius**3.14;
3.14;
}}
}}
Processing
(C) VTC, Prepared by [email protected] 38
Basic operations
Answer: Flowcharts
Graphically depict the logical steps to
carry out a task and show how the steps
relate to each other.
Output:
Output:
speed
speed
End
(C) VTC, Prepared by [email protected] 44
Start
Example
Input:
number1
Problem:
A program is required to get three Input:
numbers and then add them together number2
and give their total.
Input: Input:
number3
3 numbers
Process: Process:
calculate the total of the 3 numbers total = number1 +
number2 + number3
Output:
total of the 3 numbers
Output:
total
End
(C) VTC, Prepared by [email protected] 45
Decision flow chart – Selection
Comparing two
different values and
selecting alternative
actions false true
Is condition true?
Example Input:
Octopus balance
Problem:
Determine if Octopus payment Input:
is accepted. If so, what the payment amount
new balance is?
Input: amount
Octopus balance, <=
false true
(balance+50)
payment amount
Process: Output: newBalance =
balance - amount
Check existing balance. “Insufficient
Balance”
Deduct payment amount from
balance if it is sufficient. Output:
newBalance
Output:
new balance or “insufficient
Balance”
Repeating a group of
actions
Is condition true?
false
true
Process step(s)
set numStudent=0
Example
set total=0
Input:
add 1 to numStudent
individual marks
Process: true more student
Sum individual marks to a total. marks?
Count the number of students.
Calculate average as false
total/count.
average = total / numStudent
Output:
average mark
Output: average