Class-level Methods and Inheritance

Slides:



Advertisements
Similar presentations
CS320n –Visual Programming Class-level Methods and Inheritance – Part 1 Mike Scott (Slides 4-3-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger.
Advertisements

Class-level Methods Alice. World / Class Method World method A general method that may refer to multiple objects; not closely associated with any particular.
Class-level Methods and Inheritance Part 1 Alice.
Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class.
Review of Chapter 4 Sections 1 and 2 World-level methods involve two or more objects break a large problem into smaller, logical units follow a design.
Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with a specific class of objects. Examples A person.
Class-level Methods and Inheritance MMP 220 Multimedia Programming This adapted material was prepared for students in MMP220 as as part of a curriculum.
Parameters and World-level methods Alice. Our Dragon world The dragon must to take off and fly, to carry the princess.
World-level Methods with Parameters Alice. Larger Programs As you become more skilled in writing programs, you will find that your programs quickly begin.
While: Indefinite Loops Alice. Repetition In some situations, we don’t know exactly how many times a block of instructions should be repeated. All we.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Programming in Alice Chapter 2 Part 2. Events Editor Identifies what method is executed (run) when the “Play” button is hit 2.
Classes, Objects, and World-level Methods Alice. Programming in Alice© 2006 Dr. Tim Margush2 Class / Object Class A template describing the characteristics.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Classes, Objects, and World-level Methods
Today’s Agenda 1.Collect Pre-Lab 4 2.Alice Programming Assignment Storyboards 3.Classes 4.Objects 5.Methods 6.Assign pair programming teams and meet upstairs.
Classes, Objects, and World-level Methods Alice. Larger Programs As you become more skilled in writing programs, you will find that programs quickly increase.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Classes, Objects, and World-level Methods Alice. Larger Programs As you become more skilled in writing programs, you will find that programs quickly increase.
Class-level Methods Chapter 6 part 1. Classes and Objects Classes o In Alice, classes are predefined as 3D models Objects o An object is an instance of.
Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.
What we will do today Learn about functions in Alice.
Functions Alice.
Classes, Objects, and World-level Methods Alice. Larger Programs As you become more skilled in writing programs, you will find that programs quickly increase.
Creating An Animation Program Alice. Recall We began the animation creation process We introduced the concept of storyboard We will continue using the.
Variables and Inheritance Part 1
Programming: Simple Control Structures Part 2 – Repetition Alice.
Variables and Functions Alice. Naming is Important If you get a new pet one of the first things you do is name it Gives you a way to refer to the new.
Functions Sec 8-11 Web Design. Objectives The Student will: Understand what a function is Know the difference between a method and a function Be able.
Programming: Simple Control Structures
Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value.
Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with a specific class of objects. Examples A person.
World-level Classes Chapter 6 Part 2. Programs Grow Program code grows larger over time This makes it more difficult to read and process the code in our.
Parameters Alice. A beetle band Our task is to create an animation for a bug band as an advertisement for their next concert.
CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.
Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with a specific class of objects. Examples A person.
1 Quiz Show Programming Terms. 2 Alice - 3D Virtual Programming Vocabulary Quiz Board Chapter 1 Chapter 2a Chapter 2b Chapter 3 Chapter 4 $100 $200 $300.
Classes, Objects, and World-level Methods
Classes, Objects, and World-level Methods
Functions Sec 51 Web Design.
Learn about functions in Alice
While: Indefinite Loops
Classes, Objects, and World-level Methods
Functions Sec 8-11 Web Design.
Classes, Objects, and World-level Methods
World-level Methods with Parameters
Learning Java with Alice 3.0 Game Design Kathy Bierscheid
Parameters Alice.
CS320n –Visual Programming
Classes, Objects, and World-level Methods
Programming: Simple Control Structures
Class-level Methods Alice.
Classes, Objects, and World-level Methods
Programming: Simple Control Structures
Parameters and World-level methods
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Simple Control Structures
Programming: Simple Control Structures
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Variables and Inheritance Part 1
Functions Alice.
Programming: Simple Control Structures
Parameters Alice.
Functions Alice.
Programming: Simple Control Structures
Classes, Objects, and World-level Methods
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Functions Alice.
Functions Alice.
Class-level Methods and Inheritance Part 2
Presentation transcript:

Class-level Methods and Inheritance Alice

Class-level Methods Some actions are naturally associated with a specific class of objects. Examples A person walking A wheel rolling We can write our own methods to define an action for a specific class of objects -- a class-level method.

An example (building technique) How can we create a skate method for ice skater objects? We need to: tell Alice to associate the new method (the one we are about to write) with an ice skater, and write a new method to animate the ice skater skating.

Demo: The solution First, to associate the animation with the ice skater select the iceSkater tile in the Object Tree select the methods tab in the details panel click on the create new method button

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 each require several motion instructions, so we will break down these two actions into smaller steps

Stepwise Refinement Refinement of slideLeft Do in order Lift right leg and turn upper body forward Lower right leg and return body upright Skate: Do together 1) move forward 2 meters 2) 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

Demo Ch04Lec3Skater Concepts illustrated in this example world A method defined for a specific type of object defines an action for that object. A method can call other methods. In this example, the skate method calls slideRight and slideLeft. Active Learning: Students write a method to have the skater move her arms up and lift one leg, spin around, and then return arms and legs to their original position.

Reuse Writing methods to make an ice skater perform a skating motion is an intricate task. We would like to have the iceSkater skate in other worlds without having to write the methods again. The idea of being able to use previously written program code in another program is known as reuse.

A new class 1) Rename iceSkater as cleverSkater. 2) Save out as a new class. Alice saves the new class as CleverSkater.a2c

Inheritance The CleverSkater class inherits all the properties and methods from the original IceSkater class, and also has the 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.

Importing CleverSkater An instance of the CleverSkater class can be added to a new world – use File|Import. Active learning: Students create a new world and instantiate a CleverSkater object.

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 and an instance of the new class is added to another world, Alice will open an Error dialog box to tell you something is wrong.

Bad Example What if there is no penguin in the new world where a cleverSkater object is imported?

Problem Suppose 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.

Parameter A solution is to write a class-level method with an object parameter that allows you to pass in the 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

Translation to Code Most of the skateAround storyboard design is straightforward and easy to code. One step, however, requires some thought: cleverSkater move to whichObject -- what distance should the cleverSkater move?

Calling a built-in function The instruction to move the skater to whichObject (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.

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

Demo Ch04Lec3SkateAround Concepts illustrated: A parameter acts as a placeholder for the object that will be passed in A call to the distance to function returns a number value A math expression can be created as part of an instruction Active learning: Students add several objects to the world and call the skateAround method with different arguments.

Assignment Read Chapter 4-3 Read Tips & Techniques 4 Class-level methods and Inheritance Read Tips & Techniques 4 Visible and Invisible Objects

Lab Chapter 4 Lec3 Lab Chapter 4 Project