Subtopic Notes

20.1 Programming Paradigms

20. Further Programming

Chapter 20 - Further Programming

Programming Paradigm

  • Style or approach to programming that defines how programs are structured, written, and executed
  • It provides a way of thinking about how to solve problems using a programming language

Types of Programming Paradigms

Low-Level Programming

  • Close to machine code, allows direct control of hardware.
  • Uses various addressing modes: immediate, direct, indirect, indexed, relative.
  • Examples: Assembly language, Machine code.

Imperative (Procedural) Programming

  • Focuses on how to perform tasks using statements, procedures, and sequential execution.
  • Traditional style of programming commonly used in most programming languages.1
  • Example: Pascal, C

Object Oriented Programming (OOP)

  • Focuses on objects that encapsulate data and behavior.
  • Features: Encapsulation, inheritance, polymorphism, abstraction.
  • Examples: Java, Python

Declarative Programming

  • Focuses on what to solve, not how to solve it.
  • Uses rules, facts, and queries; the language manages control flow.
  • Examples: SQL.

Low-Level Programming (Assembly)

OpcodeOperandExplanation
Modes of Addressing
LDM#nImmediate addressing. Load the number n to ACC
LDD<address>Direct addressing. Load the contents of the location at the given address to ACC
LDI<address>Indirect addressing. The address to be used is at the given address. Load the contents of this second address to ACC.
LDX<address>Indexed addressing. Form the address from <address> + the contents of the index register. Copy the contents of this calculated address to ACC
LDR#nImmediate addressing. Load the number n to IX
Data movement
MOV<register>Move the contents of the accumulator to the given register (IX)
STO<address>Store the contents of ACC at the given address
Arithmetic operations
ADD<address>Add the contents of the given address to the ACC
ADD#n/Bn/&nAdd the number n to the ACC
SUB<address>Subtract the contents of the given address from the ACC
SUB#n/Bn/&nSubtract the number n from the ACC
INC<register>Add 1 to the contents of the register (ACC or IX)
DEC<register>Subtract 1 from the contents of the register (ACC or IX)
Unconditional and conditional instructions (jumps)
JMP<address>Jump to the given address
JPE<address>Following a compare instruction, jump to <address> if the compare was True
JPN<address>Following a compare instruction, jump to <address> if the compare was False
Compare instructions
CMP<address>Compare the contents of ACC with the contents of <address>
CMP#nCompare the contents of ACC with number n
CMI<address>Indirect addressing. The address to be used is at the given address. Compare the contents of ACC with the contents of this second address
Input and output of data
INKey in a character and store its ASCII value in ACC
OUTOutput to the screen the character whose ASCII value is stored in ACC
Terminate
ENDReturn control to the operating system
Bit manipulation
AND#n /Bn/&nBitwise AND operation of the contents of ACC with the operand
AND<address>Bitwise AND operation of the contents of ACC with the contents of <address>
XOR#n/Bn/&nBitwise XOR operation of the contents of ACC with the operand
XOR<address>Bitwise XOR operation of the contents of ACC with the contents of <address>
OR#n/Bn/&nBitwise OR operation of the contents of ACC with the operand
OR<address>Bitwise OR operation of the contents of ACC with the contents of <address>
LSL#nBits in ACC are shifted logically n places to the left. Zeros are introduced on the right hand end
LSR#nBits in ACC are shifted logically n places to the right. Zeros are introduced on the left hand end
LabelOpcodeOperandExplanation
<label>:<opcode><operand>Labels an instruction
<label>:<data>Gives a symbolic address <label> to the memory location with contents <data>

All questions will assume there is only one general purpose register available
(Accumulator)
ACC denotes Accumulator
IX denotes Index Register
<address> can be an absolute or symbolic address
# denotes a denary number, e.g. #123
B denotes a binary number, e.g. B0101010
& denotes a hexadecimal number, e.g. &4A

Declarative Programming

Declarative programming is fact-oriented, meaning you specify what is true rather than the exact sequence of steps to execute. The language handles the execution order automatically.

Facts
A fact is a statement that is unconditionally true.

color(apple, red). /* Apple is red */ color(banana, yellow). /* Banana is yellow */

Using Variables
Variables allow querying data flexibly

fruitColor(apple, red). fruitColor(banana, yellow). fruitColor(grape, purple). fruitColor(Fruit, yellow). /* Returns: banana */ fruitColor(_, purple). /* Returns: grape */ _ is anonymous variable

