Why is my program not returning a percent?

In summary: It's division by integer (allScores) and integer division truncates the fractional part. So, if your accepted percentage was 80%, it would be truncated to 0.
  • #1
Arnoldjavs3
191
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
  • #2
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 Arnoldjavs3
  • #3
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!
 
  • #4
Arnoldjavs3 said:
Well that is embarrassing!
You're not the first to make that mistake, by a long shot.
 
  • Like
Likes Arnoldjavs3

1. Why is my program not returning a percent?

There could be several reasons why your program is not returning a percent. Some possible explanations include:

  • Your program may not be properly calculating or formatting the percentage value.
  • There may be a logical error in your code that is preventing the correct calculation of the percentage.
  • The input data may be incorrect or incomplete, leading to an inaccurate percentage result.
  • There may be an issue with the programming language or framework you are using that is causing the error.
  • It is also possible that there is a bug or glitch in your code that is causing the program to not return a percent.

If you are having trouble identifying the specific issue, try debugging your code or seeking assistance from a colleague or online community.

2. How do I know if my program is returning the correct percentage?

To ensure that your program is returning the correct percentage, you can follow these steps:

  • Check the calculations and logic in your code to make sure they are accurate.
  • Test your program with different input values to see if the percentage changes accordingly.
  • Compare the results of your program with manual calculations or with results from a trusted source.
  • Use debugging tools or print statements to track the flow of your program and identify any errors.

If you are still unsure about the accuracy of your program, seek feedback from other programmers or consult online resources for assistance.

3. Can the format of my input data affect the program's percentage calculation?

Yes, the format of your input data can definitely impact the percentage calculation in your program. Some common issues with input data that can affect percentage calculation include:

  • Missing or incorrect decimal points or commas in numerical values.
  • Inconsistent or incorrect units of measurement.
  • Empty or null values.
  • Incorrect data types or formatting.
  • Improperly formatted strings or characters.

Make sure to carefully review and validate your input data to ensure that it is in the correct format before using it in your program's calculations.

4. I am using the correct formula for calculating percentages, but my program is still not returning the expected result. Why?

There are a few possible reasons why your program may not be returning the expected result even if you are using the correct percentage formula. Some potential explanations include:

  • Your formula may be missing a step or using incorrect mathematical operators.
  • The variables or data used in the formula may be incorrect or missing.
  • There could be a logical error or typo in your code that is causing the incorrect result.
  • The formula you are using may not be appropriate for the specific problem or situation.
  • The input data may not be accurate or complete, leading to an incorrect result.

Double-check your formula and code, and make sure to thoroughly test and validate your program with different input values to identify and fix any potential issues.

5. How can I fix my program if it is not returning a percentage at all?

If your program is not returning a percentage at all, it is likely that there is an error or bug in your code that is preventing the calculation from being completed. Some steps you can take to fix this issue include:

  • Check for any syntax errors or typos in your code and fix them.
  • Use debugging tools or print statements to track the flow of your program and identify the source of the error.
  • Verify that all necessary variables and data are present and correctly formatted.
  • If you are using a function or method to calculate the percentage, make sure it is correctly defined and called.
  • Consult online resources or seek assistance from other programmers if you are having trouble identifying and fixing the issue.

By carefully reviewing and debugging your code, you should be able to fix the problem and get your program to return a percentage successfully.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
Back
Top