Variables and Inheritance A More Complex Example Alice.

Slides:



Advertisements
Similar presentations
Solving multi-step equations
Advertisements

Class-level Methods and Inheritance Part 1 Alice.
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
Linear Equations Please Read Entire Section 2.1, pages in the text.
Recursion Alice. Repetition In some situations, we don’t know exactly how many times a block of instructions should be repeated. All we know is that repetition.
Execution Control with If/Else and Boolean Functions
Adding, Subtracting, Multiplying, and Dividing Integers
CompSci 4 Chap 5 Sec 1 Oct 13, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
Interaction: Events and Event Handling
Events Chapter 7 Part 2. While a Key is Pressed Event Specialized event An event occurs when you press a key and continues until you take your finger.
Copyright © Cengage Learning. All rights reserved. Real Numbers and Their Basic Properties 1.
Events (2) (Alice In Action, Ch 6) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Creating An Animation Program Part 2 Alice. Method A segment of program code (instructions) that defines how to perform a specific task.
Today’s Agenda 1.Collect Pre-Lab 5 2.Collect Alice project storyboards 3.Events 4.Dummy Objects 5.Assign pair programming teams and meet upstairs for Lab.
Interactive Programming Sec 49 Web Design. Objectives The student will: Understand the difference between movie mode and an interactive program Understand.
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.
Interactive Programming Alice. Control of flow Control of flow -- how the sequence of actions in a program is controlled. What action happens first, what.
Chapter 2 Lesson 3 Subtracting Integers pgs What you will learn: Subtract Integers Evaluate expressions containing variables What you will learn:
Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:
Variables and Functions Alice. Naming is Important If you get a new pet one of the first things you do is name it Gives you a way to refer to the new.
Functions Sec 8-11 Web Design. Objectives The Student will: Understand what a function is Know the difference between a method and a function Be able.
Programming: Simple Control Structures
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.
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.
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.
Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with a specific class of objects. Examples A person.
Variables and Inheritance Part 1 Alice. Review: Properties A class defines properties for its own kind of object. When an object is created (instantiated),
Parameters Section 8-8 Web Design.
Functions Sec 51 Web Design.
Learn about functions in Alice
Programming: Simple Control Structures
Variables and Inheritance Part 2
Programming: Simple Control Structures
Recursion Alice.
Functions Sec 8-11 Web Design.
Recursion Alice.
Parameters Alice.
Programming: Simple Control Structures
Lists Alice.
Repetition: Definite Loops
Lists Alice.
Programming: Simple Control Structures
Parameters and World-level methods
Interactive Programming
Programming: Simple Control Structures
Repetition: Definite Loops
Repetition: Definite Loops
Variables and Inheritance Part 1
Functions Alice.
Programming: Simple Control Structures
Several Transformations
Parameters Alice.
Functions Alice.
Programming: Simple Control Structures
Interactive Programming
Functions Alice.
Functions Alice.
Variables and Inheritance A More Complex Example
Presentation transcript:

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.

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

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

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

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

Revised plan Event: right arrow right: Increment direction Event: left arrow left: Decrement direction Event: down arrow backup: Do together corvette move backward 1 meter corvette turn right/left based on 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

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.

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 negative 10 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?

Assignment Chapter 10 Section 1, pages

Lab Chapter 10 Lecture 1 Lab