I'm trying to get my code to display the highest score, lowest score and average score of a class of students provided by inputs from a teacher.
I also want to sort those students' grades from highest to lowest.
I believe the best way to do this would be to use a single student class instead of two arrays (but I don't know how to do that).
This is what I have so far:
import java.util.ArrayList;
import java.util.Scanner;
public class FinalJamesVincent {
public static void main (String[] args) {
ArrayList<Integer> bestStudentPosition = new ArrayList<Integer>();
ArrayList<Integer> worstStudentPosition = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);
System.out.print("How many students are in your class? ");
int totalStudents = Integer.parseInt(input.nextLine());
String[] names = new String[totalStudents];
double[] scores = new double[totalStudents];
double maxGrade = 0;
double minGrade = 0;
double avg = 0;
double sum = 0;
for (int i = 0; i < totalStudents; i++) {
System.out.print("Name: ");
names[i] = input.next();
System.out.print("Score: ");
scores[i] = input.nextDouble();
sum += scores[i];
if (i == 0) {
minGrade = scores[0];
}
if (scores[i] > maxGrade) {
bestStudentPosition.clear();
maxGrade = scores[i];
bestStudentPosition.add(new Integer(i));
} else if (scores[i] == maxGrade) {
bestStudentPosition.add(new Integer(i));
}
if (i > 0 && scores[i] < minGrade) {
worstStudentPosition.clear();
minGrade = scores[i];
worstStudentPosition.add(new Integer(i));
} else if (scores[i] == minGrade) {
worstStudentPosition.add(new Integer(i));
}
}
avg = sum / totalStudents;
System.out.print("Highest score: ");
for (Integer position : bestStudentPosition) {
System.out.println(maxGrade + ", " + names[position]);
}
System.out.print("Lowest score: ");
for (Integer position : worstStudentPosition) {
System.out.println(minGrade + ", " + names[position]);
}
System.out.printf("Average: %3.2f", avg);
}
public static void arraySort (int [] list){
for (int i = 0; i < list.length - 1; i++)
for (int j = i + 1; j < list.length; j++)
if (list[i] > list[j]){
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
public static void printList (int [] list){
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
System.out.println();
}
}
The given input:
How many students are in your class? 3
Name: Emily
Score: 88.4
Name: Billy
Score: 59.4
Name: Jake
Score: 95.3
The output so far is:
Highest Score: 95.3, Jake
Lowest Score: 59.6, Billy
Average: 77.77
I want it to do this but also sort the list from the given input(so at the end it should look like this):
Highest Score: 95.3, Jake
Lowest Score: 59.6, Billy
Average: 77.77
1. Jake 95.3
2. Emily 88.4
3. Billy 59.6