Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. /**
  4. This program demonstrates a solution to the
  5. Grade Book programming challenge.
  6. This program uses the data stored in the
  7. StudentInfo.txt file.
  8. */
  9.  
  10. public class GradeBookDemo
  11. {
  12. public static void main(String[] args) throws IOException
  13. {
  14. // Create a GradeBook object.
  15. GradeBook gb = new GradeBook();
  16.  
  17. // Read the data from the file.
  18. readFromFile(gb);
  19.  
  20. // Display the student data.
  21. for (int i = 1; i <= 5; i++)
  22. {
  23. System.out.println("Name : " + gb.getName(i) +
  24. "\tAverage score: " +
  25. gb.getAverage(i) +
  26. "\tGrade: " +
  27. gb.getLetterGrade(i));
  28. }
  29. }
  30.  
  31. /**
  32. The readFromFile method reads test scores from a
  33. file into a GradeBook object.
  34. @param gb The GradeBook object.
  35. */
  36.  
  37. public static void readFromFile(GradeBook gb)
  38. throws IOException
  39. {
  40. String input;
  41. double[] scores = new double[4];
  42.  
  43. // Create the necessary objects for file input.
  44. FileReader freader = new FileReader("StudentInfo.txt");
  45. BufferedReader inFile = new BufferedReader(freader);
  46.  
  47. // Read the contents of the file.
  48. for (int student = 1; student <= 5; student++)
  49. {
  50. // Read the name.
  51. input = inFile.readLine();
  52. gb.setName(student, input);
  53.  
  54. // Read the 4 test scores.
  55. for (int i = 0; i < 4; i++)
  56. {
  57. input = inFile.readLine();
  58. scores[i] = Double.parseDouble(input);
  59. gb.setScores(student, scores);
  60. }
  61. }
  62.  
  63. // Close the file.
  64. inFile.close();
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement