Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.50 KB | None | 0 0
  1. //=============================================================================
  2. // Program : Quiz Scores
  3. // Author  : Daniel Luce
  4. // Date    : 10/02/2015
  5. // Abstract: Extract student information from database. Sort scores, average
  6. //           said scores, and then display the information in descending order.
  7. //=============================================================================
  8.  
  9. import java.io.*;
  10. import java.util.StringTokenizer;
  11. public class LuceP02quizScores
  12. {
  13.   public static void main(String args[])throws IOException
  14.   {
  15.     FileReader      diskld = new FileReader(new File("p2-quiz-scores.txt"));
  16.     BufferedReader  fileld = new BufferedReader(diskld);
  17.     int numEntries = 0;
  18.     StringTokenizer tkn;
  19.     String          rec;
  20.     String namesTemp[] = new String[500];
  21.     float gradesTemp[][] = new float[500][17];
  22.     int scores[] = new int [12];
  23.     int i = 0;
  24.     int j;
  25.     int k;
  26.  
  27.  
  28.     while((rec=fileld.readLine()) !=null)
  29.       {
  30.  
  31.         j   = 0;
  32.         k   = 0;
  33.  
  34.         tkn = new StringTokenizer(rec,";,");
  35.         namesTemp[i] = tkn.nextToken();
  36.  
  37.         while (tkn.hasMoreElements())
  38.           {
  39.             gradesTemp[i][j] = Integer.parseInt(tkn.nextToken());
  40.             j++;
  41.           }
  42.  
  43.         sortScores(gradesTemp[i]);
  44.         averageTwelve(gradesTemp[i]);
  45.  
  46.         i++;
  47.         numEntries++;
  48.  
  49.       }
  50.  
  51.     String      names[] = new String[numEntries];
  52.     float       grades[][] = new float[numEntries][17];
  53.  
  54.     for(int n = 0; n < numEntries; n++)
  55.       { //copy elements into accurately sized arrays
  56.         names[n] = namesTemp[n];
  57.         grades[n] = gradesTemp[n];
  58.       }
  59.  
  60.     sortStudents(names, grades);
  61.     countsGrades(grades,scores);
  62.     displayInformation(names,grades,scores);
  63.  
  64.   }
  65.   //--------------------------------------------------------
  66.   public static void sortScores(float[] grades) //bubblesorts given array of ints
  67.   {
  68.     float temp;                                 //holding variable
  69.     boolean swapped = true;                     //checks for swaps, true to run at least once
  70.     while (swapped)
  71.       {
  72.         swapped = false;
  73.         for(int i=0; i < grades.length-1; i++)
  74.           {
  75.             if(grades[i] < grades[i+1])
  76.               {
  77.                 temp = grades[i];              //swap elements
  78.                 grades[i] = grades[ i+1 ];
  79.                 grades[i+1] = temp;
  80.                 swapped = true;               //shows a swap occurred
  81.               }
  82.           }
  83.       }
  84.   }
  85.   //-----------------------------------------------------------------
  86.   public static void sortStudents(String[] names, float[][] grades) //sorts students by average grades using bubble sort
  87.   {
  88.     float[] tempGrades;                     //holding variable for grade records
  89.     String tempNames;                       //holding variable for name records
  90.     int avgIndex = grades[0].length - 1;    //index of average grades
  91.     boolean swapped = true;                 //checks for swaps, true to run at least once
  92.     while (swapped)
  93.       {
  94.         swapped = false;
  95.         for(int j=0; j < grades.length-1; j++)
  96.           {
  97.             if(grades[j][avgIndex] < grades[j+1][avgIndex])
  98.               {
  99.                 //prepare to swap
  100.                 tempGrades = grades[j];
  101.                 tempNames = names[j];
  102.  
  103.                 //swap grades
  104.                 grades[j] = grades[j+1];
  105.                 grades[j+1] = tempGrades;
  106.  
  107.                 //swap names
  108.                 names[j] = names[j+1];
  109.                 names[j+1] = tempNames;
  110.                 swapped = true; //shows a swap occurred
  111.               }
  112.           }
  113.       }
  114.   }
  115.   //--------------------------------------------------------
  116.   public static void averageTwelve(float[] grades)
  117.   {
  118.     float avg = 0;
  119.     for(int i = 0; i < 12; i++)
  120.       {
  121.         avg += grades[i];
  122.       }
  123.     grades[grades.length - 1] = (float) (avg/12.0);
  124.   }
  125.   //--------------------------------------------------------
  126.   public static void countsGrades(float[][] grades,int[] scores)
  127.   {
  128.     int i = 0;
  129.     for(i = 0; i < grades.length; i++)
  130.       {
  131.         if(grades[i][16] >= 90 && grades[i][16] <=100)
  132.         {
  133.           scores[0] += 1;
  134.         }
  135.         else
  136.         if(grades[i][16] >=80 && grades[i][16]<90)
  137.           {
  138.             scores[1] += 1;
  139.           }
  140.         else
  141.         if(grades[i][16] >=70 && grades[i][16]<80)
  142.           {
  143.             scores[2] += 1;
  144.           }
  145.         else
  146.         if(grades[i][16] >=60 && grades[i][16]<70)
  147.           {
  148.             scores[3] += 1;
  149.           }
  150.         else
  151.         if(grades[i][16] >=0 && grades[i][16]<60)
  152.           {
  153.             scores[4] += 1;
  154.           }
  155.       }
  156.   }
  157.   //---------------------------------------------------------
  158.   public static void displayInformation(String[] names, float[][] grades, int[] scores)
  159.   {
  160.  
  161.     System.out.println();
  162.     System.out.println("Project 2 Daniel Luce");
  163.     System.out.println();
  164.     System.out.println("Num Name           Avg Best 12");
  165.     System.out.println("--- -------------  -----------");
  166.     for(int i = 0; i < names.length; i++) //loop through to print names, averages
  167.       {
  168.         System.out.printf("%2d. %-17s %.1f%n", i+1, names[i], grades[i][grades[i].length-1]);
  169.       }
  170.  
  171.     System.out.println();
  172.     System.out.println("A = " + scores[0]);
  173.     System.out.println("B = " + scores[1]);
  174.     System.out.println("C = " + scores[2]);
  175.     System.out.println("D = " + scores[3]);
  176.     System.out.println("F = " + scores[4]);
  177.   }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement