Chapter 20
Chapter 20
• The style and capability of any programming language is defined by its paradigm.
• Some programming languages, for example JavaScript, only follow one paradigm;
others, for example Python, support multiple paradigms.
• Most programming languages are multi-paradigm.
• In this section of the chapter, we will consider four programming paradigms: low-
level, imperative, object- oriented and declarative.
20.1.1 Low-level programming
• Low-level programming uses instructions from the computer’s basic instruction set.
• Assembly language and machine code both use low-level instructions.
• This type of programming is used when the program needs to make use of specific addresses and registers in a
computer, for example when writing a printer driver.
In Chapter 4, Section 4.2.4, we looked at addressing modes. These are also covered by the Cambridge International
A Level syllabus. Review Section 4.2.4 before completing Activity 20A.
20.1.2 Imperative programming
• In imperative programming, the steps required to execute a program are set out in the order they need
to be carried out.
• This programming paradigm is often used in the early stages of teaching programming.
• Imperative programming is often developed into structured programming, which has a more logical
structure and makes use of procedures and functions, together with local and global variables.
• Imperative programming is also known as procedural programming.
• Programs written using the imperative paradigm may be smaller and take less time to execute than
programs written using the object-oriented or declarative paradigms.
• This is because there are fewer instructions and less data storage is required for the compiled object
code.
• Imperative programming works well for small, simple programs.
• Programs written using this methodology can be easier for others to read and understand.
In Chapter 11, Section 11.3, we looked at structured programming. This is also covered by the Cambridge
International A Level syllabus. Review Section 11.3 then complete Activity 20B.
20.1.3 Object-oriented programming (OOP)
• Object-oriented programming (OOP) is a programming methodology that uses self-contained objects, which
contain programming statements (methods) and data, and which communicate with each other.
• This programming paradigm is often used to solve more complex problems as it enables programmers to
work with real life things.
• Many procedural programming languages have been developed to support OOP. For example, Java, Python
and Visual Basic all allow programmers to use either procedural programming or OOP.
Object-oriented programming uses its own terminology, which we will explore here.
Class
• A class is a template defining the methods and data of a certain type of object.
• The attributes are the data items in a class.
• A method is a programmed procedure that is defined as part of a class.
• Putting the data and methods together as a single unit, a class, is called encapsulation.
• To ensure that only the methods declared can be used to access the data within a class:
• attributes need to be declared as private
• the methods need to be declared as public.
For example,
• a shape can have:
• attributes :
• name, area and perimeter
• the methods :_
• set shape,
• calculate area,
• calculate perimeter. This information can be shown in a class diagram (Figure.
So, what are objects?
13
Why do we care about objects?
• Modularity - large software projects can be split
up in smaller pieces.
• Reuseability - Programs can be assembled from
pre-written software components.
• Extensibility - New software components can be
written or developed from existing ones.
14
Defining Class
A CLASS is a collection of objects that share a common
set of attributes (data) and operations (methods).
HealthClubMember
Attributes (data)
Class Operations (methods)
Objects
15
Class Diagram Notation
e.g video club member system
Class
Member
{
memberNumber
firstName Expanded view of a
lastName Class into its three
Attributes/ telephone
Data address sections:
city
Top: Class Name
etc...
Middle: attributes/ data
{
checkOutVideo
Operations/ checkInVideo Bottom: operations/
methods buyItem
etc... methods
16
Cont.. Class diagram
Basic• Terminology
Encapsulation: Data encapsulation (or data hiding) It is the concept that
data can only be accessed via the methods provided by the class.
For example, a class employee is defined and the object myStaff is instanced in these programs using
Python, VB
Module Module1
class employee:
Dim name As String
Dim staffno As Integer
End Module
Data hiding
• protects the integrity of an object by restricting access to the data and methods within that object.
• One way of achieving data hiding in OOP is to use encapsulation.
• Data hiding reduces the complexity of programming and increases data protection and the security of data.
Example of a definition of a class with private attributes
Module Module1
class employee:
Private name As String
Private staffno As Integer
Module Module1
End Module
overloading
• One way of overloading a method is to use the method with a different number of
parameters.
• For example, a class greeting is defined with the method hello.
• The object myGreeting is instanced and uses this method with no parameters or one
parameter
• When deciding whether to use inheritance or containment, it is useful to think about
how the classes used would be related in the real world.
For example
» when looking at shapes, a circle is a shape – so inheritance would be used
» when looking at the aeroplane, an aeroplane contains wings – so containment would be
used.
Consider the people on board an aeroplane for a flight. The containment diagram could look like this if there can be up to
10 crew and 350 passengers on board:
Example 4-3. Containment relationships
End Class
Object methods
In OOP, the basic methods used during the life of an object can be divided into these types:
• constructors,
• setters,
• getters,
• destructors.
A constructor
is the method used to initialise a new object.
Each object is initialised when a new instance is declared.
When an object is initialised, memory is allocated
e.g bove this is the method used to construct a new employee object.
A setter
• 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 within the object’s class can be used to make
changes to the variable within the object,
• thus making the program more robust.
• Declarative programming is used to extract knowledge by the use of queries from a situation with known facts and
rules.
• we looked at the use of SQL scripts to query relational databases. It can be argued that SQL uses declarative
programming.
Here is an example of an SQL query from Chapter 8:
• Declarative programming uses statements of facts and rules together with a mechanism
for setting goals in the form of a query.
• A fact is a ‘thing’ that is known,
• rules are relationships between facts.
• Writing declarative programs is very different to writing imperative programs.
• In imperative programming, the programmer writes a list of statements in the order that
they will be performed.
• In declarative programming, the programmer can state the facts and rules in any order
before writing the query.
• Prolog is a declarative programming language that uses predicate logic to write facts and
rules.
• For example, the fact that France is a country would be written in predicate logic as:
Declarative Programming
: Declarative languages tell the computer what is wanted but do not provide
the details of how to do it.
These languages are particularly useful when solving problems in artificial
intelligence such as medical diagnosis,
e.g Prolog ( PROgramming in LOGic)
-the programer states facts and rules
- A Fact- is something that is always unconditionally true
- A Rule – is true depending on a given condition
- The order in which the rules and facts are stated is not important. So its
easy to add new rules, delete or change rules
- Executing a prolog program involves stating a goal to be achieved.
- A goal is a statement that we are trying to prove whether or not it is True
or False
-Backtracking is going back to a previously found successful match in order
to continue a search.
Instantiation is giving a variable in a statement a value.
Predicate logic is a branch of mathematics that manipulates logical
statements that can be either True or False.
.
9 (a) (i) -describes what is to be accomplished
-not how (no algorithm written)
-the user states what is to be found/set a goal
-Consists of a set of facts and rules
-Rules are applied to the data until the goal is reached
-Mention of backtracking/instantiation
(max 1)
(ii) -Program describes how to solve the problem in a sequence of
steps/algorithm
-lends itself to top-down design/modularisation
-using procedures/functions
(max 1) [2]
(b) (i) -A class is the “blueprint” from which objects are defined // a class
consists of the properties and methods that define each object
-Plant/Tree/Bulb is a class [2]
(ii) -One class can use the properties and methods from a parent/base/super
class
-Tree/Bulb inherits the properties and methods of Plant [2]
(iii) -An object can only read/write a property value using methods of the
class // Class contains both properties and the methods to use it
-e.g. The CountryOfOrigin property can only be output using the
getCountryOfOrigin method in the class Tree [2]
FACTS & Queries
• The facts are used to set up a knowledge base.
• This knowledge base can be consulted using queries.
For example, a query about countries that speak a certain language, English, could look like this:
language(Country,english)
Note that a variable in Prolog – Country, in this example – begins with an uppercase-letter.
The results are usually shown in the order the facts are stored in the knowledge base.
A query about the languages spoken in a country, Switzerland, could look like this:
language(switzerland,Language).
• Note that PUTRECORD is the pseudocode to write a record to a data file and
• GETRECORD is the pseudocode to read a record from a data file.
sequential file
If a sequential file was required, then the student records would need to be input into an array of records first,
then sorted on the key field registerNumber, before the array of records was written to the file.