Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. /*
  2. /* Aerij Saeed
  3. * CS140-002
  4. * Fall 2016
  5. * Programming Assignment 7
  6. * 10/24/2016
  7. */
  8. package programmingassignment7;
  9. import java.util.Scanner;
  10. import java.io.FileNotFoundException;
  11. import java.io.File;
  12. import java.io.PrintWriter;
  13.  
  14. public class ProgrammingAssignment7 {
  15.  
  16. final static int MAXSIZE = 50;
  17. public static void main(String[] args)
  18. {
  19.  
  20. Scanner fin = null;
  21. PrintWriter fout = null;
  22. Scanner keyboard = new Scanner(System.in);
  23. String infileName;
  24. String outfileName;
  25. int index;
  26. String fileLine;
  27. String [] names = new String [MAXSIZE];
  28. double [] avgs = new double [MAXSIZE];
  29. String [] passNames = new String [MAXSIZE];
  30. double [] passAvgs = new double [MAXSIZE];
  31.  
  32. int numStudents = 0;
  33. int numPass = 0;
  34.  
  35. System.out.print("Enter the input file name: ");
  36. infileName = keyboard.next();
  37. fin = openInputFile(infileName);
  38. System.out.print("Enter the output file name: ");
  39. outfileName = keyboard.next();
  40. fout = openOutputFile(outfileName);
  41. numStudents = readAndLoadArray(fin, names ,avgs);
  42. sortArray (names, avgs, numStudents);
  43. numPass = passArrays(names, avgs, numStudents, passNames, passAvgs);
  44.  
  45.  
  46. fin.close();
  47. fout.close();
  48. }
  49.  
  50.  
  51. public static Scanner openInputFile(String fileName)
  52. {
  53. Scanner fin = null;
  54. try {
  55. fin = new Scanner(new File(fileName));
  56. } catch (FileNotFoundException error) {
  57. System.err.println(error);
  58.  
  59. }
  60. return fin;
  61. }
  62. public static PrintWriter openOutputFile(String fileName)
  63. {
  64. PrintWriter fout = null;
  65. try {
  66. fout = new PrintWriter(fileName);
  67. }
  68. catch (FileNotFoundException e){
  69. System.out.println("error");
  70. }
  71. return fout;
  72. }
  73. public static double calculateAverage(String line) {
  74. double avg;
  75. int sum = 0;
  76. int count = 0;
  77. int pos = line.indexOf(",");
  78. while (pos >= 0) {
  79. String num = line.substring(0, pos);
  80. sum += Integer.parseInt(num);
  81. count++;
  82. line = line.substring(pos + 1);
  83. pos = line.indexOf(",");
  84. }
  85. avg = (double) sum / count;
  86. return avg;
  87. }
  88. public static int readAndLoadArray(Scanner fin, String [] names, double [] avg)
  89. {
  90. int students = 0;
  91. while (fin.hasNext() && students < MAXSIZE)
  92. {
  93. String line = fin.nextLine();
  94. int pos = line.indexOf(",");
  95. names[students] = line.substring (0,pos);
  96.  
  97. line = line.substring(pos +1);
  98. pos = line.indexOf(",");
  99. line = line.substring (pos + 1);
  100. avg[students] = calculateAverage (line);
  101. students ++;
  102.  
  103. }
  104. return students;
  105.  
  106. }
  107. public static int passArrays(String [] names, double [] avg, int numStudents, String [] passNames, double [] passAvgs)
  108. {
  109. int passStudents = 0;
  110. for (int i = 0; i < avg.length; i ++)
  111.  
  112. {
  113. if (avg[i] > 70){
  114. passNames[passStudents] = names [i];
  115. passAvgs[passStudents] = avg [i];
  116. passStudents ++;
  117. }
  118. }
  119. return passStudents;
  120. }
  121. public static void sortArray (String[] names, double[] avgs, int numStudents)
  122. {
  123. String smallest;
  124. int ios;
  125. String tempName;
  126. double tempAvg;
  127. for (int i = 0; i < numStudents -1; i++)
  128. {
  129. smallest = names [i];
  130. ios = i;
  131. for (int j = i+1; j < numStudents; j++)
  132. {
  133. if (smallest.compareTo (names[j]) > 0)
  134. {
  135. ios = j;
  136. smallest = names [j];
  137. }
  138. }
  139. if (i != ios)
  140. {
  141. tempName = names [i];
  142. tempAvg = avgs [i];
  143. names [i] = names [ios];
  144. avgs [i] = avgs [ios];
  145. names [ios] = tempName;
  146. avgs [ios] = tempAvg;
  147. }
  148. }
  149. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement