Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables and Inheritance Part 2

Similar presentations


Presentation on theme: "Variables and Inheritance Part 2"— Presentation transcript:

1 Variables and Inheritance Part 2
Alice

2 Types of Variables Class-level variables can be of many different types (number, Boolean, Object, Color, etc.) In the previous session, the switch example used a Boolean variable (isOn) and one method (click). In this session, we will look at an example that uses a number variable and several related methods.

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

4 An intuitive storyboard
right Turn front wheels right left Turn front wheels left forward Move car forward Rotate wheels forward backup Move car backward Rotate wheels backwards

5 Problem The 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 above) This is an "accident looking for a place to happen." We are going to have a broken axel!

6 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!

7 Adding State -10 0 10 Create a class-level variable named direction
To 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. left right Create a class-level variable named direction start with direction at 0 (straight ahead) right-arrow increases direction left-arrow decreases direction

8 Create direction 1) direction is declared to be a number type.
2) Its value is initialized to 0.

9 Revised plan Do together Event: right arrow right: Event: left arrow
Increment direction Event: left arrow left: Decrement direction right and left do not turn the wheels! Event: up arrow forward: Do together corvette move forward 1 meter corvette turn right/left based on direction Event: down arrow backup: Do together corvette move backward 1 meter corvette turn right/left based on direction

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

11 left Q: Why is a negative10 used as a limit in the If statement for this method?

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

13 backup What is the difference between this method and forward?

14 Demo

15 Assignment Chapter 10-1, Variables and Inheritance Lab 10-1


Download ppt "Variables and Inheritance Part 2"

Similar presentations


Ads by Google