CymsSoul

Untitled

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