SCD - 1.2.4 - Types of Programming Language


T3 - Programming Paradigms

  • Prodecural - Python, Basic, Pascal, C#
  • Object-Oriented - Java, C++, Visual Basic, .NET, (Python)
  • Declarative - SQL, Prolog, HTML
  • Functional - Haskell, JavaScript, Logo

A paradigm is a way of programming (how the languages tell the computer what to do)

Imperative Approach - HOW you are going to get to the endpoint, e.g. steps to take a seat at a cinema

  • Series of instructions that tell the computer what to do with the input to solve the problem making imperative highly structured
  • Structured is a type of imperative programming which uses constructs (sequence, selection, iteration, recursion rather than "goto" statements)

Declarative Approach - WHAT the endpoint is, e.g. what you want, not how you are going to do it

  • Internal processes of the language finds the best way of solving a problem (e.g. SQL is used to create, amend and query databases
1 of 9

Logic Programming + Prolog

Logic - Form of declarative programming, it expresses the logic of a computation without expressing its control flow

  • Consists of logocal statements that express facts and rules about the program within a system of formal logic
  • Rules are written as logical clauses with a head and body e.g. "H is true if B1,B2 and B3 are true

Prolog is an example of logic with statements written in the form of facts (unconditionally true) and rules (true depending on a given condition) [Order of these are not important] e.g.:

  • likes(tom, jenny).  /tom likes jenny/
  • eats(ben, apples). /ben easts apples/

These can then be queried - ?- eats(ben, bananas). No /Does not match a fact in the database/

Variables in prolog are distingued with an uppercase letter - ?- eats(ben, Fruit) and then as Fruit=apples the answer returned is yes

Rules in prolog are expressed in the form of IF a is true, THEN b is true

2 of 9

Backtracking, applications of declarative and OOP

Backtracking is used in prolog which selects a route through the maze of facts and rules until it finds a dead end and then backtracks until the goal is achieved or there are no more routes - this is an important feature of declarative programming

Declarative programming is well suited to expert systems which embodies the facts and rules about a particular field of knowledge e.g. medical diagnosis, oil exploration, tax regulation, processing natural language

Object-Oriented Programming (OOP) are used to abstact details of implementation allowing code to be reused and easy to maintain, this is done by viewing the world as a collection of an object, such as: Person, Animal, Event, Data Structure

OOP programming consists of objects which each have their own attributes and methods

e.g. a program to keep records of bank accounts

  • Object – Account
  • Attribute – Account number, name of holder, type of account, balance
  • Method – Deposit money, withdraw money
3 of 9

More on Object Oriented Programming

Class - Blueprint for an object, defines attributes and methods capturing common characteristics and behaviours of an object (e.g. class vehicles with attributes wheels and method move forward), a constructor is ued to create objects based on the class, example:

  • Turtle class has a position, heading, colour, etc. , each turtle has methods such as forward, left, right
  • A new turtle object can then be created with statement - Raphael = new Turtle (x1, y1, 0, blue), as it inherits the methods then Raphael.forward(20) can then be used to move it

Encapsulation - A fundamental principle of OOP where attributes and methods are wrapped into a single entity

Information can be hidden in OOP:

  • Object's attributes are hidden (Private) and only accessed and changed through the object's methods
  • Methods are required to set (Setters) and retrieve (Getters) an object's attributes
  • To interact with an object, its methods must be accessable (Public)
4 of 9

Classes and Inheritance

A class is defined by specifing the methods and attributes that belong to it in a class definition

Inheritance - A relationship among classes where a sub-class shares all of the methods and attirbutes of a parent class e.g. classes cat and mouse will inherit the attributes and methods of the Animal class ontop of their own attributes and methods - Inheritance can be determined using the "Is a" rule - "Is object A an Object B?" e.g. is a cat an animal?

5 of 9

Polymorphism & Inheritance Diagram

An inhertited class may have methods and attributes not defined in the parent class, it may in addition, redefine methods that are defined by the parent class, this is called polymorphism

Example - Parent class bird has method eat

  • subclass Parrot may define this as eating seeds
  • subclass Eagle may define this as eating meat
  • subclass Chicken may define this as eating seeds & meat

Inheritance diagram shows what methods and attrubutes classes have and inherit

6 of 9

T4 - Assembly Language

Assembly is an example of a Low-level language and used mnemonics to represent operation codes with typically 2-4 character codes used to represent all the machine code instructions

Each processor has its own assembly language which is translated using an assemler into machine code for execution

The Little Man Computer is an imaginary computer with a limited instruction set, with instructions such as:

  • ADD - adds the contents of memory address ** to the accumulator
  • SUB - subtracts the contents of the memory address ** from the accumulator
  • STA - stores the value in the accumulator in the memory address **
  • LDA - loads the accumulator with the cocntents of the memory address **

Data can be entered by a user and stored in memory using INP - INP, STA x (stores the input in variable called x

Varaibles are declared and shown using the DAT statement e.g. x DAT

All calculations are carried out in the accumulator and an output statement OUT displays the contents of the accumulator

7 of 9

The Instruction Set, Machine Code Format and Addre

The LMC only contains 11 instructions and the imaginary computer on which it runs only has 100 memory locations, on a real computer there will be many more instructions (multiply, divide, shift left, shift right, ect.)

Machine code is held in 16 bits and can look like this:

The last 2 bits of the operation 'op' code specify the addressing mode which specifies whether the operand represents certain values, the addressing nodes are:

Immediate addressing - Operand holds an actual value

Direct addressing - Operand holds the address of the value (This is what the LMC does)

Indirect addressing - Operand is the location holding the address of the value

Index Addressing

8 of 9

Index Addressing

Using indexed addressing - address of the operand is obtained by adding a constand value to the contents of the general register (called the index register)

Used to access and array whose elements are in successive memory location by incrementing the value in the index register, successive memory locations can be accessed, in the example below, the start point (31) is a fixed value and R0 is the value that increments by 1 each time:

9 of 9


No comments have yet been made

Similar Computing resources:

See all Computing resources »See all Software resources »