Arrays Topic 4.3.9 / Core
Key points
  • declaring and manipulating linear arrays.
Vocabulary
  • data structure;;
  • static data structure;
  • index;
  • linear array;
  • element.
Resources
  • Kjell chapter 45 onwards
  • Eck chapter 7
Related topics

Starter

A String array called names:

Matt Antonia Sasha David Fiona Tania Sue - - -

An int array called testScores:

72 46 58 32 89 83 66 - - -

Tasks
  1. Follow through this presentation on Arrays.
  2. The package ClassRegister01 declares 4 arrays, each 10 elements long: a String [] array called names, two int [] arays called ages and marks and a boolean [] array called pass. It uses a for loop to display the register. Examine its code. Note how it displays the empty records at the end of the arrays (obviously this class does not have its full complement of 10 students). Your end user has requested that you do not display any empty student records - fix this.
  3. ClassRegister02 declares the arrays in the other way and allows the teacher to enter their own data. Note the way the arrays are initialised to dummy values using a for loop. Note how the inputting of a sentinel value (-) causes the input to terminate early. The user does not have to enter whether the student passed or failed as the program calculates this for itself, but what if the pass mark is not 50%? Adapt the code to allow the user to enter a pass mark right at the beginning. What does this suggest about how necessary the pass array is?
  4. What happens when a student leaves? They won't necessarily be the one at the end of the register. Examine ClassRegister03 and describe in your own words what the method deleteStudent is doing.
  5. In ClassRegister04, there is a choice menu, since the number of methods is now increasing. What is the effect of the while(true) in line 44 and the \n in line 48? How are possible errors dealt with in entering the choices? Why are else if statements used instead of just if? Rewrite the if statements of the choice menu with a switch block.
  6. ClassRegister05 allows the teacher to append (add to the end) a newly-arrived student. How does the for loop spot the next available space? What is a possible danger and how does the program cope with it? Also note that an 'are you sure?' option is now available for the deletion method.
  7. ClassRegister06 can calculate the average mark for the students. Add a method that calculates the average age. You will need to offer a choice of mark ('m') or age ('a") to the choice menu.
  8. Extension work: Rewrite the two average methods into one. Once you know their choice, pass the name of the relevant array over to the method as a parameter. Return an int?.
  9. Extension work: Rewrite the project so that the pass array is no longer needed. Every time pass/fail information needs to be displayed, it gets calculated (a run-time calculation).
  10. ClassRegister07 takes the approach of handling input and output in the choice menu rather than in the methods, then passing data to/from methods using parameters and return types. Which approach do you prefer?
  11. Java Problem Set 2.