In: Computer Science
Design a program that displays the following in Java:
Enter the grades for an Essay:
Grammer (must be 30 or less):
40
Grammer (must be 30 or less):
26.3
Spelling (must be 20 or less):
17.4
Correct Length (must be 20 or less):
19
Content (must be 30 or less):
23.5
The recorded scores are:
Grammer: 26.3
Spelling: 17.4
Correct Length: 19.0
Content: 23.5
Total score: 86.2
The grade for this essay is B
import java.util.*;
public class Main
{
//function to calculate grade
public static char grade(double score)
{
if(score>=90)
return 'A';
else if(score>=80)
return 'B';
else if(score>=70)
return 'C';
else if(score>=60)
return 'D';
else
return 'F';
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//get user input
System.out.println("Enter the grades for an Essay:
");
double grammerScore, spellingScore,
lengthScore, contentScore, totalScore;
while(true)
{
System.out.println("Grammer (must
be 30 or less): ");
grammerScore =
sc.nextFloat();
if(grammerScore<=30)
break;
}
while(true)
{
System.out.println("Spelling (must
be 20 or less):");
spellingScore =
sc.nextFloat();
if(spellingScore<=20)
break;
}
while(true)
{
System.out.println("Correct Length
(must be 20 or less):");
lengthScore = sc.nextFloat();
if(lengthScore<=20)
break;
}
while(true)
{
System.out.println("Content (must
be 30 or less):");
contentScore =
sc.nextFloat();
if(contentScore<=30)
break;
}
//calculate the total score
totalScore =
grammerScore+spellingScore+lengthScore+contentScore;
//display the output
System.out.println("\nThe recorded
scores are:");
System.out.printf("Grammer:
%.1f\n",grammerScore);
System.out.printf("Spelling:
%.1f\n", spellingScore);
System.out.printf("Correct Length:
%.1f\n", lengthScore);
System.out.printf("Content:
%.1f\n", contentScore);
System.out.printf("Total score:
%.1f\n", grammerScore);
//display the grade
System.out.println("\nThe grade for
this essay is "+grade(totalScore));
}
}
OUTPUT:
Enter the grades for an Essay:
Grammer (must be 30 or less):
40
Grammer (must be 30 or less):
26.3
Spelling (must be 20 or less):
17.4
Correct Length (must be 20 or less):
19
Content (must be 30 or less):
23.5
The recorded scores are:
Grammer: 26.3
Spelling: 17.4
Correct Length: 19.0
Content: 23.5
Total score: 26.3
The grade for this essay is B