Wohoo,! October 22, 2016
Revision 1 Objects 2 3
What is an Object? Objects Revision Tables that map keys to values Efficient data structure than using lists Three notations: square bracket, dot-nation and literal Order of growth (runtime) for accessing and updating an entry? Θ(1)! Things to put in object entry! Functions: obj["a"] = function(x) { }; Strings: obj["b"] = "bbq"; Variables: obj["c"] = my_pair; What happens when we change the value of my_pair?
Problem One: Drawing! Revision Question Following the notation introduced in Lecture 9B, draw a graphical representation of the two objects my_object and my_other_object. For Your Reference var my_object = {}; my_object["a"] = 1; my_object["b"] = 2; my_object["my very long key"] = 2; var my_other_object = { d : 1, e : "a string", f : true, my_fun : function (x) {} };
Problem One: Solution Objects Revision my_object & my_other_object
Problem Two: Some Play Revision Question Familiarize yourself with the object notation, by assigning the variable theatre_play to an object with the attributes title, number_of_spectators, venue, and number_of_actors, using the three different syntactic variants: Square bracket notation, dot-notation and literal object creation. You can use as values the data from the last theatre play that you watched. Any theatre goers?
Problem Two: Solution Objects Revision Square bracket notation var theatre_play = {}; theatre_play["title"] = "a Play featuring an iphone power block and a mechanical pencil"; theatre_play["number_of_spectators"] = 266346; theatre_play["venue"] = "M8izj_mKa-8"; theatre_play["number_of_actors"] = 2;
Revision Problem Two: Solution (continued) Dot-notation var theatre_play = {}; theatre_play.title = "a Play featuring an iphone power block and a mechanical pencil"; theatre_play.number_of_spectators = 266346; theatre_play.venue = "M8izj_mKa-8"; theatre_play.number_of_actors = 2;
Revision Problem Two: Solution (continued) Literal var theatre_play = { title: "a Play featuring an iphone power block and a mechanical pencil", number_of_spectators: 266346, venue: "M8izj_mKa-8", number_of_actors: 2 };
Problem Three: More drawing! Revision Question Draw a graphical representation for x, y, z after evaluating the following programs. var x = pair(1, my_object); var y = {q: 4, p: theatre_play}; var z = my_object; z.play = pair(theatre_play, theatre_play);
Problem Three: Solution Revision Unified diagram!
Review What is Object Oriented Programming? System is organized around objects that receive messages An object encapsulates data (state) and operations (behaviour) Major Concepts Classes and instances Methods and message passing Inheritance Polymorphism
Review Problem One: DRAW AGAIN?!!? Question Draw the box-and-pointer diagrams of the objects Vessel, ContainerShip, my_boat, my_ship that result from evaluating the following program.
Problem One: Solution Objects Review Diagram :(
Problem Two: Buying Ships Review Question Construct these objects without using a single. and without using new. After evaluating your program, the following program should work as expected. Hint Dot-notation is out. What about literal and square bracket notation?
Problem Two: Solution Objects Review Literal Version var my_boat = { displacement: 20, proto : Vessel["prototype"] }; var my_ship = { displacement: 500, containers: list("c1", "c2"), proto : ContainerShip["prototype] };
Review Problem Two: Solution (continued) Square bracket Version var my_boat = {}; my_boat["displacement"] = 20; my_boat[" proto "] = Vessel["prototype"]; var my_ship = {}; my_boat["displacement"] = 500; my_boat["containers"] = list("c1", "c2"); my_boat[" proto "] = ContainerShip["prototype"];
Review Problem One (reset): Gorilla or Student? Question Now lets create a student object named Harambe. Since Harambe was a little sick on exam day, he didnt perform well and got an exam score of 45. After the creation of the student object, verify all the attribute values of Harambe and then make Harambe introduce himself. Why does it work? Solution var harambe = new Student("Harambe", 45); harambe.introduce_self(); Why? Consider where the variables are stored!
Problem Two: Gorilla with IQ Review Question After some time, the professor realized that he had made a mistake in the grading, and Harambes exam score was amended to 60. Update Harambes exam score to 60, and verify again all the attribute values of Harambe. Is there anything wrong? Solution Change score: harambe.exam_score = 60; Why? Check if we are assigning a value or an expression?
Review Problem Three: Recording System PRO Question To fix the problem above, lets add to the Student class a method called update_score, which updates the students module grade and mood based on a given score.
Problem Three: Solution Review Solution Student.prototype.update_score = function (new_score) { this.exam_score = new_score; this.module_grade = this.exam_score < 50? "C" : this.exam_score <= 75? "B" : "A"; this.mood = this.module_grade === "C"? "still happy because I can..." : "happy"; };
Problem Four: Introduction? Review Question We now want to change the students way of introducing himself/herself such that he/she also introduces himself/herself as a student. We try to achieve this by adding these lines: Student.introduce_self = function(){ display("hello, my name is " + this.name +" and my mood is " + this.mood); display("i am also a student!"); }; Try letting Harambe introduce himself again. Is there any difference? Why?
Problem Four: Solution Review Why? Differences between prototype and instance variables.
Problem Five: Irritating Objects Review Question Correct the above problem so that a student can introduce himself/herself as a student. Can Harambe now introduce himself correctly? Will it still work if we define introduce_self in Student.prototype? Why? Solution It will work. It is due to how a function call is processed by the interpreter (using proto )
Problem One Eh: Flip/Flop Question Define a function flip (with no parameters) that returns 1 the first time it is called, 0 the second time it is called, 1 the third time, 0 the fourth time, and so on. Answer function make_flip() { var state = 0; return function() { state = (state === 1)? 0 : 1; return state; }; } var flip = make_flip();
Problem One Bee: FlipFactory Question Define a class Flip that can be used to generate flip objects. That is, we should be able to write var flip = new Flip(); Define a constructor Flip and a method flip. The first time you invoke the method flip on a Flip object, it returns 1, the second time 0, the third time 1, and so on.
Problem One Bee: Solution Solution function Flip() { this.state = 0; } Flip.prototype.flip = function() { this.state = (this.state === 1)? 0 : 1; return this.state; };