Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Please, find my mistake

Hey guys, this is the assignment that I should do today:

Write a program to read a list of exam scores given as integer percentages in the range 0 to 100. Display the total number of grades and the number of grades in each letter-grade category as follows: 90-100 is A, 80-89 is B, 70-79 is C, 60-69 is D, and 0-59 is F. Prin the percentage of the total for each letter grade, the lowest, the highest, and average grade. Use negative score as a snetinel value to indicate the end of the input.

For Example, input: 98,87,86,85,85,78,73,72,72,70,66,63,50,-1

Output should be:

Total number pf grades entered was 13

A's =1
B's=4
C's=5
D's=2
F's=1

The % of A's = 7.69&
The % of B's = 30.77%
The % of C's = 38.46%
The % of D's = 15.38%
The % of F's = 7.69&

avg = 75.77
smallest = 50
largest = 98

2. ## Re: Please, find my mistake

Here is my code:

import java.util.Scanner;
{
public static void main(String[] args)
{

int numberOFGrades = 0;
double smallest=0, largest=0, average, sum=0;
int A=0, B=0,C=0,D=0,F=0;
double percentA, percentB, percentC, percentD, percentF;

Scanner scan = new Scanner(System.in);

{

{

A++; // the total of A's

}

{

B++; // the total of B's

}
{

C++; // the total of C's

}
{

D++; // the total of D's
}
{

F++; // the total of F's

}

{
numberOFGrades++; // If the grade is negative, It will show the total of grades that was entered

}

}

//the quantity of each grade
System.out.println();
System.out.println("Total of grades entered was " + numberOFGrades);
System.out.println();
System.out.println("Total of A's = " +A);
System.out.println("Total of B's = " +B);
System.out.println("Total of C's = " +C);
System.out.println("Total of D's = " +D);
System.out.println("Total of F's = " +F);

System.out.println("The percent of A's = " + percentA + "%");
System.out.println("The percent of B's = " + percentB + "%");
System.out.println("The percent of C's = " + percentC + "%");
System.out.println("The percent of D's = " +percentD + "%");
System.out.println("The percent of F's = " + percentF + "%");

sum = A + B + C + D + F;
System.out.println("the average is " + average );
System.out.println("the smallest number is " + smallest );
System.out.println("the largest number is " + largest );

}
}

3. ## Re: Please, find my mistake

when I input different grades, the percentage is always 0, but when I input the same grade, this grade just shows 100%

Also, I don't know how to find average, and this stuff

4. ## Re: Please, find my mistake

This won't work if grade is 0 exactly.

is better.

This probably isn't going to be code that you can use directly, but it might help

(Also, are you allowing extra credit? i.e. what if someone got a 105?)

Assuming no extra credit,

letter = 'A';
else if (grade >=80 && grade <=89)
letter = 'B';
else if (grade >=70 && grade <=79)
letter = 'C';
else if (grade >=60 && grade <=69)
letter = 'D';
else if (grade >=0 && grade <=59)
letter = 'F';
else
letter = '?';

That is the result of an annoying thing in java,

for instance

double value3 = (11/5) * 100;

will return 200, not 250.

One way to make it be a double

double value3 = (double)(11)/(double)(5) * 100;

value3 is now 220.00000000000003;

I think you can use the DecimalFormat class to fix it to be two digits after the decimal.

5. ## The Following User Says Thank You to javapenguin For This Useful Post:

Daler (June 19th, 2012)

6. ## Re: Please, find my mistake

I will try it now, and if it works I will let you know!

7. ## Re: Please, find my mistake

Oh God! It works... so we have to convert integers to doubles first! Wow! Thank you so much...

Do you know how to find smallest, average, and largest grade ? Please, let me know... I am just a beginner, I am really sorry about that

8. ## Re: Please, find my mistake

Is grade meant to be entered as a double, i.e. a decimal, or as an integer?

if it's a double, then a better way of putting that would be

9. ## The Following User Says Thank You to javapenguin For This Useful Post:

Daler (June 19th, 2012)

10. ## Re: Please, find my mistake

Write a program to read a list of exam scores given as integer percentages in the range 0 to 100

11. ## Re: Please, find my mistake

It's a bit confusing as to what is what.

grade should probably be an int. You're using it as an int everywhere else.

Also, what if the input were all 100's, then you wouldn't have any smallest as it is only checking for between 1 and 59 (unless you fixed it to check for 0 too.)

so if all numbers weren't equal but were all passing, then you'd have no smallest.

Assuming there is one grade only, then that one grade would be both smallest and largest, right? (And average too I think.)

With only one grade read in, that sole grade would automatically be the largest and the smallest, right?

Any subsequent, if there are any, grades would have to be either smaller, larger, or the same as the first one.

Grade should be of type int then.

Since grade is of type int, smallest and largest should be too. Average might not be (it could be a decimal in theory perhaps.)

Smallest should be set to something large. If you initialize it to 0, then unless somebody gets a -1, which you said they can't, that will be the smallest, even if the actual smallest grade happens to be a 100.

Try making smallest 101 to start out with.

0.) If it happens that your first number entered is less than 0 or greater than 100, than you'll have to change some things in your output as your only grade value will be invalid. Smallest will be 101, (101 was chosen because you could just put an if statement like

if (smallest == 101)
to check to see if your first entered grade happened to be an invalid one. If it was 100, then if you actually had all 100's then it would act like your first value was invalid even though it wasn't. ) If the first value is not invalid, then
1.) Read in first value. Make that smallest and largest.
2.) For each new value, if larger than largest, make that the new largest, if smaller than smallest, make that the new smallest.

As for average, if you mean the average of all the scores, then simply add then all together every time you read in one and then at the end divide by the total number of (valid) grades.

Also, your
if (grade > 0) block isn't necessary.

If your grade is less than 0, it will fail the while loop and not even go to that part. Simply increase the gradeCount every time a new grade is read in.

12. ## Re: Please, find my mistake

I followed all your steps, and everything works except "the smallest".. it's still becomes the negative number... what the problem?

13. ## Re: Please, find my mistake

Thanks a lot for your help! I have already written the program and it actually worked.. I used the DecimalFormat class as you said before!

Thanks a lot! I really appreciate it...

So how to make it as "solved"?