Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:11am
Edited 31-Aug-12 7:04am
v5
Comments
Wes Aday at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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  
v2
Comments
lewax00 at 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)

  Print Answers RSS
0 CPallini 385
1 Sergey Alexandrovich Kryukov 262
2 OriginalGriff 200
3 George Jonsson 134
4 Richard MacCutchan 110
0 OriginalGriff 6,344
1 Sergey Alexandrovich Kryukov 5,890
2 CPallini 5,175
3 George Jonsson 3,559
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 31 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100