Create a program that takes in a student’s full names and marks for 3 In-class exercises, then calculate the average of this marks. This average mark is then categorized as follows: Marks obtained Grade Level Message 80 – 100 1 Magnificent!! 70 – 79 2 Excellent!! 60 – 69 3 Good work!! 50 – 59 4 Good!! 2 DUE DATE: MONDAY 06 OCTOBER 2021 BEFORE 23h59 0 – 49 0 Fail – Try again next Year!! Greater than 100 or less than 0 X Invalid Marks!!, Marks too high. Or Invalid Marks!!, Negative marks not allowed.
Sample run 1:
Enter student full names: Goodluck Johnathan
Enter 3 marks separated by spaces: 78 94.6 88
Output: Student name: Goodluck Johnathan
Grade Level: 1
Comment: Magnificent!!
Sample run 2:
Enter student full names: Ellen Johnson Sirleaf
Enter 3 marks separated by spaces: 200 104.87 99.9
Output: Student name: Ellen Johnson Sirleaf
Grade Level: X
Comment: Invalid Marks!!, Marks too high.
import java.util.Scanner;
public class StudentGrades {
public static int determineGradeLevel(double averageMark)
{
if(averageMark>=80 && averageMark<=100)
return 1;
if(averageMark>=70 && averageMark<80)
return 2;
if(averageMark>=60 && averageMark<70)
return 3;
if(averageMark>=50 && averageMark<60)
return 4;
return 0;
}
public static String getGradeComment(double gradeLevel)
{
if(gradeLevel == 1)
return "Magnificent";
if(gradeLevel == 2)
return "Excellent";
if(gradeLevel == 3)
return "Good work";
if(gradeLevel == 4)
return "Good";
return "Fail - Try again next Year";
}
public static boolean isValidMark(double mark)
{
return mark >= 0 && mark <= 100;
}
public static boolean isValidMarks(double firstMark, double secondMark, double thirdMark)
{
return isValidMark(firstMark) && isValidMark(secondMark) && isValidMark(thirdMark);
}
public static void main(String[] args)
{
Scanner userInput = new Scanner(System.in);
System.out.print("Enter student fullname: ");
String studentName = userInput.nextLine();
System.out.print("Enter 3 marks separated by spaces: ");
double firstMark = userInput.nextDouble();
double secondMark = userInput.nextDouble();
double thirdMark = userInput.nextDouble();
System.out.println();
System.out.println("Output:");
System.out.println("Student name: " + studentName);
if(isValidMarks(firstMark, secondMark, thirdMark)) {
double averageMark = (firstMark + secondMark + thirdMark) / 3;
int gradeLevel = determineGradeLevel(averageMark);
String commentForGrade = getGradeComment(gradeLevel);
System.out.println("Grade Level: " + gradeLevel);
System.out.println("Comment: " + commentForGrade + "!!");
}
else
{
System.out.println("Grade Level: "+"X");
System.out.print("Comment: "+ "Invalid marks!!");
if(firstMark > 100 || secondMark > 100 || thirdMark > 100)
System.out.print("Marks too high.");
else
System.out.println("Marks too low.");
}
}
}
Comments
Leave a comment