210 likes | 236 Views
CS320n –Visual Programming. Classes, Objects, and World- Level Methods Mike Scott (Slides 4-1). What We Will Do Today. Look at ways of doing the fish problem from last time Define Classes, Objects, and Methods in Alice Define and create world level methods more complicated snowman movie.
E N D
CS320n –Visual Programming Classes, Objects, and World- Level Methods Mike Scott (Slides 4-1)
What We Will Do Today • Look at ways of doing the fish problem from last time • Define Classes, Objects, and Methods in Alice • Define and create world level methods • more complicated snowman movie Classes, Objects, and World Level Methods
Fish Circling Around Island Smooth Jagged Play Movie Classes, Objects, and World Level Methods
Larger Programs • As you become more skilled in writing programs, you will find that your programs quickly begin to increase to many, many lines of code. • Fish program about 30 already • How many things can you keep in short term memory? • Games and other "real world" software applications can have thousands, even millions of lines of code. • Want to organize large programs into small manageable pieces • big rocks into little rocks Classes, Objects, and World Level Methods
Classes, Objects, and Methods • Alice uses classes, objects, and methods as basic programming components. • it is just one way to think about programming • object oriented • there are many others • These components help you to • organize a large program into small pieces • design and think about an intricate program • find and remove errors (bugs) Classes, Objects, and World Level Methods
In your programs,you have been using… • Classes • In Alice, classes are predefined as 3D models • Objects • An object is an instance of a class. Class: Fish (Uppercase name) Objects: fish, fish1, fish2, fish3 (lowercase names) Classes, Objects, and World Level Methods
In your programs,you have also used… • built-in (predefined) methods • Examples: move, turn to face, say • World.my first method • Example: In the snowpeople world (Chapter 2), we wrote program code where the snowman tried to get the attention of a snowwoman. As a reminder, see next slide… Classes, Objects, and World Level Methods
Snowman Program Classes, Objects, and World Level Methods
Snowman Program Classes, Objects, and World Level Methods
More on the Snowman Program • Snowwoman blushes and then turns back to her friend • Snowman turns away and shakes his head • Show Movie Classes, Objects, and World Level Methods
Modifying the program • Modify program to get snowman to try to get snowwoman’s attention twice • To make this modification must add more lines of code • program getting longer, more complicated, more difficult to think about and understand, harder to make changes • program is suffering a “code explosion” • Show movie of trying to get attention twice Classes, Objects, and World Level Methods
Try twice to get attention • Notice the wait command in between the two attempts to get the snowwoman’s attention • wait is one of the commands at the bottom of the method editor Classes, Objects, and World Level Methods
Why is this “Bad”? • Not hard to copy and paste code, right? • Easy to simply say “make copy” of the Do together block and insert a wait • … but, getting a little harder to maneuver around the program with all that code • … and what if we want to change something about the getting attention behavior? • blink eyes twice or change duration of blink Classes, Objects, and World Level Methods
A Solution • A solution to the problem is to • define our own method • name the new method catchAttention • Then, we can drag-and-drop the catchAttention method into the edit box just like the built-in methods Classes, Objects, and World Level Methods
Demo: The solution • First, to associate the new method with the World • select the World tile in the Object Tree • select the methods tab in the details area • click on the "create new method" button Classes, Objects, and World Level Methods
Demo • A demonstration of writing the catchAttention method Classes, Objects, and World Level Methods
World-level method • catchAttention is a world-level method because it -is defined as a method for World • if the method has instructions that involve more than one object it must be a world level method • This method only involves the snowman so we could have made it a method for the snowman Classes, Objects, and World Level Methods
Using the catchAttention method The catchAttention method is executed by calling (invoking) the method from my first method Classes, Objects, and World Level Methods
Testing Method Independently • Can test method by adding event to the program • disable other methods Classes, Objects, and World Level Methods
Why? • Why do we want to write our own methods? • saves time -- we can call the method again and again without reconstructing code • Easy to make changes to behavior • only have to change in one place • reduces code size – we call the method rather than writing the instructions again and again • allows us to "think at a higher level" • can think “catchAttention" instead of “say “Toot” moving eyes up and down" • the technical term for "think at a higher level" is "abstraction" Classes, Objects, and World Level Methods
Exercise for Class • Do problem Chapter 4, exercise 2, Confused Kanga on page 110 of the book • see web for description Classes, Objects, and World Level Methods