Programming langauge is JAVA: a) Write a method char getGrade(int score, int max
ID: 3830756 • Letter: P
Question
Programming langauge is JAVA:
a) Write a method char getGrade(int score, int maxPoints) that returns the letter grade corresponding to an e_x_a_m score.
The grading scale is determined by maxPoints.
A score 0.9*maxPoints and <= maxPoints is an ‘A’,
a score 0.8*maxPoints < 0.9*maxPoints is a ‘B’
and so on down to ‘F’ which is any nonnegative score < 0.6*maxPoints.
A score < 0 or > maxPoints gets a grade of ‘X’.
b) Write a method char getGrade(int score) which is like in Part a but using a default value of 10 for maxPoints. Use a switch statement this time, rather than if-else.
Explanation / Answer
a)
char getGrade(int score, int maxPoints)
{
char grade;
if (score < 0 || score > maxPoints)
return 'X';
if (score >= 0.9*maxPoints && score <= maxPoints)
{
grade = 'A';
}
else if (score >= 0.8*maxPoints && score < 0.9*maxPoints)
{
grade = 'B';
}
else if (score >= 0.7*maxPoints && score < 0.8*maxPoints)
{
grade = 'C';
}
else if (score >= 0.6*maxPoints && score < 0.7*maxPoints)
{
grade = 'D';
}
else
{
grade = 'F';
}
return grade;
}
b)
char getGrade(int score)
{
char grade = ' ';
switch(score)
{
case 10:
case 9:
grade = 'A';
break;
case 8:
grade = 'B';
break;
case 7:
grade = 'C';
break;
case 6:
grade = 'D';
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
grade = 'F';
break;
default:
grade= 'X';
break;
}
return grade;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.