Click here to Skip to main content
Sign Up to vote bad
good
See more: Java
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):
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 31 Aug '12 - 4:11
Edited 31 Aug '12 - 7:04

Comments
Wes Aday - 31 Aug '12 - 10:42
And no it's not right. You are not displaying the grade anywhere. But if you ran it, you would already know that.

3 solutions

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!
  Permalink  
Comments
lewax00 - 31 Aug '12 - 11:51
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.
  Permalink  
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..
        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..
  Permalink  
Comments
lewax00 - 31 Aug '12 - 13:17
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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 263
1 Sergey Alexandrovich Kryukov 254
2 Maciej Los 161
3 Tadit Dash 110
4 Richard MacCutchan 95
0 Sergey Alexandrovich Kryukov 10,264
1 OriginalGriff 7,917
2 CPallini 4,181
3 Rohan Leuva 3,522
4 Maciej Los 3,125


Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 31 Aug 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid