CymsSoul

Untitled

Jan 22nd, 2019
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class GradeDistribution {
  4.  
  5. int[] starCount = new int[6];
  6. double countAccepted;
  7. int grade;
  8.  
  9.  
  10. public void enterGradeValues(Scanner scanner) {
  11. int scores = scanner.nextInt();
  12. double gradeCountTotal = 0;
  13.  
  14. while (scores != -1) {
  15.  
  16. if ((scores >= 0) && (scores <= 60)) {
  17. grade = distributeGrades(scores);
  18. tallyStarCount(grade);
  19. }
  20.  
  21. gradeCountTotal++;
  22. scores = scanner.nextInt();
  23. }
  24.  
  25. printStars();
  26. getAverage(countAccepted, gradeCountTotal);
  27.  
  28. }
  29.  
  30. private int distributeGrades(int score) {
  31. int points = score;
  32.  
  33. if (points <= 29) {
  34. grade = 0;
  35. } else if (points >= 30 && points <= 34) {
  36. grade = 1;
  37. countAccepted++;
  38. } else if (points >= 35 && points <= 39) {
  39. grade = 2;
  40. countAccepted++;
  41. } else if (points >= 40 && points <= 44) {
  42. grade = 3;
  43. countAccepted++;
  44. } else if (points >= 45 && points <= 49) {
  45. grade = 4;
  46. countAccepted++;
  47. } else {
  48. grade = 5;
  49. countAccepted++;
  50. }
  51. return grade;
  52. }
  53.  
  54. /*
  55. All grades besides zero are accepted.
  56. Accepted percentage is calculated with the formula 100 * accepted/gradeCountTotal
  57. */
  58. private void getAverage(double accepted, double gradeCountTotal) {
  59.  
  60. System.out.println("Acceptance percentage: " + 100 * accepted / gradeCountTotal);
  61.  
  62. }
  63.  
  64. /*
  65. keeps a running total of the number of times
  66. a score falls into a specific grade.
  67. */
  68. private void tallyStarCount(int grade) {
  69.  
  70. if (grade == 5) {
  71. starCount[5] += 1;
  72. } else if (grade == 4) {
  73. starCount[4] += 1;
  74. } else if (grade == 3) {
  75. starCount[3] += 1;
  76. } else if (grade == 2) {
  77. starCount[2] += 1;
  78. } else if (grade == 1) {
  79. starCount[1] += 1;
  80. } else if (grade == 0) {
  81. starCount[0] += 1;
  82. }
  83.  
  84. }
  85.  
  86. // Prints the Grade Distribution
  87. // number of grades are printed as stars.
  88.  
  89. private void printStars() {
  90. System.out.println("Grade distribution:");
  91. for (int x = starCount.length - 1; x >= 0; x--) {
  92. System.out.print(x + ": ");
  93.  
  94. for (int y = 0; y < starCount[x]; y++) {
  95. System.out.print("*");
  96. }
  97.  
  98. System.out.println("");
  99. }
  100. }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment