Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value."— Presentation transcript:

1 Questions Alice

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

3 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.

4 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.

5 Example A 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!

6 Design Storyboard A 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

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

8 Demo Demonstrate program for bouncing the ball over the net.

9 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.

10 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.

11 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.

12 Demo: A first attempt

13 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?

14 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

15 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

16 numberOfRevolutions Question

17 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?

18 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).

19 Assignment Read Chapter 6 -1, Questions Lab 6-1


Download ppt "Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value."

Similar presentations


Ads by Google