A Java program which accepts grades and prints grade distribution

I’m doing the MOOC Java course, this is the problem:

This is a link to the problem in detail if you’d prefer that, CTRL+F “distribution”

The input of the program is a set of exam scores of a course. Each score is an integer. When -1 is entered, the program stops asking for further input.

After the scores have been read, the program prints the grade distribution and acceptance percentage of the course.

Grade distribution is formed as follows:

  • Each exam score is mapped to a grade using the same formula as in exercise 18. If the score is not within the range 0-60 it is not taken into account.

  • The number of grades are printed as stars, e.g. if there are 2 scores that correspond to grade 5, the line 5: ** is printed. If there are no scores that correspond to a particular grade, the printed line is 4:

All the grades besides zeros are accepted, so in the above 7 out of 8 participants were accepted. Acceptance percentage is calculated with the formula 100*accepted/allScores.

The formula in exercise 18 is (Run the snippet below, I don’t think SE lets you format a table directly):

<table >       <tr>         <th>Points</th>         <th>Grade</th>              </tr>       <tr>         <td>0-29</td>         <td>Failed</td>         </tr>       <tr>         <td>30-34</td>         <td>1</td>        </tr>        <tr>         <td>35-39</td>         <td>2</td>        </tr>        <tr>         <td>40-44</td>         <td>3</td>        </tr>        <tr>         <td>45-49</td>         <td>4</td>        </tr>        <tr>         <td>50-60</td>         <td>5</td>        </tr>     </table>

I wrote code that seems to work fine based on my tests, no compile errors, no run-time errors (except for cases where you enter a string instead of a number or a ridiculously large number).

I tested it using input from the table above, i.e I entered the numbers (0, 29, 30, 34, 35, 39, 40, 44, 45, 49, 50, 60), and there were two stars printed in each grade range, as expected. I also tested it with numbers inbetween the ranges, and numbers outside of [0, 60]. I found no logical errors.

I found the names I used too repetitive, GradeDistribution class, 3 arrays called gradeRanges, gradeDistributionList, gradeList.

Other problems: The logic of calculating gradeDistribution seems too nested, should I have used switch-case? Should I have used static methods instead of a class? Should I have combined the methods calculateGradeDistribution and printGradeDistribution into one? Am I worrying too much about little things?

This is main:

import java.util.ArrayList; import java.util.Scanner;  public class Main {     public static void main(String[] args) {         Scanner reader = new Scanner(System.in);         ArrayList<Integer> grades = new ArrayList<Integer>();         int number = 0;         System.out.println("Type exam scores, -1 to end");         do{             number = Integer.parseInt(reader.nextLine());             if(number != -1){                 grades.add(number);             }         }while(number != -1);          GradeDistribution syrianGradeDistribution = new GradeDistribution();         syrianGradeDistribution.calculateGradeDistribution(grades);         syrianGradeDistribution.printGradeDistribution();         System.out.println(syrianGradeDistribution.acceptancePercentage());     } } 

This is the GradeDistribution class:

