Contents
Grade Calculation Solution
Rita is working as a science teacher in an International school. She is the Class Teacher of class V and was busy in calculating the grade for each student in her class, based on his/her total marks obtained in SA1 assessment.
Since she found it very difficult to calculate the grade, she approached you to develop an application which can be used for completing her task faster. You need to implement a java program using thread to calculate the grade for each student. Student details should be obtained from the user in the console.
Requirement 1: Calculate the grade for each student.
Calculate the grade based on total marks (sum of all marks) as shown below obtained by each student and set the same in result attribute for respective student.
Total Marks | Grade |
---|---|
400 to 500 | A |
300 to 399 | B |
200 to 299 | C |
Less than 200 | E |
Assumption:
Each student will have only five subjects and marks of each subject will be greater than or equal to 0 and lesser than or equal to 100. Hence the maximum Total marks obtained by each student will be 500. And the minimum Total marks obtained by each student will be 0.
Component Specification: GradeCalculator (Thread Class)
Component Name | Type(Class) | Attributes | Methods | Responsibilities |
---|---|---|---|---|
GradeCalculator | String studName char result int[] marks | Include getters and setter method for all the attributes. Include a two argument constructor in the given order – studName and marks. | Set the values for all the attributes via constructor. | |
calculate the grade for each student | GradeCalculator | public void run() | Calculate the grade based on total marks and set the same to result attribute. |
- The class and methods should be declared as public and all the attributes should be declared as private.
- Create a class called Main with the main method and get the inputs like number of threads and Student details from the user.
- The student details will be in the form of String in the following format studName:mark1:mark2:mark3:mark4:mark5.
- Parse the student details and set the values of studName and marks attributes in GradeCalculator thread class using constructor.
- Invoke the GradeCalculator thread class to calculate the grade based on total marks and set the same to result attribute.
- Display the Student name and Grade obtained by each student as shown in the sample input and output.
SOLUTION
Program: Grade Calculation Solution GradeCalculator.java
public class GradeCalculator extends Thread {
private String studName;
private char result;
private int[] marks;
public GradeCalculator(String studName, int[] marks) {
this.studName = studName;
this.marks = marks;
}
public String getStudName() {
return studName;
}
public void setStudName(String studName) {
this.studName = studName;
}
public char getResult() {
return result;
}
public void setResult(char result) {
this.result = result;
}
public int[] getMarks() {
return marks;
}
public void setMarks(int[] marks) {
this.marks = marks;
}
@Override
public void run() {
int totalMarks = 0;
for (int mark : marks) {
totalMarks += mark;
}
if (totalMarks <= 500 && totalMarks >= 400) {
result = 'A';
} else if (totalMarks < 400 && totalMarks >= 300) {
result = 'B';
} else if (totalMarks < 300 && totalMarks >= 200) {
result = 'C';
} else if (totalMarks < 200 && totalMarks >= 0) {
result = 'E';
}
}
}
Program: Grade Calculation Solution Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of Threads:");
int n = scanner.nextInt();
GradeCalculator[] gradeCalculators = new GradeCalculator[n];
Thread[] threads = new Thread[n];
for (int i = 0; i < n; ++i) {
System.out.println("Enter the String:");
String string = scanner.next();
String[] strings = string.split(":");
int[] marks = new int[5];
String studName = strings[0];
for (int j = 1; j < 6; ++j) {
marks[j - 1] = Integer.parseInt(strings[j]);
}
gradeCalculators[i] = new GradeCalculator(studName, marks);
threads[i] = new Thread(gradeCalculators[i]);
threads[i].start();
threads[i].interrupt();
}
for (int i = 0; i < n; ++i) {
System.out.println(gradeCalculators[i].getStudName() + ":" + gradeCalculators[i].getResult());
}
}
}
Related: