Arrays Topic 4.3.9 / Core
- declaring and manipulating linear arrays.
- data structure;;
- static data structure;
- linear array;
- Kjell chapter 45 onwards
- Eck chapter 7
String array called names:
int array called testScores:
- Follow through this presentation on Arrays.
ClassRegister01declares 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
forloop 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.
ClassRegister02declares 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
forloop. 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?
What happens when a student leaves? They won't necessarily be the one at the end of the register. Examine
ClassRegister03and describe in your own words what the method
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 ifstatements used instead of just
if? Rewrite the
ifstatements of the choice menu with a
ClassRegister05allows the teacher to append (add to the end) a newly-arrived student. How does the
forloop 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.
ClassRegister06can 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.
Extension work: Rewrite the two
averagemethods into one. Once you know their choice, pass the name of the relevant array over to the method as a parameter. Return an
Extension work: Rewrite the project so that the
passarray is no longer needed. Every time pass/fail information needs to be displayed, it gets calculated (a run-time calculation).
ClassRegister07takes 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?
- Java Problem Set 2.