+ All Categories
Home > Documents > An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

Date post: 18-Jan-2018
Category:
Upload: abel-blankenship
View: 229 times
Download: 0 times
Share this document with a friend
Description:
An Object-Oriented Approach to Programming Logic and Design 3 Objectives (continued) Sort arrays of objects Use two-dimensional and multi-dimensional arrays Use a built-in Arrays class
37
An Object-Oriented An Object-Oriented Approach to Approach to Programming Logic and Programming Logic and Design Design Chapter 8 Advanced Array Concepts
Transcript
Page 1: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to An Object-Oriented Approach to

Programming Logic and DesignProgramming Logic and Design

Chapter 8Advanced Array Concepts

Page 2: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 2

ObjectivesObjectives

• Remain within array bounds• Use a for loop to process arrays• Declare an array of objects• Pass arrays to methods• Sort array elements

Page 3: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 3

Objectives (continued)Objectives (continued)

• Sort arrays of objects• Use two-dimensional and multi-dimensional

arrays• Use a built-in Arrays class

Page 4: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 4

Remaining Within Array BoundsRemaining Within Array Bounds

• An array has a finite size• Size measured by number of elements or by

number of bytes in the array• Because an array’s elements are all the same

type, array size in bytes is a multiple of the number of elements

• Out of bounds: when you try to use a subscript that is not within the range of the declared array’s subscripts

Page 5: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 5

Remaining Within Array Bounds Remaining Within Array Bounds (continued)(continued)

Page 6: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 6

Remaining Within Array Bounds Remaining Within Array Bounds (continued)(continued)

Page 7: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 7

Using a Using a forfor Loop to Process Arrays Loop to Process Arrays

• for loop can step through each element of an array

• Set the for loop to start at 0 and end at the highest subscript

• Remember: the highest subscript is one less than the number of elements (array size)

• A named constant can be used for the upper limit of the for loop

Page 8: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 8

Using a Using a forfor Loop to Process Arrays Loop to Process Arrays (continued)(continued)

Page 9: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 9

Using a Using a forfor Loop to Process Arrays Loop to Process Arrays (continued)(continued)

A more efficient way:

Page 10: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 10

Declaring an Array of ObjectsDeclaring an Array of Objects

• An array can hold elements of any type, such as numbers, strings, objects, etc.

• For an array of objects, declare the array with the type of the object (i.e., the class name)

Examples: Employee emp[7]Employee emp[NUM_EMPLOYEES]

Page 11: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 11

Declaring an Array of Objects Declaring an Array of Objects (continued)(continued)

• If the class constructor requires parameters, you must provide the values when you declare the object array

• Example: Employee emp[NUM_EMPLOYEES] = {101, 12.45}, {103, 7.50}, {119, 13.25}, {213, 15.00}, {218, 8.40}, {395, 16.00}, {405, 9.00}

Page 12: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 12

Declaring an Array of Objects Declaring an Array of Objects (continued)(continued)

• When using an object’s method, place the subscript after the array name and before the dot preceding the method name

Example:numeric MAX = NUM_EMPLOYEES – 1for x = 0 to MAX print emp[x].getEmpNum(),” “,

emp[x].getEmpSal() endfor

Page 13: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 13

Passing Arrays to MethodsPassing Arrays to Methods

• An array element can be passed as a parameter just like any simple variable

• An array element is passed “by value” • Only a copy of the value of the array element is

given to the method; the original array element remains unchanged

Page 14: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 14

Passing Arrays to Methods (continued)Passing Arrays to Methods (continued)

Page 15: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 15

Passing Arrays to Methods (continued)Passing Arrays to Methods (continued)

Page 16: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 16

Passing Arrays to Methods (continued)Passing Arrays to Methods (continued)

• You can pass an entire array as a parameter• The array is passed “by reference”: a pointer to

the original array is given to the method• Any changes made to the array in the method are

made to the actual array

Page 17: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 17

Passing Arrays to Methods (continued)Passing Arrays to Methods (continued)

Example: constant numeric NUM_ELEMENTS = 4 constant numeric MAX = NUM_ELEMENTS - 1 numeric someNums[NUM_ELEMENTS] =

5, 10, 15, 20

methodGetsArray(someNums,MAX)

Page 18: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 18

Passing Arrays to Methods (continued)Passing Arrays to Methods (continued)

Page 19: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 19

Passing Arrays to Methods (continued)Passing Arrays to Methods (continued)

