180 likes | 483 Views
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
E N D
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 • Machine languages • FORTRAN, COBOL, BASIC • C (CSC 2300)
Example 1: void main() { printf("Hello World\n"); }
Example 2: main() { int x = 30000; int y; y = x+x; printf("twice x is %d\n",y); }
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.
Example 1: public class HelloWorld { public static void main( String[] args ) { System.out.println("Hello World"); } }
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; } }
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()); } }
Example 3: • Robot (really three objects that must interact to solve line following problem) • http://www.ttuembassy.com/DrizzleIntro/
Classes • Object definition Class MotorClass { float speed = 5; void Clockwise(…) { } void Cclockwise(…) { } void Stop(…) { } } MotorClass Motor1 = new MotorClass(); Motor1.Clockwise(); Motor1.Stop(); Motor1.Cclockwise();
Example OO languages • Java (CSC 2010, 2020) • C++ (CSC 2300) • Smalltalk • C# • Python
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
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
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)?
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
Examples (print (+ A B)) (while (<= number 10) body of loop)
Factorial example: (defun factorial (x) (if (eql x 0) 1 (* x (factorial (- x 1)))))