1 / 25

Mastering Arrays in Java Programming: Learn, Implement, and Practice

Explore arrays, array declaration, processing, and manipulation in Java programming. Practice problems & exercises included.

erobitaille
Download Presentation

Mastering Arrays in Java Programming: Learn, Implement, and Practice

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 • Read 10.3-10.5 • No quiz • Program 6 due

  2. Program 6 • Any questions?

  3. Problem 2 • Write a program that finds the smallest of several integers. Assume that input will end when a sentinel value of –999 is read. Do not count –999 as one of the integers to consider.

  4. Problem 3 • Write a program that sums a sequence of integers. Assume that the first integer read specifies the number of values remaining to be entered. Your program should read only one value at a time. A typical input sequence might be 5 100 200 300 400 500

  5. Practice Problem • Write Java code to ask for a student’s exam score out of 100. Your program is then to match the exam score to a letter grade and print the grade to the screen. The letter grade is to be calculated as follows: 90 and above A 80-89 B 70-79 C 60-69 D below 60 F

  6. Arrays • Sometimes we need to process lots of similar elements • We could create a variable for each element • But this can lead to very unwieldy code • Instead, we use arrays

  7. What is an Array? • A sequence of elements, all of the same type. • An array has a name just like other variables. The entire array is referred to by this name. • An array is a special kind of object.

  8. Examples?

  9. Creating Arrays • How do we declare an array of Student objects? • How do we create an array of Student objects? • How do we put a Student object into the array?

  10. Accessing a Particular Element

  11. Processing the Whole Array

  12. Problem 1 • Write code to print all of the Students in our array, assuming that the Student class includes an appropriate toString method.

  13. Finding the One You Want

  14. Problem 2 • Write a method to locate and print a particular Student, given the Student’s name. Assume that the Student class has a getName method.

  15. 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[];

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

  17. 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.

  18. 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];

  19. 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]

  20. 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.

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

  22. 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};

  23. 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

  24. 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.

  25. 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.

More Related