variables and inheritance a more complex example alice

15
Variables and Inheritance A More Complex Example Alice

Upload: annabel-fletcher

Post on 03-Jan-2016

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Variables and Inheritance A More Complex Example Alice

Variables and InheritanceA More Complex Example

Alice

Page 2: Variables and Inheritance A More Complex Example Alice

A steerable car

The task is to create a car-steering simulation. To steer the car, the user presses keys to control:

• moving the car forward or backward• turning the wheels right or left

Of course, as the wheels are turned (right or left) and the car is moving forward (or backward), the car should steer right or left.

Page 3: Variables and Inheritance A More Complex Example Alice

An intuitive storyboardright

Turn front wheels right

left

Turn front wheels left

backup Move car backward Rotate wheels backwards

forward Move car forward Rotate wheels forward

Page 4: Variables and Inheritance A More Complex Example Alice

ProblemThe problem is the actions are not coordinated.

left turns the wheels left, but

forward rotates the wheels forward while the entire car is moving forward

(Of course, right and backUp are opposites of the above)

This is an "accident looking for a place to happen." The animation is going to look like the car has a broken axel!

Page 5: Variables and Inheritance A More Complex Example Alice

Solution

When the car is moving, we need to coordinate the left-right turning action (steering) with the forward or backward movement.But, to coordinate the actions, we have to (somehow) remember how far the wheels have been turned to the left or right – the direction in which the wheels are turned.In other words, we need to know the state of the front wheels of the car object!

Page 6: Variables and Inheritance A More Complex Example Alice

Adding StateTo keep track of the amount of turn on the front wheels (the direction), we can use a linear scale, where positive values indicate the degree of turn to the right and negative values to the left.

-10 0 10

left right

Create a class-level variable named direction start with direction at 0 (straight ahead) right-arrow increases direction left-arrow decreases direction

Page 7: Variables and Inheritance A More Complex Example Alice

Create new variable

1) direction is declared to be a number type.

2) Its value is initialized to 0.

Page 8: Variables and Inheritance A More Complex Example Alice

Revised planEvent: right arrowright:

Increment direction

Event: left arrowleft:

Decrement direction

Event: down arrowbackup:

Do together corvette move backward 1 meter corvette turn right/left based on direction

right and left do not turn the wheels!

Event: up arrowforward: Do together corvette move forward 1 meter corvette turn right/left based on direction

Page 9: Variables and Inheritance A More Complex Example Alice

Demo

Ch10Lec1bSteeringCorvette

Concept illustrated in this example A class-level variable can be used to track the state of an object in an interactive animation, where the user's actions change the state.

Page 10: Variables and Inheritance A More Complex Example Alice

right

Q: Why do we use an If statement?

A: To limit the amount the user can direct the car to turn right – real cars have limits, too.

Page 11: Variables and Inheritance A More Complex Example Alice

left

Q: Why is a negative 10 used as a limit in the If statement for this method?

Page 12: Variables and Inheritance A More Complex Example Alice

forward

Q: Why is the corvette's direction multiplied by 2 and divided by 360?

Q: What happens if the value of direction is less than 0?

Page 13: Variables and Inheritance A More Complex Example Alice

backup

What is the difference between this method and forward?

Page 14: Variables and Inheritance A More Complex Example Alice

Assignment

Chapter 10 Section 1, pages 257-261

Page 15: Variables and Inheritance A More Complex Example Alice

Lab

Chapter 10 Lecture 1 Lab