I am taking an intro to java programming class, and I have never done programming before, ever. I'm in a little over my head and my book sucks. My teacher just gave us an assignment after not giving us anything for a month. We need to do the following:
Write a Java program that processes students’ grades.
The program will:
Repeatedly prompt the user to enter new score or enter -1 to exit
Assign a grade from F to A to this student and print both the score and
the grade in table with an appropriate header in an output file. The
exact form of the table is left for you to choose
Count how many A’s, B’s, etc in the class
Calculate the largest and the lowest score of every grade. For example
the highest A might be 98 and the lowest is 91 and so on
Calculate the average score of every grade
Calculate the highest and lowest score in the entire class
After printing the above mentioned table, report all cumulative results
in the output file with appropriate messages. E.G.
o There were 5 A students in class
o The highest A in class was 99
An example output is given next page.
Your program should:
Maintain good indentations that make it readable.
Include enough comments to demonstrate your understanding of the
different parts of the program and what their functions are.
Store the output in file called grades.txt
Be able to deal with the total absence of one or more grades. For
example, in some cases there are no A’s in class
Make sure that the program is your own work. You may be required to have
a discussion about your program with Dr. Ali before your grade for this
homework is finalized.
We have to make a table that looks like this and have the information below it as well.
Score Grade
20 F
98 A
91 A
87 B
85 B
89 B
90 A
62 D
30 F
99 A
There are 4 A student(s) in class
The highest A is 99
The lowest A is 90
The average A is 94.5
++++++++++++++
There are 3 B student(s) in class
The highest B is 89
The lowest B is 85
The average B is 87
++++++++++++++
There are no C students this time
++++++++++++++
There are 1 D student(s) in class
The highest D is 62
The lowest D is 62
The average D is 62
++++++++++++++
There are 2 F student(s) in class
The highest F is 30
The lowest F is 20
The average F is 25
++++++++++++++
To top it all off, it has to print out to a file. I have some of it figured out. I can't figure out the table. Right now the first number I enter is completely ignored and then it doesn't let me enter in any other numbers after the second number is entered and then the rest of the information is wrong.
Here is my (working) code:
And this is what it gives me:// Programming Assingment Grades Grades.java // grades class uses switch statement to counter letter grades. import java.util.Scanner; // program uses class Scanner public class Grades { public static void main( String[] args ) { int total = 0; //sum of grades int gradeCounter = 0; // number of grades entered int aCount = 0; // count of A grades int aGrade = 0; // sum of A grades int aMax = 0; // max of A grade int aMin = 0; // min of A grade int bCount = 0; // count of B grades int bGrade = 0; // sum of B grades int cCount = 0; // count of C grades int cGrade = 0; // sum of C grades int dCount = 0; // count of D grades int dGrade = 0; // sum of D grades int fCount = 0; // count of F grades int fGrade = 0; // sum of F grades int grade; // grade entered by user Scanner input = new Scanner( System.in ); System.out.printf( "%s\n%s\n", "Enter the students grade 0-100", "Type -1 when finished" ); // enter grades and terminate when done grade = input.nextInt(); while ( grade != -1 ) { grade = input.nextInt(); total += grade; // add grade to total ++gradeCounter; // increment number of grades // letter-grade counter if ( grade >= 90 && grade <= 100 ) { grade = 'A'; ++aCount; // increment aCount aGrade += grade; // sum A grades } else if ( grade >= 80 ) { grade = 'B'; ++bCount; // increment cCount bGrade += grade; // sum B grades } else if ( grade >= 70 ) { grade = 'C'; ++cCount; // increment cCount cGrade += grade; // sum C grades } else if ( grade >= 60 ) { grade = 'D'; ++dCount; // increment dCount dGrade += grade; // sum D grades } else { grade = 'F'; ++fCount; // increment fCount fGrade += grade; // sum F grades } { // display grade table System.out.println("----------------------"); System.out.println("| Score |Grade |"); System.out.println("----------------------"); for ( int g = grade; g <= gradeCounter; grade++) System.out.printf("| ", grade); if ( aCount != 0 ) { int avgA; // A Average avgA = aGrade / aCount; System.out.printf("There are %d A student(s) in class \n", aCount); // aCount System.out.printf("The Average A is %d\n", avgA); } else { System.out.printf("There are no A students this time \n"); } System.out.print("++++++++++++++ \n"); if ( bCount != 0 ) { int avgB; // B Average avgB = bGrade / bCount; System.out.printf("There are %d B student(s) in class \n", bCount); // bCount System.out.printf("The Average B is %d\n", avgB); } else { System.out.printf("There are no B students this time \n"); } System.out.print("++++++++++++++ \n"); if ( cCount != 0 ) { int avgC; // C Average avgC = cGrade / cCount; System.out.printf("There are %d C student(s) in class \n", cCount); // cCount System.out.printf("The Average C is %d\n", avgC); } else { System.out.printf("There are no C students this time \n"); } System.out.print("++++++++++++++ \n"); if ( dCount != 0 ) { int avgD; // D Average avgD = dGrade / dCount; System.out.printf("There are %d D student(s) in class \n", dCount); // dCount System.out.printf("The Average D is %d\n", avgD); } else { System.out.printf("There are no D students this time \n"); } System.out.print("++++++++++++++ \n"); if ( fCount != 0 ) { int avgF; // F Average avgF = fGrade / fCount; System.out.printf("There are %d F student(s) in class \n", fCount); // fCount System.out.printf("The Average F is %d\n", avgF); } else { System.out.printf("There are no F students this time \n"); } System.out.print("++++++++++++++ \n"); } } } }
c:\Java>java Grades
Enter the students grade 0-100
Type -1 when finished
1
2
----------------------
| Score |Grade |
----------------------
There are no A students this time
++++++++++++++
There are no B students this time
++++++++++++++
There are no C students this time
++++++++++++++
There are no D students this time
++++++++++++++
There are 1 F student(s) in class
The Average F is 70
++++++++++++++
First I tried to do this with a switch statement but after asking for help all my teacher really said was that I should be using an if/else statement, not a switch statement. I'm super frustrated and really need help.