70% oooooo T-Mobile 9:12 PM The program will grade a series of exams and then pr
ID: 3813869 • Letter: 7
Question
70% oooooo T-Mobile 9:12 PM The program will grade a series of exams and then print a grade report for students in a course. input: An instructor has a class of students each of whom takes a multiple-choice exam with 10 questions. For each student in the class, there is one line in the input file. The line contains the answers that student gave for the exam. The input file named "grade data.txt" will have the following format: line 1: the key for the exam (e.g.) bccbbadbca. lines 2-n: bccbbbdbbb bccbbadbca bccbbadbbb a set of answers. You know you are done when you get to a line with no data. Note: You will not know in advance how many exams you have to grade and you DO NOT need to store the exam answers in your program. Processing: The program is to read the input file and grade each exam and print out the score for that exam. You will also keep track of how many students earned each score (0-10) and print a report after the grading. output: Here is an example of how the output might appear. You will write the report to an output file named "grade report.bt student 1 student 2 10 student 3 1 etc Final Report 10 4 high score 10 low score 1 mean score 6.25 NOTES: The program must be modular, with significant work done by functions. Each function should perform a single, well-defined task. When possible, create re- usable functions. Do not write trivial functions such as a function to read a single value from an input file. o e g. you should have a function that grades the exam and returns the score when passed the key and and one set of answers. o You should probably have functions that take an array and number of questions as parameters and find the high, low, and mean. Don't calculate the mean, high, and low as you grade the exams.Explanation / Answer
//Input File : grade_data.txt
bccbbadbca
bccbbbdbbb
bccbbadbca
bccbbadbbb
bcabbbdbab
bbabbabbaa
bcccbadbcc
cbbbbabdbc
bccccccccc
abaaaaaaaa
bccaaaddda
bbbcccaaaa
aaabbbccca
abcabcabca
aaaaacccbb
bccbbadbcb
abaaaaaaab
//Grades.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;
public class Grades {
//Attributes
String sName;
int marks;
//Getters and Setters
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public int getMarks() {
return marks;
}
//Calculating Marks of each Student
public void setMarks(String key,String answers) {
int i=0,count=0;
while(key.length()>i){
//Comparing each character
if(key.charAt(i)==answers.charAt(i)){
count++;
}
i++;
}
this.marks = count;
}
//Calculating Low High and Mean of Scores
//Method takes Array of scores and total students
public String getLowMeanHigh(Object marks[],int total){
Integer high = 0;
Integer low = (Integer) marks[0];
Integer sum=0;
//Looping marks
for(Object o:marks){
//Casting Object into Integer
Integer i = (Integer)o;
sum=sum+i;
if(high<i)
high=i; // High value
if(low>i)
low=i; // Low Value
}
//Calculating Mean Value
Double mean = (double)sum/(double) total;
return high+" "+low+" "+mean;
}
public static void main(String[] args) {
try{
//InputStream
FileReader fr = new FileReader("D:\grade_data.txt");
BufferedReader br = new BufferedReader(fr);
//List to store Marks
ArrayList<Integer> al = new ArrayList<Integer>();
//OutputStream
FileWriter fw = new FileWriter("D:\grade_report.txt");
BufferedWriter bw = new BufferedWriter(fw);
//Sorted Map Contains Key as Marks and Value as Count of that Marks
TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
String line;
int i=0;
String key="";
Grades s;
//Reading each student marks
while((line=br.readLine())!=null){
if(i==0){ // First line is exam key
key=key+line;
}else{
s = new Grades();
s.setsName("Student "+i);
//Calling setMarks by passing key and students marks
s.setMarks(key, line);
bw.write(s.getsName()+"-"+s.getMarks());
//Adding marks to List
al.add(s.getMarks());
bw.newLine();
//Checking whether map contains Key of marks
if(map.containsKey(s.getMarks())){
//Storing Marks, Count of Marks
map.put(s.getMarks(), map.get(s.getMarks())+1);
}else {
//Storing Marks and Count into Map
map.put(s.getMarks(), 1);
}
}
i++;
}
bw.newLine();
bw.write("Final Report");
bw.newLine();
bw.write("-------------");
bw.newLine();
//Getting all Keys into Set
Set<Integer> set = map.keySet();
Object[] marks = al.toArray();
Iterator<Integer> itr = set.iterator();
int total=0;
//Iterating Set of Marks
while(itr.hasNext()){
//Getting mark and Count of marks from Map
Integer mark = itr.next();
Integer count = map.get(mark);
bw.write(mark+"-"+count);
bw.newLine();
//Finding total count of students
total=total+count;
}
bw.newLine();
//Creating Grades object
s = new Grades();
//Calling method to Calculate High,Low and Mean
String data = s.getLowMeanHigh(marks,total);
String hlm[] = data.split(" ");
bw.write("High Score - "+hlm[0]);
bw.newLine();
bw.write("Low Score - "+hlm[1]);
bw.newLine();
bw.write("Mean Score - "+hlm[2]);
System.out.println("Results Report Generated...");
bw.close();fw.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
//grade_report.txt
Student 1-7
Student 2-10
Student 3-8
Student 4-6
Student 5-6
Student 6-8
Student 7-3
Student 8-4
Student 9-2
Student 10-6
Student 11-2
Student 12-4
Student 13-5
Student 14-0
Student 15-9
Student 16-1
Final Report
-------------
0-1
1-1
2-2
3-1
4-2
5-1
6-3
7-1
8-2
9-1
10-1
High Score - 10
Low Score - 0
Mean Score - 5.0625
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.