Arrays

advertisement
Programming with Alice
Computing Institute for K-12 Teachers
Summer 2011 Workshop
Session 6
Introduction

Boolean Expressions – In programming, a Boolean
expression is an expression that is either true or false.
These are the Boolean logic functions we will discuss:
not (
), and (
), and or (
).

Arrays – Arrays are used to store multiple pieces of related
data of the same type in order to easily and quickly find
and use the data later in the program. Since Alice is type
sensitive, if an array is created, it only holds a certain type
of variable. For example, if we needed to store multiple
numbers for later use we could use a number array where
only numbers can be stored, if we needed to store multiple
words for later use, we could use a string array.
Boolean Review

Some basic Boolean operators we discussed in Session 3
are listed below. These return true or false and can be used
in control structures such as a conditional branch or a while
loop.

There are three other commonly used Boolean functions
that can be combined with Boolean operators, variables, or
other functions to create extremely flexible expressions.
Boolean Expressions

Now we will discuss these Boolean functions and provide
examples of each.

Boolean functions accept Boolean parameters and return a
Boolean value.

The AND function is used when two conditions need to be
met in order to execute specified blocks. If the first
condition is true and the second condition is true, then this
operator returns true. If either of the conditions are false,
the expression returns false. For example:

Since 3 < 4 evaluates to true and 8 == 8 evaluates to
true, then the AND function returns true which makes the
predicate part of the if block to evaluate to true which
them makes the camera say “Hello!”
Boolean Expressions

In this example, 5 isn’t less than 4 so it evaluates to false.
Even though one condition is true (8 == 8), using the AND
operator requires both conditions to return true.

The OR operator is very similar to the AND operator except it
only requires one condition to be met.

Even though in this example 5 == 1 evaluates to false, all the
function needs in order evaluate to true is one condition to
evaluate true. The full expression evaluates true because 3 is
less than 4.
Boolean Expressions

The NOT function is another useful tool that changes true to false and
false to true, for example:

In the above example, because 5 is not equal to 3, and the not block
was used, the expression evaluates to true. The NOT operator in the
next example will show the if block not leading into the consequent.

Because the expression 5 == 5 is true, using the NOT function causes
the predicate part of the If/Else structure to evaluate to false. The
say block will not be executed. These Boolean functions can be used
in the conditional part of a while loop control structure but are
commonly found in the predicate part of an If/Else control structure.
Arrays

Arrays are an essential programming tool. As stated
previously, arrays can store multiple values that the
programmer may need later in their program. Creating an
array, in a way, is like creating multiple variables. For
example, if we were to have an array of length 5. That
means we have 5 separate values held within the array.

Each value in the array is indexed by a number. In
programming, indices begin at zero so the first element is
indexed with the number 0 and the second element is
indexed with the number 1. Indexing is the way we keep
track of where a specific value is located in the array.

The length of an array needs to be specified by the
programmer before the program begins. This means that it
is impossible to change the length while the program is
running.
Beginning of Spelling Check
Project

Let’s do a spelling check example. This may seem like an
advanced example please know there are many other ways to
reach the same outcome. First, find an object you like that will
be giving the exam. Set up the object so it informs the user of
a checking test.

This test will display multiple words using the object. These
words will be displayed one by one followed by a question
such as “If this word is correct, type correct, if it is incorrect,
type incorrect, the user then will need to respond by either
typing correct, or incorrect.

This means we will need at least two arrays. One to hold all
the words to be displayed and the other to use as a key. The
key will hold the correct answers to later be compared with
the user’s responses.

Drag a loop to the main method and choose 10 times. Since
an Array length in Alice can’t change as the program
continues, we know exactly how many values the array will be
holding. For this project you will need at least two arrays each
having 10 elements. You may need to use multiple variables
as well.
Creating an Array

Creating an array is similar to creating a variable. Let’s
create everything in the world object. Click “create new
variable,” select string variable and check the “make a
Array” box as shown below. Also, make sure you are
creating an array, not a list. Lists will be covered in the
next chapter along with arrays in more detail.

The window should drop down and you should add a new
item until this array contains a total of 10 values (up to
item 9). Note: Currently the below array has a length of 3.
Continuing Project with Arrays

Now we just need to name the array and fill the values.
Let’s name this array “words” and fill the items with
correctly and incorrectly spelled words. These words will be
displayed later by using your object’s say method and a
loop.

Now show the complicated version of the loop we placed
earlier. This is so we can use the index inside of the loop.

A loop of this nature is perfect for stepping through an
array. The index of the array begins at zero and the index
of this loop begins at zero. This is exactly what we need to
step through the array.
Continuing Project with Arrays

Drag a say method and place it inside the loop and choose
“Hello” for now. Now drag the array named “words” to
replace “Hello,” mouse-over “ith item from array” and
select 0 for now.

The first box of the “ith item from array” block represents
the index of the item and the second box represents the
array name and whether or not it belongs to the world or a
specific object.

Drag the index block (located next to the word “loop” in
your loop) replace the zero in the index area of the
“ith item from array” block.
Closer Look

Let’s take a closer look at exactly what is going on right
now. The index starts at 0, so when your object’s say
method is executed, it takes the item at array index 0.

The index variable then increments to 1 followed by the
object’s say method being called with the item at array
index 1.

Let’s go ahead and test the project and see what happens.
The object should go through the array of words we
created.

Now use the join function to join something like “The word
is ” in the first part and drag the “ith item from Array”
block to the second part.
Second Array

Create a second array, choose type String, name this array
“key,” and fill the ten values with the word “incorrect” or
“correct” depending on the item at that same index in the
“word” array. For example, if the item at element 0 is
“translate,” the associated word that would be located in
this array would be “correct” since translate is spelled
correctly. On the other hand if value at index 1 of the
“word” array is “truste,” the associated word would be
“incorrect” because trust was incorrectly spelled.

Use the join function to join a sentence such as “The word
is “ joined with what the object is currently saying. It
should look something like the snippet below.

Put a picture right here.
Continuing Project

Place an If/Else control structure inside the loop below the
current block. We will need at least two variables, one is a
string variable named temp, the other, a number variable
named count with initial value of 0. Set the temp variable
above the If/Else structure but below the say block. Use an
“ask user for string” block as the value of the temp
variable. The snippet below is what we should have so far.
Continuing Project

Now drag the temp variable to the predicate part of the
if/else structure, mouse-over == and choose default string.
Now we will do what we did before and set it up to look like
this. Notice how the marked areas are so similar.

Hint: This was done in the previous slides.
Continuing Project

We will now use the count variable we previously created.
If the word the user entered is equal to the correct word
from the key array, then count increases by 1. Below is an
example of what the If/Else control structure should look
like.

On your own, also make additions to the program so the
object responds to the answer in some way. (talking,
moving, etc.) You should also have the object say the
percent correct.
Project 6

Implement a program that asks the user for a list of
numbers, stores those numbers into an array, then finds
the largest and smallest values and prints them to the
screen.
Download