Variables and Inheritance Part 2

Slides:



Advertisements
Similar presentations
Class-level Methods and Inheritance Part 1 Alice.
Advertisements

Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class.
Review of Chapter 4 Sections 1 and 2 World-level methods involve two or more objects break a large problem into smaller, logical units follow a design.
Events Chapter 7. Interactivity The real world is interactive User determines order of actions instead of programmer.
Events Chapter 7. Interactive Real world is interactive User determines order of actions instead of programmer.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Introduction to Alice Alice is named in honor of
Alice: A Visual Introduction to Programming Chapter 1 Part 3.
Learn Alice Basic and Storyboard
MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal.
Execution Control with If/Else and Boolean Functions
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Interaction: Events and Event Handling
Events (2) (Alice In Action, Ch 6) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Variables and Inheritance A More Complex Example Alice.
What we will do today Learn about functions in Alice.
Functions Alice.
Variables and Inheritance Part 1
Programming: Simple Control Structures Part 2 – Repetition Alice.
Negative Power or Negative Rotation makes robot go backwards. But if both are negative, the robot will go forward! Move Steering Block.
Interactive Programming Alice. Control of flow Control of flow -- how the sequence of actions in a program is controlled. What action happens first, what.
Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value.
Lists Alice. Collections In some animations, several objects must perform the same actions Example: A marching band, where the band members are performing.
Interactive Programming Alice. Control of flow Control of flow -- how the sequence of actions in a program is controlled. What action happens first, what.
Solving One-Step Inequalities
Lesson 8C Animation and Events. Step 3: Animation Drag it into the method and have him say “let her go!” Click on the knight in the object tree and scroll.
CompSci 4 Chap 10 Nov 22, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
CS320n –Visual Programming Execution Control with If / Else and Boolean Functions (Slides 6-2-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for.
Variables and Inheritance Part 1 Alice. Review: Properties A class defines properties for its own kind of object. When an object is created (instantiated),
OVERVIEW In this challenge, you will:
OVERVIEW In this challenge, you will:
Functions Sec 51 Web Design.
Learn about functions in Alice
OVERVIEW In this challenge, you will:
Chapter Four: Motion 4.1 Position, Speed and Velocity
Functions Sec 8-11 Web Design.
MOTION.
Solve Systems of Linear Equations by Elimination
Speed & Velocity.
Accelerated Math 6 August 22, 2013
Algebra: Equations and Inequalities
3-4A Solving Multi-Step Inequalities
Programming: Simple Control Structures
Solving and Graphing Linear Inequalities
Programming: Simple Control Structures
Objects in Alice: Positioning and Moving Them
Chapter 4-1 The Foundations of Physical Science
Motion and Force. Motion and Force Chapter Three: Motion 3.1 Position and Velocity 3.2 Graphs of Motion 3.3 Acceleration.
Motion and Force. Motion and Force Chapter Twelve: Distance, Time, and Speed 12.1 Distance, Direction, and Position 12.2 Speed 12.3 Graphs of Motion.
Interactive Programming
Programming: Simple Control Structures
Final Review Slides The following set of slides are designed to review the topics that will be on the final assessment. Topics will include reflections,
Distance vs Displacement
Question 6.
Variables and Inheritance Part 1
Motion and Force. Motion and Force Chapter Three: Motion 3.1 Position and Velocity 3.2 Graphs of Motion 3.3 Acceleration.
Functions Alice.
ICT Gaming Lesson 3.
Chapter Four: Motion 4.1 Position, Speed and Velocity
Alice and Daisies: Positioning and Moving Objects in Alice
Lesson One: Objects in Alice: Positioning and Moving Them
Inequalities and their Graphs
Motion and Force. Motion and Force Chapter Three: Motion 3.1 Position and Velocity 3.2 Graphs of Motion 3.3 Acceleration.
Functions.
Functions Alice.
Programming: Simple Control Structures
Interactive Programming
Functions Alice.
Functions Alice.
Variables and Inheritance A More Complex Example
Presentation transcript:

Variables and Inheritance Part 2 Alice

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.

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.

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

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!

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!

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

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

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

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.

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

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?

backup What is the difference between this method and forward?

Demo

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