Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class GradeDistribution {
- int[] starCount = new int[6];
- double countAccepted;
- int grade;
- public void enterGradeValues(Scanner scanner) {
- int scores = scanner.nextInt();
- double gradeCountTotal = 0;
- while (scores != -1) {
- if ((scores >= 0) && (scores <= 60)) {
- grade = distributeGrades(scores);
- tallyStarCount(grade);
- }
- gradeCountTotal++;
- scores = scanner.nextInt();
- }
- printStars();
- getAverage(countAccepted, gradeCountTotal);
- }
- private int distributeGrades(int score) {
- int points = score;
- if (points <= 29) {
- grade = 0;
- } else if (points >= 30 && points <= 34) {
- grade = 1;
- countAccepted++;
- } else if (points >= 35 && points <= 39) {
- grade = 2;
- countAccepted++;
- } else if (points >= 40 && points <= 44) {
- grade = 3;
- countAccepted++;
- } else if (points >= 45 && points <= 49) {
- grade = 4;
- countAccepted++;
- } else {
- grade = 5;
- countAccepted++;
- }
- return grade;
- }
- /*
- All grades besides zero are accepted.
- Accepted percentage is calculated with the formula 100 * accepted/gradeCountTotal
- */
- private void getAverage(double accepted, double gradeCountTotal) {
- System.out.println("Acceptance percentage: " + 100 * accepted / gradeCountTotal);
- }
- /*
- keeps a running total of the number of times
- a score falls into a specific grade.
- */
- private void tallyStarCount(int grade) {
- if (grade == 5) {
- starCount[5] += 1;
- } else if (grade == 4) {
- starCount[4] += 1;
- } else if (grade == 3) {
- starCount[3] += 1;
- } else if (grade == 2) {
- starCount[2] += 1;
- } else if (grade == 1) {
- starCount[1] += 1;
- } else if (grade == 0) {
- starCount[0] += 1;
- }
- }
- // Prints the Grade Distribution
- // number of grades are printed as stars.
- private void printStars() {
- System.out.println("Grade distribution:");
- for (int x = starCount.length - 1; x >= 0; x--) {
- System.out.print(x + ": ");
- for (int y = 0; y < starCount[x]; y++) {
- System.out.print("*");
- }
- System.out.println("");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment