Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and...

42
Class-level Methods and Inheritance Alice

Transcript of Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and...

Page 1: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Class-level Methodsand Inheritance

Alice

Page 2: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Class-level MethodsSome actions are naturally associated witha specific class of objects.

Examples A person walking A wheel rolling

We can write our own methods to definean action for a specific class of objects --a class-level method.

Page 3: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

An example(building technique)

How can we create a skate method for ice skaterobjects?

We need to:(1) tell Alice to associate the

new method (the one weare about to write) with anice skater, and

(2) write a new method toanimate the ice skaterskating.

Page 4: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Demo: The solution

First, to associate theanimation with the ice skater

• select the iceSkater tile inthe Object Tree

•select the methods tab inthe details panel

•click on the create newmethod button

Page 5: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Storyboard for skate

The slide actions each require severalmotion instructions, so we will breakdown these two actions into smallersteps

Skate:Do together move skater forward 2 meters Do in order slide on left leg slide on right leg

Page 6: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Stepwise RefinementRefinement of slideLeftDo in order Lift right leg and turn upper body forward Lower right leg and return body uprightSkate:

Do together1) move forward 2 meters2) Do in order slideLeft slideRight Refinement of slideRight

Do in order Lift left leg and turn upper body forward Lower left leg and return body upright

Page 7: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Demo

Ch04Lec3SkaterConcepts illustrated in this example world

A method defined for a specific type of objectdefines an action for that object. A method can call other methods.

In this example, the skate method callsslideRight and slideLeft.

Page 8: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Reuse

Writing methods to make an ice skaterperform a skating motion is an intricatetask.We would like to have the iceSkater skatein other worlds without having to write themethods again.The idea of being able to use previouslywritten program code in anotherprogram is known as reuse.

Page 9: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

A new class1) Rename iceSkater ascleverSkater.

2) Save out as a newclass. Alice saves thenew class asCleverSkater.a2c

Page 10: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Inheritance

The CleverSkater class inherits all the properties and methods fromthe original IceSkater class, and also has the newly defined methods (skate,slideLeft, slideRight)

In other programming languages, theconcept of creating a new class basedon a previously defined class is calledinheritance.

Page 11: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Importing CleverSkater

An instance of the CleverSkater class can beadded to a new world – use File|Import.

Page 12: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

GuidelinesTo avoid potential misuse of class-levelmethods, follow these guidelines:

Avoid references to other objects Avoid calls to world-level methods Play a sound only if the sound has been importedand saved out as part of the new class

If these guidelines are not followed and aninstance of the new class is added to anotherworld, Alice will open an Error dialog boxto tell you something is wrong.

Page 13: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Bad Example

What if there is no penguin in the new worldwhere a cleverSkater object is imported?

Page 14: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

ProblemSuppose you really want to write a class-levelmethod where another object is involved?For example, a method to make the skaterskate around another object-- in this scene, thepenguin.

Page 15: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Parameter

A solution is to write a class-level method withan object parameter that allows you to pass inthe specific object.cleverSkater.skateAround

Parameter: whichObject

Do in order

Do together

cleverSkater turn to face whichObject

cleverSkater lift right leg

cleverSkater move to whichObject

cleverSkater turn around whichObject

Page 16: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Translation to CodeMost of the skateAround storyboarddesign is straightforward and easy tocode.One step, however, requires somethought:

cleverSkater move to whichObject -- what distance should the cleverSkater move?

Page 17: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Calling a built-in functionThe instruction to move the skater to whichObject(penguin, in this example) would look like this:

Unfortunately, the skater will collide with the penguinbecause the distance between two objects is measuredcenter-to-center.

Page 18: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

ExpressionTo avoid a collision, use a math operator to create anexpression that adjusts the distance.Math operators in Alice:

addition + subtraction − multiplication * division /

Example:

Page 19: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Demo

Ch04Lec3SkateAroundConcepts illustrated:

A parameter acts as a placeholder for theobject that will be passed in A call to the distance to function returns anumber value A math expression can be created aspart of an instruction

Page 20: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Interactive Programming

Alice

Page 21: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Control of flow

Control of flow -- how the sequence of actions ina program is controlled.

What action happens first, what happens next, andthen what happens…and so on.

In movie-style programs (Chapters 1-4) thesequence of actions is determined by theprogrammer

Creating a storyboard designWriting program methods to carry out thedesigned sequence

Page 22: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Interactive AnimationsIn interactive programs, the sequenceof actions is determined at runtimewhen the user provides input

clicks the mousepresses a key on the keyboardsome other source of input

In essence, control of flow is now “inthe hands of the user!”

Page 23: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Events

Each time the user provides some sort ofinput, we say an event is generated.

An event is “something that happens”

Page 24: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Event Handling methodsAn event may

Trigger a response, or Move objects into positions that create some condition (e.g.,a collision) that triggers a response.

A method is called to carry out the response. We callthis kind of method an event handling method.When an event is linked to a method that performs anaction, a behavior is created.

Page 25: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Example

