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.