BBIT 223/ CISY 210
Object Oriented Programming
Programming Paradigms: An
Overview
25.09.2012
Salesio M. Kiura
• Today
– Recap – Programming Paradigms – Exercises / Sample codeRecap
• From Last week
– Discussed course objectives
– Introduced the concept of Paradigms
• What is a paradigm:
– In science, paradigm describes distinct concepts or thought patterns in any scientific discipline or other epistemological context
• Programming Paradigm (wikipedia)
– A programming paradigm is a fundamental style of computer programming. There are four main paradigms : object-oriented, imperative,
functional and logic programming
A paradigm is not a language!
• Programming Paradigm
– programming “technique” (?) – way of thinking about programming – view of a program
• Programming Language
– consists of words, symbols, and rules for writing a program
Programming paradigms
• Imperative Programming
– program as a collection of statements and procedures affecting data (variables)
• Object-Oriented Programming
– program as a collection of classes for interacting objects
• Functional Programming
– program as a collection of (math) functions
Imperative Programming
• What Makes a Language Imperative?
– Programs written in imperative programming languages consist of
• A program state
• Instructions that change the program state
– Program instructions are “imperative” in the grammatical sense of imperative verbs that express a command
• Imperative verbs are also known as ‘bossy
verbs’- they tell people what to do! e.g. close
the door; empty the bin; eat your dinner!
• Commands in an imperative language are similar to the native machine instructions of traditional computer hardware – the von Neumann-Eckley model.
• Some old Imperative Languages – assembly languages
– 1954-1955: Fortran (FORmulaTRANslator) – Late 1950’s: Algol (ALGOrithmicLanguage)
Example
• What is it?
• What programming
features/aspects can
you identify?
• What has been the
successor?
• Defining Characteristics of Imperative
Languages:
– Statements are commands
• Command order is critical to correct execution; • Programmers control all aspects: algorithm
specification, memory management, variable declarations, etc
– They work by modifying program state
• Imperative: Summary
– Imperative programming is the oldest programming paradigm
– It is based on the von Neumann-Eckley model of a computer
– It works by changing the program state through assignment statements
– Procedural abstraction & structured programming are its design techniques.
Programming paradigms
• Imperative Programming
– program as a collection of statements and procedures affecting data (variables)
• Object-Oriented Programming
– program as a collection of classes for interacting objects
• Functional Programming
Functional Programming
• Functional programming is style of
programming in which the basic method of
computation is the application of functions to
arguments;
• A functional language is one that supports and
encourages the functional style.
• Example:
– The computation method is variable assignment
sum= 0;
for (int i = 1; i ≤ 10; ++i) total = total+i;
• In a functional language this would be
different:
– sum [1..10] Other examples: – Average [1..5] – Prod [2 5]– The computation method is function application
• functional programming is a programming
paradigm that treats computation as the
evaluation of mathematical functions and
avoids state and mutable data
Programming paradigms
• Imperative Programming
– program as a collection of statements and procedures affecting data (variables)
• Object-Oriented Programming
– program as a collection of classes for interacting objects
• Functional Programming
– program as a collection of (math) functions
• Logic programming
Logic Programming
• Based on Logic !
• Logic programming is based on the idea of
using logical sentences to represent programs
and to perform computation
• E.g.
• Example languages
– Prolog – LISP – Etc• Application areas:
– Knowledge representation – Problem solving – etcParadigms: Summary
• Programming Paradigm
– A way of conceptualizing what it means to
perform computation and how tasks to be carried out on the computer should be structured and organized.
– Computation refers to Implementation of an I/O relation
•• Imperative programs
Imperative programs describe the details of
describe the details of
HOW
HOW the results are to be obtained, in terms
the results are to be obtained, in terms
of the underlying machine model.
of the underlying machine model.
• Functional/Logic programs specify WHAT is to
be computed abstractly, leaving the details of
data organization and instruction sequencing
to the interpreter.
Illustration Exercise
• Given the expression (to be computed) as:
a+b+c
• Which is imperative, which is functional?
1. 1.
• T := a + b; T := T + c;
2. 2.
• Load a; Add b; Add c
• Push a; Push b; Add; Push c; Add
Illustration Exercise
• Which is imperative, which is functional? –
answer:
1. Functional
• T := a + b; T := T + c; (Intermediate Code Intermediate Code )
2.
2. Impreative
Impreative
• Load a; Add b; Add c (Accumulator MachineAccumulator Machine) • Push a; Push b; Add; Push c; Add (Stack MachineStack Machine)
Paradigms - continued
• Object-Oriented Style
– Involves programming with Abstract Data Types
•• ADTs specify/describe behaviors.ADTs specify/describe behaviors.
– The basic program unit is a Class
•• Implementation of an ADT.Implementation of an ADT.
• Abstraction is enforced by encapsulation.
– The basic Run-time unit is an Object
•• Instance of a class.Instance of a class. • Has an associated state.
Procedural vs Object-Oriented
programming
• Procedural:
• Emphasis on procedural abstraction. • Top-down design; Step-wise refinement. • Suited for programming in the small.
• Object oriented
• Emphasis on data abstraction.
JAVA
• Is used for creating:
– intelligent consumer-electronic devices (cell phones)
– Web pages with dynamic content
Java life cycle
• Java programs normally undergo four phases:
• Edit (Source code (.java))
Programmer writes program (and stores program on disk) • Compile (Byte codes (.class) , as (.exe) in c++
Compiler creates bytecodes from program (.class as .exe in c++)
• Load
Class loader stores bytecodes in memory • Execute
Interpreter: translates bytecodes into machine language
• Other concepts
– The Java Application Programming Interface (API)
• a large collection of ready-made software components. It is grouped into libraries of related classes and interfaces; these libraries are known as packages.
• E.g. System.out.*; java.util.* – Java Virtual Machine (JVM)
Portability of Java
• Through the Java VM, the same application is
Java technology series
• Java EE vs. Java SE
– EE: enterprise edition (web services, distribution, RMI, …) – SE: standard edition (stand alone applications)
• Development Tools
– The main tools you'll be using are the javac compiler, the java launcher (java), and the javadoc documentation tool.
• Application Programming Interface (API)
– Java SE Development Kit 6 (JDK 6)
– Offers a wide array of useful classes ready for use in your own applications.
• User Interface Toolkits
– Swing and Java 2D toolkits to create Graphical User Interfaces (GUIs).
• Integration Libraries
– Application Programming Interface (API) – Java RMI (Remote Method Invocation) over
Internet and Inter-ORB (Object Request Broker) protocol technology enable database access
• Recap Part 2: Getting started with Java (Using
Jcreator)
• Exercise objective:
– Get started with Java and understand foundations of a Java program
Practice: Your own work
• Required
– Using jCreator, edit (create), compile (build)and run a simple java application that displays “Hello World”
– 20 Mins (because of the typing involved)
setting up JCreator?
public class HelloWorld
{
//main method
public static void main (String args [])
{
System.out.println("Hello World!");
}
• In the Java programming language, every
application must contain a main method whose
signature is:
• public static void main(String[] args)
– The modifiers public and static can be written in either order (public static or static public).
– You can name the argument anything you want, but most programmers choose "args" or "argv.“
– This is the string array that will contains the command line arguments.
• The main method is the entry point for your application and will subsequently invoke all the other methods required by your program.
• System.out.println("Hello World!");
– uses the System class from the API to print the "Hello World!" message to standard output.
Exercise
• Due next week
– Form groups of at least 3 or 4 members
– Formally describe the programming Ideas that were discussed last week
– Post the results as comments in the class blog
http://oop2012sept.wordpress.com/as a comment (use the “leave a reply” link)
– Write a program that stores two integers (e.g. x = 3, y = 2). Then in the program, you should interchange the values such that the value in x is stored in y and vice versa