Page 20: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 20

Sorting Array ElementsSorting Array Elements

• Sorting: arranging a series of objects in some logical order

• Ascending order: smallest at the beginning, largest at the end

• Descending order: largest at the beginning, smallest at the end

• Sorting can be done by comparing two values, and swapping their positions to achieve the correct order

Page 21: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 21

Sorting Array Elements (continued)Sorting Array Elements (continued)

• Use a temporary variable to hold the current value when swapping values

Example:If valA > valB then temp = valA valA = valB valB = tempendif

Page 22: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 22

Sorting Array Elements (continued)Sorting Array Elements (continued)• Bubble sort: compare pairs of items, swapping if out of

order, until the smallest item “bubbles” up to the top of the list

• Steps:1. Place all values to be sorted into an array2. Compare the first two numbers in the array3. If not in order, swap them4. Compare 3rd value with 2nd value and swap if

necessary5. Continue will all values in same manner

Page 23: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 23

Sorting Array Elements (continued)Sorting Array Elements (continued)

• The list may have to be processed several times to get all items into order.for b = 0 to ARRAY_SIZE - 2 if someNums[b] > someNums[b + 1] then temp = someNums[b] someNums[b] = someNums[b + 1] someNums[b + 1] = temp endifendfor

Page 24: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 24

Sorting Array Elements (continued)Sorting Array Elements (continued)

• At first pass, the largest value “sinks” to the bottom of the list

• Therefore, you do not have to compare the last value on the next pass.

• Each successive pass through the array places the next largest value in the element in the next last position, so one less value needs to be compared

Page 25: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 25

Sorting Array Elements (continued)Sorting Array Elements (continued)

Page 26: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 26

Sorting Arrays of ObjectsSorting Arrays of Objects

• Arrays of objects can be sorted in a similar fashion

• The comparison of objects may be different• Objects are usually compared by comparing a

field value • Example:

Sort an Employee object by calling getEmpSal()

Page 27: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 27

Sorting Arrays of Objects (continued)Sorting Arrays of Objects (continued)

Page 28: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 28

Using Two-Dimensional and Using Two-Dimensional and Multidimensional ArraysMultidimensional Arrays

• A one-dimension array can be envisioned as a column of values.

Page 29: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 29

Using Two-Dimensional and Using Two-Dimensional and Multidimensional Arrays (continued)Multidimensional Arrays (continued)

• A two-dimension array can be envisioned as a table, with rows and columns.

Page 30: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 30

Using Two-Dimensional and Using Two-Dimensional and Multidimensional Arrays (continued)Multidimensional Arrays (continued)

• To declare a two-dimension array, use two sets of brackets for the number of rows and columns

Example: numeric someNumbers[3][4]• Two dimensional array can be initialized in the declaration.Example:numeric someNumbers[3][4] = {8, 9, 10, 11}, {1, 3, 12, 15}, {5, 9, 44, 99}

Page 31: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 31

Using Two-Dimensional and Using Two-Dimensional and Multidimensional Arrays (continued)Multidimensional Arrays (continued)

numeric rents[4][3] = (400, 450, 510}, {500, 560, 630}, {625, 676, 740}, {1000, 1250, 1600}

Page 32: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 32

Using a Built-In Using a Built-In ArraysArrays Class Class

• Many OOP languages provide an Arrays class containing useful methods for manipulating arrays

• Most methods are overloaded to handle different types of data

• If the language you are using does not have a built-in Arrays class, consider building one yourself

Page 33: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 33

Using a Built-In Using a Built-In ArraysArrays Class Class (continued)(continued)

Page 34: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 34

Using a Built-In Using a Built-In ArraysArrays Class Class (continued)(continued)

Page 35: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 35

Using a Built-In Using a Built-In ArraysArrays Class Class (continued)(continued)

Page 36: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 36

SummarySummary

• Subscript values must stay in bounds• for loop is useful for processing every element

in an array• Array can hold elements of any type• When passed as parameters, array elements

are passed by value• When an entire array is passed as a parameter,

it is passed by reference

Page 37: An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.

An Object-Oriented Approach to Programming Logic and Design 37

Summary (continued)Summary (continued)

• Arrays are useful for sorting items into an ascending or descending order

• To sort an array of objects, compare using a field in the object

• Single dimension array is a column of values• Two-dimension array is a table of values• A built-in Arrays class usually provides

common methods for manipulating arrays


Recommended