1 / 19

CompSci 4 Chap 4 Sec 3- Part 1 Feb 15, 2005

CompSci 4 Chap 4 Sec 3- Part 1 Feb 15, 2005. Prof. Susan Rodger. Note: thanks to Wanda Dann and Steve Cooper for slide ideas. Announcements. Assignment 4 is due Thursday Must include your code on Alice web page Done automatically if you select the option

jenny
Download Presentation

CompSci 4 Chap 4 Sec 3- Part 1 Feb 15, 2005

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. CompSci 4Chap 4 Sec 3- Part 1Feb 15, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas

  2. Announcements • Assignment 4 is due Thursday • Must include your code on Alice web page • Done automatically if you select the option • Check consulting hours, lots of modifications this week and next!

  3. What we will do today • Lecture on Chap 4, Sec 3 • Class-level Methods and Inheritance – Part 1 • Classwork

  4. Animated Actions • Some actions are more naturally associated with a specific class of objects rather than the overall world • Examples • A person walking • A wheel rolling • A fish swimming

  5. Class-level Methods • Write a method to add abilities/functions to a specific class of objects • Class-level method • NOT world-level method • Now show how to build class-level method

  6. An Example • How can we create a skate method for ice skater objects? • We need to: • Associate the new method with an ice skater • Write the new method to animate the ice skater NOTE: ice skater in the book and in Alice don’t quite match up in the names of body parts.

  7. The solution To associate the animation with the ice skater • Select iceSkater tile in Object Tree • Select methods tab in details area • Click on “create new method” button

  8. Storyboard for skate Skate: Do Together move skater forward 2 meters Do in order slide on left leg slide on right leg • The slide actions • Require several motion instructions • We’ll break these two actions into smaller pieces • Technique is stepwise refinement

  9. Refined storyboard for skate slideLeft: Do in order lift right leg and turn upper body forward lower right leg and return body upright Skate: Do Together move forward 2 meters Do in order slideLeft slideRight slideRight: Do in order lift left leg and turn upper body forward lower left leg and return body upright

  10. Writing the code • Next step – translate design into code • For slideLeft, possible translation is: Design Step Lift the right leg Turn upper body forward Lower the right leg Return the body upright Instruction Turn the right leg forward Turn upper body forward Turn the right leg backward Turn the upper body backward

  11. SlideLeft and Demo

  12. Correspondence of design to code Skate: Do Together move skater forward 2 meters Do in order slide on left leg slide on right leg

  13. Question • Writing methods to make ice skater perform a skating motion – intricate task • Would like to reuse these new methods in another world • How can you make skate method available for an ice skater in a different world?

  14. Answer: Save out as a new class • Rename iceSkater as cleverSkater • Save out as a new class. Alice saves the new class as CleverSkater.a2c

  15. Inheritance • The CleverSkater class • inherits all the properties and methods from the original IceSkater class • has newly defined methods (skate, slideLeft, slideRight) • In other programming languages, the concept of creating a new class based on a previously defined class is called inheritance

  16. Using CleverSkater • An instance of the CleverSkater class can be added to a new world

  17. Benefits of Inheritence • Inheritance supports • Reuse of program code • Programmer writes code once • Use code later in different programs • Sharing code with others on a team project

  18. Classwork today • Create a new class • Inherit from another class that has 4 limbs • Create at least four new methods • One of the new methods should invoke one of the other new methods • Repeat with another class • Save out new classes and read into another world • See handout for more details

  19. Classwork todaycreate a new class example crouching pouncing walking turn and smile

More Related