CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.

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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Fall 2007ACS-1805 Ron McFadyen1 Programming Concepts Chapter 4 introduces more advanced OO programming techniques. Construction of a programs usually requires:
CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Classes, Objects, and World-level Methods Alice. Programming in Alice© 2006 Dr. Tim Margush2 Class / Object Class A template describing the characteristics.
Fall 2008ACS-1805 Ron McFadyen1 Programming Concepts Chapter 4 introduces more advanced OO programming techniques. Construction of a programs usually requires:
Methods Tutorial: Part One By Deborah Nelson Duke University Under the direction of Professor Susan Rodger June 9, 2008.
Variables and Functions Chapter Variables Named storage location in computer’s memory Programs may need to store data when running o Stored in.
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.
Classes, Objects, and World-level Methods Section 47 Web Design.
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.
Alice 2.0 Introductory Concepts and Techniques Project 1 Exploring Alice and Object-Oriented Programming.
Poses Tutorial Alex Boldt Under the direction of Professor Susan Rodger Duke University July 2015.
Teaching a character to walk in more than one world: Parameters and Inheritance. By Lana Dyck under the direction of Professor Susan Rodger Duke University.
Methods Tutorial: Part Two By Deborah Nelson Duke University Professor Susan Rodger June 16, 2008.
By Deborah Nelson Duke University Professor Susan Rodger July 13, 2008.
Scene changes By Deborah Nelson Duke University Under the direction of Professor Susan Rodger June
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.
Variables and Inheritance Part 1
Skater World: Part Two By Deborah Nelson Duke University Under the direction of Professor Susan Rodger June 1,
CompSci 4 Chap 5 Sec 2 Oct 18, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
CompSci 4 Chap 4 Sec 1 Sept 15, 2005 Prof. Susan Rodger.
Programming: Putting Together the Pieces Built-in Questions and Expressions Alice.
CS320n –Visual Programming Classes, Objects, and World- Level Methods Mike Scott (Slides 4-1)
Programming: Putting Together the Pieces Built-in Functions and Expressions 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.
Creating a 3D Interactive Story Prof. Susan Rodger Duke University July 19, 2007.
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.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
CompSci 4 Chap 5 Sec 2 Oct 9, 2008 Prof. Susan Rodger.
CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.
CompSci 4 Chap 5 Sec 1 and 2 Oct 14, 2010 Prof. Susan Rodger.
Headshots in Alice Duke University Professor Susan H. Rodger Gaetjens Lezin July 2008.
CompSci 4 Chap 10 November 4, 2010 Prof. Susan Rodger.
CompSci 4 Chap 10 Nov 22, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with a specific class of objects. Examples A person.
Variables and Inheritance Part 1 Alice. Review: Properties A class defines properties for its own kind of object. When an object is created (instantiated),
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.
Functions Sec 51 Web Design.
Learn about functions in Alice
Functions Sec 8-11 Web Design.
World-level Methods with Parameters
CS320n –Visual Programming
Class-level Methods Alice.
Variables and Inheritance Part 1
Functions Alice.
Professor Susan Rodger
Functions Alice.
Class-level Methods and Inheritance
Functions Alice.
Functions Alice.
Class-level Methods and Inheritance Part 2
Presentation transcript:

CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger

Announcements Assignment 3 is due midnight today –Turn in zip file with 2 Alice worlds and README text file OR upload all three files –Need a web page with 2 snapshots and explanation for both Alice worlds Assignment 4 out today Skipping Chapter 5 (will come back to it) Read Chapter 6.1 for next class Reading Quiz

Key concepts today World method vs. class method Inheritance Using built-in functions Applying math for more precision

Animated 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 swimming

Class-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 method

An Example How can we create a skate method for ice skater objects? We need to: 1)Associate the new method with an ice skater 2)Write the new method to animate the ice skater

The solution To associate the animation with the ice skater –Select iceSkater tile in Object Tree –Select methods tab in details area –Click on “create new method” button

Storyboard for skate The slide actions –Require several motion instructions –We’ll break these two actions into smaller pieces –Technique is stepwise refinement Skate: Do Together move skater forward 2 meters Do in order slide on left leg slide on right leg

Refined storyboard for skate Skate: Do Together move forward 2 meters Do in order slideLeft slideRight slideLeft: Do in order lift right leg and turn upper body forward lower right leg and return body upright slideRight: Do in order lift left leg and turn upper body forward lower left leg and return body upright

Writing the code Next step – translate design into code For slideLeft, possible translation is: Design Step Lift the right leg Turn upper body forward Lower the right leg Return the body upright Instruction Turn the right leg forward Turn upper body forward Turn the right leg backward Turn the upper body backward

SlideLeft and Demo Where is wait?

Correspondence of design to code Skate: Do Together move skater forward 2 meters Do in order slide on left leg slide on right leg

Question 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 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 –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 inheritance

Using CleverSkater An instance of the CleverSkater class can be added to a new world

Benefits 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 project

Guidelines 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 wrong

Bad Example 1

Bad Example 2

Problem 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 penguin

Solution Class-level method with object parameter 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

Translating 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 move

Calling the function Code to move skater to whichObject Oops, skater will collide with penguin! Distance between two objects is measured center-to-center

Expressions To avoid collision –Use math operator to create an expression that adjusts the distance Math operators in Alice addition + subtraction - multiplication * division / Example:

How to put in an Expression Result:

Result Stops before penguinSkates around penguin

asSeenBy For skater to skate around another object –Pass whichObject as an argument to asSeenBy parameter in turn instruction

More on AsSeenBy Use invisible object (isShowing set to false) to have objects fly around in a circle

Testing 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 worlds

Classwork today create a new class example crouching pouncing walking turn and smile

Scene Change Highlights Import a class with all the ground color textures Fade to black, fade back in Must change the atmosphereColor and ambientLightColor to black and then change back Need to STORE THE OLD color values –Create a variable for storing the color

The color property variables

Classwork today Create a new class –Inherit from animal 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 and isShowing each in some method (not necessarily the same method) Create a world to change scenes Save out new classes and put in change scenes world See handout for more details