1 / 21

For Friday

For Friday. No reading WebCT quiz 20. Program 7. Any questions?. Exam 2. Tonight 8-10 pm STV 101. Review Questions?. Primitive Array Declaration. To specify that a variable is an array, we include square brackets, [], in the declaration. int [] scores; char [] gradeArr;

mariobrown
Télécharger la présentation

For Friday

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. For Friday • No reading • WebCT quiz 20

  2. Program 7 • Any questions?

  3. Exam 2 • Tonight 8-10 pm • STV 101

  4. Review Questions?

  5. Primitive Array Declaration • To specify that a variable is an array, we include square brackets, [], in the declaration. • int [] scores; • char [] gradeArr; • The square brackets can come after the variable name, as they do in some other languages: • int scores[]; • char gradeArr[];

  6. Review • Since arrays are objects, we create them using what keyword?

  7. Array Creation • In the creation, we have to specify the type and size of the array:scores = new int[5];gradeArr = new char[10];price = new double[20]; • Once the array is created, the size of the array cannot be changed.

  8. Array Creation continued • We often use named constants or variables for the size in an array declaration:final int SIZE = 10;final int MAX_ELEMS = 15;int [] arr = new int[SIZE];double[] flArr = new double[MAX_ELEMS];

  9. Accessing Individual Elements • Subscripts (or indices) always start at 0, so an array with 5 elements has one at 0, one at 1, one at 2, one at 3, and one at 4. • We access a particular array element by using the array name followed by the index in square brackets: score[0] arr[9]

  10. Using Array Elements • All of the following are valid:score[0] = 4;score[0] += 7;score[1] = score[0] -2;score[2] = score[1] + 5 * score[0]; score[j] = score[j + 1]; • Note: index can be any integral expression.

  11. Getting Data into Arrays score[0] = 30; grade[3] = ‘A’; price[2] = 10.39;

  12. Array Initialization • We can put initial values into an array when we create it. • We must list all of the values:int [] num = {58, 43, 60, 21, 38};

  13. Array Practice • Create an array to hold the tax for up to 10 different sales • Create an array to hold the final letter grades for a class with up to 40 students • Create an array of integers which holds the final average for those 40 students • Create an array of characters with initial values ‘a’, ‘d’, ‘y’, and ‘w’ • Assign TAX_RATE * price to the first item in your first array

  14. Problem 1 • Write Java code to read values from the keyboard to fill the array scores. Input should stop when a negative number is entered. The maximum size of the array is in a constant ARR_SIZE.

  15. Problem 2 • Write Java code to add up the first num_elements values in the array myVals and store the sum in the variable mySum.

  16. Searching An Array • Search through array for particular element • Why would we want to do this? • How can we do it?

  17. Linear Search Set found to false Start with initial array element DOWHILE not found and more elements IF the current element matches the target THEN set found to true ELSE advance to next element END IF ENDDO IF found THEN return current index as the search result ELSE return -1 as the search result. END IF

  18. The Biggest Index • Write a method, MaxIndex, that receives as parameters an integer array and the number of elements in that array. The function should return the index (not the value) of the largest element in the array. If there is more than one element with that value, return the index of the first element with the largest value.

  19. Practice • Declare and create an array of Employee objects. • Declare and create an array of TimeCard objects. • Assume Employee has methods getName() and computePay(int hours). Assume TimeCard has a method getHours. Further assume that the arrays declared above have been filled with corresponding information. Write code to print out the pay for all of the employees.

  20. Deleting from an Array

  21. Array and File Practice • Assume you have a file containing information about a store’s inventory. Each line of the file has a part number, a price, a number on hand, and a number ordered. Write a function to read data from this file into an array of Part objects. We don’t know ahead of time how many parts there are, but we will need to know later in the program.

More Related