Imperative Programming Paradigm. Procedural Programming.

Slides:



Advertisements
Similar presentations
Understand and appreciate Object Oriented Programming (OOP) Objects are self-contained modules or subroutines that contain data as well as the functions.
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
MT311 (Oct 2007) Java Application Development Object-Oriented Programming Languages Tutorial 8.
4. Object-Oriented Programming Procedural programming Structs and objects Object-oriented programming Concepts and terminology Related keywords.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 OBJECT-ORIENTED CONCEPTS. 2 What is an object?  An object is a software entity that mirrors the real world in some way.  A software object in OOP.
Chapter 12: Support for Object-Oriented Programming
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
1 Software Testing and Quality Assurance Lecture 12 - The Testing Perspective (Chapter 2, A Practical Guide to Testing Object-Oriented Software)
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Stéphane Ducasse6.1 Essential Concepts Why OO? What is OO? What are the benefits? What are the KEY concepts? Basis for all the lectures.
Object-oriented Programming Concepts
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Object-oriented design CS 345 September 20,2002. Unavoidable Complexity Many software systems are very complex: –Many developers –Ongoing lifespan –Large.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 12 Topics Introduction Object-Oriented Programming.
Object Oriented Programming
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Introduction to Object-oriented Programming Introduction to Object-oriented Programming CMPS 2143.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CSCI-383 Object-Oriented Programming & Design Lecture 4.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
CONCEPTS OF OBJECT ORIENTED PROGRAMMING. Topics To Be Discussed………………………. Objects Classes Data Abstraction and Encapsulation Inheritance Polymorphism.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Unified Modeling Language, Version 2.0
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Abstraction ADTs, Information Hiding and Encapsulation.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
Chapter 10, Slide 1 ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:  process abstraction  data abstraction Both provide:  information.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object-Oriented Programming Chapter Chapter
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
S.Ducasse Stéphane Ducasse 1 Essential OO Concepts Stéphane Ducasse.
ISBN Object-Oriented Programming Chapter Chapter
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
1 Unified Modeling Language, Version 2.0 Chapter 2.
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
ISBN Chapter 12 Support for Object-Oriented Programming.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:
Polymorphism.
12 Data abstraction Packages and encapsulation
Week 6 Object-Oriented Programming (2): Polymorphism
Software Design Lecture : 12.
CPS120: Introduction to Computer Science
Lecture 10 Concepts of Programming Languages
Presentation transcript:

Imperative Programming Paradigm

Procedural Programming

Imperative/Procedural Paradigm The imperative paradigm is characterized by the finite state machine computational model. The imperative programming paradigm assumes that the program maintains a modifiable store. By changing the values of variables we alter what is stored, thus record state changes. Computations are performed through a sequence of steps specified by a list of commands. When imperative programming is combined with subprograms, it is called procedural programming.

Procedural Programming Shared Store (State) Function 1Function 3Function 5 Function 2Function 4

Procedural Programming (2) The model does not scale well and large programs that use this model are difficult to maintain. It is difficult to find which functions are effected when the structure of the shared store is changed in any way. It is difficult to find relationships between functions that communicate indirectly through the shared store.

Modules One solution is to divide a program into units of separately compiled components called modules. Most procedural languages have some mechanism for creating modules. Each module has its own store which is shared internally. A module is encapsulated by a wall through which identifiers are invisible unless explicitly declared part of the modules interface. Modules are less powerful than abstract data types because they cannot be instantiated.

Object-Oriented Programming

Topics Object-oriented thinking –Agents, messages, responsibility Abstract data types Inheritance Polymorphism Late binding

Object-Oriented Thinking Problem: How to get flowers to Grandma in California? Procedural solution: –1. Plant flowers –2. Water flowers –3. Pick flowers –4. Drive to CA Object-oriented solution: –1. Call local florist

Agents and Responsibilities Me Local Florist CA Florist Delivery Person Grandma Flower ArrangerWholesaler Grower

Narrowing the Semantic Gap The power of metaphor –Thinking about problems in terms of slots and values doesn’t provide much insight into how a program should be structured. –Thinking in terms of behaviors and responsibilities of agents, brings to mind a wealth of intuition, ideas, and understanding from everyday experience. Computation as simulation –Define a set of entities and how they interact, then set the system in motion. Programming by delegation –Object-oriented programmers think first about passing the buck.

Responsibility and Independence Ask not what you can do to your data structures, but what your data structures can do for you. - Timothy Budd

Abstract Data Types Quiz: What is the IEEE754 standard 32-bit representation of the floating point decimal number ? When (for example) a float is incremented, its representation is changed. How is it possible for a programmer to accomplish the change without knowing the underlying representation?