Build an air show flight simulator. In an airshow, the pilot uses biplane controls to performacrobatic stunts.

Page 26: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Problem

The whole idea in a flight simulator is toallow the user to control the flight path.The problem is: how do we write ourprogram code to provide a guidancesystem that allows the user to be the pilot?

Page 27: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Solution

Use keyboard input Up-arrow key to move the biplane forward Spacebar to make the biplane do a barrel turn

(Note: other sets of keys could be used, we just arbitrarily pickeda couple of keys on the keyboard.)

Write event handler methods that respond to each keypress

Page 28: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Storyboards

Since two keys are used, two events are possible – sotwo storyboards are needed:

Each storyboard outlines an event handler thatresponds to a particular event.

Event: Spacebar press

Response: Do together roll biplane a full revolution play biplane engine sound

Event:: Up Arrow key press

Response: Do together

move biplane forward play biplane engine sound

Page 29: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

DemoCh05Lec1BiplaneAcrobatConcepts illustrated:

Events are created in the event editor A method is called to handle each event Synchronize the duration of the animationwith the length of a sound. To change thelength of a sound, use audio editing software.

Page 30: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Parameters andEvent-Handler Methods

Alice

Page 31: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Mouse input

Interactive programs often allow the userto use a mouse to click

buttons in a windows-based interface targets in a game a checklist of items on a business form

In this session, we look at how touse mouse input.

Page 32: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Example

People are trapped ina burning building andthe user will click themouse cursor on theperson to be rescuednext.

In setting up this demo world, we positioned the firetruck so the distance of the ladder from the 1st floor is 1meter, 2nd floor is 2 meters, and 3rd floor is 3 meters.

Page 33: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Storyboard

Three people are to be rescued. So, we couldwrite three different methods.

Page 34: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

A Better Solution

A better solution is to write one eventhandler method and send in theinformation needed to perform the action.

firetruck.savePerson:

parameters: whichFloor, whichPerson, howFarDo in order point ladder at whichFloor extend the ladder howFar meters whichPerson slide down the ladder to the fire truck pull the ladder back howFar meters

whichFloor and whichPerson are Object parameters

howFar is a Number parameter

Page 35: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Demo

Ch05Lec2SavePeople Concepts illustrated:

Parameters allow one event handling methodto handle three different events. The fireAnim object has a built-in event andevent handling method.

Page 36: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Example 2 Zeus was a powerfulgod in Greek mythology.When Zeus was angry,he would shoot athunderbolt out of theheavens to strike anyonewho got in the way.

The user will choose thephilosopher who will bethe next target of Zeus’sanger.

Page 37: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

StoryboardA possible design is a method with an Object parameter, namedwho, for the object that was clicked.

The actions in this storyboard are complex. We can break the actions down into simpler steps usingstepwise refinement.

Event: An object is mouse-clicked

Event handler: shootBolt

Parameter: who — the object that was clicked

Do in orderprepare to strike the object that was clickedthunder plays and lightning strikes the object that was clickedlightning is repositioned for the next strike

Page 38: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Event: An object is mouse-clicked

Event handler: shootBolt

Parameter: who — the object that was clicked

Do in ordercall prepareToShoot method — send who as the targetcall lightningAndThunder method — send who as the targetlightning move to cloud’s position

prepareToShoot:

Parameter: targetDo together turn Zeus to face the target make the lightning bolt visible

lightningAndThunder:

Parameter: target Do together play sound call specialEffects method — send target

specialEffects:

Parameter: target

Do in order Do together lightning bolt move to target smoke move to target Do together set smoke to visible set lightning to invisible call smoke cycle — built-in method set target color to black move target up and down

Page 39: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Demo

Ch05Lec2Zeus Concepts illustrated:

The shootBolt method is at the top of thestoryboard design. This method calls twoother methods – a driver method. object under the mouse cursor is used in theWhen mouse is clicked event to pass theclicked object as the target

Page 40: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

OpacityIn this example, opacity is used to make thelightning bolt visible and invisible.

In setting up the initial scene, the lightning bolt ismade invisible by setting its opacity to 0 (0%). In preparing to shoot the lightning bolt, it was madevisible by setting its opacity to 1 (100%).

(Review Tips & Techniques 4 for more details aboutopacity.)

Page 41: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

Built-in methodscycleSmoke is a special built-in method for the smokeobject. The duration of cycleSmoke is about 2 1/2seconds.Several statements in the shootBolt and specialEffectsmethods use a move to instruction

The move to instruction moves an object to a particular positionin the world. In the example above, the lightening bolt is movedto the position of the cloud.

(The move to instruction is described in detail in Tips &Techniques 2.)

Page 42: Class-level Methods and Inheritance - GitHub Pages · 2020-01-16 · Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with ...

TestingWhen parameters are used in interactiveprogramming, it is especially important totest that all possible parameter valueswork as expected.

What happens if you click on eachphilosopher, one at a time?

Also try things that shouldn’t work.What happens if you click on a column?What happens if you click on aphilosopher twice?