0

This program takes a set of exam scores from the user, checks the grade range from gradeDistro class and should return an acceptance percentage when called. However, my function getter method getPercentage() does not return the percentage. Could use input on what is wrong here.


import java.util.Scanner;
import java.util.ArrayList;


public class Main {
    public static void main(String[] args){
        int pos = 0;



        ArrayList<Integer> numbers = new ArrayList<>();

        Scanner scanner = new Scanner(System.in);
        gradeDistro grades = new gradeDistro();

        System.out.println("Please enter your grades: ");

        while(true){
            int val = scanner.nextInt();
            if (val == -1){
                break;
            }
            numbers.add(val);
        }

        for(Integer num: numbers){
            pos += num;
            grades.scoreToLetterGrade(num);
        }
        
        System.out.println("Grade distribution: ");

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

Below is the GradeDistro class

import java.util.ArrayList;


public class gradeDistro {
    private int score;
    private int accepted;
    private int result;

    ArrayList<Integer> entries = new ArrayList<>();

    public gradeDistro(){

    }

    public int scoreToLetterGrade(int entry) {

        // This function saves score and accept to global variables

        int accept = 0;

        if (entry >= 50 && entry <= 60) {
            accept++;
        }
        else if (entry >= 45 && entry <= 49) {
            accept++;
        }
        else if (entry >= 40 && entry <= 44) {
            accept++;
        }
        else if (entry >= 35 && entry <= 39) {
            accept++;
        }
        else if (entry >= 30 && entry <= 34) {
            accept++;
        }
        this.accepted += accept;


        if (!(entry < 30)) {
            entries.add(entry);
            this.score += entry;
        }
        return 0;
    }


    public void acceptancePercentage(){
        if(this.score > 0){
            int result;
            result = (this.accepted * 100) / score;
            this.result = result;
        }


    }

    public void getScores(){
        for(int inst: entries){
            System.out.println(inst);
        }
    }

    public int getPercentage(){
        return this.result;
    }




}

Note: When I debug this Program, the internal function printStream is shown to me instead.

wild_o
  • 41
  • 5
  • When are you setting the `result` value? Is the method setting the value invoked? – Vincent C. Oct 12 '20 at 05:01
  • @VincentC. In the gradeDistro class, I have a global variable named 'result.' I use the first method in gradeDistro to get the total score and save it to the global variable 'score.' In the concurrent method, I create an instance variable 'result' (again) to save to the global result. – wild_o Oct 12 '20 at 19:14
  • I thought simply create an instance of gradeDistro for main to access would let me get around placing a setter. – wild_o Oct 12 '20 at 19:31
  • I am not sure I understand. I was mostly pointing out that in the code snippet you exhibit here the `result` is _never_ defined, hence you most likely missed something or did not provide enough code for us to help you out. – Vincent C. Oct 13 '20 at 07:33
  • @VincentC.You mean set it to zero? I'll go ahead and test that change. And this is all the code I have at the moment. – wild_o Oct 13 '20 at 21:53
  • I mean your instance of `gradeDistro` seems to require a call to `acceptancePercentage()` to see it's attribute `result` set. – Vincent C. Oct 14 '20 at 23:53

1 Answers1

0

I believe you want to get result from gradeDistro, but the only time it is populated is when you call acceptancePercentage(). Looking at your first code, you never do.

  • I thought I did. I feed 'result' using the parameter value in my ScoreToLetterGrade method and apply it to the global method 'result.' In the acceptance percentage method, I created an instance variable called result. Even when I debug the global result does receive value. – wild_o Oct 12 '20 at 19:12