Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 152: Programming Language Paradigms March 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

Similar presentations


Presentation on theme: "CS 152: Programming Language Paradigms March 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak."— Presentation transcript:

1 CS 152: Programming Language Paradigms March 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak www.cs.sjsu.edu/~mak

2 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 2 Midterm Solution: Question 1a  Write a recursive Scheme procedure call that has three parameters, a math library list, a function name, and a numeric argument. It returns the result of evaluating the named function expression using the argument. You may write any necessary helper procedures.  You want to write procedure call so that it will work with any similarly structured “math library”. (define math-library '((double (+ x x)) (triple (+ x x x)) (square (* x x)) (cube (* x x x)) ))

3 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 3 Midterm Solution: Question 1a, cont’d  To make use of a function expression expr such as '(+ x x), you can turn it into a lambda expression... ... which can be eval ’d into a procedure:  Then apply the procedure to an argument arg : (define math-library '((double (+ x x)) (triple (+ x x x)) (square (* x x)) (cube (* x x x)) )) (list 'lambda (list 'x) expr)  (lambda (x) (+ x x))) ((eval (list 'lambda (list 'x) expr)) arg) (eval (list 'lambda (list 'x) expr))  #

4 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 4 Midterm Solution: Question 1a, cont’d  So the problem becomes a recursive search for the correct function expression to apply to the argument: (define math-library '((double (+ x x)) (triple (+ x x x)) (square (* x x)) (cube (* x x x)) )) (define call (lambda (library name arg) (cond ((null? library) '()) ((eq? name (caar library)) ((eval (list 'lambda (list 'x) (cadar library))) arg)) (else (call (cdr library) name arg)) ))) Function name Function expression

5 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 5 Midterm Solution: Question 1b  Suppose Scheme didn’t have the if special form and therefore you decided to invent your own:  Describe what happens when you use my-if in the factorial procedure, and explain why that happens. (define my-if (lambda (test true-part false-part) (cond (test true-part) (else false-part) ))) (define fact (lambda (n) (my-if (= n 0) 1 (* n (fact (sub1 n))) )))

6 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 6 Midterm Solution: Question 1b, cont’d  Procedure fact goes into an infinite loop.  Note that my-if is a procedure. Therefore, is a procedure call with three arguments. All three arguments are always evaluated during a call, even when n is 0.  Infinite looping when n goes negative. (define my-if (lambda (test true-part false-part) (cond (test true-part) (else false-part) ))) (define fact (lambda (n) (my-if (= n 0) 1 (* n (fact (sub1 n))) ))) (my-if (= n 0) 1 (* n (fact (sub1 n))))

7 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 7 Midterm Solution: Question 2a  What does predicate guzzinta do?  guzzinta instantiates Q (quotient) to be the integer result of N (numerator) divided by D (denominator). How many times does D “guzzinta” (goes into) N ? _ /*1*/ is_integer(0). /*2*/ is_integer(N) :- is_integer(M), N is M+1. /*3*/ guzzinta(D, N, Q) :- is_integer(Q), /*4*/ P1 is Q * D, /*5*/ P2 is (Q + 1) * D, /*6*/ P1 =< N, /*7*/ P2 > N, /*8*/ !. ?- guzzinta(3, 14, R). R = 4. ?- guzzinta(2, 3, R). R = 1.

8 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 8 Midterm Solution: Question 2b  Explain how the second query above is satisfied in terms of resolution, unification, backtracking, and the cut operation. _ /*1*/ is_integer(0). /*2*/ is_integer(N) :- is_integer(M), N is M+1. /*3*/ guzzinta(D, N, Q) :- is_integer(Q), /*4*/ P1 is Q * D, /*5*/ P2 is (Q + 1) * D, /*6*/ P1 =< N, /*7*/ P2 > N, /*8*/ !. ?- guzzinta(3, 14, R). R = 4. ?- guzzinta(2, 3, R). R = 1.

9 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 9 Midterm Solution: Question 2b, cont’d /*1*/ is_integer(0). /*2*/ is_integer(N) :- is_integer(M), N is M+1. /*3*/ guzzinta(D, N, Q) :- is_integer(Q), /*4*/ P1 is Q * D, /*5*/ P2 is (Q + 1) * D, /*6*/ P1 =< N, /*7*/ P2 > N, /*8*/ !. [trace] 3 ?- guzzinta(2, 3, R). Call: (6) guzzinta(2, 3, _G2166) ? creep Call: (7) is_integer(_G2166) ? creep Exit: (7) is_integer(0) ? creep Call: (7) _G2242 is 0*2 ? creep Exit: (7) 0 is 0*2 ? creep Call: (7) _G2248 is (0+1)*2 ? creep Exit: (7) 2 is (0+1)*2 ? creep Call: (7) 0=<3 ? creep Exit: (7) 0=<3 ? creep Call: (7) 2>3 ? creep Fail: (7) 2>3 ? creep Redo: (7) is_integer(_G2166) ? creep Call: (8) is_integer(_G2239) ? creep Exit: (8) is_integer(0) ? creep Call: (8) _G2166 is 0+1 ? creep Exit: (8) 1 is 0+1 ? creep Exit: (7) is_integer(1) ? creep Call: (7) _G2245 is 1*2 ? creep Exit: (7) 2 is 1*2 ? creep Call: (7) _G2251 is (1+1)*2 ? creep Exit: (7) 4 is (1+1)*2 ? creep Call: (7) 2=<3 ? creep Exit: (7) 2=<3 ? creep Call: (7) 4>3 ? creep Exit: (7) 4>3 ? creep Exit: (6) guzzinta(2, 3, 1) ? creep R = 1.  Match Rule 3 (resolution).  Instantiate D to 2 and N to 3 (unification).  First subgoal: Match Fact 1.  Fail the fifth subgoal (2 > 3).  Backtrack from Fact 1 to Rule 2.  Cut prevents a final backtrack. _

10 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 10 Midterm Solution: Question 2c /*1*/ is_integer(0). /*2*/ is_integer(N) :- is_integer(M), N is M+1. /*3*/ guzzinta(D, N, Q) :- is_integer(Q), /*4*/ P1 is Q * D, /*5*/ P2 is (Q + 1) * D, /*6*/ P1 =< N, /*7*/ P2 > N, /*8*/ !. [trace] 3 ?- guzzinta(2, 3, R). Call: (6) guzzinta(2, 3, _G2166) ? creep Call: (7) is_integer(_G2166) ? creep Exit: (7) is_integer(0) ? creep Call: (7) _G2242 is 0*2 ? creep Exit: (7) 0 is 0*2 ? creep Call: (7) _G2248 is (0+1)*2 ? creep Exit: (7) 2 is (0+1)*2 ? creep Call: (7) 0=<3 ? creep Exit: (7) 0=<3 ? creep Call: (7) 2>3 ? creep Fail: (7) 2>3 ? creep Redo: (7) is_integer(_G2166) ? creep Call: (8) is_integer(_G2239) ? creep Exit: (8) is_integer(0) ? creep Call: (8) _G2166 is 0+1 ? creep Exit: (8) 1 is 0+1 ? creep Exit: (7) is_integer(1) ? creep Call: (7) _G2245 is 1*2 ? creep Exit: (7) 2 is 1*2 ? creep Call: (7) _G2251 is (1+1)*2 ? creep Exit: (7) 4 is (1+1)*2 ? creep Call: (7) 2=<3 ? creep Exit: (7) 2=<3 ? creep Call: (7) 4>3 ? creep Exit: (7) 4>3 ? creep Exit: (6) guzzinta(2, 3, 1) ? creep R = 1.  Why is the cut necessary?  Cut prevents a final backtrack after a solution is found. Q is bound to the quotient.  Without the cut, if you press ; after the solution, the program goes into an infinite loop with ever increasing values of N.

11 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 11 Midterm Solution: Question 3  Classes and objects that you create for a Smalltalk program become part of the entire “ecosystem” of previously defined Smalltalk classes and objects.  A typical Java program is standalone. It may use previously defined classes and packages. But unless you “jar” the program into the previously defined packages, it does not become part of the Java ecosystem. _

12 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 12 Eclipse C/C++ Development Tool (CDT)  Install CDT as a plug-in to an existing Eclipse installation: http://www3.ntu.edu.sg/home/ehchua/programming/how to/EclipseCpp_HowTo.html You will also need to install a C/C++ compiler separately.  Now you can develop, debug, and run Java, C, and C++ programs within a single IDE. _

13 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 13 Allocation of Java Objects  When created, Java objects are always allocated in the runtime heap. heap: An area of memory reserved at run time to store dynamically allocated objects.  Java objects are always explicitly dynamically allocated with new. _

14 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 14 Allocation of Java Objects, cont’d  A Java object is automatically garbage-collected when there are no longer any references to it.  The class’s finalize method is called by the garbage collector. Java provides a do-nothing finalize method for every class. You can override the class. There is no guarantee which thread will invoke the finalize method. _

15 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 15 Allocation of Java Objects, cont’d public class MyClass { private int field; public MyClass(int f) { this.field = f; System.out.println("MyClass constructor called: field = " + field); } protected void finalize() { System.out.println("MyClass finalize called: field = " + field); } public class AllocateTest { public static void main(String[] args) { MyClass mc = new MyClass(12); } MyClass constructor called: field = 12 Demo CONSTRUCTOR

16 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 16 Allocation of C++ Objects  Automatically allocated on the runtime stack via a declaration. Automatically deallocated when going out of scope.  Explicitly dynamically allocated in the runtime heap with new. Must be explicitly deallocated with delete. _

17 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 17 Allocation of C++ Objects, cont’d  C++ class MyClass : class MyClass { private: int field; public: MyClass(int f) : field(f) { cout << "MyClass constructor called: field = " << field << endl; } ~MyClass() { cout << "MyClass destructor called: field = " << field << endl; } }; CONSTRUCTOR DESTRUCTOR

18 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 18 Allocation of C++ Objects, cont’d void test() { cout << endl << "Automatic allocation on the runtime stack:" << endl; MyClass mc(11); cout << endl << "Dynamic allocation:" << endl; MyClass *mcp = new MyClass(12); cout << endl << "Explicit deletion of dynamically allocated object:" << endl; delete(mcp); cout << endl << "Automatic deallocation when going out of scope:" << endl; } int main() { test(); } Automatic allocation on the runtime stack: MyClass constructor called: field = 11 Dynamic allocation: MyClass constructor called: field = 12 Explicit deletion of dynamically allocated object: MyClass destructor called: field = 12 Automatic deallocation when going out of scope: MyClass destructor called: field = 11 Demo

19 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 19 Virtual vs. Static Method Binding  Some object-oriented languages do virtual binding of methods at run time. Example: Java  Other object-oriented languages can do either static binding or virtual binding. Example: C++ _

20 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 20 Java Virtual Methods  Java class Animal : public class Animal { public void what() { System.out.println("I am Animal."); } public void sound() { System.out.println("I make animal noises."); } public void identify() { what(); sound(); } }

21 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 21 Java Virtual Methods, cont’d  Class Animal has a subclass Cat :  The subclass overrides methods what() and sound(). The annotation @Override ensures proper overriding. public class Cat extends Animal { @Override public void what() { System.out.println("I am Cat."); } @Override public void sound() { System.out.println("I purr."); } }

22 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 22 Java Virtual Methods, cont’d  The main class VirtualTest1. public class VirtualTest1 { public static void main(String[] args) { Animal animal = new Animal(); Cat cat = new Cat(); System.out.println("\nAfter dynamic allocation:"); animal.identify(); cat.identify(); System.out.println("\nAfter animal = cat:"); animal = cat; animal.identify(); } }

23 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 23 Java Virtual Methods, cont’d public class Animal { public void what() { System.out.println("I am Animal."); } public void sound() { System.out.println("I make animal noises."); } public void identify() { what(); sound(); } public class VirtualTest1 { public static void main(String[] args) { Animal animal = new Animal(); Cat cat = new Cat();... animal.identify(); cat.identify();... } } I am Animal. I make animal noises. I am Cat. I purr.  Java methods are virtual. The correct methods are determined at run time according to the types of the objects. Variable animal refers to an Animal object, and variable cat refers to a Cat object.

24 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 24 Java Virtual Methods, cont’d  Now variable animal also refers to a Cat object. Virtual methods enable the right methods to be called at run time. public class Animal { public void what() { System.out.println("I am Animal."); } public void sound() { System.out.println("I make animal noises."); } public void identify() { what(); sound(); } public class VirtualTest1 { public static void main(String[] args) { Animal animal = new Animal(); Cat cat = new Cat();... animal = cat; animal.identify();... } } I am Cat. I purr.

25 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 25 Java Virtual Methods, cont’d  It doesn’t matter if we change the type of variable cat to Animal. Variable cat still refers to a Cat object. Demo public class Animal { public void what() { System.out.println("I am Animal."); } public void sound() { System.out.println("I make animal noises."); } public void identify() { what(); sound(); } public class VirtualTest2 { public static void main(String[] args) { Animal animal = new Animal(); Animal cat = new Cat();... animal.identify(); cat.identify();... } } I am Animal. I make animal noises. I am Cat. I purr.

26 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 26 C++ Static and Virtual Methods  C++ class Animal : class Animal { public: void what() { cout << "I am Animal." << endl; } virtual void sound() { cout << "I make animal noises." << endl; } void identify() { what(); sound(); } };  C++ methods are static by default. Specify virtual to make a method virtual instead. STATIC VIRTUAL

27 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 27 C++ Static and Virtual Methods, cont’d  Class Animal has a subclass Cat :  The subclass overrides methods what() and sound(). _ class Cat : public Animal { public: void what() { cout << "I am Cat." << endl; } void sound() { cout << "I purr." << endl; } };

28 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 28 C++ Static and Virtual Methods, cont’d  Main: int main() { Animal animal; Cat cat; cout << endl << "After automatic allocation:" << endl; animal.identify(); cat.identify(); cout << endl << "After animal = cat:" << endl; animal = cat; animal.identify(); Animal *animalp = new Animal(); Cat *catp = new Cat(); cout << endl << "After dynamic allocation:" << endl; animalp->identify(); catp->identify(); cout << endl << "After animalp = catp:" << endl; animalp = catp; animalp->identify(); return 0; }

29 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 29 C++ Static and Virtual Methods, cont’d  Method what() is static. Animal method identify() will always call static Animal method what() regardless of the object type. class Animal { public: void what() { cout << "I am Animal." << endl; } virtual void sound() { cout << "I make animal noises." << endl; } void identify() { what(); sound(); } }; int main() { Animal animal; Cat cat; animal.identify(); cat.identify();... } I am Animal. I make animal noises. I am Animal. I purr.

30 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 30 C++ Static and Virtual Methods, cont’d  Variable animal gets a copy of variable cat ’s contents. But its type is still an Animal object. int main() { Animal animal; Cat cat;... animal = cat; animal.identify(); } I am Animal. I make animal noises. class Animal { public: void what() { cout << "I am Animal." << endl; } virtual void sound() { cout << "I make animal noises." << endl; } void identify() { what(); sound(); } };

31 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 31 C++ Static and Virtual Methods, cont’d  Dynamically allocating objects. Animal method what() is still static. class Animal { public: void what() { cout << "I am Animal." << endl; } virtual void sound() { cout << "I make animal noises." << endl; } void identify() { what(); sound(); } }; int main() { Animal *animalp = new Animal(); Cat *catp = new Cat(); animalp->identify(); catp->identify();... } I am Animal. I make animal noises. I am Animal. I purr.

32 SJSU Dept. of Computer Science Spring 2014: March 19 CS 152: Programming Language Paradigms © R. Mak 32 C++ Static and Virtual Methods, cont’d  Now pointer variable animalp also refers to a Cat object. int main() { Animal *animalp = new Animal(); Cat *catp = new Cat();... animalp = catp; animalp->identify(); } I am Animal. I purr. Demo class Animal { public: void what() { cout << "I am Animal." << endl; } virtual void sound() { cout << "I make animal noises." << endl; } void identify() { what(); sound(); } };


Download ppt "CS 152: Programming Language Paradigms March 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak."

Similar presentations


Ads by Google