Guest

Rohit's Program Code

By: a guest on Jan 28th, 2012  |  syntax: Java  |  size: 3.70 KB  |  hits: 40  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. /**
  2.  * Name: Rohit Kothur
  3.  * Assignment: Group Project
  4.  * School: Phoenix Country Day School
  5.  * Date: 1/31/2012
  6.  */
  7.  
  8. import java.io.*;
  9. import java.util.*;
  10.  
  11. public class Group_Project {
  12.  
  13.           // file input
  14.         private static FileInputStream inFile;
  15.         private static InputStreamReader inReader;
  16.         private static  BufferedReader reader;
  17.         private static StringTokenizer strTkn;
  18.  
  19.     public static void main(String[] args) throws IOException {
  20.                 String[] studentID = new String[127];
  21.                 String[] answers = new String[127];
  22.                 int[] numGrade = new int[127];
  23.                 char[] letGrade = new char[127];
  24.  
  25.         initFile();
  26.         String answerKey = readAnswerKey();
  27.         readStudents(studentID, answers);
  28.         getGrades(answerKey, answers, numGrade, letGrade);
  29.  
  30.         printStudent(studentID, numGrade, letGrade);
  31.         printClass(numGrade, letGrade);
  32.  
  33.         inFile.close();
  34.     }
  35.  
  36.     public static void initFile() throws IOException
  37.         {
  38.             inFile = new FileInputStream ("C:\\Users\\Reivei\\Desktop\\Computer Classes\\!!VHSAPCSData\\tfquizdata.txt"); // notice the double slash marks
  39.             inReader = new InputStreamReader(inFile);
  40.             reader = new BufferedReader(inReader);
  41.         }
  42.  
  43.         public static String readAnswerKey() throws IOException
  44.         {
  45.                 String answerKey = reader.readLine();
  46.                 System.out.println(answerKey);
  47.                 return answerKey;
  48.     }
  49.  
  50.     public static void readStudents(String[] studentID, String[] answers) throws IOException
  51.     {
  52.         String line;
  53.         for(int i = 0; i < studentID.length; i++)
  54.         {
  55.                 line = reader.readLine();
  56.                 strTkn = new StringTokenizer(line);
  57.                 studentID[i] = strTkn.nextToken();
  58.                 answers[i] = strTkn.nextToken();
  59.         }
  60.     }
  61.  
  62.     public static void getGrades(String answerKey, String[] answers, int[] numGrade, char[] letGrade)
  63.     {
  64.                 for(int i = 0; i < answers.length; i++)
  65.                 {
  66.                         int grade = 0;
  67.                         for(int j = 0; j < answers[i].length(); j++)
  68.                         {
  69.                                 if(answers[i].charAt(j) == answerKey.charAt(j))
  70.                                 {
  71.                                         grade ++;
  72.                                 }
  73.                         }
  74.  
  75.                         numGrade[i] = grade;
  76.                 }
  77.  
  78.                 for(int i = 0; i < letGrade.length; i++)
  79.                 {
  80.                         if(numGrade[i] == 10)
  81.                         {
  82.                                 letGrade[i] = 'A';
  83.                         }
  84.  
  85.                         else if(numGrade[i] == 9)
  86.                         {
  87.                                 letGrade[i] = 'B';
  88.                         }
  89.  
  90.                         else if(numGrade[i] == 8 || numGrade[i] == 7)
  91.                         {
  92.                                 letGrade[i] = 'C';
  93.                         }
  94.  
  95.                         else if(numGrade[i] == 6 || numGrade[i] == 5)
  96.                         {
  97.                                 letGrade[i] = 'D';
  98.                         }
  99.  
  100.                         else
  101.                         {
  102.                                 letGrade[i] = 'F';
  103.                         }
  104.                 }
  105.     }
  106.  
  107.         public static void printStudent(String[] studentID, int[] numGrade, char[] letGrade)
  108.         {
  109.                 for(int i = 0; i < studentID.length; i++)
  110.         {
  111.                 System.out.println("Student " + studentID[i] + " got " + numGrade[i] + " out of 10 questions right and received a(n) " + letGrade[i] + ".");
  112.         }
  113.         }
  114.  
  115.         public static void printClass(int[] numGrade, char[] letGrade)
  116.         {
  117.                 System.out.println(numGrade.length + " students took this quiz.");
  118.  
  119.                 int average = 0;
  120.                 for(int grade : numGrade)
  121.                 {
  122.                         average += grade;
  123.                 }
  124.                 average /= numGrade.length;
  125.  
  126.                 System.out.println("The class average was " + average + " out of ten.");
  127.  
  128.                 int aCount = 0, bCount = 0, cCount = 0, dCount = 0, fCount = 0;
  129.  
  130.                 for(char grade : letGrade)
  131.                 {
  132.                         if(grade == 'A')
  133.                         {
  134.                                 aCount++;
  135.                         }
  136.                         if(grade == 'B')
  137.                         {
  138.                                 bCount++;
  139.                         }
  140.                         if(grade == 'C')
  141.                         {
  142.                                 cCount++;
  143.                         }
  144.                         if(grade == 'D')
  145.                         {
  146.                                 dCount++;
  147.                         }
  148.                         if(grade == 'F')
  149.                         {
  150.                                 fCount++;
  151.                         }
  152.                 }
  153.  
  154.                 System.out.println(aCount + " students received A's.");
  155.                 System.out.println(bCount + " students received B's.");
  156.                 System.out.println(cCount + " students received C's.");
  157.                 System.out.println(dCount + " students received D's.");
  158.                 System.out.println(fCount + " students received F's.");
  159.         }
  160. }