Week 02 Theory Slides-S
Week 02 Theory Slides-S
PRG510S
― Linus Torvalds
3
Programming Language
A program is written in a specific programming language
Just like people use different languages e.g. English, Silozi, Afrikaans and
etc.;
a program can also be written in a specific language e.g. C++, C#, Pascal,
Visual Basic, Java and etc.
Each language uses a specific set of predefined words and has its own
syntax (rules of grammar)
A person who writes computer programs is called a programmer
Most modern programming languages are designed to be relatively easy
for people to understand and use: high-level languages 4
Programming Language
Computer programs are in written a specific programming language, using keywords
from that language.
The keywords that constitute a computer program are written as a logical sequence
that represent a step-by-step approach to solving a problem.
A step-by-step solution to a problem, when written in a language that can easily be
understood by people and not by computers, is called an Algorithm.
Algorithms are either written using pseudocode, flowcharts or Nassi-Schneiderman
diagrams.
The set of instructions for solving a problem that are written in a specific
programming language are often referred to as source code
Computers do not think on their own, if you give them wrong instructions they give
us wrong results (Garbage In Garbage Out) :Java ? 5
Programming Language
There are many programming languages.
Some languages are designed for specific purposes while other languages are
designed for general use and are known as general purpose programming languages
general purpose programming languages can be used for writing programming for
solving problems whose nature ranges from scientific, engineering, business, and etc
Two main categories :
• Low – level programming languages
• High – level programming languages
6
Low level Programming Languages –
machine language
All computers only understand one language known as machine
language .
Machine language consists of sequence of zeroes and ones
Example of an arbitrary code in machine language is as follows:
0010 1010 0001 1101
0011 1100 1010 1111
0101 0110 1101 0101
1101 1111 0010 1001
7
Drawbacks of machine language
The two major drawbacks machine language are:
It is very easy to mistakenly type in a 0 in place of 1 or vis versa, especially if the
sequence of binary number is quite long. It is also difficult to notice such a mistake.
It takes a long time to write a program in machine language, it even takes a longer
time for people to understand a program written in machine language.
As a result, only few people write programs in machine language
the old days, to make it easier to write programs, programmers invented
another low level programming language known as assembly language
is a symbolic form of machine language that is easier for people to read
8
Assembly language
Definition:
is a symbolic form of machine language that is easier for people to read
Assembly language helped programmers to avoid remembering instructions given in
form of long sequences of 0s and 1s.
It uses short, and easy-to-remember phrases such as JMP, MOV, ADD, etc which
represent specific machine-language instructions
It is easier to understand, write and to modify assembly language than machine
language.
Programs are modified when the program requirements change is not working
correctly the first time you wrote it
A person can only modify a program when he/she understands how that program
works so as to know what to and how to modify 9
An example of Assembly Language Program
An assembly language program that adds two numbers is
shown below (You need not worry about details):
MOV ax,5H
MOV bx,4H
ADD ax,bx
10
Programming Languages – assembler
Assembly language source code must be translated into
machine language for it to be understood by computers
To achieve this, programmers developed an assembler, which is
a special program that translate assembly language into
machine language.
Thus all programs which are written in assembly language must
be fed into an assembler
11
Disadvantages assembly language over
machine language
Programs written in assembly language run slower and take up more
memory space than their machine language equivalent
Programs written in assembly language cannot easily be transferred from
one computer to another
It is still tedious, time-consuming, and complicated to write a program in
assembly language. Only a few programmers are currently writing large
programs in assembly language
NB: A language, like machine language and assembly language, whose
instruction set is closer to the machine (hardware) is classified as a low-
level language
12
High Level Programming Languages
The easier it is to read and write a program written in a specific
programming language, the slower and the larger are the
programs it creates
Every programmer wants to create programs that are easy to
write, and which run very fast while taking up as little space as
possible
High level programming languages were created to overcome
the problem of understanding assembly language but have the
disadvantages of running slowly and requiring more space than
assembly language 13
Advantages of high-level programming languages
over Low level programming languages
Programs written in high-level languages:
are easier to read and write than those written in low level
programming languages
can easily be ported from one computer to another; without massive
rewriting, which is not the case with programs written in assembly and
machine languages
Disadvantages high-level languages:
different compiler or interpreter for each type of language or computer
system
NOT Java :slightly different and much more versatile approach: Compile
+ Interpreter 14
Examples of high-level programming
languages
FORTRAN • Objective-C
Pascal • Java
Ada • Visual Basic
Basic • C# (C Sharp)
• COBOL
C
• Javascript
C++ • PHP
Delphi • etc
Prescribed book PG 40 - 47
15
Java
popular and general-purpose programming language
Class-based, object-oriented, and designed to have as few
implementation dependencies as possible
Intended for Write Once, Run Anywhere (WORA)
Java was originally developed by James Gosling at Sun
Microsystems/Oracle (1991 - 1995*)
Quite reputable
First language used for Android app development
16
Java Features:
Very secure, multi-threaded and Object-Oriented Language
Platform Independence: Support for all major OSs
Free and powerful Development Environment
Portability and Scalability (from Mobile Applications to Enterprise
Computing)
Plenty of free (often Open Source) Libraries
Support for Web Development (e.g. Applets and Servlets) Good
Performance
Networking Support (Distributed Computing)
...and more that I forgot to mention 17
Java portability, Why?
Java compiler does not translate your program into the machine
language.
Instead it translates the program into a language called bytecode
• Not the machine language for any particular system/OS
• Mainly for hypothetical computers called virtual machine
Bytecode is then translated into machine language by Java Virtual
machine(JVM)
JVM is a kind of an interpreter.
All computers that runs Java hence need the JVM
18
Interpreter
An interpreter converts your source code into machine language and stores them in
memory each time you run the program
Interpretation is the process of instant translation of a source code into an executable
form; e.g. into a code that can be understood by a machine:
During interpretation, first the source code is scanned for correctness
The source code is then translated into some intermediate internal form for execution
Interpretation does not produce a target machine code. Hence, target code is interpreted by
software rather than hardware
Programs that are written on interpreter based programming languages:
will always require the source code in order to run them
are slower to run than those that written on compiler based programming language
19
Compiler
A compiler converts your source code into machine language instructions the first
time you run your program. It saves those machine language instructions in a file;
usually called the EXE or executable file The EXE file can be run even if the source code is not
available
Compiled programs run are faster than those that use interpreters
The process of translating source code into machine code (compilation) consists of
several phases
a) Two of those phases that are worth mentioning here are Intermediate code generation In this phase a version
of the program is produced in some intermediate form which is not machine dependent but can run on different
machines.
• Examples of an intermediate code are
i) p-code which is produced after compiling a Pascal program.
ii) byte-code which is produced after compiling a java program.
b) Code Generation In this phase the source code is translated into the target machine code
20
Compiler and Interpreters
Interpreters
Basic
Prolog
Javascript,PHP
Lava, Smalltalk ..etc.
Compilers:
Delphi
C++
Rust,Ada
• Compilers or Interpreters(byte code):
C#, Java, Python ..etc.
21
120-Seconds Discussions
Lists at least 6 concepts learnt today
Add your own understanding to each concept
Which concept is not clear to you?
22
Problem solving
Problem :a situation or environment that people must seek a solution for.
Algorithm : precise and complete steps that should be followed in order
to solve a given problem: written in pseudocode or FlowChart
Pseudocode: mixture of English and certain programming language(java)
• Basically writing parts of the algorithm in the easiest language (English or Java)
Flowchart :a diagrammatic representation of an algorithm
• https://www.programiz.com/article/flowchart-programming 23
Pseudocode and Flowchart example
24
Problem solving life cycle
Source:
https://www.linkedin.com/pulse/teach-kids-life-
skills-abraham-femi-abraham
25
Problem solving Approaches
Abstraction
Examples:
o Drawing a plan when one wants to build a house
o Stating major components wants to have when building a car
Divide-and-conquer
• Examples:
o Creating different departments/sections in a milling company
Heuristics
Examples:
o Old people can tell whether it’s going to rain by looking at the type of clouds (experience )
Branch-and-bound
Reduction: transform-and-conquer
26
Anatomy of a Java program
Java comments. Are always
ignored by the compiler
27
Anatomy of a Java program
31
Running Java from CMD with Arguments
Java portability means running and compiling can be done via CMD
No need for a specific IDE, but the JDK is a must
Make sure your PATH is correctly set (PRACTICAL)
Compile : javac <filename>.java
E.g. javac CustomGreeting.java
Run : java <filename> //No file extension
E.g. java CustomGreeting
CMD allows for arguments to be passed to the Main method
Hence :public static void main(String[] args)
32
Running Java from CMD with Arguments
Editing can be done with any Text Editor e.g. Notepad (Notepad++)
javac CustomGreeting.java
java CustomGreeting Desmond Tutu
Make sure the package declaration is removed if you are using an IDE before you run
you work on CMD, Below is the expected outcome:
33
Further Reading
https://www.tutorialspoint.com/java/
• Mobile app available : Java Offline Tutorial
https://www.javatpoint.com/how-to-set-path-in-java
https://www.javatpoint.com/java-tutorial
https://www.youtube.com/watch?v=6qp8AiEPMYk
Many many……many more out there !!!!
34