scripting for interactive media

38
UFCFY5-30-1 Multimedia Studio Scripting for Interactive Media Declaring and Using Arrays

Upload: derek-mccormick

Post on 02-Jan-2016

36 views

Category:

Documents


0 download

DESCRIPTION

Scripting for Interactive Media. Declaring and Using Arrays. Agenda. Why Use an Array Array Declaration in ActionScript Searching and Sorting an Array The for - next loop Parallel Arrays Array Access Errors Tutorial Example AFI Movies. Need to Store Five Numbers. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Scripting for Interactive Media

Declaring and Using Arrays

Page 2: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Agenda

Why Use an Array Array Declaration in Java Searching and Sorting an Array Using a for - next loop with Arrays Parallel Arrays Array Access Errors

Page 3: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Need to Store Five Numbers

Use individual variables for each number stored:

int numberOne;int numberTwo;int numberThree;int numberFour;int numberFive;

messy - have to process as individual items ratherthan as a collection of grouped items. Solution - use an Array - contiguous data storage

Page 4: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Arrays and Data Storage

15

95

14

70

23

Elements

The value stored at array element number 2 is 14

Arrays store values in ‘elements’ (compartments)

i.e. contiguous memory locations

0

3

2

4

1

Page 5: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Arrays and Data Storage

15

95

14

70

23

Elements

The array has 5 elements 0..4

The value stored at array element number 4 is 23

Array element numbering starts at 0

0

3

2

4

1

Thus the size of the array is 5

Page 6: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Arrays and Data Types

15

23

14

70

5

An Array ofnumbers (integers)

Gibson

Fender

Aria

Martin

Array of stringsLes Paul

Page 7: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Declaring Arrays in Java

// create a new array

[] numberArray = new int[5];

// store the numbers into the array// 15 95 14 70 23

numberArray[0] = 15;numberArray[1] = 95;numberArray[2] = 14;numberArray[3] = 70;numberArray[4] = 23;

Page 8: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

The for – next Loop

// loops for a set number of times

for (start; condition; increment) {

// do something here

}

Page 9: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Accessing Array Elements

// iterate through the array

for (int index = 0; index <= 4; index++) {System.out.println(numberArray[index]);

}

// outputs each number in the Array

Page 10: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

System Output from for loop

1595147023

Page 11: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Adding Array Elements

// example of declaring specific(literal) values for an array

int [] numberValues = {2, 5, 23, 37, 11, 19, 49, 6, 15, 41} ;

int total = 0;

for (int index = 0; index <= 9; index++){

total = total + numberValues[index];

}

System.out.println("The total of all the numbers is:" + total);

Page 12: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Summing the Values of an Array 2

// total the numbers stored in the number array using array// length method

int index;// loop control variable int sum = 0;

for (index = 0; index <= numberArray.length; index++){sum = sum + numberArray[index]

}

System.out.printl("The sum of the numbers in the array is " + sum)

Page 13: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Declaring String Arrays in Java

// create a new array

String [] nameArray = new String[5];

// store the names into the array

nameArray[0] = “Bugs Bunny”;nameArray[1] = “Daffy Duck”;nameArray[2] = “Yosemite Sam”;nameArray[3] = “Mickey Mouse”;nameArray[4] = “Porky Pig”;

Page 14: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

for..next loop to output contents

for (int index = 0; index <= 4; index++){

System.out.println(nameArray[index]);

}

Page 15: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

System Output

Bugs Bunny

Daffy Duck

Yosemite Sam

Mickey Mouse

Porky Pig

Page 16: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

for..next loop to search for a match

for (int index = 0; index <=4; index++){

if(nameArray[index] == "Mickey Mouse"){

System.out.println("Walt Disney Studios :" + nameArray[index]);

} // end if

else {

System.out.println("Warner Bros. Pictures :" + nameArray[index]);

}// end else

} // end for

Page 17: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

System Output from Search

Warner Bros. Pictures :Bugs Bunny

Warner Bros. Pictures :Daffy Duck

Warner Bros. Pictures :Yosemite Sam

Walt Disney Studios :Mickey Mouse

Warner Bros. Pictures :Porky Pig

Page 18: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Typical Aspects of using Arrays

• Search an array for a given value

• Sort an array in a given order

• Find the size of the array

• Use the length method in searching algorithms

• Compare the contents of one array with another array

Page 19: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Beware! The classic runtime error is where a program tries to access an array element beyond the array’s size.

