questions alice. functionality a question receives value(s), performs some computation on the...

19
Questions Alice

Upload: gerard-phelps

Post on 21-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value

Questions

Alice

Page 2: Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value

Functionality

A question receives value(s), performs some computation on the value(s), and returns (sends back) a value.

Page 3: Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value

Types of questions

The type of a question depends on the type of value it returns.

a calculated value (a number)

a specific object

a color

etc.

Page 4: Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value

Built-in questions

We used one of Alice's built-in questions in the skateAround method for the cleverSkater.

Let's take a look at another example.

Page 5: Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value

ExampleA common task in sports game programs is to bounce a ball. To illustrate how this is done, let's bounce the ball over the net. (The ball has been positioned 1 meter from the net.)

The ball should move up and forward and then down and forward in a pattern that looks something like this:

Note: This looks easy – but do not be deceived!

Page 6: Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value

Design StoryboardA design for a world-level method:

To reach the top of the net, we know the ball should move forward 1 meter (we positioned the ball 1 meter in front of the net) but how far upward should the ball move to clear the net?

World.ballOverNet:

Do in order toyball turn to face the net Do together toyball move up toyball move forward Do together toyball move down toyball move forward

Page 7: Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value

Height

We can use the built-in height question to determine the height of the net and move the ball up that distance.

Page 8: Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value

Demo

Demonstrate program for bouncing the ball over the net.

Page 9: Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value

Problem

The ball does not bounce over the net.

The problem is that we cannot easily tell "which way is up" – from the perspective of the ball.

Page 10: Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value

Solution

Actually, we are thinking "up" and "down" relative to the ground – so we can orient the ball (and also the net) with the ground.

Now, when the code is run, the ball will bounce over the net.

Page 11: Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value

Rolling the ball

Another sports game action involves rolling a ball along the ground.

We want a realistic motion rather than a slide.

The ball must simultaneously move and roll.

Page 12: Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value

Demo: A first attempt

Page 13: Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value

Revising the approach

The ball is made to roll 1 revolution. Suppose we want the ball to roll a certain distance along the ground.

How can we make the ball roll the correct number of revolutions to cover a given distance along the ground?

Page 14: Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value

Number of revolutions

The number of revolutions depends on the size of the ball

The number of revolutions is distance/( P * diameter)

But there is no built-in question to return the number of revolutions

We will write our own!

one revolution

four revolutions

Page 15: Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value

Parameters

We want to return the value computed as distance/( * diameter)

Obviously, what is needed is the ball’s diameter

the ball object has a built-in width question

the distance the ball is to travel can be sent as a parameter to the question

Page 16: Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value

numberOfRevolutions Question

Page 17: Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value

Demo: Calling the Question

This is a test value.

We should run the animation with several test values to be sure it works as expected.

What happens if you use a negative value?

Page 18: Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value

Levels of Questions

As with methods, you can write questions as either class-level or world-level. (The question just presented was class-level.)The guidelines for class-level methods also apply to class-level questions:

No references to other objects. No references to world-level questions you have written (built-in world-level questions are fine to use).

Page 19: Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value

Assignment

Read Chapter 6 -1, Questions

Lab 6-1