Object-Oriented Programming with Java, part I – Exercise 102

This exercise asks us to implement a grades distribution program in an program form we decide.

My Code

Main

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        Grades Grades = new Grades();

        int readUser = 0;
        System.out.println("Type exam scores, -1 completes: ");
        while (readUser != (-1)) {
            readUser = Integer.parseInt(reader.nextLine());
            Grades.addGrade(readUser);
            
        }

        Grades.assStars();


        
        System.out.println("Grade distribution:");
        int i = 5;
        while (i >= 0){
            System.out.print(i + ": ");
            Grades.printStars(i);
            System.out.println("");
            i--;
        }
        
        System.out.print("Acceptance percentage: ");
        System.out.println(Grades.getAccepted());
        

    }
}

Grades


import java.util.ArrayList;

public class Grades {

    private ArrayList<Integer> grades = new ArrayList<Integer>();
    private int[] gradeNum = {0, 0, 0, 0, 0, 0};
    private int accepted;
    private int total;

    public Grades() {

    }

    public void addGrade(int grade) {
        if (grade >= 0 && grade <= 60) {
            grades.add(grade);
        }
    }

    public void assStars() {
        for (Integer read : grades) {

            if (read <= 0 && read >= 60) {
                break;
            } else if (read >= 1 && read <= 29) {
                gradeNum[0] = gradeNum[0] + 1;
                total++;
            } else if (read >= 30 && read <= 34) {
                gradeNum[1] = gradeNum[1] + 1;
                accepted++;
                total++;
            } else if (read >= 35 && read <= 39) {
                gradeNum[2] = gradeNum[2] + 1;
                total++;
                accepted++;
            } else if (read >= 40 && read <= 44) {
                gradeNum[3] = gradeNum[3] + 1;
                total++;
                accepted++;
            } else if (read >= 45 && read <= 49) {
                gradeNum[4] = gradeNum[4] + 1;
                total++;
                accepted++;
            } else if (read >= 50 && read <= 60) {
                gradeNum[5] = gradeNum[5] + 1;
                total++;
                accepted++;
            }
        }
    }
    
    public void printStars(int cell){
        int i = 0;
        while (i < gradeNum[cell]){
            System.out.print("*");
            i++;
        }
    }
    
    public double getAccepted(){
        
        return (double)100*accepted/total;
    }

}

Model Code

Main

import java.util.Scanner;

 

public class Main {

    public static void main(String[] args) {

        Scanner lukija = new Scanner(System.in);

        // implement your program here

        // do not put all to one method/class but rather design a proper structure to your program

        

        // Your program should use only one Scanner object, i.e., it is allowed to call 

        // new Scanner only once. If you need scanner in multiple places, you can pass it as parameter

        GradeDistribution distribution = new GradeDistribution();

 

        System.out.println("Type exam scores, -1 completes: ");

        while (true) {

            int luku = Integer.parseInt(lukija.nextLine());

            if (luku == -1) {

                break;

            }

 

            distribution.addAScore(luku);

        }

 

        distribution.tulosta();

    }

}

 

GradeDistribution

public class GradeDistribution {

    private int[] grades;

 

    public GradeDistribution() {

        grades = new int[6];

    }

 

    public void addAScore(int score) {

        if (score < 0 || score > 60) {

            return;

        }

 

        int theGrade = grade(score);

        grades[ theGrade]++;

    }

 

    public void tulosta() {

        System.out.println("Grade distribution:");

        for (int i = 5; i >= 0; i--) {

            System.out.println(i + ": " + stars(grades[i]));

        }

 

        System.out.println("Acceptance percentage: " + acceptancePercentage());

    }

 

    //  since the following are only to internal use, those have been defined as private

    private String stars(int n) {

        String tahdet = "";

        for (int i = 0; i < n; i++) {

            tahdet += "*";

        }

        return tahdet;

    }

 

    private int grade(int score) {

 

        if (score < 30) {

            return 0;

        }

        if (score < 35) {

            return 1;

        }

        if (score < 40) {

            return 2;

        }

        if (score < 45) {

            return 3;

        }

        if (score < 50) {

            return 4;

        }

 

        return 5;

    }

 

    private double acceptancePercentage() {

        double all = 0;

        for (int number : grades) {

            all += number;

        }

        double failed= grades[0];

 

        return 100 * (all - failed) / all;

    }

}

Comments

Well, as to be expected there are a lot of difference between my code and the model code. I prefer the model code. The use of if statements to assign the grades is much easier. In fact, most of the model code is simpler and easier to understand than mine.

Leave a comment