PROGRAMMMETHODSMyNote 2
PROGRAMMMETHODSMyNote 2
METHODS
• What is a program?
• What is a programming language?
• Who is a programmer?
Program
There are many definitions of what a program is, the following are some
acceptable examples;
A program is a group of related instructions that aim to perform a task(s).
A complete set of instructions a computer can interpret and follow to
perform a task(s).
A set of instructions for a computer(s) to follow.
Complete algorithm for computer(s) to solve specific problems.
Programming Language
a) Imperative languages
b) Functional Languages
c) Logic Programming
d) Object-oriented Languages
e) Declarative Languages
f) Scripting Languages
g) Parallel Languages
Language Models
e) Declarative Languages: They are used to give a general idea as how the
various parts of a program are related (variables and functions)
f) Scripting Languages: They usually work in with larger applications to
perform simple or complex tasks e.g. vbscript and javascript.
g) Parallel Languages: Structured data or processes that communicate with
each other e.g. Ada and C#
Language Generations
• First Generation(1GL): Also known as machine language is
numerical(binary) and machine dependent. Programs in 1GL
are executed fastest compared other generations. Its very
difficult writing machine code.
• Second Generation(2GL): The 2GL permitted the use of
symbolic instructions and addresses. Also known as the
assembly language it was a easier to use and understand than
machine code.
• Third Generation(3GL): These languages are machine
independent, making it easier to cross-platform programs.
This leaves time for the programmer to focus more on the
logic of the program.
Language Generations
• What is an object?
• What are the characteristics of objects?
• What can an object never be?
Object
• A value
• A process
• Time
Types of Class
ENCAPSULATION
ABSTRACTION
POLYMORPHISM
INHERITANCE
Encapsulation and Abstraction
Encapsulation: This is the process of binding or wrapping data and
methods under a single unit in order to keep both safe from outside
interference and misuse of codes.
a) Inheritance
b) Association
c) Multiplicity
Inheritance
• The process whereby objects inherit attributes from other objects and
classes inherit their attributes from other classes.
Types of Inheritance
Single Inheritance
Multi-Level Inheritance
Hierarchical Inheritance
Multiple Inheritance
Hybrid Inheritance
NB: A Super class or also be known as Base or Parent class and a Child class
can also be known as Derived or Sub class.
• Single Inheritance: This is where a child class inherits from a parent class.
Parent Class
Child Class
• Multi-Level Inheritance: A child class inherits from a super class which in
turn inherits from another super class.
Super Class
Super Class
Child Class
• Hierarchical Inheritance: This is where a child class inherits from a parent
class which inherits from a grandparent and so on.
Grandparent Class
Parent Class
Child Class
• Multiple Inheritance: This is where a child class inherits from two or more
super classes.
Child Class
• Hybrid Inheritance: This is a combination of the various types of inheritance
like single, multi-level, multiple and hierarchical inheritance.
Association
One-to-One
One-to-Many
Many-to-Many
Multiplicity
Multiplicity: It’s a type of task in which an entity has too many kind of
relationship.
For example; an organization with more than one owning.
Summary
We learnt;
What is meant by a program
Who a programmer is
What is meant by programming language
The concept of programming language models
The idea of programming language generations
An introduction to structured programming
An introduction to object-oriented concepts, such as;
Classes
Encapsulation and Information hiding (Abstraction)
Messages and Operations (Methods/functions)
Relationships
Polymorphism
Objective Questions
6. Which of the following has to do with objects with the same name that perform operations in different
ways.
a. Association b. Classes c. Polymorphism
7. An instance of a class is known as a(n)….
a. Structure b. Property c. Object
8. …… shows how two or more classes or objects are connected (related) together.
a. Inheritance b. Association c. Multiplicity
9. ……… and ……….. classes are the two types of classes
a. Abstract b. Obstruct c. concrete d. Real
10. Using Object Oriented Programming lets programmers create reusable code.
a. True b. False c. Neither d. Both
Answers
1. c
2. b
3. d
4. b,c
5. b
6. c
7. c
8. b
9. a , c
10. a
Excercise
Form groups and research on the following topics; each group should tackle
one topic
Discuss the pros and cons of OOP
Discuss the language generations and their significance to modern
programming
Notes
Rapid Application Development(RAD): RAD is a programming system
employed to create applications very fast and efficiently. RAD systems
provide easy to use and customisable GUIs and toolkits so
programmers can focus on logic. E.g. visual basic, visual studio, Delphi
etc.
Tool Command Language(TCL): It is used for prototyping applications
as well as developing CGI. Its Library can be extended.
Prototyping: A technique used in development when you make a
working version of the program and test it in its operating environment
to ascertain whether it viable. In other words it is a blue print of the
program.
Common Gateway Interface(CGI): A specification for the transfer of
information between the word wide web(www)and a CGI program.
Practical Extraction and Report Language(PERL): It is a programming
language for processing text.
Algorithm: Logical finite steps taken to solve a problem.
Chapter 2
Variables, Control Structures and Calculations
Objectives;
Understand what variables are
Identify what data types are and how to use them with variables
Describe the naming conventions for variables
Identify and use arithmetic operators
Understand the importance of order of precedence
Describe control structure
Use calculations to create computer programs
Understand and apply algorithms
Understand and use flowcharts
Variables
• You must decide what to put into variables i.e. a bowl designated for serving
humans would not be used for serving animals.
• Data types tell the computer what kind of data the variable would contain.
This makes it possible for the computer to reserve the relevant space for the
variable
Data Types
Variable names
should not start with a number i.e. 3age etc. but can have numbers within
the name or at the end of name .
Should not contain a dot(.) i.e. first.name etc. .
Should not contain spaces i.e. first name etc. .
Should not contain special characters i.e. first#name, first~name, etc. .
Naming Variables
• + means add
• - means subtract
• * means multiply
• / means divide
• % means modulo
Arithmetic Operators
Order of precedence:
I.Brackets
II.Exponents
III.Multiplication
IV.Division
V.Addition
VI.Subtraction
Assignment Operator
Equals to(=): The assignment operator is the = symbol. It is used to give the
value of what is to its right to what is in the left for example int a; a=10;
If… (SELECTION)
if(condition){
--statement(s)--
} else{
--statement(s)--
}
Types of Control Structure
Care should be taken to choose the appropriate condition to prevent infinite loops.
Types of Control Structure
Start / Stop
Action/Process
• Symbols: Flow
Decision
Connector
Summary
• We learnt;
• What variables are and how to use them
• Variable types(data types) and names
• Control structure 1 (Sequence)
• Control structure 2 (Selection)
• Control structure 3 (Loops)
• Using pseudocode
• Using flowchart
Questions
1. What is pseudocode?
2. What are the advantages of using pseudocode name four(4)?
3. In what instances do we use the sequence ?
4. In what instances do we use the selection ?
5. In what instances do we use the loops?
6. What symbol is used as the assignment operator?
Exercise 2.7
TRY:
Write the pseudocode and draw a flowchart for a program to calculate the
wages of a salesman according to the following rules.
The wage is calculated at a rate of 15% of sales. If the salesman has been
with the company more than three years, he has a loyalty bonus of 10% of
his calculated wage.
Exercise 2.7 Answer
Pseudocode
Use variables: sales, years, wage, bonus OF Type Real
name OF TYPE string
Display “Enter the name of the salesman:”
Accept name
Display “Enter the number of years with the company”
Accept years
Display “Enter the sales figures:”
Accept sales
wage=sales*15/100
If years>3
Then bonus=wage*10/100
else bonus=0
endif
wage=wage+bonus
Print name ”has sales figures of ” sales, ” a bonus of” bonus “and “ wage “wages”
End program
Exercise 2.7 Answer
Flowchart
Page 1 Page 3
Page 2
START P1 P2
Yes
Years >
3
Display “Enter
the name of Display “Enter the
sales figures” No
salesman”
Calculate
bonus = 0
Accept name Accept sales
Calculate
bonus = wage* 10/100
Display “Enter the Calculate
number of years with wage = sales*15/100
the company”
Display name “has sales
figures of “sales “ a
bonus of” bonus “and “
Accept years wage “wages”
STOP
P2 P3
Note
Array: It is a group of related variables or a continuous
space in memory for a group of related variables declared
together
Constant: A data item or quantity whose value does not
change.
Pseudocode (Pseudolanguage): A design technique used
to give a general description of a software module usually
interpreted by programmers.
Identifier: A string of characters used to name an element
of a program.
Chapter 3
Data Analysis and Problems
Objectives;
Understand the software development process
Ability to create and analyse structure diagrams for structured programs
Explain variable and fixed length records
Organise information
Understand the structure of data
Analyse problems
Program Development Process
Testing: In this stage the solution is tested against real life scenarios. The
solution is tested by the developers (alpha test) the clients/users are given
the chance to use the program to ascertain whether it meets their
requirements (beta test).
Implementation and support: After the solution passes every test, it is
implemented. The solution and the documentations are finalized for use
by the clients. After implementation the developers provide after sales
service such as updates, upgrades etc. .
Structure Diagrams
• Structure are diagrams programmers use to help visualise solutions they are
creating.
Data Structure
O
Selection
Data Structure
Module
*
Student
*
Results
Second Name
First Name
Assignment Exam
Organising Information
Top –down development: If the program is complicated, then you need to break
it down into simpler tasks in a stepwise manner.
Stepwise refinement: an approach in software development in which a complex
program is gradually refined through a sequence of intermediate representations
to yield a final program in some programming language.
Modular programming: It is a programming style were a program is decomposed
into a set of components(modules). Each module is of manageable size, is well
defined and has an interface to connect with other modules.
CRC Cards
C – Class
R – Responsibility
C – Collaboration
We learnt;
Program development process
Structure Diagrams
Organising Information
Fixed and variable length records
Analysing the problem
Structured Programming Diagrams
CRC cards
Questions