Rules
Rules define relationships based on facts:

parent(alice, bob). parent(alice, carol). parent(dan, alice). sibling(X, Y) :- parent(P, X), parent(P, Y), not(X = Y). sibling(bob, Sibling). /* Returns: carol */

Object Oriented Programming (OOP)

  • Class: A blueprint or template that defines the structure and behavior (attributes and methods) of objects.
  • Object: An instance of a class.
  • Attributes/Properties: Variables that store data or characteristics of an object.
  • Methods: Functions defined inside a class that describe the object’s behavior.
  • Instance: A specific, individual object created from a class.
  • Encapsulation: Bundling of data and methods into a single unit (class) and restricting direct access to data using access modifiers (like private or public).
  • Getters and Setters: Methods used to access (get) or modify (set) private attributes.
  • Inheritance: The mechanism that allows one class child to inherit attributes and methods from another parent.
  • Polymorphism: The ability of methods to behave differently based on the object calling them, usually through method overriding or overloading.
  • Containment (Aggregation): When a class contains objects of another class as part of its data (a “has-a” relationship).
Python
class Student: # Instance Variable only exists in the specific instance. # The variables inside the constructor are instance variable # i.e. Changing an instance variable will only affect that specific # instance # Constructor # 4 Parameters: name, department, age, cgpa def __init__(self, name, department, age, cgpa): # To use Encapsulation, make the variables private and # use getter/setters self.__name = name #Private Variable (Denoted by leading __) self.department = department #Public Variable self.age = age self.cgpa = cgpa #Instance Method (Normal method using self) def showDetails(self): print("Name:",self.__name) print("Department:",self.department) print("Age:",self.age) print("CGPA:",self.cgpa) ##Getter def get_name(self): return self.name ##Setter def set_name(self, name): self.name = name #Inheritance class Fresher(Student): # Parent Class: Student # The class being inherited from, also called base class. # Child Class: Fresher # The class that inherits from another class, also called derived # class. def __init__(self, name, department, age, cgpa = 0): super().__init__(name, department, age, cgpa) # Method Overriding — same method name, different behavior # (Example of polymorphism) def showDetails(self): super().showDetails() print(f'Fresher: True') # Aggregation Example # Aggregation means one class contains another class as part of it # (a "has-a" relationship) # Here, Course has a Student object, but both can exist independently. class Course: def __init__(self, course_name, student): self.course_name = course_name self.student = student # Aggregation: Student object passed as parameter def showCourseDetails(self): print(f"Course Name: {self.course_name}") print("Student Enrolled:") self.student.showDetails() # Using Student object inside Course #Driver Code (Main Code) s1 = Student("Helal", "CSE", 21, 3.91) s1.showDetails() print("-----------------------") s2 = Student("Mugdha", "ECE", 21, 3.85) s2.showDetails() print("-----------------------") print("Aggregation Example:") course1 = Course("Data Structures", s1) course1.showCourseDetails()

OOP Pseudocode

Methods and Properties
Methods and properties can be assumed to be public unless otherwise stated. Where the access level is relevant to the question, it will be explicit in the code using the keywords PUBLIC or PRIVATE.

Example code

PRIVATE Attempts : INTEGER Attempts ← 3 PUBLIC PROCEDURE SetAttempts(Number : INTEGER) Attempts ← Number ENDPROCEDURE PRIVATE FUNCTION GetAttempts() RETURNS INTEGER RETURN Attempts ENDFUNCTION

Methods will be called using object methods, for example:
Player.SetAttempts(5)
OUTPUT Player.GetAttempts()

Constructors and Inheritance

Constructors will be procedures with the name NEW.

CLASS Pet PRIVATE Name : STRING PUBLIC PROCEDURE NEW(GivenName : STRING) Name ← GivenName ENDPROCEDURE ENDCLASS

Inheritance is denoted by the INHERITS keyword; superclass/parent class methods will be called using the keyword SUPER, for example:

CLASS Cat INHERITS Pet PRIVATE Breed: INTEGER PUBLIC PROCEDURE NEW(GivenName : STRING, GivenBreed : STRING) SUPER.NEW(GivenName) Breed ← GivenBreed ENDPROCEDURE ENDCLASS

To create an object, the following format is used:
<object name> ← NEW <class name>(<param1>, <param2> ...)

For example:

MyCat ← NEW Cat("Kitty", "Shorthaired")

Footnotes

  1. Note: Imperative Programming concepts are covered in the topic Programming Basics of this book