Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts."— Presentation transcript:

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

2 An Object-Oriented Approach to Programming Logic and Design 2 Objectives Remain within array bounds Use a for loop to process arrays Declare an array of objects Pass arrays to methods Sort array elements

3 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

4 An Object-Oriented Approach to Programming Logic and Design 4 Remaining 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

5 An Object-Oriented Approach to Programming Logic and Design 5 Remaining Within Array Bounds (continued)

6 An Object-Oriented Approach to Programming Logic and Design 6 Remaining Within Array Bounds (continued)

7 An Object-Oriented Approach to Programming Logic and Design 7 Using a for 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

8 An Object-Oriented Approach to Programming Logic and Design 8 Using a for Loop to Process Arrays (continued)

9 An Object-Oriented Approach to Programming Logic and Design 9 Using a for Loop to Process Arrays (continued) A more efficient way:

10 An Object-Oriented Approach to Programming Logic and Design 10 Declaring 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]

11 An Object-Oriented Approach to Programming Logic and Design 11 Declaring an Array of Objects (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}

12 An Object-Oriented Approach to Programming Logic and Design 12 Declaring an Array of Objects (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 – 1 for x = 0 to MAX print emp[x].getEmpNum(),” “, emp[x].getEmpSal() endfor

13 An Object-Oriented Approach to Programming Logic and Design 13 Passing 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

14 An Object-Oriented Approach to Programming Logic and Design 14 Passing Arrays to Methods (continued)

15 An Object-Oriented Approach to Programming Logic and Design 15 Passing Arrays to Methods (continued)

16 An Object-Oriented Approach to Programming Logic and Design 16 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

17 An Object-Oriented Approach to Programming Logic and Design 17 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)

18 An Object-Oriented Approach to Programming Logic and Design 18 Passing Arrays to Methods (continued)

19 An Object-Oriented Approach to Programming Logic and Design 19 Passing Arrays to Methods (continued)

20 An Object-Oriented Approach to Programming Logic and Design 20 Sorting 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

21 An Object-Oriented Approach to Programming Logic and Design 21 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 = temp endif

22 An Object-Oriented Approach to Programming Logic and Design 22 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 array 2.Compare the first two numbers in the array 3.If not in order, swap them 4.Compare 3rd value with 2nd value and swap if necessary 5.Continue will all values in same manner

23 An Object-Oriented Approach to Programming Logic and Design 23 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 endif endfor

24 An Object-Oriented Approach to Programming Logic and Design 24 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

25 An Object-Oriented Approach to Programming Logic and Design 25 Sorting Array Elements (continued)

26 An Object-Oriented Approach to Programming Logic and Design 26 Sorting 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()

27 An Object-Oriented Approach to Programming Logic and Design 27 Sorting Arrays of Objects (continued)

28 An Object-Oriented Approach to Programming Logic and Design 28 Using Two-Dimensional and Multidimensional Arrays A one-dimension array can be envisioned as a column of values.

29 An Object-Oriented Approach to Programming Logic and Design 29 Using Two-Dimensional and Multidimensional Arrays (continued) A two-dimension array can be envisioned as a table, with rows and columns.

30 An Object-Oriented Approach to Programming Logic and Design 30 Using Two-Dimensional and 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}

31 An Object-Oriented Approach to Programming Logic and Design 31 Using Two-Dimensional and Multidimensional Arrays (continued) numeric rents[4][3] = (400, 450, 510}, {500, 560, 630}, {625, 676, 740}, {1000, 1250, 1600}

32 An Object-Oriented Approach to Programming Logic and Design 32 Using a Built-In Arrays 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

33 An Object-Oriented Approach to Programming Logic and Design 33 Using a Built-In Arrays Class (continued)

34 An Object-Oriented Approach to Programming Logic and Design 34 Using a Built-In Arrays Class (continued)

35 An Object-Oriented Approach to Programming Logic and Design 35 Using a Built-In Arrays Class (continued)

36 An Object-Oriented Approach to Programming Logic and Design 36 Summary 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

37 An Object-Oriented Approach to Programming Logic and Design 37 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


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

Similar presentations


Ads by Google