1 / 31

Creating New Methods in Alice

Creating New Methods in Alice. Create a world. We will create a world with three objects from Lewis Carroll’s Alice’s Adventures in Wonderland: The Cheshire Cat The White Rabbit Alice We will write methods to make them jump up and down.

oberon
Télécharger la présentation

Creating New Methods in Alice

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. Creating New Methods in Alice

  2. Create a world • We will create a world with three objects from Lewis Carroll’s Alice’s Adventures in Wonderland: • The Cheshire Cat • The White Rabbit • Alice • We will write methods to make them jump up and down. • Open a new world with a grass template and let’s begin.

  3. CamelCase • Find the Cheshire Cat class in the Animals folder of the objects gallery. • Notice how the name of this class is constructed (CheshireCat). • The names of classes and objects in Alice follow a pattern called the CamelCase naming convention. • CamelCase is used and recommended by many software developers (e.g., Microsoft).

  4. CamelCase, cont’d • CamelCase is the practice of writing compound names without using blank spaces, but capitalizing the first letter of each name that forms the compound name. • In Alice in Wonderland, the proper name of the character is Cheshire Cat. • In CamelCase, we write CheshireCat.

  5. Using CamelCase • We will use this convention for all classes. • We the same convention with one difference for all: • Objects • Methods • Parameters • Variables • For these we do not capitalize the first letter • E.g., the object is named, cheshireCat.

  6. Editing our initial scene 1. Add instances of the following classes: • CheshireCat • WhiteRabbit • AliceLiddell

  7. Editing, cont’d 2. Arrange the characters as seen below:

  8. Editing, cont’d 3. Make each character face the camera.

  9. Editing, cont’d • Exit Scene Editor Mode

  10. Designing a method • The design of a new method begins with specifications. • In the simple case of this tutorial, the specifications are given: • The three characters appear on a grass background. • aliceLiddell jumps • whiteRabbit jumps • cheshireCat jumps

  11. Organizational Chart Triple Jump aliceLiddell jump whiteRabbit jump cheshireCat jump

  12. Program outline Triple Jump Program • aliceLiddell jump • whiteRabbit jump • cheshireCat jump

  13. Expanded outline Triple Jump Program • aliceLiddell jump • aliceLiddell move up • aliceLiddell move down • whiteRabbit jump • whiteRabbit move up • whiteRabbit move down • cheshireCat jump • cheshireCat move up • cheshireCat move down

  14. Coding the jump methods • Top-down design and modular development suggest that separate jump methods should be written for each object. • To make an object jump, we’re going to move the character up one meter and then back down one meter.

  15. Create a jump method for whiteRabbit • Click the whiteRabbit tile in the Object Tree. • Make sure the methods tab in the Details Area is displayed. • Click the create new method button. • Type jump as the name for the new method and click OK.

  16. Used for methods that require some input data Name of the method Method header Instruction zone

  17. Coding whiteRabbit.jump • Your method should include an instruction to make whiteRabbit move up one meter followed by an instruction to make it move down one meter. • As defined by our specification

  18. Coding whiteRabbit.jump, cont’d 1. Drag the whiteRabbit move tile from the left side of the screen and drop it on the Do Nothing phrase. Note: The tile will be surrounded by a red border as you drag it. The border will turn green when you reach the right place.

  19. Coding whiteRabbit.jump, cont’d • When you drop the tile into the instruction zone, a menu will appear asking you the direction in when you want to move the object. Select up for the direction. Select 1 meter for the amount. Note: Direction and amount are parameters or input data for the move method.

  20. Coding whiteRabbit.jump, cont’d 3.Drag another copy of the whiteRabbit move tile from the left into the instruction zone below your first instruction. This time set the direction to down, and the distance again to 1 meter.

  21. Creating the other methods • Select the cheshireCat tile in the Object Tree, and make sure its methods are displayed in the Details Area. • Click the create new method button, and then name the method jump. • Add an instruction to make cheshireCat move up 1 meter. • Add an instruction to make cheshireCat move down 1 meter. • Create a jump method for aliceLiddell in a similar manner.

  22. Creating the main method • Now we need a method that calls all three jump methods in the order defined by our specification. • This method will be your main method at the top of the organizational chart. • World.my first method is often used as this top-level method. We’ll do that here. • This a world-level method, associated with the world itself rather than with any particular object.

  23. Creating the main method 1. Start by clicking the world tile in the Object tree, and then click the methods tab, if necessary. Note: This looks the same as for an object’s methods.

  24. Creating the main method • Locate the my first method tile and click the edit button. • Click the aliceLiddell tile in the Object tree and find her methods. • Click the jump tile and drage a copy of it into the instruction zone of world.my first method • Add a call to the jump methods for whiteRabbit and cheshireCat in the same way.

  25. Testing and Debugging • Click the Play button. • Watch what happens. Did it perform according to your original specifications? • Save your world as tripleJump.

  26. Debugging • Each method should perform a single task. • If an object is doing something wrong, then you should ask which of your methods performs that task. • If you have an error, then it could be either the main method or one of the jump methods. • If the three objects are jumping in the wrong order then main method must be coded incorrectly.

  27. Generic methods and parameters • Let’s create a generic jump method that will work for any object. • To do this we need to use variable parameters. • A variable parameter is a name for a memory location that temporarily stores a value while a method is running.

  28. Variables • Variables are a lot like the properties of objects. • They have data types, just like properties. • They are stored in the memory of a computer, just like properties. • However, properties are associated with an object, and their values are maintained as log as the object exists. • Variables are associated with a particular method, and their values exist only inside the method. • Once the method stops running their values are gone.

  29. Parameters • A parameter is a variable whose value is passed from one method to another. • Not unlike a baton in a relay race. • We had to specify the direction and amount to move in the call to cheshireCat.move. • The values are parameters for the primitive cheshireCat.move method. • Our cheshireCat.move method passed the values for the params when calling move.

More Related