1 / 21

CS320n –Visual Programming

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.

bconyers
Download Presentation

CS320n –Visual Programming

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. CS320n –Visual Programming Classes, Objects, and World- Level Methods Mike Scott (Slides 4-1)

  2. 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

  3. Fish Circling Around Island Smooth Jagged Play Movie Classes, Objects, and World Level Methods

  4. 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

  5. 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

  6. 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

  7. 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

  8. Snowman Program Classes, Objects, and World Level Methods

  9. Snowman Program Classes, Objects, and World Level Methods

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. Demo • A demonstration of writing the catchAttention method Classes, Objects, and World Level Methods

  17. 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

  18. Using the catchAttention method The catchAttention method is executed by calling (invoking) the method from my first method Classes, Objects, and World Level Methods

  19. Testing Method Independently • Can test method by adding event to the program • disable other methods Classes, Objects, and World Level Methods

  20. 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

  21. 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

More Related