Exam Grading For this question, you will use multi-dimensional arrays to write a
ID: 3862352 • Letter: E
Question
Exam Grading For this question, you will use multi-dimensional arrays to write a program that grades a multiple choice exam. Your code for this question should go in a file Exam Grading. Java We strongly recommend that you complete the first three warm-up questions before starting this problem. Write a method grade AH Students that takes as input a two dimensional char array containing student responses to a multiple choice exam, and a one dimensional char array containing the solutions to each question. Each element in the 2d array is an array representing one student's responses to the entire exam. This method returns a double [] representing the grades for each student as percentages. Assume that all questions are worth 1 point. You may also assume that all char values are upper-case letters, and valid exam responses. If, at any point, the number of responses for a student does not match the number of questions on the exam (the number of solutions), your method should throw an Illegal Argument Exception that contains a message letting the user know which student (by index) caused the error, as well as what the two conflicting lengths were. Below is an example. Suppose we have a multiple choice exam with 6 questions and written by 4 students: char[][] responses = {{'C', 'A', 'B', 'B', 'C', 'A'}, {'A' 'A', 'B', 'B', 'B' 'B'}, {'C', 'B', 'A', 'B', 'C', 'A'}, {'A', 'B' 'A', 'B', 'B' 'B'}}; char[] solutions = {'C', 'A', 'B', 'B', 'C', 'C'}; The output of grade AH Students would look like: [83.33333333333334, 50.0, 50.0, 16.666666666666664]Explanation / Answer
Exam.java
import java.util.Arrays;
public class Exam {
public static void main(String[] args) {
//Creating an 2-Dimensional char array
char[][] responses ={{'C','A','B','B','C','A'},
{'A','A','B','B','B','B'},
{'C','B','A','B','C','A'},
{'A','B','A','B','B','B'}};
//Creating one dimensional char array
char[] solutions={'C','A','B','B','C','C'};
//calling the method by passing the two arrays as arguments
double[] grades=gradeAllStudents(responses,solutions);
//Displaying the result
System.out.println(Arrays.toString(grades));
}
//This method is used to calculate the score of each student in the each
private static double[] gradeAllStudents(char[][] responses,
char[] solutions) {
int count;
double grades[]=new double[responses.length];
for(int i=0;i<responses.length;i++)
{
count=0;
for(int j=0;j<responses[0].length;j++)
{
if(responses[i][j]==solutions[j])
{
count++;
}
}
grades[i]=(double)(count*100)/responses[0].length;
}
return grades;
}
}
______________________
output:
[83.33333333333333, 50.0, 50.0, 16.666666666666668]
_____________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.