Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.StringTokenizer;
  6. import java.util.*;
  7.  
  8.  
  9.  
  10. public class Test {
  11.  
  12.  
  13. private static FileInputStream inFile;
  14. private static InputStreamReader inReader;
  15. private static BufferedReader reader;
  16.  
  17.  
  18. private static List<Student> classroom = new ArrayList<Student>(); // ArrayList to store the classroom.
  19. private static int [] scores = new int[classroom.size()];
  20.  
  21.  
  22.  
  23.  
  24.  
  25. public static void main (String args[]) throws IOException
  26. {
  27. initFile();
  28. getData();
  29. System.out.print(classroom); //output of the complete class.
  30. sort();
  31. System.out.print(classroom); //output after sorting.
  32. inFile.close();
  33.  
  34. }
  35.  
  36. // preparing the file for input
  37.  
  38. public static void initFile() throws IOException
  39.  
  40.  
  41. {
  42. inFile = new FileInputStream ("E:\\!!VHSAPCSData\\truefalse.txt");
  43. inReader = new InputStreamReader(inFile);
  44. reader = new BufferedReader(inReader);
  45.  
  46. }
  47.  
  48.  
  49. // Separate the id from the answers and store the answers in an array.
  50.  
  51. public static void getData() throws IOException
  52. {
  53. String line = reader.readLine(); //Seed
  54.  
  55. String[] answerkey = new String[10]; //Store the answer key from the first line of the txt file.
  56.  
  57. for(int i=0; i<answerkey.length; i++){ // take that line and place each answer in an array.
  58.  
  59. answerkey[i]=line.substring(i,i+1);
  60. }
  61.  
  62. line = reader.readLine(); // read the following line of the txt file.
  63.  
  64.  
  65. while(line != null) // Read and create a student for each line.
  66. {
  67. String[] answers = new String[10];
  68. StringTokenizer strTkn = new StringTokenizer(line);
  69. String id = strTkn.nextToken();
  70. String answerline = strTkn.nextToken();
  71.  
  72.  
  73. for(int i=0; i<answers.length; i++){
  74.  
  75. answers[i]=answerline.substring(i, i+1);
  76.  
  77. }
  78.  
  79. Student stu = new Student(id,answers);
  80.  
  81. stu.grade(answerkey, answers);
  82.  
  83. classroom.add(stu);
  84.  
  85.  
  86.  
  87.  
  88. line = reader.readLine(); //updating what is being read
  89.  
  90. }
  91.  
  92. }
  93.  
  94.  
  95.  
  96. // In this method you should sort the classroom in ascending order depending on the score obtained on the quiz.
  97. public static void sort() throws IOException{
  98.  
  99.  
  100. for (int i = scores.length; i == 0; i ++){
  101. scores.add(((Test) classroom).returnScore(i));
  102. }
  103. for (int i = classroom.size() -1 ; i > 1; i--){
  104.  
  105. for (int j = 0; j < i; j++){
  106.  
  107. if (scores[j] < scores[j+1]){
  108.  
  109. bubbleSwap(j, j+1);
  110.  
  111. }
  112.  
  113.  
  114. }
  115.  
  116.  
  117. }
  118.  
  119. }
  120. public static void bubbleSwap (int index1, int index2){
  121. int temp = scores[index1];
  122. scores[index1] = scores[index2];
  123. scores[index2] = temp;
  124.  
  125. }
  126.  
  127. public int returnScore(int index)throws IOException {
  128. classroom.get(index);
  129. index = ((Student) classroom).getScore();
  130. return index;
  131.  
  132. }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement