230 likes | 411 Views
15-101 Built-in Functions & Arithmetic Expressions. Recall :. A curupira is playing soccer in the rainforest. The curupira has a soccer ball and will kick the soccer ball toward a pond in the forest. Each kick causes the soccer ball to move forward a random distance .
E N D
15-101 Built-in Functions & Arithmetic Expressions
Recall: • A curupira is playing soccer in the rainforest. The curupira has a soccer ball and will kick the soccer ball toward a pond in the forest. Each kick causes the soccer ball to move forward a random distance. • If the curupira gets the soccer ball in the water, the curupira scores a point and does a dance, or else he disappears in disgust
To start Download and open the world you were working on from last session: - or - CurupiraSoccerRunToObject.a3p
Where we left off Working on running to the soccer ball Do in order curupira runs to soccer ball curupira kicks the soccer ball a random distance If soccer ball is in the water, score 1 point curupira dances else curupira disappears
Our implementation Curupira moves forward, but how far? How can we be sure that 1 meter is the correct distance?
Known vs Unknown Distance • In the PenguinBowling project, the penguin was positioned exactly 1 m from the pen – a known distance. • pushObjectprocedure moves this Penguin objectforward 1 meter to collide with the target object • In the CurupiraSoccer project, the distance between the soccer ball and the curupira is unknown.
Functions • A function asks a question (to check a condition or compute a value). • In Alice, a function is used to get the information we need concerning • the properties of objects • Is the water blue? • the relationship of one object to another • What is the distance between the curupira and the soccer ball?
Alice 3 Methods Panel Performs an action; does NOT compute and return a value Computes and returns a value; does NOT perform an action Sets or gets a value that is stored in a property variable.
Return a value of a specific data type Built-in functional methods Joints Decimal Number Boolean String
Calling a functional method We use the getDistanceTo function to determine the distance between the curupira and the <target>
Calling a functional method • Video demonstration of adding getDistanceToTile
Calling a functional method A call to a function is embedded within a larger statement (that is, a call to a function is NOT a syntactically complete statement) Example: Calling getDistanceTo The return value (distance value returned by the function)is used as an argument in the move statement
Problem: Collision • When our program is run, the curupira seems to move through the ball – too much collision.
The problem is that distance between two objects is measured center-to-center. • One way to solve this problem is to subtract a small number from the distance.
Arithmetic Expressions Traditional math and formula computations are implemented as arithmetic expressions in a computer program. An arithmetic expression is a combination of operators and operands evaluates to a numeric value
A numeric operand can be either • A whole number (integer) • Or a floating pt number (decimal number).
Arithmetic operators are symbols that can easily be typed on a keyboard Addition + Subtraction - Multiplication * Division / Remainder (mod) %
Arithmetic expressions in Alice Click pull-down menu arrow for getDistanceToand select Math Build expression from the cascading menus.
Arithmetic expressions in Alice • Video demonstration of building an arithmetic expression
Binary operation Cascading menus assume a binary operation Operand Operator Operand Resulting statement: