Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to rewrite my current program to display the sudents in increasing order

ID: 3641451 • Letter: I

Question

I need to rewrite my current program to display the sudents in increasing order of the number of correct answers. Here is the code I have so far.


public class GradeExam {

/**
* @param args
*/
public static void main(String[] args) {
// Students' answers tot he questions
char[][] answers = {
{'A','B','A','C','C','D','E','E','A','D'},
{'D','B','A','B','C','A','E','E','A','D'},
{'E','D','D','A','C','B','E','E','A','D'},
{'C','B','A','E','D','C','E','E','A','D'},
{'A','B','D','C','C','D','E','E','A','D'},
{'B','B','E','C','C','D','E','E','A','D'},
{'B','B','A','C','C','D','E','E','A','D'},
{'E','B','E','C','C','D','E','E','A','D'}};
//Key to the questions
char[] keys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'};

//Grade all answers
for (int i = 0; i< answers.length; i++) {
//Grade one student
int correctCount = 0;
for (int j = 0; j < answers[i].length; j++) {
if (answers[i][j] == keys[j])
correctCount++;
}
System.out.println("Student " + i + "'s correct count is " + correctCount);
}

}

}


Explanation / Answer

If you don't want to sort an extra array, then delete everything that has "id" term in it, but you'll lost track of the student id

public class GradeExam {
  
    public static void main(String[] args) {
        // Students' answers tot he questions
        char[][] answers = {
            {'A','B','A','C','C','D','E','E','A','D'},
            {'D','B','A','B','C','A','E','E','A','D'},
            {'E','D','D','A','C','B','E','E','A','D'},
            {'C','B','A','E','D','C','E','E','A','D'},
            {'A','B','D','C','C','D','E','E','A','D'},
            {'B','B','E','C','C','D','E','E','A','D'},
            {'B','B','A','C','C','D','E','E','A','D'},
            {'E','B','E','C','C','D','E','E','A','D'} };
        //Key to the questions
        char[] keys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'};
        //Id of the students
        int[] id = new int[answers.length];
        //Grade of the students
        int[] grade = new int[answers.length];

        //Grade all answers
        for (int i = 0; i < answers.length; i++) {
            //Grade one student
            int correctCount = 0;
            for (int j = 0; j < answers[i].length; j++) {
                if (answers[i][j] == keys[j])
                correctCount++;
            }
            id[i] = i; //You can fix Student 0 by assign i + 1 to id[i]
            grade[i] = correctCount;
            System.out.println("Student " + id[i] + "'s correct count is "
                    + grade[i]);
        }
      
        sortGrades(id, grade);
      
        System.out.println();
        for (int i = 0; i < answers.length; i++) {
            System.out.println("Student " + id[i] + "'s correct count is "
                    + grade[i]);
        }
      
    }
  
    //Simple selection sort to sort 2 parallel arrays
    //sort largest to smallest
    //If you don't want to sort an extra array, then delete everything that has "id" term in it
    private static void sortGrades(int[] id, int [] grade) {
        int minIndex;
        int minGrade;
        int minId;
        for (int startScan = 0; startScan < grade.length - 1; startScan++)
        {
            minIndex = startScan;
            minGrade = grade[startScan];
            minId = id[startScan];

            for(int index = startScan + 1; index < grade.length; index++)
            {
                if (grade[index] < minGrade)
                {
                    minGrade = grade[index];
                    minId = id[index];
                    minIndex = index;
                }
            }
            grade[minIndex] = grade[startScan];
            grade[startScan] = minGrade;
            id[minIndex] = id[startScan];
            id[startScan] = minId;
        }
    }
  
}