DOC PREVIEW
Duke CPS 004 - Chapter 4

This preview shows page 1-2-15-16-31-32 out of 32 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CompSci 4Chap 4 Sec 3Sep 22, 2005Prof. Susan RodgerNote: thanks to Wanda Dann and Steve Cooper for slide ideasAnnouncements• Assignment 3 is due midnight today– Adding code to web page is optional• Assignment 4 out today• Read Chapter 6.1 for next class• Reading QuizAnimated 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 swimmingClass-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 methodAn Example• How can we create a skate method for ice skater objects?We need to:1) Associate the new method with an ice skater2) Write the new method to animate the ice skaterThe solutionTo associate the animation with the ice skater– Select iceSkater tile in Object Tree– Select methods tab in details area– Click on “create new method” buttonStoryboard for skate•The slide actions– Require several motion instructions– We’ll break these two actions into smaller pieces– Technique is stepwise refinementSkate:Do Togethermove skater forward 2 metersDo in orderslide on left legslide on right legRefined storyboard for skateSkate:Do Togethermove forward 2 metersDo in orderslideLeftslideRightslideLeft:Do in orderlift right leg and turn upper body forwardlower right leg and return body uprightslideRight:Do in orderlift left leg and turn upper body forwardlower left leg and return body uprightWriting the code• Next step – translate design into code•For slideLeft, possible translation is:Design StepLift the right legTurn upper body forwardLower the right legReturn the body uprightInstructionTurn the right leg forwardTurn upper body forwardTurn the right leg backwardTurn the upper body backwardSlideLeft and DemoWhere is wait?Correspondence of design to codeSkate:Do Togethermove skater forward 2 metersDo in orderslide on left legslide on right legQuestion• 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?Answer: Save out as a new class1) Rename iceSkater as cleverSkater2) Save out as a new class. Alice saves the new class as CleverSkater.a2cInheritance• 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 inheritanceUsing CleverSkater• An instance of the CleverSkater class can be added to a new worldBenefits 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 projectGuidelines• To avoid misuse of class level methods– Avoid references to other objects– Avoid calls to world-level methods– Play a sound only if the sound has been imported and saved out as part of the new class• If these guidelines are not followed and an instance of the new class is added to another world– Alice will open an Error dialog box to tell you something is wrongBad Example 1Bad Example 2Problem• What if you were convinced you needed to write a class-level method where another object is involved?• For example, a method for ice skater to skate around another object – here a penguinSolution• Class-level method with object parametercleverSkater.skateAroundParameter: whichObjectDo in orderDo togethercleverSkater turn to face whichObjectcleverSkater lift right legcleverSkater move to whichObjectcleverSkater turn around whichObjectTranslating Design into Code• Most of skateAround storyboard easy to code• Last two steps, require more thought– cleverSkater move to whichObject• What distance should cleverSkater move?– cleverSkater turn around whichObject• How do we tell cleverSkater to turn (in a circle) around another object?Built-in Functions (or questions)• The built-in function distance to– used to determine the distance the skater must moveCalling the function• Code to move skater to whichObjectOops, skater will collide with penguin!Distance between two objects ismeasured center-to-centerExpressions• To avoid collision– Use math operator to create an expression that adjusts the distance• Math operators in Aliceaddition + subtraction -multiplication * division /•Example:How to put in an Expression2 3 1 4 Result:ResultStops before penguin Skates around penguinasSeenBy• For skater to skate around another object–Pass whichObject as an argument to asSeenBy parameter in turn instructionMore on AsSeenBy• Use invisible object (isShowing set to false) to have objects fly around in a circleTesting• Each time you create a new class, test it!– Add an instance of new class to new world– Write a short test program • Test each new method• Testing increases your confidence in the ability to reuse your code in other worldsClasswork todaycreate a new class examplecrouchingpouncingwalkingturn and smileClasswork 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• Create a second new class inherited from another object– with at least 4 methods– At least two of the methods must have a parameter– Use AsSeenBy, isShowing and math each in some method (not necessarily the same method)• Save out new classes and read into another world• See handout for more


View Full Document

Duke CPS 004 - Chapter 4

Documents in this Course
Lecture

Lecture

18 pages

Chapter 7

Chapter 7

18 pages

Chapter 9

Chapter 9

15 pages

Java 1

Java 1

24 pages

Java 3

Java 3

11 pages

Lecture

Lecture

10 pages

Chapter 4

Chapter 4

28 pages

Chap 2

Chap 2

16 pages

Graphics

Graphics

20 pages

Lecture

Lecture

12 pages

HTML

HTML

16 pages

Java 1

Java 1

6 pages

Chapter 4

Chapter 4

16 pages

The Plan

The Plan

25 pages

Lecture

Lecture

16 pages

Chapter 6

Chapter 6

21 pages

Lecture

Lecture

18 pages

Lecture

Lecture

23 pages

Lecture

Lecture

16 pages

Lecture

Lecture

19 pages

Lecture

Lecture

12 pages

Lecture

Lecture

5 pages

Lecture

Lecture

26 pages

Lecture

Lecture

16 pages

Chapter 7

Chapter 7

23 pages

Lecture

Lecture

21 pages

Lecture

Lecture

4 pages

Lecture

Lecture

4 pages

Lecture

Lecture

8 pages

Lecture

Lecture

4 pages

Lecture

Lecture

10 pages

Java

Java

4 pages

CompSci 4

CompSci 4

18 pages

Lecture

Lecture

26 pages

CompSci 4

CompSci 4

12 pages

HTML

HTML

17 pages

Lecture

Lecture

16 pages

Chapter 5

Chapter 5

22 pages

Lecture

Lecture

4 pages

Chapter 4

Chapter 4

10 pages

Chapter 2

Chapter 2

15 pages

Chapter 8

Chapter 8

14 pages

Lecture

Lecture

15 pages

Load more
Download Chapter 4
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 4 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 4 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?