1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Why is my program not returning a percent?

  1. Nov 20, 2016 #1
    1. The problem statement, all variables and given/known data
    Code (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("*");
            }
       
        }
     
    }
     
    2. Relevant equations


    3. 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.
     
  2. jcsd
  3. Nov 20, 2016 #2

    Mark44

    Staff: Mentor

    Code (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
     
  4. Nov 20, 2016 #3
    Well that is embarrassing!
     
  5. Nov 20, 2016 #4

    Mark44

    Staff: Mentor

    You're not the first to make that mistake, by a long shot.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Why is my program not returning a percent?
  1. Help with my program (Replies: 9)

Loading...