Presentation is loading. Please wait.

Presentation is loading. Please wait.

15-101 Variables & Random Number Generation.  A penguin is playing arctic basketball. The penguin has a basketball and will push the basketball toward.

Similar presentations


Presentation on theme: "15-101 Variables & Random Number Generation.  A penguin is playing arctic basketball. The penguin has a basketball and will push the basketball toward."— Presentation transcript:

1 15-101 Variables & Random Number Generation

2  A penguin is playing arctic basketball. The penguin has a basketball and will push the basketball toward a hole in the ice. Each push causes the basketball to move forward a random distance.  If the penguin gets the basketball in the water, the penguin scores two points. Recall: Problem

3 Problem Solving step 2  Design an algorithm and set up scene Do in order penguin swagger penguin push basketball a random distance If basketball is in the water, score 2 points

4  Implementation – translate algorithm into code (note that the basketball is now the target) Problem Solving step 3 Do in order penguin swagger penguin push basketball a random distance If basketball is in the water, score 2 points This italicized item will be modified / added in future presentations Need to modify

5  At runtime, a program is allocated a stack of memory (bytes) in which to store its data. – Several data items may be stored in the stack Runtime stack Data items

6  A variable is a name for a location in memory where an item of data is stored  Each data item in the stack is given a unique name (an identifier) Example: in diagram at right, done is a name for location 4  We say a variable “represents a specific value”  The value stored in a variable can and will change when a program runs  Thus -> variable Variable done

7  The data item stored at a specific location has a specific data type, which describes the characteristics of the stored information  Range of values  Legal operations  Data types used in Alice 3

8 Declaring a variable  Drag the local tile into the code editor

9  A variable is declared by specifying – Type – Name – Initial value Declaring a variable Double randDistance = 0.0; AliceJava Assignment: Stores a value in the variable's memory space

10 At the time a variable is declared, it’s data type determines how many bytes of memory the variable is allocated. Example: Byte Allocation randDistance has 8 bytes of memory where a real number may be stored

11  The previously written pushObject procedure uses the distance parameter to move the target object forward 2.75 meters  but we want the target (basketball) to move forward a random distance...so we need to send a random number to the distance parameter. Value sent to parameter

12  In MyScene’s run method, Add a local variable, randDistance, that will represent a random distance.  Replace default initialization value (0.0) with a random value in the range of 0.5 to 5.0 (Why?) Random distance for the ball

13  replace 0.0 with one of the Random number functions Random functions

14  A function designed to generate a random sequence of numbers or symbols that lack any pattern, i.e. appear random - Wikipedia Random number functions  The next value in the random sequence will be in the range 0.0 <= value < 1.0  The next value in the random sequence will be in the range first ??? <= value < second ??? Which function is better for our purposes?

15 Build the random number expression  Build the random expression from the cascading menus.  Resulting statement:

16 Use the randDistance variable  We have generated a random value in the range 0.5 >= value < 5.0, and stored it in our local variable, randDistance  We now need to use this variable as the argument to the pushObject method for the distance parameter  Resulting statement: click select

17 Problem Solving Step 4: Testing Because we are using random numbers, we need to run this code many times to convince ourselves that successive runs yield random results. Alice provides visual feedback Another common technique for testing is to display a value as a textual string

18 Problem Solving Step 4: Testing Because we are using random numbers, we need to run this code many times to convince ourselves that successive runs yield random results. Alice provides visual feedback Another common technique for testing is to display a value as a textual string

19  We could use say or think to display the value in Alice.  Example, using say

20 Displaying a value: Alice The think and say procedural methods expect a String argument To create a String, we can use concatenation – Concatenate means: to append one String to another String – Symbol for concatenation is '+' Example: “hello” + “ world” → “hello world”

21 Example: Alice Drag in say statement tile -> Select “hello” to begin Replace “hello” Select concatenate symbol tile Select operands

22 Custom String One of the operands is a custom string, so Alice displays a dialog box where a string can be entered Resulting statement

23 Display a value: Java The run method in Java: public void run() { penguin.swagger(); Double randDistance = RandomUtilities.nextDoubleInRange(0.5, 5.0); penguin.pushObject(ball, randDistance); penguin.say("Distance moved: " + randDistance); } Note the quote marks around the custom string

24 System output: Java Java defines a System output (by default, to the console box on the monitor screen) public void run() { penguin.swagger(); Double randDistance = RandomUtilities.nextDoubleInRange(0.5, 5.0); penguin.pushObject(ball, randDistance); penguin.say("Distance moved: " + randDistance); System.out.println("Distance moved: " + randDistance); } println – prints on the current line and then moves the cursor to the next line to prepare for next line of output


Download ppt "15-101 Variables & Random Number Generation.  A penguin is playing arctic basketball. The penguin has a basketball and will push the basketball toward."

Similar presentations


Ads by Google