/**
* Name: Rohit Kothur
* Assignment: Group Project
* School: Phoenix Country Day School
* Date: 1/31/2012
*/
import java.io.*;
import java.util.*;
public class Group_Project {
// file input
private static FileInputStream inFile;
private static InputStreamReader inReader;
private static BufferedReader reader;
private static StringTokenizer strTkn;
public static void main(String[] args) throws IOException {
String[] studentID = new String[127];
String[] answers = new String[127];
int[] numGrade = new int[127];
char[] letGrade = new char[127];
initFile();
String answerKey = readAnswerKey();
readStudents(studentID, answers);
getGrades(answerKey, answers, numGrade, letGrade);
printStudent(studentID, numGrade, letGrade);
printClass(numGrade, letGrade);
inFile.close();
}
public static void initFile() throws IOException
{
inFile = new FileInputStream ("C:\\Users\\Reivei\\Desktop\\Computer Classes\\!!VHSAPCSData\\tfquizdata.txt"); // notice the double slash marks
inReader = new InputStreamReader(inFile);
reader = new BufferedReader(inReader);
}
public static String readAnswerKey() throws IOException
{
String answerKey = reader.readLine();
System.out.println(answerKey);
return answerKey;
}
public static void readStudents(String[] studentID, String[] answers) throws IOException
{
String line;
for(int i = 0; i < studentID.length; i++)
{
line = reader.readLine();
strTkn = new StringTokenizer(line);
studentID[i] = strTkn.nextToken();
answers[i] = strTkn.nextToken();
}
}
public static void getGrades(String answerKey, String[] answers, int[] numGrade, char[] letGrade)
{
for(int i = 0; i < answers.length; i++)
{
int grade = 0;
for(int j = 0; j < answers[i].length(); j++)
{
if(answers[i].charAt(j) == answerKey.charAt(j))
{
grade ++;
}
}
numGrade[i] = grade;
}
for(int i = 0; i < letGrade.length; i++)
{
if(numGrade[i] == 10)
{
letGrade[i] = 'A';
}
else if(numGrade[i] == 9)
{
letGrade[i] = 'B';
}
else if(numGrade[i] == 8 || numGrade[i] == 7)
{
letGrade[i] = 'C';
}
else if(numGrade[i] == 6 || numGrade[i] == 5)
{
letGrade[i] = 'D';
}
else
{
letGrade[i] = 'F';
}
}
}
public static void printStudent(String[] studentID, int[] numGrade, char[] letGrade)
{
for(int i = 0; i < studentID.length; i++)
{
System.out.println("Student " + studentID[i] + " got " + numGrade[i] + " out of 10 questions right and received a(n) " + letGrade[i] + ".");
}
}
public static void printClass(int[] numGrade, char[] letGrade)
{
System.out.println(numGrade.length + " students took this quiz.");
int average = 0;
for(int grade : numGrade)
{
average += grade;
}
average /= numGrade.length;
System.out.println("The class average was " + average + " out of ten.");
int aCount = 0, bCount = 0, cCount = 0, dCount = 0, fCount = 0;
for(char grade : letGrade)
{
if(grade == 'A')
{
aCount++;
}
if(grade == 'B')
{
bCount++;
}
if(grade == 'C')
{
cCount++;
}
if(grade == 'D')
{
dCount++;
}
if(grade == 'F')
{
fCount++;
}
}
System.out.println(aCount + " students received A's.");
System.out.println(bCount + " students received B's.");
System.out.println(cCount + " students received C's.");
System.out.println(dCount + " students received D's.");
System.out.println(fCount + " students received F's.");
}
}