Classlevel Methods and Inheritance Part 2 Alice Guidelines

  • Slides: 14
Download presentation
Class-level Methods and Inheritance Part 2 Alice

Class-level Methods and Inheritance Part 2 Alice

Guidelines To avoid potential misuse of class-level methods, follow these guidelines: Avoid references to

Guidelines To avoid potential misuse of class-level methods, follow these guidelines: 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 an instance of the new class is added to another world, Alice will open an Error dialog box to tell you that something is wrong.

Bad Example 1

Bad Example 1

Bad Example 2

Bad Example 2

Problem What if you are convinced that you really want to write a class-level

Problem What if you are convinced that you really want to write a class-level method where another object is involved? For example, a method to make the skater skate around another object-- in this scene, the penguin.

Solution A solution is to write a class-level method with an object parameter that

Solution A solution is to write a class-level method with an object parameter that allows you to pass in the specific object. clever. Skater. skate. Around Parameter: which. Object Do in order Do together clever. Skater turn to face which. Object clever. Skater lift right leg clever. Skater move to which. Object clever. Skater turn around which. Object

Translating design into code Most of the skate. Around storyboard design is straightforward and

Translating design into code Most of the skate. Around storyboard design is straightforward and easy to code. The last two steps, however, require some thought: clever. Skater move to which. Object -what distance should the clever. Skater move? clever. Skater turn around which. Object -how do we tell clever. Skater to turn (in a circle) around another object?

Built-in Questions The built-in question, distance to can be used to determine the distance

Built-in Questions The built-in question, distance to can be used to determine the distance the skater must move.

Calling the question The instruction to move the skater to which. Object (penguin, in

Calling the question The instruction to move the skater to which. Object (penguin, in this example) would look like this: Unfortunately, the skater will collide with the penguin because the distance between two objects is measured center-to-center.

Expressions To avoid a collision, use a math operator to create an expression that

Expressions To avoid a collision, use a math operator to create an expression that adjusts the distance. Math operators in Alice: addition + multiplication * Example: subtraction division /

as. Seen. By To have the skater skate around another object, pass which. Object

as. Seen. By To have the skater skate around another object, pass which. Object as an argument to the as. Seen. By parameter in a turn instruction. For more details on as. Seen. By, review Tips & Techniques 2.

Demonstrate skate. Around

Demonstrate skate. Around

Testing Each time you create a new class, test it: Add an instance of

Testing Each time you create a new class, test it: Add an instance of the new class to a new world. Write a short testing program to test each new method. Testing increases your confidence in the ability to reuse your code in other worlds.

Lab 4 -3 Chapter 4 Project

Lab 4 -3 Chapter 4 Project