1.11M Members

Java Beginner Grading Program

 
0
 

I'm a beginning programmer and am stumped on the last part of this assignment. I can't seem to figure out how to have the Letter Grade show. Here's what I have so far, any help as to where I'm going wrong would be much appreciated!!

import java.util.Scanner;

public class Lab1 {
public static void main (String args []){
    Scanner input = new Scanner(System.in);
    System.out.println("Enter Student's Name ");
    String studentName = input.nextLine();
    System.out.println("Please Enter your Lab Grade, Participation Grade, Test 1 Grade, Test 2 Grade, and Final Exam Grade");
    int Labs = input.nextInt();
    if (Labs > 56)
        System.out.println("Error: Your Labs Grade " + Labs + " is too high!");
    int Participation = input.nextInt();
    if (Participation > 4)
        System.out.println("Error: Your Participation Grade " + Participation + " is too high!");
    int Test1 = input.nextInt();
    if (Test1 > 12)
        System.out.println("Error: Your Test 1 Grade " + Test1 + " is too high!");
    int Test2 = input.nextInt();
    if (Test2 > 12)
        System.out.println("Error: Your Test 2 Grade " + Test2 + " is too high!");
    int FinalExam = input.nextInt();
    if (FinalExam > 16)
        System.out.println("Error: Your Final Exam Grade " + FinalExam + " is too high!");
    int  FinalGrade = (Labs + Participation + Test1 + Test2 + FinalExam);
    if (FinalGrade > 100)
        System.out.println("Error: Your Final Grade " + FinalGrade + " is too high!");
    char LetterGrade;
    if (FinalGrade >= 97)
        LetterGrade = 'A';
    System.out.println("The final grade for " + studentName + "is an " + LetterGrade + "+");
    if (FinalGrade >= 93)
        LetterGrade = 'A';
    System.out.println("The final grade for " + studentName + "is an " + LetterGrade);
    if (FinalGrade >= 90)
        LetterGrade = 'A';
    System.out.println("The final grade for " + studentName + "is an " + LetterGrade + "-");
    if (FinalGrade >= 87)
        LetterGrade = 'B';
    System.out.println("The final grade for " + studentName + "is a " + LetterGrade + "+");
    if (FinalGrade >= 83)
        LetterGrade = 'B';
    System.out.println("The final grade for " + studentName + "is a " + LetterGrade);
    if (FinalGrade >= 80)
        LetterGrade = 'B';
    System.out.println("The final grade for " + studentName + "is a " + LetterGrade + "-");
    if (FinalGrade >= 77)
        LetterGrade = 'C';
    System.out.println("The final grade for " + studentName + "is a " + LetterGrade + "+");
    if (FinalGrade >= 73)
        LetterGrade = 'C';
    System.out.println("The final grade for " + studentName + "is a " + LetterGrade);
    if (FinalGrade >= 70)
        LetterGrade = 'C';
    System.out.println("The final grade for " + studentName + "is a " + LetterGrade + "-");
    if (FinalGrade >= 60)
        LetterGrade = 'D';
    System.out.println("The final grade for " + studentName + "is a " + LetterGrade);
    if (FinalGrade <= 59)
        LetterGrade = 'F';
    System.out.println("The final grade for " + studentName + "is an " + LetterGrade);
}
}
 
0
 

OK, you have code to show a letter grade. What exactly is wrong with the results it gives you?
Maybe you are getting multiple letter grades? Eg if the grade is 80 then it will pass the if tests on lines 43, 46, 49, 52 and 55. Have a look at "else if" rather than "if" and see how that can help you stop processing subsequent if tests once you have passed one of them.

 
0
 

You're asking for too much information in one go in line 8. Think of the user entering it all.

So many if ... statements. Prime candidate for a case statement. (Have you covered those yet?)
Bear in mind when you use
if xxx
if yyy
if zzz
.. that your program has to evaluate ALL of those possibilities.

Using if xxx
else if yyy
else if zzz,
it only has to check one for a positive and ignore the rest. (Ref: Code style and speed overhead)

I would also get into the habit of using {} brackets to enclose what your if statements are doing, even if it's just a line. It clears up a lot of confusion.

How about also getting the data entered first in one section, then in the next part, do the processing of the grades etc..

 
0
 

Thank you both for your help, I've tried both "if" and "else if". Also, we have yet to cover CASE statements (This is the 2nd Week of class). The issue seems to be that it's telling me that LetterGrade has not been initialized??

 
0
 

One more comment (don't want to overwhelm Greg :D).

There's a LOT of redundancy in the code.

System.out.println("The final grade for " + studentName + "is a " + LetterGrade);

The above is used 11 times!

If you used a String for letterGrade instead of a char, you can store "A+", "A" and "A-" as separate grades, so you would just have one line of output near the bottom.

 
0
 

LetterGrade has not been initialized??

Assign it a value when you define it. Variables defined inside of methods need to be assigned values.

 
0
 

Ok, I changed the variable type from char to String. It's still telling me that:

"The local variable LetterGrade may not have been initialized"

I know this is very redundant, I've been working on this for a while, so I've tried adding onto the code thinking that this may help, it seems as this is an issue of defining LetterGrade though and once I do that it should work!

 
0
 

Assign it a value when you define it. Variables defined inside of methods need to be assigned values.

 
0
 

Thank you all, I have that part figured out.... ONE LAST THING: now when I enter the scores, depending on how high it is, it will display that the grade is also every LetterGrade lower than the correct LetterGrade...

 
0
 

Please post what the program outputs, describe what is wrong with it and show what you want the output to look like.

 
0
 

The output is as follows:

Enter Student's Name
Greg
Please Enter your Lab Grade, Participation Grade, Test 1 Grade, Test 2 Grade, and Final Exam Grade
56 4 12 11 15
The final grade for Greg is an A+
The final grade for Greg is an A
The final grade for Greg is an A
The final grade for Greg is a B+
The final grade for Greg is a B
The final grade for Greg is a B-
The final grade for Greg is a C+
The final grade for Greg is a C
The final grade for Greg is a C-
The final grade for Greg is a D
Total: 98

For a 98 I need it only to say that the final grade is an A+...

 
0
 

Greg, if you follow the logic, this is what you are telling the program to do.

(Assuming a finalGrade = 98)

if (finalGrade >= 97) <---------- yes it is! Assign him A
.
.
.
if (finalGrade >= 73) <---------- yes it is! Assign him C

Try modifying the logic in your code to distinguish between grades. It's catching everything. This is why if ... else if is better.

How about posting your modified code so we can follow you?

(Notice the naming convention for variables as well. First letter small.- then capitalise on the next word if it has two.)

Question Answered as of 1 Year Ago by NormR1, Starstreak and JamesCherrill
 
0
 

Thanks everyone for all your help... I'm sure you haven't heard the last of me! lol

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article