Why is my program not returning a percent?

  • Thread starter Thread starter Arnoldjavs3
  • Start date Start date
  • Tags Tags
    Percent Program
Click For Summary

Discussion Overview

The discussion revolves around a Java program designed to calculate and return the acceptance percentage of exam scores. Participants are examining why the program consistently returns a value of 0.0 for the acceptance percentage, despite varying inputs.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant notes that the method returnPercentage is intended to calculate the acceptance percentage but always returns 0.0.
  • Another participant highlights the specific line of code where the division occurs, questioning the type of division being performed: this.accepted/allScores.
  • A later reply expresses embarrassment over the oversight, suggesting that the issue may be a common mistake among programmers.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the exact nature of the division issue, but there is a shared recognition of the potential mistake in the code.

Contextual Notes

There is an implied assumption that the division may be resulting in integer division, which could lead to the acceptance percentage being calculated as 0 when this.accepted is less than allScores.

Arnoldjavs3
Messages
191
Reaction score
3

Homework Statement


Java:
import java.util.ArrayList;

public class Scores {
    private ArrayList<Integer> examScores = new ArrayList<Integer>();
    private int[] howManyScorers;
    private int accepted;
    private double acceptedPercent;
 
    public Scores(ArrayList<Integer> scores) {
        this.examScores = scores;
        this.howManyScorers = new int[6];
        this.accepted = 0;
        this.acceptedPercent = 0;
    }
 
    public String returnPercentage() {
        int allScores = this.examScores.size();
        double acceptanceRate = 100*(this.accepted/allScores);
        this.acceptedPercent = acceptanceRate;
        System.out.println();
        return "Acceptance percentage: " + this.acceptedPercent;
    
    }
 
    public void distributeScorers() {
        for (int scorers : examScores) {
            if (scorers >= 0 && scorers <= 29) {
                this.howManyScorers[0] += 1;
            }
            else if (scorers>=30 && scorers<=34) {
                this.howManyScorers[1] += 1;
                this.accepted += 1;
            }
            else if (scorers>=35 && scorers<=39) {
                this.howManyScorers[2] += 1;
                this.accepted += 1;
            }
            else if (scorers>=40 && scorers<=44) {
                this.howManyScorers[3] += 1;
                this.accepted += 1;
            }
            else if (scorers>=45 && scorers<=49) {
                this.howManyScorers[4] += 1;
                this.accepted += 1;
            }
            else if (scorers>=50 && scorers<=60) {
                this.howManyScorers[5] += 1;
                this.accepted += 1;
            }
        
        }
    }
 
    public void printGradeDistribution() {     
        int length = this.howManyScorers.length;
        System.out.println();
        System.out.println("Grade distribution: ");
    
        for (int i = 5; i>0; i--) {
            System.out.print(i + ": ");
            printFormat(this.howManyScorers[i]);
            System.out.println();
        }
        this.returnPercentage();
    
    
    }
 
    public void printFormat(int howMany) {     
        for (int i = 0; i<howMany; i++) {
            System.out.print("*");
        }
    
    }
 
}

Homework Equations

The Attempt at a Solution


So essentially, the code is supposed to return a percent of how many people were accepted. But regardless of how many failed or passed, it always returns as 0.0!

My brain is a little tired so you'll have to excuse me for lack of efficiency. But I still believe I should understand why this is happening. Here is the results after compiling:

http://prntscr.com/d9sl4b

My method returnPercentage is meant to give the percent.
 
Physics news on Phys.org
Java:
public String returnPercentage() {
        int allScores = this.examScores.size();
        double acceptanceRate = 100*(this.accepted/allScores);  <<--- here
        this.acceptedPercent = acceptanceRate;
        System.out.println();
        return "Acceptance percentage: " + this.acceptedPercent;
What kind of division is going on in this expression? -- this.accepted/allScores
 
  • Like
Likes   Reactions: Arnoldjavs3
Mark44 said:
Java:
public String returnPercentage() {
        int allScores = this.examScores.size();
        double acceptanceRate = 100*(this.accepted/allScores);  <<--- here
        this.acceptedPercent = acceptanceRate;
        System.out.println();
        return "Acceptance percentage: " + this.acceptedPercent;
What kind of division is going on in this expression? -- this.accepted/allScores
Well that is embarrassing!
 
Arnoldjavs3 said:
Well that is embarrassing!
You're not the first to make that mistake, by a long shot.
 
  • Like
Likes   Reactions: Arnoldjavs3

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K