AS & A LEVEL COMPUTER SCIENCE 9618
PROGRAMMING PARADIGM
A programming style or classification or the features that programming language
has.
CATEGORIES OF PROGRAMMING PARADIGMS
1. Low level language (Assembly language)
2. Imperative
3. Object Oriented
4. Declarative
1 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
ASSEMBLY LANGUAGE / LOW LEVEL LANGUAGE
BINARY MULTIPLICATION
2 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
ASSEMBLY LANGUAGE / LOW LEVEL LANGUAGE
CASE 1 : MULTIPLY BY 2
3 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
ASSEMBLY LANGUAGE / LOW LEVEL LANGUAGE
CASE 2 : MULTIPLY BY 4
We can not multiply with numbers which are not included in the table of 2 for
example 9 and 5
NOTE: For Division we use logical shift right
4 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
ASSEMBLY LANGUAGE / LOW LEVEL LANGUAGE
INCREMENT IN A VARIABLE
Example
LDD Count
INC Acc
STO Count
5 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
LOW LEVEL LANGUAGE
6 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
LOW LEVEL LANGUAGE
7 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
LOW LEVEL LANGUAGE
8 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
LOW LEVEL LANGUAGE
9 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
LOW LEVEL LANGUAGE
10 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
LOW LEVEL LANGUAGE
11 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
LOW LEVEL LANGUAGE
12 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
LOW LEVEL LANGUAGE
13 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
DECLARATIVE PROGRAMMING
1. Mostly used for artificial intelligence and expert systems.
2. Does not require coding in terms of imperative
3. Programming language that uses FACTS and RULES
4. Uses prolog based on predicate algebra
5. (dot) is necessary to mark the end of a fact
6. Variables use capital letters or Capitalized format
FACTS: Things that are known
RULES: These are used to define relationships between facts
Note: In imperative programming we use declarative statements and then define
their sequence of execution(collection of statements executed by computer one by
one). In declarative programming sequence does not matter, we define facts and
rules and then use them to make decisions
14 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
DECLARATIVE PROGRAMMING
Declarative programming is used to extract knowledge by the use of queries
from a situation with known facts and Rules
Things that are known Relationship between facts
EXAMPLES OF FACTS
student( Davis ) Ends a fact
student(Bill) Bill is a student
teacher( Ivan, Bill ) Ivan is the teacher of Bill
NOTE: Facts are written to create a knowledge base and then this knowledge base
can be used
15 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
DECLARATIVE PROGRAMMING
FACTS
fruit(Orange).
fruit(Banana).
Fruit(pineapple).
child(Davis).
Child(Bill).
COMBINING THE ABOVE FACTS
1. likes(Davis, Orange).
2. likes(Bill, Banana)
INTEPRETATION
1. Orange is a fruit
2. Davis is a child
3. Davis likes Orange
4. Bill likes Banana
16 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
DECLARATIVE PROGRAMMING
RULES
Rules apply to all facts
They are used to define relationships between facts
1. brother(X,Y):- X is a brother of Y if likes(X,Z) AND(,) likes(Y,Z)
2. it_is_afruit(X):- It is a fruit if that element is found in the list of facts fro fruits.
NOTE:
1. Facts have constants which never change
2. Rules have variables or constants that can change
HOW TO WRITE DECLARATIVE PROGRAMMING STATEMENTS IN IDE
1. Download and install prolog
2. Open notepad and declare all your statements
3. Save file with .pl file name extension
4. Close and double click on the file to open in Prolog IDE
5. Write goals(questions to test the facts using rules)
17 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
DECLARATIVE PROGRAMMING
EXAMPLE
male(Davis). female(Lucy). parent(James, Davis).
male(James). female(Dianna). parent(Living, Lucy).
male(Living). female(Gretah). parent(Dianna, Gretah).
RULES
1. father(X,Y):- parent(X,Y) , male(X).
2. mother(X,Y):- parent(X,Y), female(X).
3. grandparent(X,Y):- parent(X,Z) , parent(Z,Y).
4. grandfather(X,Y):- grandparent(X,Y) , male(X).
5. grandchild(X,Y):- grandparent(Y,X).
INTEPRETATION
1. X is a father of Y if X is a parent of Y and is male
2. X is a mother of Y if X is a parent of Y and is female
3. X is a grand parent of Y if X is a parent of Z and Z is a parent of Y
4. X is a grand father of Y if X if a grand parent of Y and is a male
5. X is a grand child of Y if Y is a grand parent of X
18 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
DECLARATIVE PROGRAMMING
NOTE: Do not use CAPITAL letter as your first letter unless it is a VARIABLE
19 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
DECLARATIVE PROGRAMMING
20 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
DECLARATIVE PROGRAMMING
21 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
DECLARATIVE PROGRAMMING
22 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
DECLARATIVE PROGRAMMING
23 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
DECLARATIVE PROGRAMMING
24 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
DECLARATIVE PROGRAMMING
25 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
DECLARATIVE PROGRAMMING
26 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
DECLARATIVE PROGRAMMING
27 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
IMPERATIVE PROGRAMMING / PROCEDURAL
IMPERATIVE/ PROCEDURAL
A sequence of steps that change the state of the program
The steps are in order they should be carried out
Split the program into modules which are sub routines
Note:
1. Programs written using the imperative paradigm may be smaller and take less
time to execute. This is because there are fewer instructions and less storage
required for the compiled object code
2. Imperative programming works for small and simple program
3. Programs written using this methodology can be easier for others to read and
understand
28 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
OBJECT ORIENTED PROGRAMMING
A more organized way of programming
The data and the functions that belong to single entity can be grouped in a
single OBJECT
Each object holds its specific information
STUDENT
Name Contact Email Age Class
ATTRIBUTES
An object not only contains attributes but also some actions
Those actions are basically functions known as METHOD
29 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
OBJECT ORIENTED PROGRAMMING
CLASS
Class is a template which defines
the methods and attributes of
certain types of objects
30 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
OBJECT ORIENTED PROGRAMMING
31 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
OBJECT ORIENTED PROGRAMMING
Note: Putting the data ( attributes ) and methods together as a single unit (class) is
called encapsulation
32 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
OBJECT ORIENTED PROGRAMMING
ACCESS MODIFIERS
These are keywords that are used to define the accessibility level for all class,
objects with the class and methods with in the class
ACCESS MODIFIERS IN OBJECT ORIENTED PROGRAMMING
PUBLIC: The property or methods of a class an be accessed from everywhere.
This is default
PROTECTED: The property or method can be accessed within the class and by
classes derived from that class
PRIVATE: The property or methods of a class can ONLY be accessed with in the
class
FRIEND (INTERNAL): Used to specify that access is limited to the current
assembly
PROTECTED FRIEND: Used to specify that access is limited to the current
assembly or types derived from the containing class
33 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
INHERITANCE
The derived class can use the properties from the parent class without re-
declaring them
The derived class can use the methods from the parent class without re-
declaring them
OR
The derived class can extend properties from parent class
The derived class can extend methods from parent class
34 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
INHERITANCE
35 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
INHERITANCE
36 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
POLYMORPHISM
Is when methods are redefined for derived classes
Re-define the same method Although some function output will be
different
37 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
CONTAINMENT
A class include an instance of another class
Aggregation
The contained object can exist outside of its super class
Object is destroyed when super class is destroyed
38 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
FEATURES OF OOP
1. Access modifiers
2. Inheritance
3. Methods
4. Properties
5. Classes
6. Polymorphism
SETTERS : Is a method used to control changes to any variable that is declared
within an object. When a variable is declared as private, only the setters declared
can be sued to make changes
GETTERS: Is a method that gets the value of a property of an object
39 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
GETTERS AND SETTERS
EXPLAIN HOW GET AND SET METHODS ARE USED TO SUPPORT SECURITY AND
INTEGRITY
1. Used to change the properties that are set to private by only using get and set
methods
2. Provide encapsulation
3. prevent accidental damages
4. To make sure data is valid
5. Hides data
6. The get method allow the data to be returned
7. The set method allows the data to be changed
40 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
PSEUDOCODE OF OBJECT ORIENTED PROGRAMMING
DEFINING A CLASS
CLASS Person
Private Name : String KEYWORD FOR A CONSTRUCTOR
Private Address: String
Private Age : Integer
Public PROCEDURE NEW ( FullName: String, Location: String, DOB: Integer)
Name = FullName
Address = Location
Age = DOB
ENDPROCEDURE
ENDCLASS
41 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
PSEUDOCODE OF OBJECT ORIENTED PROGRAMMING
INHERITANCE
CLASS Student INHERITS Person
Private StdClass : String
Private Course : String
Private StudentType : String
Public PROCEDURE NEW ( FullName: String, Age: Integer, MyClass: String )
SUPER.NEW ( FullName)
SUPER.NEW( Age)
StdClass = MyClass
ENDPROCEDURE
ENDCLASS
42 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
PSEUDOCODE OF OBJECT ORIENTED PROGRAMMING
SETTER AND GETTER
CLASS Person
SETTER
Private Name : String
PUBLIC PROCEDURE SETName( N: STRING)
Name = N
ENDPROCEDURE GETTER
PUBLIC PROCEDURE GETName() RETURN STRING
RETURN Name
ENDPROCEDURE CONSTRUCTOR
PUBLIC PROCEDURE NEW ( FullName: String )
Name = FullName
ENDPROCEDURE
ENDCLASS
43 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
PSEUDOCODE OF OBJECT ORIENTED PROGRAMMING
CREATING AN OBJECT
To create an object the following format is used
<Object-Name> = NEW <ClassName> (Parameter1, Parameter2)
Example
Student1 = NEW Student(“Bill”, “YR12”)
CALLING METHODS
Student1 . SETName(“Bill”)
Student1.GETName()
Result: Bill
CALLING A PRIVATE PROPERTY IN A DERIVED CLASS
Student1.Name Error as it is a private attribute
44 Davis_Kazibwe@2023KIS