The question below involves 2d boolean arrays. Design and implement an applicati
ID: 3811355 • Letter: T
Question
The question below involves 2d boolean arrays.
Design and implement an application that reads in a set of 2000 grades into a two-dimensional array from a .txt file called studentanswers that represent the results from a test of 20 true-false questions for 100 students. Each row of the array represents the answers of one of the 100 students in the class. Each column represents the answers of each student to a specific question on the test. Each cell of the array contains either the Boolean value “true” or “false.” Read in the values from a second .txt file called questionanswers to be stored in a second one-dimensional array of 20 Boolean values that represent the correct answers to the 20 questions. Your program should compute and print out the number of correct answers for each student in chart form, the number of students who got each of the 20 questions correct in chart form, the average quiz grade and the standard deviation of the grades.
questionAnswers.txt:
true
true
true
true
true
true
true
true
true
true
false
true
false
true
false
true
false
true
false
true
(second file contains 2000 lines. I will only use 40).
studentAnswers.txt:
true
true
true
true
true
true
true
true
true
true
false
true
false
true
false
true
false
true
false
true
true
true
true
true
true
true
true
true
true
true
false
true
false
true
false
true
false
true
false
true
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class GradesFromFile {
public static void main(String[] args) {
// Student Data
boolean[][] studentAnswers = new boolean[100][20];
String aFileName = "Provide Input file here and the extension should be .txt";
// Answers data
boolean answers[] = new boolean[20];
String answersFile = "Answer file here and the extension should be .txt";
// Result
int[] allCorrect = new int[100]; // stores indexes of students who
// scored all correct
int[] score = new int[100]; // scores of students
try {
BufferedReader br = new BufferedReader(new FileReader(aFileName));
int row = 0;
String temp = "";
while ((temp = br.readLine()) != null) {
if (row >= 100) {
break; // break loop if file contains more than 100 student
// entires
}
String[] tokens = temp.split(" "); // Assumming each line in txt
// file is one student and
// results are separated by
// space for each question.
for (int i = 0; (i < tokens.length && i < 20); i++) {
studentAnswers[row][i] = Boolean.valueOf(tokens[i]);
}
row++;
}
} catch (IOException e) {
System.out.println("ERROR : " + e);
}
// Reading answers from file
try {
BufferedReader br = new BufferedReader(new FileReader(answersFile));
int index = 0;
String temp = "";
while ((temp = br.readLine()) != null) {
answers[index] = Boolean.valueOf(temp); // Assuming each line in
// answers.txt file is
// answer for each
// question
index++;
}
} catch (IOException e) {
System.out.println("ERROR: " + e);
}
// Prints correct answers of each student
for (int i = 0; i < 100; i++) {
System.out.print("Student " + (i + 1) + " -> ");
int noOfCorrect = 0;
for (int j = 0; j < 20; j++) {
if (studentAnswers[i][j] == answers[j]) {
System.out.print(j + " ");
noOfCorrect++;
} else {
System.out.print("-" + " ");
}
}
if (noOfCorrect == 20) {
allCorrect[i] = i;
}
score[i] = noOfCorrect * 5;
System.out.println(" No of correct answers : " + noOfCorrect);
System.out.println("Grade Score : " + getGrade(score[i]));
}
// Average Grade Score and Standard Deviation
HashMap<String, List<Integer>> map = new HashMap<>();
map.put("A", new ArrayList<Integer>());
map.put("B", new ArrayList<Integer>());
map.put("C", new ArrayList<Integer>());
map.put("D", new ArrayList<Integer>());
map.put("F", new ArrayList<Integer>());
for (int studentScore : score) {
String grade = getGrade(studentScore);
switch (grade) {
case "A":
map.get("A").add(studentScore);
break;
case "B":
map.get("B").add(studentScore);
break;
case "C":
map.get("C").add(studentScore);
break;
case "D":
map.get("D").add(studentScore);
break;
case "F":
map.get("F").add(studentScore);
break;
}
}
Set<String> keys = new TreeSet(map.keySet());
for (String key : keys) {
System.out.println("Standard deviation " + key + " : " + printSD(map.get(key)));
}
}
/*
* To calculate the standard deviation of those numbers: 1. Work out the
* Mean (the simple average of the numbers) 2. Then for each number:
* subtract the Mean and square the result. 3. Then work out the mean of
* those squared differences. 4. Take the square root of that and we are
* done!
*/
public static double printSD(List<Integer> list) {
double sum = 0;
if (list.size() == 0)
return 0.0;
for (int val : list)
sum += val;
double mean = sum / list.size();
for (int i = 0; i < list.size(); i++) {
sum += Math.pow((list.get(i) - mean), 2);
}
if (sum == 0)
return 0.0;
return Math.sqrt(sum / list.size());
}
public static String getGrade(int score) {
String grade = "";
if (score >= 90)
grade = "A";
else if (score >= 80)
grade = "B";
else if (score >= 70)
grade = "C";
else if (score >= 60)
grade = "D";
else
grade = "F";
return grade;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.