• June 19th, 2012, 05:35 PM
Daler
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
• June 19th, 2012, 05:37 PM
Daler
Here is my code:

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

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

}

}

System.out.println();
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 );

}
}
• June 19th, 2012, 05:38 PM
Daler
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
• June 19th, 2012, 06:20 PM
javapenguin

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';
letter = 'B';
letter = 'C';
letter = 'D';
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.
• June 19th, 2012, 06:33 PM
Daler

I will try it now, and if it works I will let you know!
• June 19th, 2012, 06:41 PM
Daler
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
• June 19th, 2012, 06:46 PM
javapenguin
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
• June 19th, 2012, 06:49 PM
Daler
Write a program to read a list of exam scores given as integer percentages in the range 0 to 100
• June 19th, 2012, 07:10 PM
javapenguin
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.
• June 19th, 2012, 10:24 PM
Daler