Student Record List With Parallel Arrays
Back to the Program-A-Day homepage
ProblemGiven two parallel arrays, where the first array stores the a list of student's names and the second array stores the list of grades, print out the name of the student and their corresponding grade. Find the student with the highest grade and the student with the lowest grade and print the results. Use a honoursRoll[] boolean array to store true if the corresponding student is eligible for honours (>=80) and print the results. |
Problem Solving | |
---|---|---|
SolutionIn this example, you are given two parallel arrays with 10 elements in each array, String [] students = {"Joey Chase","Alan Gibbs","Ellen Kauffman","Thomas Lembeck", "Alexa Bright","Seth Benson","Terry Logan","Keven Gail","Shelley Fryman","Jill Beckam"}; int [] grades = {90,63,77,85,91,54,82,55,53,89}; The first array stores a list of student names and the second array stores a list of the corresponding student's grades.
public static void printList(String [] students, int [] grades) { : } Start writing your print list method with a for loop and use the array's length function (ex: student.length) to get the size of an array. Arrays can be fixed sizes but in many cases the size may not be given, the length function is a useful tool that will cover a broader range of unknown possibilities.
for(int i = 0; i < students.length; i++) { : }
public static void printList(String [] students, int [] grades) { System.out.println("====Student List===="); System.out.println(""); for(int i = 0; i < students.length; i++) { System.out.println(students[i] + "\t" + grades[i]); } System.out.println(""); System.out.println(""); }
int min = 0; int max = 0; We initialize it to zero so we have a default starting position to start the comparison. Next, create a for loop (similar to the previous for loop), where the loop will be used to go through each element in the array. During each iteration, we will compare the value of the iterated index position with the index postion stored in the min and max variable. In this example, the values that we will focus our comparisons on are located in the grades[] array. If a new smaller value is found, replace the index position of min with the new index position. Just how it sounds, your if statement should look like this, if(grades[i] < grades[min]) { min = i; }
if(grades[i] > grades[max]) { max = i; }
public static void findMinMax(String [] students, int [] grades) { int min = 0; int max = 0; for(int i = 0; i < grades.length; i++) { if(grades[i] > grades[max]) { max = i; } if(grades[i] < grades[min]) { min = i; } } System.out.println("====Student with Highest Grade===="); System.out.println(""); System.out.println(students[max] + "\t" + grades[max]); System.out.println(""); System.out.println(""); System.out.println("====Student with Lowest Grade===="); System.out.println(""); System.out.println(students[min] + "\t" + grades[min]); System.out.println(""); System.out.println(""); } Keep in mind that when your program exits this method, your min and max variables will disappear along with the method (unless if you return the values or store them somewhere else).
In this example we want to store the boolean results for future use, so when you create the new method there will be a return value of a boolean[] array. public static boolean [] eligibleForHonours(String [] students, int [] grades) { : }
In this example, we will be calling this method from the main method, so you will need a variable in the main method that will store a boolean array type variable. boolean [] honourRoll; : honourRoll = eligibleForHonours(students,grades);
boolean [] honoursRoll = new boolean[grades.length];
for(int i = 0; i < grades.length; i++) { if(grades[i] >= 80) { honoursRoll[i] = true; } else { honoursRoll[i] = false; } }
public static boolean [] eligibleForHonours(String [] students, int [] grades) { boolean [] honoursRoll = new boolean[grades.length]; for(int i = 0; i < grades.length; i++) { if(grades[i] >= 80) { honoursRoll[i] = true; } else { honoursRoll[i] = false; } } return honoursRoll; }
Create a new method and pass in the appropriate parameters. In this case, we will only need students[] and honoursRoll[] arrays. public static void printHonours(String [] students, boolean [] honoursRoll) { : }
for(int i = 0; i < honoursRoll.length; i++) { if(honoursRoll[i]) { System.out.println(students[i]); } }
public static void printHonours(String [] students, boolean [] honoursRoll) { System.out.println("====Honours Roll List===="); System.out.println(""); for(int i = 0; i < honoursRoll.length; i++) { if(honoursRoll[i]) { System.out.println(students[i]); } } System.out.println(""); System.out.println(""); }
printList(students,grades); findMinMax(students,grades); honourRoll = eligibleForHonours(students,grades); printHonours(students,honourRoll);
| ||
Code |