All pastes #2107923 Raw Edit

Anonymous

public text v1 · immutable
#2107923 ·published 2012-01-30 23:06 UTC
rendered paste body
package testprog;

import java.io.*; //file input
import java.util.StringTokenizer; //reads file

public class TestProg {
    private static FileInputStream inFile;
    private static InputStreamReader inReader;
    private static BufferedReader reader;
    
    static void findFile() throws IOException
        {
        inFile = new FileInputStream ("Z:\\Downloads\\data.txt");
        inReader = new InputStreamReader(inFile);
        reader = new BufferedReader(inReader);
        }
    
    static int getArrayLength() throws IOException
    {
        int arraylength = 0;
        String line = reader.readLine();
        while (line != null){
            arraylength++;
            line = reader.readLine();
        }
        return arraylength;
    }
    
    static void getData(int arraylength) throws IOException
        {
        StringTokenizer strTkn;
        String line = reader.readLine();
        boolean firstLine = true;
        
        //sets up arrays
        char[] keyArr = new char[10];
        String[] stuAnswersArr = new String[arraylength];
        String[] idArr = new String[arraylength];
        //used for adding student info to array
        int loopCount = 0;
        
        //reads each line and adds data to array
        while (line != null)
            {
            strTkn = new StringTokenizer(line);    
            if (firstLine == true)
                {
                    String key = strTkn.nextToken();
                    firstLineData(key, keyArr);
                    firstLine = false;
                }
            else if (firstLine == false)
                {
                String studentId = strTkn.nextToken();
                String answers = strTkn.nextToken();
                
                //adds student id to array
                idArr[loopCount] = studentId;
                // adds answers to array
                stuAnswersArr[loopCount] = answers;
                loopCount += 1;
                }
            line = reader.readLine();
            }
        
        //ouput
        int[] numericGrades = numGrade(stuAnswersArr, keyArr, arraylength);
        //printArr(idArr, numericGrades, letGrade(numericGrades, arraylength) );
        //System.out.println("The Class Average is " + avgNumGrade(numericGrades));
        //printHistogram((letGradeHist(letGrade(numericGrades,arraylength))));
        //print out how many quizes were taken
        System.out.println("There were " +arraylength + " quizes taken.");
        
               
        }
    
    static char[] firstLineData(String line, char[] keyArray)
    {
        for(int i=0; i < 10; i++)
        {
            keyArray[i] = line.charAt(i);
        }
        return keyArray;
    }
    static int[] numGrade(String[] stuAnswersArr, char[] keyArr, int arraylength) {
    
        int[] numericGrades = new int[arraylength];
        int grade=0;
       
        for (int i=0; i<arraylength; i++) 
        {
            for (int j=0; j<10; j++)
            {

                if (keyArr[j] == stuAnswersArr[i].charAt(j))
                    {
                    //The above line is not working for some reason. I did what you said in the comment.
                    //the problem was that you needed to use () and not []
                    grade += 10;
                    }     

            }
            //assigns grade to student and resets grade variable
            numericGrades[i]=grade;
            grade = 0;
        }
        return numericGrades;
    }
    
    static String[] letGrade(int[] numericGrades, int arraylength)
    {
        String[] letterGrades = new String[arraylength];
        //loop that converts numeric grade to a letter grade
        for (int i=0; i<arraylength; i++)
        {
            if (numericGrades[i]>=90){letterGrades[i]="A";}
            else if (numericGrades[i]>=80){letterGrades[i]="B";}
            else if (numericGrades[i]>=70){letterGrades[i]="C";}
            else if (numericGrades[i]>=60){letterGrades[i]="D";}      
            else if (numericGrades[i]<=50){letterGrades[i]="F";} 
        }
        return letterGrades;
    }
    
    static int avgNumGrade(int[] numericGrades)
    {
        int sum = 0;
        //loop that adds all of the numeric grades to the sum 
        for (int i = 0; i < numericGrades.length; i++){
		sum += numericGrades[i];
	}
        int avg = sum/(numericGrades.length);
        return avg;
    }
    
    static int[] letGradeHist(String[] letGrades)
    {
        //freq[0] = # of A's
        //freq[1] = # of B's
        //freq[2] = # of C's
        //freq[3] = # of D's
        //freq[4] = # of F's
        int[] freq = new int[5]; 
        //loop to count letter grades and store values in array freq
	for (int i = 0; i < letGrades.length; i++) {
            //to compare strings, use the .equals function, not the == 
		if (letGrades[i].equals("A")){freq[0]++;}
		else if (letGrades[i].equals("B")){freq[1]++;} 
		else if (letGrades[i].equals("C")){freq[2]++;}
		else if (letGrades[i].equals("D")){freq[3]++;}			
		else if (letGrades[i].equals("F")){freq[4]++;}
			
	}
        return freq;
    }
    
    static void printArr(String[] id, int[] numGrade, String[] letGrade){
        //prints out data
        for (int i=0; i < id.length; i++){
                System.out.println(id[i] + " " + numGrade[i] + " " + letGrade[i]);
        }
    }
    
    static void printHistogram(int[] freq)
    {
        //prints frequency of grades
        for (int i = 0; i < 5; i++) {
		if (i == 0) {System.out.println("No. of A's: " + freq[i] );}
		else if (i == 1){System.out.println("No. of B's: " + freq[i] );}			
		else if (i == 2){System.out.println("No. of C's: " + freq[i] );}			
		else if (i == 3){System.out.println("No. of D's: " + freq[i] );}			
		else if (i == 4){System.out.println("No. of F's: " + freq[i] );}
			
	}
    }

    public static void main(String[] args) throws Exception 
    {
        //reads file & finds out how many people took quiz
        //starts from 0 because first line is answer key & doesn't count
        findFile();
        int arrLength = getArrayLength();
        
        //computes rest of program
        findFile();
        getData(arrLength);
    }
}