Back to the Program-A-Day homepage
Problem
Given 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.
|
|
SideSectionTitle
Taken from http://www.flickr.com/photos/daniello/565304023/
An image or By Students section
|
Solution
The solution...
|
Code
Solution Code
public class RearrangingParallelArrays {
public static void main(String[] args) {
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};
boolean [] honourRoll;
printList(students,grades);
findMinMax(students,grades);
honourRoll = eligibleForHonours(students,grades);
printHonours(students,honourRoll);
}
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("");
}
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("");
}
public static boolean [] eligibleForHonours(String [] students, int [] grades) {
boolean [] honoursRoll = new boolean[students.length];
for(int i = 0; i < students.length; i++) {
if(grades[i] >= 80) {
honoursRoll[i] = true;
}
else {
honoursRoll[i] = false;
}
}
return honoursRoll;
}
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("");
}
}
|
Back to the Program-A-Day homepage