0

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
JamesV.
  • 55
  • 6
  • Create a `Student Object Class` and have setters and getters for things like `score` and `name`. Take a look at [this tutorial](http://www.tutorialspoint.com/java/java_encapsulation.htm) and also [this stackoverflow question](http://stackoverflow.com/questions/2036970/how-do-getters-and-setters-work). – robotlos May 06 '16 at 00:29
  • how would this help me? – JamesV. May 06 '16 at 00:32
  • The way you're doing things at this point is not object oriented. You have different data structures working together to give you the information for a particular student, instead of having a `Student` object with properties that you can set. This will help you turn your program into an object oriented one, which is the proper solution to your question `"How can I convert this code from using two arrays to using a single class named student?"` Take a look at [this Java tutorial](https://docs.oracle.com/javase/tutorial/java/concepts/) for further detail. No one will convert your code for you. – robotlos May 06 '16 at 00:36
  • As far as sorting goes, take a look at [Java Comparator](https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html). – robotlos May 06 '16 at 00:41

0 Answers0