The history teacher at your school needs help grading a True

The history teacher at your school needs help grading a True/False test. The students’ IDs and test answers are stored in a file. The first entry in the file contains the answers to the test in the following form: TFFTFFTTTTFFTFTFTFTT Every other entry in the file is the student\'s ID, followed by a blank, followed by the student\'s response. For example, the entry: ABC54301 TFTFTFTT TFTFTFFTTFT indicates that the student\'s ID is ABC54301 and the answer to question 1 is True, the answer to question 2 is False, and so on. This student did not answer question 9. The exam has 20 questions, and the class has more than 150 students. Each correct answer is awarded two points, each wrong answer gets -1 point, and no answer gets 0 points. Write a program that processes the test data. The output should be the student\'s ID, followed by the answers, followed by the test score, followed by the test grade. Assume the following grade scale: 90% 100%, A; 80% 89.99%, B; 70% 79.99%, C; 60% 69.99%, D; and 0% 59.99%, F.

Solution

Please follow the code and comments for description :

CODE :

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class StudentGrades {

public static void main(String[] args) throws Exception { // driver method that throws exception clause

ArrayList al = new ArrayList();
char correctRes[] = \"TFTTFTFFTTFFFTFTFTTF\".toCharArray();
File file = new File(\"input.txt\"); // read the file
Scanner sc = new Scanner(file); // get the sc object
System.out.println(\"-------------------------------------------------------------------------\");
System.out.println(\"Student ID\\t\\tAnswers\\t\\tTest Score\\tTest Grade\");
System.out.println(\"-------------------------------------------------------------------------\");
  
while (sc.hasNext()) { // iterate over the data in the file
String lineData = sc.nextLine(); // get the data
String sId = lineData.substring(0, lineData.indexOf(\" \")); // seperate over the spaces
String responses = lineData.substring(lineData.indexOf(\" \"), lineData.length()); // get the responses
char resArray[] = responses.trim().toCharArray(); // string to array
int pointEarned = 0; // local varaibles
if (Arrays.equals(resArray, correctRes)) { // check for the points earned
pointEarned = 40;
} else {
for (int i = 0; i < 20; i++) { // iterate over the loop of the counts
if (correctRes[i] == resArray[i]) {
pointEarned = pointEarned + 2; // get the updated count
} else if (resArray[i] == \' \') {
pointEarned = pointEarned + 0; // get the updated count
} else if (correctRes[i] != resArray[i]) {
pointEarned = pointEarned - 1; // get the updated count
}
}
}
double percentage = pointEarned * 2.5; // check for the percentage
String grade = null;
if (percentage >= 90 && percentage <= 100) { // get the grades calculated
grade = \"A\";
} else if (percentage >= 80 && percentage <= 89.99) {
grade = \"B\";
} else if (percentage >= 70 && percentage <= 79.99) {
grade = \"C\";
} else if (percentage >= 60 && percentage <= 69.99) {
grade = \"D\";
} else if (percentage >= 0 && percentage <= 59.99) {
grade = \"F\";
}
  
System.out.println(sId + \"\\t\" + responses + \"\\t\\t\" + pointEarned + \"\\t\\t\" + grade); // print the data
}
System.out.println(\"\"); // new line character
}
}

input.txt :

ABC54301 TFTTTFTTFTTTFTFFTTFT
ABC54302 TFTFT TTFTFTFTFTTTFT
ABC54303 TFTTFTFFTTFFFTFTFTTF
ABC54304 TFTFFFTTFT TFTTFTTTT
ABC54304 FFTFTFTFFTFFFTTTTTFT

OUTPUT :

-------------------------------------------------------------------------
Student ID       Answers       Test Score   Test Grade
-------------------------------------------------------------------------
ABC54301   TFTTTFTTFTTTFTFFTTFT       7       F
ABC54302   TFTFT TTFTFTFTFTTTFT       11       F
ABC54303   TFTTFTFFTTFFFTFTFTTF       40       A
ABC54304   TFTFFFTTFT TFTTFTTTT       8       F
ABC54304   FFTFTFTFFTFFFTTTTTFT       10       F

Hope this is helpful.

The history teacher at your school needs help grading a True/False test. The students’ IDs and test answers are stored in a file. The first entry in the file co
The history teacher at your school needs help grading a True/False test. The students’ IDs and test answers are stored in a file. The first entry in the file co

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site