I'm learning Java in a MOOC right now and this is my solution for exercise grade statistics.
This is the exercise:
In this exercise we create a program for printing statistics for points in course. The program receives points (integers from zero to one hundred) as input, based on which it prints statistics about grades. Reading of input stops when the user enters the number -1. Numbers that are not within the interval [0-100] should not be taken into account when calculating the statistics.
PART 1 Point averages
Write a program that reads integers representing course point totals from the user. Numbers between [0-100] are acceptable and the number -1 ends the reading of input. Other numbers are erroneous input, which should be ignored. When the user enters the number -1, the program should print the average of the point totals that were input.
PART 2 Point average for points giving a passing grade
Extend the program, such that it in addition to giving the point average of all totals also provides the point average for points giving a passing grade.
A passing grade is achieved by getting a minimum of 50 course points. You may assume that the user always provides at least one integer between [0-100]. If there are no numbers giving a passing grade, the program should print a line "-" where the average would be.
PART 3 Pass percentage
Extend the program from the previous part, such that it also print the pass percentage. The pass percentage is calculated using the formula 100 * passing / participants.
PART 4 Grade distribution
Extend the program, such that it also prints the grade distribution.Each point total is converted to a grade based on the above table. If a point total isn't within [0-100], it should be ignored.
The grade distribution is printed out as stars. E.g. if there is one point total giving the grade 5, then it should print the row 5: *. If there are no point totals giving a particular grade, then no stars should be printed for it. In the sample below this is true for e.g. the grade 4.
This is my solution:
Main.java:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Write your program here -- consider breaking the program into
// multiple classes.
UserInterface ui = new UserInterface(scanner);
ui.start();
}
}
PointsStatistics.java:
import java.util.ArrayList;
public class PointsStatistics {
private ArrayList<Integer> allpoints;
private ArrayList<Integer> passingpoints;
public PointsStatistics() {
this.allpoints = new ArrayList<>();
this.passingpoints = new ArrayList<>();
}
public void add(int points) {
if (points > 0 && points >= 50 && points <= 100) {
this.passingpoints.add(points);
}
if (points > 0 && points <= 100) {
this.allpoints.add(points);
}
}
public double pointsAverage() {
double sum = 0.0;
for (Integer points : this.allpoints) {
sum += points;
}
return sum / this.allpoints.size();
}
public double possingPointsAverage() {
double sum = 0.0;
for (Integer points : this.passingpoints) {
sum += points;
}
return sum / this.passingpoints.size();
}
public double passingPercentage() {
double percentage = (this.passingpoints.size() * 1.0) / this.allpoints.size();
return percentage * 100;
}
public void gradeDistribution() {
ArrayList<Integer> grades = new ArrayList<>();
for (Integer points : this.allpoints) {
if (points < 50) {
grades.add(0);
} else if (points < 60) {
grades.add(1);
} else if (points < 70) {
grades.add(2);
} else if (points < 80) {
grades.add(3);
} else if (points < 90) {
grades.add(4);
} else if (points <= 100) {
grades.add(5);
}
}
for (int i = 5; i >= 0; i--) {
System.out.print(i + ": ");
for(Integer grade : grades) {
if (grade == i) {
System.out.print("*");
}
}
System.out.println("");
}
}
}
UserInterface.java
import java.util.Scanner;
public class UserInterface {
private PointsStatistics points;
private Scanner scanner;
public UserInterface(Scanner scanner) {
this.points = new PointsStatistics();
this.scanner = scanner;
}
public void start() {
System.out.println("Enter point totals, -1 stops: ");
while(true) {
int enterPoints = Integer.valueOf(scanner.nextLine());
if( enterPoints == -1) {
break;
}
this.points.add(enterPoints);
}
System.out.println("Point average (all): " + this.points.pointsAverage());
System.out.println("Points average (passing): " + this.points.possingPointsAverage());
System.out.println("Pass percentage: " + this.points.passingPercentage());
points.gradeDistribution();
}
}
So what do you think? It works, but is it an optimal solution? Thank-you.