CS 320 n Visual Programming Classlevel Methods and

  • Slides: 20
Download presentation
CS 320 n –Visual Programming Class-level Methods and Inheritance – Part 1 Mike Scott

CS 320 n –Visual Programming Class-level Methods and Inheritance – Part 1 Mike Scott (Slides 4 -3 -1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slides Visual Programming Class level Methods and Inheritance Part 1

Reminders • Assignment due tonight by 11 pm Visual Programming Class level Methods and

Reminders • Assignment due tonight by 11 pm Visual Programming Class level Methods and Inheritance Part 1 2

What We Will Do Today • Learn about class level methods and inheritance •

What We Will Do Today • Learn about class level methods and inheritance • class work Visual Programming Class level Methods and Inheritance Part 1 3

Animated Actions • Some actions are more naturally associated with a specific class of

Animated Actions • Some actions are more naturally associated with a specific class of objects rather than with the overall world. • Examples – A person walking – A wheel rolling – A fish swimming • If the action involves 2 or more objects in the world it should be a world level method Visual Programming Class level Methods and Inheritance Part 1 4

Class-level Methods • Write a method to add abilities / functions / actions /

Class-level Methods • Write a method to add abilities / functions / actions / behaviors to a specific class of objects – class-level method – NOT world-level methods • Demo how to build class-level method Visual Programming Class level Methods and Inheritance Part 1 5

An Example • How can we create a skate method for ice skater objects?

An Example • How can we create a skate method for ice skater objects? We need to: (1) create a new method for the ice skater (2) add code to the new method to make it look like the skater “skates” Visual Programming Class level Methods and Inheritance Part 1 6

Demo: The solution First, to associate the behavior with the ice skater • select

Demo: The solution First, to associate the behavior with the ice skater • select the ice. Skater tile in the Object Tree • select the methods tab in the details area • click on the "create new method" button Visual Programming Class level Methods and Inheritance Part 1 7

Storyboard for skate Skate: Do together move skater forward 2 meters Do in order

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 – require several motion instructions – … so we’ll break these two actions into smaller pieces – a technique known as stepwise refinement (and abstraction) Visual Programming Class level Methods and Inheritance Part 1 8

An Aside • Shouldn’t the skater already know how to skate? • Should people

An Aside • Shouldn’t the skater already know how to skate? • Should people already know how to walk? • The objects in Alice are simply 3 D models created in other programs – articulating, coloring, setting center point • When added to Alice all objects have certain basic behaviors, move, turn, roll, … • programmers must write code for more complex behaviors – CLASS-LEVEL METHODS! Visual Programming Class level Methods and Inheritance Part 1 9

But This Seems So Hard • Anecdote 1: Show Kelly Scott anything “cool” the

But This Seems So Hard • Anecdote 1: Show Kelly Scott anything “cool” the computer does • Anecdote 2: Finding Nemo. Making the viewer believe Dory can barely say what she is thinking and how sad she is • It is hard. It is complex. Take a look at the penguin method walk Visual Programming Class level Methods and Inheritance Part 1 10

Penguin Method Walk … and there is more after this. Bottom line. Interesting behaviors

Penguin Method Walk … and there is more after this. Bottom line. Interesting behaviors are usually complex Visual Programming Class level Methods and Inheritance Part 1 11

Refined storyboard for skate Skate: Do together 1) move forward 2 meters 2) Do

Refined storyboard for skate Skate: Do together 1) move forward 2 meters 2) Do in order slide. Left slide. Right Visual Programming Refinement of slide. Left Do in order Lift right leg and turn upper body forward Lower right leg and return body upright Refinement of slide. Right Do in order Lift leg and turn upper body forward Lower left leg and return body upright Class level Methods and Inheritance Part 1 12

Writing the code • The next step is to translate the design into program

Writing the code • The next step is to translate the design into program code. • For the slide. Left method, a possible translation is: Design step Instruction Lift the right leg Turn upper body forward Lower the right leg turn the right thigh forward turn the upper body forward turn the right thigh backward Return the body upright turn the upper body backward Visual Programming Class level Methods and Inheritance Part 1 13

Demo • Demonstration of – slide. Left – slide. Right – skate • Could

Demo • Demonstration of – slide. Left – slide. Right – skate • Could we parametize slide. Left and slide. Right? – should we? Visual Programming Class level Methods and Inheritance Part 1 14

Correspondence of design to code Skate: Do together move skater forward 2 meters Do

Correspondence of design to code Skate: Do together move skater forward 2 meters Do in order slide on left leg slide on right leg Visual Programming Class level Methods and Inheritance Part 1 15

Question • Writing the methods to make an ice skater perform a skating motion

Question • Writing the methods to make an ice skater perform a skating motion is an intricate task. • Most likely, you would like to reuse these new methods in other worlds. • How can you make the skate method available for an ice skater in a different world? – any other ice skaters in this world DO NOT know how to skate Visual Programming Class level Methods and Inheritance Part 1 16

Answer: Save out as a new class 1) Rename ice. Skater as advanced. Skater.

Answer: Save out as a new class 1) Rename ice. Skater as advanced. Skater. 2) Save out as a new class. Alice saves the new class as Adcanced. Skater. a 2 c Visual Programming Class level Methods and Inheritance Part 1 17

Inheritance • The Advanced. Skater class – inherits all the properties and methods from

Inheritance • The Advanced. Skater class – inherits all the properties and methods from the original Ice. Skater class, and also – has the newly defined methods (skate, slide. Left, slide. Right) • In other programming languages, the concept of creating a new class based on a previously defined class is called inheritance. – object oriented programming. As opposed to procedural programming or functional programming o or … Visual Programming Class level Methods and Inheritance Part 1 18

Using Clever. Skater • An instance of the Clever. Skater class can be added

Using Clever. Skater • An instance of the Clever. Skater class can be added to a new world – use File|Import. Visual Programming Class level Methods and Inheritance Part 1 19

Benefits of Inheritance • Inheritance supports – the reuse of program code -- allows

Benefits of Inheritance • Inheritance supports – the reuse of program code -- allows the programmer to write code once and use it again later in different programs. – sharing code with others in a team project – The best code in your program is the code you didn’t have to write! Visual Programming Class level Methods and Inheritance Part 1 20