import java.util.Scanner;
public class StudentMarks {
double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks = totalMarks; } public double getTotalMarks() { return totalMarks; } public void setGrade(String grade) { this.grade = grade; } public String getGrade() { return grade; } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter number of students: "); int num = input.nextInt(); StudentMarks data[] = new StudentMarks[num]; for (int i = 0; i < data.length; i++) { System.out.println("Enter marks"); double marks = input.nextDouble(); data[i] = new StudentMarks(); data[i].setTotalMarks(marks); if (marks < 40) { data[i].setGrade("E"); } if (marks >= 40 && marks <= 44) { data[i].setGrade("D"); } if (marks >= 45 && marks <= 49) { data[i].setGrade("D+"); } if (marks >= 50 && marks <= 54) { data[i].setGrade("C-"); } if (marks >= 55 && marks <= 59) { data[i].setGrade("C"); } if (marks >= 60 && marks <= 64) { data[i].setGrade("C+"); } if (marks >= 65 && marks <= 69) { data[i].setGrade("B-"); } if (marks >= 70 && marks <= 74) { data[i].setGrade("B"); } if (marks >= 75 && marks <= 79) { data[i].setGrade("B+"); } if (marks >= 80 && marks <= 84) { data[i].setGrade("A"); } if (marks >= 85 && marks <= 100) data[i].setGrade("A+"); } int count1 = 0; int count2 = 0; int count3 = 0; int count4 = 0; for (int i = 0; i < num; i++) { StudentMarks show = data[i]; String g = show.getGrade(); if (g.equals("A+")) { count1++; } if (g.equals("A")) { count2++; } if (g.equals("D")) { count3++; } if (g.equals("E")) { count4++; } } System.out.println("Number of student getting A and A+ grade: " + (count1 + count2)); System.out.println("Number of student getting D and E grade: " + (count3 + count4)); }
}
Explanation:
In the code, the user is allowed to enter the number of students appearing for the exams and enter their marks one by one. These marks is then checked for the grade. There is an scale to get the grade according to that scale. The grades is to be stored for the particular marks with setGrade() method. The four counters is initialized in order to count the number of students getting A, A+, D and E grade. To count the number of students getting A and A+, the counter values of A and A+ is added. Similarly, the counter values of D and E is added in order to find out the number of students getting A and E marks.
Ads