Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=============================================================================
- // Program : Quiz Scores
- // Author : Daniel Luce
- // Date : 10/02/2015
- // Abstract: Extract student information from database. Sort scores, average
- // said scores, and then display the information in descending order.
- //=============================================================================
- import java.io.*;
- import java.util.StringTokenizer;
- public class LuceP02quizScores
- {
- public static void main(String args[])throws IOException
- {
- FileReader diskld = new FileReader(new File("p2-quiz-scores.txt"));
- BufferedReader fileld = new BufferedReader(diskld);
- int numEntries = 0;
- StringTokenizer tkn;
- String rec;
- String namesTemp[] = new String[500];
- float gradesTemp[][] = new float[500][17];
- int scores[] = new int [12];
- int i = 0;
- int j;
- int k;
- while((rec=fileld.readLine()) !=null)
- {
- j = 0;
- k = 0;
- tkn = new StringTokenizer(rec,";,");
- namesTemp[i] = tkn.nextToken();
- while (tkn.hasMoreElements())
- {
- gradesTemp[i][j] = Integer.parseInt(tkn.nextToken());
- j++;
- }
- sortScores(gradesTemp[i]);
- averageTwelve(gradesTemp[i]);
- i++;
- numEntries++;
- }
- String names[] = new String[numEntries];
- float grades[][] = new float[numEntries][17];
- for(int n = 0; n < numEntries; n++)
- { //copy elements into accurately sized arrays
- names[n] = namesTemp[n];
- grades[n] = gradesTemp[n];
- }
- sortStudents(names, grades);
- countsGrades(grades,scores);
- displayInformation(names,grades,scores);
- }
- //--------------------------------------------------------
- public static void sortScores(float[] grades) //bubblesorts given array of ints
- {
- float temp; //holding variable
- boolean swapped = true; //checks for swaps, true to run at least once
- while (swapped)
- {
- swapped = false;
- for(int i=0; i < grades.length-1; i++)
- {
- if(grades[i] < grades[i+1])
- {
- temp = grades[i]; //swap elements
- grades[i] = grades[ i+1 ];
- grades[i+1] = temp;
- swapped = true; //shows a swap occurred
- }
- }
- }
- }
- //-----------------------------------------------------------------
- public static void sortStudents(String[] names, float[][] grades) //sorts students by average grades using bubble sort
- {
- float[] tempGrades; //holding variable for grade records
- String tempNames; //holding variable for name records
- int avgIndex = grades[0].length - 1; //index of average grades
- boolean swapped = true; //checks for swaps, true to run at least once
- while (swapped)
- {
- swapped = false;
- for(int j=0; j < grades.length-1; j++)
- {
- if(grades[j][avgIndex] < grades[j+1][avgIndex])
- {
- //prepare to swap
- tempGrades = grades[j];
- tempNames = names[j];
- //swap grades
- grades[j] = grades[j+1];
- grades[j+1] = tempGrades;
- //swap names
- names[j] = names[j+1];
- names[j+1] = tempNames;
- swapped = true; //shows a swap occurred
- }
- }
- }
- }
- //--------------------------------------------------------
- public static void averageTwelve(float[] grades)
- {
- float avg = 0;
- for(int i = 0; i < 12; i++)
- {
- avg += grades[i];
- }
- grades[grades.length - 1] = (float) (avg/12.0);
- }
- //--------------------------------------------------------
- public static void countsGrades(float[][] grades,int[] scores)
- {
- int i = 0;
- for(i = 0; i < grades.length; i++)
- {
- if(grades[i][16] >= 90 && grades[i][16] <=100)
- {
- scores[0] += 1;
- }
- else
- if(grades[i][16] >=80 && grades[i][16]<90)
- {
- scores[1] += 1;
- }
- else
- if(grades[i][16] >=70 && grades[i][16]<80)
- {
- scores[2] += 1;
- }
- else
- if(grades[i][16] >=60 && grades[i][16]<70)
- {
- scores[3] += 1;
- }
- else
- if(grades[i][16] >=0 && grades[i][16]<60)
- {
- scores[4] += 1;
- }
- }
- }
- //---------------------------------------------------------
- public static void displayInformation(String[] names, float[][] grades, int[] scores)
- {
- System.out.println();
- System.out.println("Project 2 Daniel Luce");
- System.out.println();
- System.out.println("Num Name Avg Best 12");
- System.out.println("--- ------------- -----------");
- for(int i = 0; i < names.length; i++) //loop through to print names, averages
- {
- System.out.printf("%2d. %-17s %.1f%n", i+1, names[i], grades[i][grades[i].length-1]);
- }
- System.out.println();
- System.out.println("A = " + scores[0]);
- System.out.println("B = " + scores[1]);
- System.out.println("C = " + scores[2]);
- System.out.println("D = " + scores[3]);
- System.out.println("F = " + scores[4]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement