1 / 18

Programming Paradigms

Programming Paradigms. CSC 2001. Overview. Imperative programming Object-oriented programming Declarative programming Functional programming. Imperative programming. Traditional approach Sequence of commands to transform data into solution Examples All the pseudo-code we have seen

nuru
Download Presentation

Programming Paradigms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Paradigms CSC 2001

  2. Overview • Imperative programming • Object-oriented programming • Declarative programming • Functional programming

  3. Imperative programming • Traditional approach • Sequence of commands to transform data into solution • Examples • All the pseudo-code we have seen • Machine languages • FORTRAN, COBOL, BASIC • C (CSC 2300)

  4. Example 1: void main() { printf("Hello World\n"); }

  5. Example 2: main() { int x = 30000; int y; y = x+x; printf("twice x is %d\n",y); }

  6. Object-oriented programming • Problems addressed by identifying entities called objects. • An object has properties together with the set of things that can be done to it. • These objects interact to solve the problem.

  7. Example 1: public class HelloWorld {   public static void main( String[] args )   {      System.out.println("Hello World");   } }

  8. Example 2: public double withdraw( double amount ) { // See if amount can be withdrawn if (amount > 0.0 && balance >= amount) { balance = balance - amount; return amount; } else // Withdrawal not allowed return 0.0; } public double getBalance() { return balance; } } public class Account { protected double balance; // Constructor to initialize balance public Account( double amount ) { balance = amount; } // Overloaded constructor for empty balance public Account() { balance = 0.0; } public void deposit( double amount ) { if (amount >= 0) { balance = balance + amount; } }

  9. Example 2 cont: class AccountDemo { public static void main(String[] args) { // Create an empty account Account my_account = new Account(); // Deposit money my_account.deposit(250.00); // Print current balance System.out.println ("Current balance " + my_account.getBalance()) // Withdraw money double withdrawAmount = my_account.withdraw(80.00); if (withdrawAmount < 80.00) System.out.println(“Withdrawal failed”); else System.out.println(“Withdraw successful”); // Print remaining balance System.out.println ("Remaining balance " + my_account.getBalance()); } }

  10. Example 3: • Robot (really three objects that must interact to solve line following problem) • http://www.ttuembassy.com/DrizzleIntro/

  11. Classes • Object definition Class MotorClass { float speed = 5; void Clockwise(…) { } void Cclockwise(…) { } void Stop(…) { } } MotorClass Motor1 = new MotorClass(); Motor1.Clockwise(); Motor1.Stop(); Motor1.Cclockwise();

  12. Example OO languages • Java (CSC 2010, 2020) • C++ (CSC 2300) • Smalltalk • C# • Python

  13. Imperative vs. OO • Imperative • step-by-step thinking • recipe • OO • attempts to model “real world” • reusable components/modules • student class • might be able to reuse in lots of programs that involve student data

  14. Declarative programming • Program consists of describing the problem to a general purpose problem solver. • The problem solver can then try to figure out how to reach a solution. • Example • Prolog • Provide Prolog with a set of facts about the “world” and rules about how to reason with facts. • Can then ask it to solve problems for you based on the facts and rules that it knows

  15. Prolog example /* Note that ancestor(A, B) means that A is an ancestor of B. */ ancestor(bob, susan). ancestor(A, X) :- parent(A, X). ancestor(A, X) :- parent(A, C), ancestor(C, X). /* Note that parent(P, C) means that P is a parent of C. */ parent(fred, sally). parent(tina, sally). parent(sally, john). parent(sally, diane). parent(sam, bill). ancestor(fred, john)? ancestor(sally, sam)?

  16. Functional programming • Primitives are basic functions that are combined (nested) to construct higher-order functions and solve problems • Typically don’t declare variables • Examples • LISP (LISt Processing), Scheme

  17. Examples (print (+ A B)) (while (<= number 10) body of loop)

  18. Factorial example: (defun factorial (x) (if (eql x 0) 1 (* x (factorial (- x 1)))))

More Related