Click here to Skip to main content
15,880,854 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
my Assignment:
Write a program that will do the following tasks. Declare and initialize two variables that represent a student's scores on two tests. The grade average should be computed by summing the test scores and dividing by 2. Then, a letter grade should be determined based on the average grade. The following scoring shall be used: A = 90-100, B = 80-89, C = 70-79, D = 65-69, F = 64 or less. The program should display the average test score and the student's letter grade.

My answer (can you check and fix):
Java
public class TestGrades
{
    public static void main (String [] args)
    {
        int gradeone=75;
        int gradetwo=80;
        int testscore= ((gradeone + gradetwo)/2);
        char grade;

        System.out.println("\n" + "your test score is" + testscore);

        if(testscore >= 90 )
  	  grade='A';
		
	elseif(testscore >= 80 );
	  grade='B';

	elseif(testscore >= 70 );
	  grade='C';
		
	elseif(testscore >= 65 );
	  grade='D';

	else
	  grade='F';
    }
}
Posted
Updated 31-Aug-12 7:04am
v5
Comments
[no name] 31-Aug-12 10:42am    
And no it's not right. You are not displaying the grade anywhere. But if you ran it, you would already know that.

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.
Try it yourself, you may find it is not as difficult as you think!
 
Share this answer
 
Comments
lewax00 31-Aug-12 11:51am    
I think this is a fair question, he's made an attempt and wants it verified by people with experience, he's not asking us to write it for him.
You have a couple problems, but if you try to run it these should be pointed out by the compiler.

But, here's a quick rundown of the problems I see:
- else if should be two words, you have no space between them
- There should not be a semicolon at the end of your else if lines
- As mentioned by Wes, you forgot to display the grade

And some tips:
- You shouldn't need to concatenate string literals, you can change "\n" + "your test score is" to just "\nyour test score is"
- You probably want to add a space to the end of that string as well, or you'll get output like "your test score is76" instead of "your test score is 76"
- Even though { and } aren't required for if statements with only one line in the body, use them anyways. If you ever want to add another line later it's a pain to add them then, and most Java IDEs will add the closing brace when you type the opening one.
 
Share this answer
 
I see there are some conditional errors(consider a case,if your testscore is 95 then what will be the grade?because all conditions true for it) in your if else conditions..it will work better just like as it..
Java
    if(testscore >= 90 )
  grade='A';

else if((testscore >= 80 ) && (testscore < 90))
  grade='B';

else if((testscore >= 70 ) && (testscore < 80))
  grade='C';

else if((testscore >= 65 ) && (testscore < 70))
  grade='D';

else
  grade='F';

there are also some syntax errors which is informed you earlier by lewax00..
 
Share this answer
 
v2
Comments
lewax00 31-Aug-12 13:17pm    
That's not necessary, if the grade is 85 it would fails the first condition (>= 90), then the second condition will be checked (>= 80), then it will assign the grade and exit the if-block. Nothing else will be evaluated. Those less than statements are already handled implicitly (e.g. a grade cannot make it to the check for B unless it is less than 90, so that will always evaluate to true in any case the evaluation occurs, because in any case where it is false the evaluation is never made).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900