fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. public class Main {
  9. public static void main(String[] args) {
  10. char[][] answers = getStudentsAnswers();
  11. int[][] result = gradeStudentsAnswers(answers);
  12. announceResult(result);
  13. }
  14.  
  15. /**
  16.   * get students' answers for 10 multiple choice questions
  17.   * @return student's answers for 10 MCQs
  18.   */
  19. private static char[][] getStudentsAnswers() {
  20. char[][] answers = {
  21. {'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
  22. {'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'},
  23. {'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'},
  24. {'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'},
  25. {'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
  26. {'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
  27. {'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
  28. {'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
  29. };
  30.  
  31. return answers;
  32. }
  33.  
  34. /**
  35.   * grade students on the basis of number of correct answers
  36.   * @param answers student's responses for MCQs
  37.   * @return a descending list based on number of correct responses
  38.   */
  39. private static int[][] gradeStudentsAnswers(char[][] answers) {
  40. char[] answerKeys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'};
  41. // final list: [student id, student correct responses]
  42. int[][] finalList = new int[answers.length][2];
  43. int i = 0;
  44. for (int studentNo = 0; studentNo < answers.length; ++studentNo) {
  45. int correctAnswers = 0;
  46. for (int answerNo = 0; answerNo < answers[studentNo].length;
  47. ++answerNo) {
  48. if (answers[studentNo][answerNo] == answerKeys[answerNo]) {
  49. ++correctAnswers;
  50. }
  51. }
  52. finalList[i][0] = studentNo;
  53. finalList[i][1] = correctAnswers;
  54. ++i;
  55. }
  56. sortFinalList(finalList);
  57.  
  58. return finalList;
  59. }
  60.  
  61. /**
  62.   * sorts the student final list on the basis of number of correct responses
  63.   * @param finalList list generated after grading
  64.   */
  65. private static void sortFinalList(int[][] finalList) {
  66. // selection sort on the basis of score
  67. for (int row = 0; row < finalList.length - 1; ++row) {
  68. int maximumIndex = row;
  69. for (int col = row + 1; col < finalList.length; ++col) {
  70. if (finalList[col][1] > finalList[row][1]) {
  71. maximumIndex = col;
  72. }
  73. }
  74. if (maximumIndex != row) {
  75. int[] temp = finalList[row];
  76. finalList[row] = finalList[maximumIndex];
  77. finalList[maximumIndex] = temp;
  78. }
  79. }
  80. }
  81.  
  82. private static void announceResult(int[][] result) {
  83. for (int i = 0; i < result.length; ++i) {
  84. System.out.printf("Rank #%d: Student %d, Correct Responses: %d / 10\n",
  85. i + 1, result[i][0], result[i][1]);
  86. }
  87. }
  88. }
Success #stdin #stdout 0.12s 34228KB
stdin
Standard input is empty
stdout
Rank #1: Student 4, Correct Responses: 8 / 10
Rank #2: Student 7, Correct Responses: 7 / 10
Rank #3: Student 1, Correct Responses: 6 / 10
Rank #4: Student 2, Correct Responses: 5 / 10
Rank #5: Student 0, Correct Responses: 7 / 10
Rank #6: Student 5, Correct Responses: 7 / 10
Rank #7: Student 6, Correct Responses: 7 / 10
Rank #8: Student 3, Correct Responses: 4 / 10