1 / 23

15-101 Built-in Functions & Arithmetic Expressions

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 .

cathal
Download Presentation

15-101 Built-in Functions & Arithmetic Expressions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 15-101 Built-in Functions & Arithmetic Expressions

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

  3. To start Download and open the world you were working on from last session: - or - CurupiraSoccerRunToObject.a3p

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

  5. Our implementation Curupira moves forward, but how far? How can we be sure that 1 meter is the correct distance?

  6. Curupira does not run far enough

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

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

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

  10. Return a value of a specific data type Built-in functional methods Joints Decimal Number Boolean String

  11. Calling a functional method We use the getDistanceTo function to determine the distance between the curupira and the <target>

  12. Calling a functional method • Video demonstration of adding getDistanceToTile

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

  14. Problem: Collision • When our program is run, the curupira seems to move through the ball – too much collision.

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

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

  17. A numeric operand can be either • A whole number (integer) • Or a floating pt number (decimal number).

  18. Arithmetic operators are symbols that can easily be typed on a keyboard Addition + Subtraction - Multiplication * Division / Remainder (mod) %

  19. Arithmetic expressions in Alice Click pull-down menu arrow for getDistanceToand select Math Build expression from the cascading menus.

  20. Arithmetic expressions in Alice • Video demonstration of building an arithmetic expression

  21. Binary operation Cascading menus assume a binary operation Operand Operator Operand Resulting statement:

  22. Complete runToBall procedure

  23. Curupira does not move through the soccer ball

More Related