Advertisement
Nickpips

Untitled

Oct 23rd, 2015 (edited)
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. /* Alvin Bello
  2. * Block 6
  3. * Grade Book Lab
  4. */
  5.  
  6. package main;
  7.  
  8. import java.text.DecimalFormat;
  9. import java.util.InputMismatchException;
  10. import java.util.Scanner;
  11.  
  12. public class Main {
  13.  
  14. static Scanner scan = new Scanner(System.in);
  15. String[][] grades;
  16. int students, tests, avg;
  17. DecimalFormat dec;
  18.  
  19. public static void main(String args[]) {
  20. int students = 0;
  21. while(true) {
  22. try {
  23. while(students < 1 || students > 30) {
  24. System.out.println("How many students do you want? You must have at least 1 and at most 30.");
  25. students = scan.nextInt();
  26. }
  27. } catch (InputMismatchException e) {
  28. System.out.println("Students are integers -_-");
  29. scan.nextLine();
  30. continue;
  31. } catch (Exception e) {
  32. System.out.println("An unexpected exception occured, lets try again.");
  33. scan.nextLine();
  34. continue;
  35. }
  36. break;
  37. }
  38. while(true) {
  39. try {
  40. System.out.println("How many tests will you have? If you enter a negative amount you will have 0");
  41. int tests = scan.nextInt();
  42. tests = (tests < 0) ? 0 : tests;
  43. scan.nextLine();
  44. Main run = new Main(students, tests);
  45. } catch (InputMismatchException e) {
  46. System.out.println("You must have an integral amount of tests -_-");
  47. scan.nextLine();
  48. continue;
  49. } catch (Exception e) {
  50. System.out.println("An unexpected exception occured, lets try again.");
  51. scan.nextLine();
  52. continue;
  53. }
  54. break;
  55. }
  56. }
  57.  
  58. public Main(int students, int tests) {
  59. this.students = students;
  60. this.tests = tests;
  61. grades = new String[students+1][tests+3];
  62. grades[0][0] = "";
  63. grades[0][grades[0].length-2] = "Average";
  64. grades[0][grades[0].length-1] = "Grade";
  65. dec = new DecimalFormat("0.##");
  66. while(true) {
  67. try {
  68. System.out.println("Now let's enter the names of your students");
  69. for(int i = 1; i < grades.length; i++) {
  70. System.out.println("Enter the name of student " + i);
  71. grades[i][0] = scan.nextLine();
  72. }
  73. } catch (Exception | Error e) {
  74. System.out.println("You messed up");
  75. scan.nextLine();
  76. continue;
  77. }
  78. break;
  79. }
  80. while(true) {
  81. try {
  82. System.out.println("Now let's enter the names of your tests.\n");
  83. for(int i = 1; i < grades[0].length-2; i++) {
  84. System.out.println("Enter the name of test " + i);
  85. grades[0][i] = scan.nextLine();
  86. }
  87. } catch (Exception | Error e) {
  88. System.out.println("You messed up");
  89. scan.nextLine();
  90. continue;
  91. }
  92. break;
  93. }
  94. while(true) {
  95. try {
  96. System.out.println("Now let's enter the scores of the students.\n");
  97. for(int i = 1; i < grades.length; i++) {
  98. for(int j = 1; j < grades[i].length-2; j++) {
  99. System.out.println("Enter " + grades[i][0] + "\'s grade for test " + grades[0][j]);
  100. double score = scan.nextDouble();
  101. score = score<0?0:score;
  102. grades[i][j] = dec.format(score);
  103. }
  104. }
  105. } catch (InputMismatchException e) {
  106. System.out.println("I told you to enter numbers! Now we have to start over.");
  107. scan.nextLine();
  108. continue;
  109. } catch (Exception e) {
  110. System.out.println("An unexpected exception occured, lets try again.");
  111. scan.nextLine();
  112. continue;
  113. }
  114. break;
  115. }
  116. for(int i = 1; i < grades.length; i++) {
  117. for(int j = 1; j < grades[i].length-2; j++)
  118. avg += Double.parseDouble(grades[i][j]);
  119. avg /= grades[i].length-3;
  120. grades[i][grades[i].length-2] = dec.format(avg);
  121. if(avg >= 89.5) grades[i][grades[i].length-1] = "A";
  122. else if(avg >= 79.5) grades[i][grades[i].length-1] = "B";
  123. else if(avg >= 69.5) grades[i][grades[i].length-1] = "C";
  124. else if(avg >= 59.5) grades[i][grades[i].length-1] = "D";
  125. else if(avg < 59.5) grades[i][grades[i].length-1] = "F";
  126. }
  127. System.out.println("Here is your class:\n");
  128. for(int i = 0; i < grades.length; i++) {
  129. for(int j = 0; j < grades[i].length; j++)
  130. System.out.print(grades[i][j] + "\t");
  131. System.out.println();
  132. }
  133. System.out.println("Here are your test averages:\n");
  134. double testAvg = 0;
  135. for(int j = 1; j < grades[0].length-2; j++) {
  136. for(int i = 1; i < grades.length; i++)
  137. testAvg += Double.parseDouble(grades[i][j]);
  138. testAvg /= grades.length - 1;
  139. System.out.println("The average for " + grades[0][j] + " is " + testAvg);
  140. }
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement