0% found this document useful (0 votes)
8 views

Programming Paradigms

The document discusses different programming paradigms and how they are used to solve problems. It covers imperative, declarative, functional, logic and object-oriented paradigms. The object-oriented paradigm uses classes and objects to combine data and behavior.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Programming Paradigms

The document discusses different programming paradigms and how they are used to solve problems. It covers imperative, declarative, functional, logic and object-oriented paradigms. The object-oriented paradigm uses classes and objects to combine data and behavior.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Programming Paradigms

- Different approaches to programming


- Each have pros and cons
- Used to solve the same problem
- Many programming languages are multi-paradigm
- Some languages lean towards supporting one particular paradigm

Imperative
Explicit sets of commands that specify how to solve
- Structured
- Programming to interfaces with clear control flow
- Procedural
- Using subroutines to carry out specific tasks on data
- Object Oriented
- Using objects to combine behaviour and data

Declarative
Expressing what the program should solve
E.g. SQL: SELECT Name, Address FROM Employees;
- Functional
- Expresses problems as a series of functions and functions calls

1. Imperative
E.g. Free Basic

2. Functional
Good when you have a fixed set of things, and as your code evolves, you primarily
add new operations on those existing things
Remove side effects
Directly links input to output

3. Logic
E.g. Prolog

4. Object Oriented
E.g. Java & Python
Good when you have a fixed set of operations on things
Classes
- Contain attributes & methodsProduce “objects”
Inheritance
- Subclasses inherit all the attributes and methods of the superclass. These
subclasses act as extensions to the superclasses.
Abstraction
- The process of designing classes so they are reduced to their necessary attributes and methods
Attributes are meaningful information that are treated as characteristics or features
Objects have behaviours, these are called ‘methods’ or ‘functions’ (not the same thing)
Methods are like actions
Objects interact with each other, dynamic interactions
Users initiate object activity, events and input get started in ‘Main’

Stub: situation in code where it is still under development. E.g. a button that returns a message that it isn’t
finished yet instead of completing its action.

public class Main {

public class Headphones {

String charge = "Micro usb";


String[] controls = {"power", "volume", "skip", "play/pause"};
String colour = "red/black";

static boolean power = false;


static in volume = 0;

public static void powerOn(){


power = true;
}
public static void powerOff(){
power = false;
}
public static void volumeUp(){
volume++;
}
public static void volumeDown(){
volume--;
}
}
public static void main(String[] args) {
Pen p = new Pen();

System.out.println(p.colour);
System.out.println(p.point);
System.out.println(p.type);

System.out.println(p.clicked);

p.click

System.out.println(p.clicked);

public class Pen {

String type = "gel";


String colour = "blue";
int point = 10;

boolean clicked = false;

public static void click() {


clicked = true;
}

public static void unclick() {


clicked = false;
}
}
}

You might also like