-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarksCalculator.java
More file actions
72 lines (60 loc) · 2.24 KB
/
MarksCalculator.java
File metadata and controls
72 lines (60 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.util.ArrayList;
import java.util.Scanner;
class Student {
private ArrayList<Double> marks;
private int totalSubjects;
public Student(int totalSubjects) {
this.totalSubjects = totalSubjects;
this.marks = new ArrayList<>();
}
public void addMark(double mark) {
marks.add(mark);
}
public double getTotalMarks() {
return marks.stream().mapToDouble(Double::doubleValue).sum();
}
public double getAveragePercentage() {
return getTotalMarks() / totalSubjects;
}
public String getGrade() {
double average = getAveragePercentage();
if (average >= 90) return "A+";
if (average >= 80) return "A";
if (average >= 70) return "B+";
if (average >= 60) return "B";
if (average >= 50) return "C+";
if (average >= 40) return "C";
return "F";
}
}
public class MarksCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Enter number of subjects: ");
int totalSubjects = scanner.nextInt();
if (totalSubjects <= 0) {
System.out.println("Subjects must be greater than zero.");
return;
}
Student student = new Student(totalSubjects);
for (int i = 1; i <= totalSubjects; i++) {
System.out.print("Enter marks for subject " + i + ": ");
double mark = scanner.nextDouble();
if (mark < 0 || mark > 100) {
System.out.println("Marks must be between 0 and 100.");
return;
}
student.addMark(mark);
}
System.out.println("\nResults:");
System.out.printf("Total Marks: %.2f\n", student.getTotalMarks());
System.out.printf("Average Percentage: %.2f%%\n", student.getAveragePercentage());
System.out.println("Grade: " + student.getGrade());
} catch (Exception e) {
System.out.println("Invalid input. Please enter numbers only.");
} finally {
scanner.close();
}
}
}