import java.util.ArrayList; import java.util.Collections;  public class GradeDistribution {     private ArrayList<Integer> gradeRanges = new ArrayList<>();     private ArrayList<Integer> gradeDistributionList = new ArrayList<>();      public GradeDistribution(){         Collections.addAll(gradeRanges, 0, 30, 35, 40, 45, 50, 61);         Collections.addAll(gradeDistributionList, 0, 0, 0, 0, 0, 0);     }      public void calculateGradeDistribution(ArrayList<Integer> gradeList){         for(int grade: gradeList){             if(grade < 0 || grade > 60){ //invalid grades                 continue;             }             for(int i = 0; i < gradeRanges.size() -1 ; i++){                  if(grade >= gradeRanges.get(i) && grade < gradeRanges.get(i + 1)){                     gradeDistributionList.set(i, gradeDistributionList.get(i)+ 1);                  }             }         }     }      public void printGradeDistribution(){         for(int i = 0; i < gradeDistributionList.size(); i++){             System.out.print(i + ": ");             for(int j = 0; j < gradeDistributionList.get(i); j++){                 System.out.print("*");             }             System.out.println();         }     }      public double acceptancePercentage(){         int allScores = 0;         for(int number: gradeDistributionList){             allScores += number;         }         int acceptedScores = allScores - gradeDistributionList.get(0);         double acceptancePercentage = 100.0 * acceptedScores / allScores;         return acceptancePercentage;     } } 

The code above compiles without any errors on Windows 10, Java 11.0.2

This is a very simple program, but yet, I still have many questions about the choices I made, how can I become confident in my choices and know what’s acceptable and what’s not?

Reads a inFile and grades tests scores and finds max, min, and avg

This was my assignment.

Write a C++ program that reads in a series of test scores. If the test score is >= 90 && , <= 100; print the letter grade ‘A’. If the score is >= 80 but < 90, print the letter grade ‘B’. if the test score is >= 70 and < 80, print the letter grade ‘C’. If the test score is >= 60 but < 70 print the letter grade ‘D’ If the test score is < 60 print the letter grade ‘F’. Print the score and the corresponding grade. Determine and print the max score, the min score, the average score, and the number of tests. Terminate on a negative test score.

My code works fine but I feel like there’s a better way to do it. I talked to a friend and he said since I was reading from a file a array would be better. How would I got about that?

#include <iostream> #include <fstream> #include <iomanip> #include <string>  using namespace std;  int main() {     ifstream inFile;      int Score, MaxScore, MinScore, AvgScore, NumTests, TotalScore, Num;     string Grade;      NumTests = 0;     TotalScore = 0;     MaxScore = 0;     MinScore = 100;      inFile.open("indata6.txt");      if (!inFile)      {         cout << "Failed to find inFile." << endl;         return 1;     }      cout << "Test Scores & Grades" << endl;     cout << "--------------------" << endl;      while ( inFile >> Score)      {         NumTests = NumTests + 1; // Setting number of tests         TotalScore = TotalScore + Score; // Adding the Total score for the Avg equation          if ( Score > MaxScore) // If statements to find Max score         {             MaxScore = Score;              if ( Score >= 90 &&  Score <= 100) // If statements to find letter Grades             {                 Grade = "A";                 cout << Score << " " << Grade << endl;             }              else if ( Score >= 80)             {                 Grade = "B";                 cout << Score << " " << Grade << endl;             }              else if ( Score >= 70)             {                 Grade = "C";                 cout << Score << " " << Grade << endl;             }              else if ( Score >= 60)             {                 Grade = "D";                 cout << Score << " " << Grade << endl;             }              else if ( Score >= 0)             {                 Grade = "F";                 cout << Score << " " << Grade << endl;             }              else             {                 cout << "Invalid Test Score:" << Score << endl;                 return 2;              }          }          else if ( Score < MinScore) // If statement to find Min score         {             MinScore = Score;              if ( Score >= 90 &&  Score <= 100) // If statements to find letter grades             {                 Grade = "A";                 cout << Score << " " << Grade << endl;             }              else if ( Score >= 80)             {                 Grade = "B";                 cout << Score << " " << Grade << endl;             }              else if ( Score >= 70)             {                 Grade = "C";                 cout << Score << " " << Grade << endl;             }              else if ( Score >= 60)             {                 Grade = "D";                 cout << Score << " " << Grade << endl;             }              else if ( Score >= 0)             {                 Grade = "F";                 cout << Score << " " << Grade << endl;             }              else              {                 cout << "Invalid Test Score:" << Score << endl;                 return 2;             }         }          else // When a score isnt Max or Min         {                if ( Score >= 90 &&  Score <= 100) // More if statements to find letter grade             {                 Grade = "A";                 cout << Score << " " << Grade << endl;             }              else if ( Score >= 80)             {                 Grade = "B";                 cout << Score << " " << Grade << endl;             }              else if ( Score >= 70)             {                 Grade = "C";                 cout << Score << " " << Grade << endl;             }              else if ( Score >= 60)             {                 Grade = "D";                 cout << Score << " " << Grade << endl;             }              else if ( Score >= 0)             {                 Grade = "F";                 cout << Score << " " << Grade << endl;             }              else              {                 cout << "Invalid Test Score:" << Score << endl;                 return 2;             }         }     }      AvgScore = TotalScore / NumTests; // Finding the avg test score      cout << "Max Score: " << MaxScore << endl; // Printing Max score     cout << "Min Score: " << MinScore << endl; // Printing Min score     cout << "Avg Score: " << AvgScore << endl; // Printing Avg score     cout << "Number of Tests: " << NumTests << endl; // Printing Number of Tests      return 0; }