for (index = 0; index <=5; index++)The array has only five elements, but the code is attempting to access element number six (5), which does not exist.

The computer speak is the code has ‘fallen off the end of the array’

Page 20: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Parallel Arrays

[email protected]

name

John

Ben

Freda

Sue

Mike

email

[email protected]

[email protected]

[email protected]

[email protected]

Page 21: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Parallel Array Search

[email protected]

name

John

Ben

Freda

Sue

Mike

email

[email protected]

[email protected]

[email protected]

[email protected]

Search on name

Extract email on name

Page 22: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Parallel Array Search

[email protected]

name

John

Ben

Freda

Sue

Mike

email

[email protected]

[email protected]

[email protected]

[email protected]

Search on name

Find occurrences on name of email

Page 23: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Arrays of Data (sounds)

Array of .mp3 audio files

footsteps.mp3

shell.mp3

siren.mp3

alarm.mp3

ambience.mp3

Page 24: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Summary

• Arrays are a fundamental structured data type in any programming language

• Arrays store values usually of the same type in a contiguous way which allows the data to be accessed and manipulated as a collection of data

• Typical array access is to search and sort the contents

• Associated data may be held in parallel arrays and use cross referencing to extract the required information

• Algorithms that manipulate arrays must stay within the array bounds or runtime errors will occur causing the program to ‘freeze’ or ‘crash’.

Page 25: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Revise Last Week’s Concepts

Three new coding structures and one new variable type

Page 26: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

The Lottery Draw

How will this week’s new concepts help to code a solution to the Lottery Draw scenario?

Page 27: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Tutorial Example AFI Movies

The first of the American Film Institute (AFI)

100 Years series of cinematic milestones.

AFI's 100 Years…100 films is a list of

the 100 best American films, as determined by the

American Film Institute from a poll of more than 1,500

artists and leaders in the film industry who chose from

a list of 400 nominated films.

The 100-best list was unveiled in 1998.

Page 28: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Parallel Arrays filmList and userRating

filmList

7

3

9

8

6

userRating

Citizen Kane

Casablanca

The Godfather

Gone with the Wind

Lawrence of Arabia

Page 29: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

Parallel Arrays filmList and userRating

7

3

9

8

6

Citizen Kane

Casablanca

The Godfather

Gone with the Wind

Lawrence of Arabia

element 0 not used

1

2

3

4

5

Page 30: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

var filmList:Array = new Array();

filmList[1] = "Citizen Kane";

filmList[2] = "Casablanca";

filmList[3] = "The Godfather";

filmList[4] = "Gone with the Wind";

filmList[5] = "Lawrence of Arabia";

.

.

filmList[99]= "Guess Who's Coming to Dinner";

filmList[100]= "Yankee Doodle Dandy";

AFI Movies Array- filmList

Page 31: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

var userRating:Array = new Array();

userRating[1] = 7;

userRating[2] = 6;

userRating[3] = 8;

userRating[4] = 3;

userRating[5] = 9;

.

.

userRating[99] = 2;

userRating[100] = 7;

AFI Movies Array- userRating

Page 32: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

var index:int // loop variable for the for loop index

// two functions to initialize the data

setFilms();// initializes the film list

setRating(); initializes the user-ratings

Index Loop Variable and Array Initialization

Page 33: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

// output all the films in the filmList array

for(index=1; index <= 100; index++){

trace(filmList[index]);

}

Output all the films in the filmList Array

Page 34: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

// output all the films in the array

// and show the user rating

for(index=1; index <= 100; index++){

trace(filmList[index] + " " + userRating[index]);

}

}

Output all the films and show the user rating

Page 35: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

// output all the films where userRating > 7

for(index=1; index <= 100; index++){

if(userRating[index] > 7){

trace(filmList[index] + " " + userRating[index]);

}

}

Output all the films with a user rating greater than 7

Page 36: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

// output all the films where userRating = 5

for(index=1; index <= 100; index++){

if(userRating[index] == 5){

trace(filmList[index] + " " + userRating[index]);

}

}

Output all the films with a user-rating of 5

Page 37: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

correct

if(userRating[index] == 5)comparison

incorrect

if(userRating[index] = 5)assignment

this will output all the movies!

Watch Out for a Logical Error!

Page 38: Scripting for Interactive Media

UFCFY5-30-1Multimedia Studio

// use the these lecture slides to carry out the exercises

// e.g. output all the films in the filmList array

for(index=1; index <= 100; index++){

// put your code here

}

Tutorial with Working Code for you to Modify