Abstract Data Types (2) float is an ADT, defined in terms of a set of operations (+, -, ++, etc.) not its internal representation. float x, y; // x and y are instances of type float Objects are instances of programmer defined ADT’s.

Abstract Data Types (3) Example: Stack ADT defined in terms of operations push, pop, etc. Stack s1 = new Stack(); Stack s2 = new Stack(); s1.push(5); s2.push(6); s1.push(7); int i = s1.pop(); // 7 int j = s1.pop(); // 5 int k = s2.pop(); // 6

Classes Most object-oriented languages use classes to implement ADT’s. class Stack { private int [] data = new int[1000]; private int top = 0; public void push(int n) { data[top++] = n; } public int pop() { return data[--top]; } }

Preventing the Y2K problem ? interface Date { // Methods for comparing dates public boolean before(Date d); public boolean after(Date d); public boolean equals(Date d); // Formatting and parsing public String toString(); public void parse(String s); // Month, day, year manipulation public int getMonth(); public void setMonth(int mon); public int getDay(); public void setDay(int day); public int getYear(); public void setYear(int year); }

Y2K Problem (2) // Check if expiration date // has passed Date today; Textfield month, year; Boolean expired; // Code to initialize variables, set up GUI, etc… //... int mon = today.getMonth(); int yr = today.getYear(); int mon2 = Integer.parse(month.getText()).intValue(); int yr2 = Integer.parse(year.getText()).intValue(); if (yr == yr) expired = (mon2 > mon); else expired = (yr > yr );

Y2K Problem (3) interface Date { // Precision constants public final int YEAR = 0; public final int MONTH = 1; public final int DAY = 2; // Methods for comparing dates public boolean before(Date d); public boolean after(Date d); public boolean equals(Date d); public boolean before(Date d, int precision); public boolean after(Date d, int precision); public boolean equals(Date d, int precision); // Formatting and parsing public String toString(); public void parse(String s); }

Y2K Problem (4) // Check if expiration date // has passed Date today, expiration; Textfield month, year; Boolean expired; // Code to initialize variables, set up GUI, etc… //... String expDate = month.getText() + “/” + year.getText(); expiration.parse(expDate); expired = today.after(expiration, Date.MONTH);

Object-Oriented Paradigm Inheritance –Reuse mechanism by which a new class is derived from an existing class Overriding –The redefinition of an inherited method in a subclass Polymorphism –The ability of a variable to refer to objects of more than one type Late-binding –The selection of the method to invoke based on the dynamic (runtime) type of the object receiving the message

Example: Taxonomy of Animals class Animal { private float weight; boolean canFly() { return false; } } class Mammal extends Animal { private Color hairColor; } class Bear extends Mammal { } class Bird extends Animal { boolean canFly() { return true; } } class Penguin extends Bird { boolean canFly() { return false; } } Animal a; boolean b; a = new Bird(); b = a.canFly(); // true inheritance overriding polymorphism late binding

Class hierarchies Penguin p = new Penguin(); p Animal Bird Penguin

Implementation of OO Language class A { private int x; private int y; public int foo(int n) { return n*(x+y); } } A a1 = new A(); A a2 = b1; // OK class B extends A { private int i; public int bar() { return i*(x+y); // error return foo(i); // OK } B b1 = new B(); B b2 = a1; // error int x int y int foo(int) int x int y int foo(int) int i int bar() impl of foo impl of bar a1b1

Example: CAD Drawing module struct Circle { Point center; int radius; }; struct Rect { Point topLeft; Point bottomRight; }; enum { CIRCLE, RECT, … }; struct Shape { int type; union { Circle c; Rect r; //… } shape; }; void drawCircle(struct Circle) { // … } void redraw(struct Shape s[], int n) { for (int i = 0; i < n; i++) switch(s[i].type) { case CIRCLE: drawCircle(s[i].shape.c); break; case RECT: drawRect(s[i].shape.r); break; // … }

CAD Drawing module (OO) class Shape { public void draw() { } } class Circle extends Shape { Point center; int radius; public void draw() { // … } class Rect extends Shape { Point topLeft; Point bottomRight; public void draw() { // … } void redraw(Shape[] s, int n) { for (int i = 0; i < n; i++) s[i].draw(); }

Key Points Object-oriented programming style may be viewed as: message passing, simulation, metaphor, or delegation. Data abstraction is central to OOP –Defining and using abstract data types Inheritance, Polymorphism, and Late-binding promote the development of robust, reusable code.