Repository navigation
Expand file tree
/
Copy path23-StudentResult.java
More file actions
60 lines (53 loc) · 1.58 KB
/
Copy path23-StudentResult.java
File metadata and controls
60 lines (53 loc) · 1.58 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
import java.util.Scanner;
class StudentResult {
private String name;
private int[] marks;
public StudentResult(String name, int[] marks) {
this.name = name;
this.marks = marks;
}
public int calculateTotal() {
int total = 0;
for (int mark : marks) {
total += mark;
}
return total;
}
public double calculatePercentage() {
return (double) calculateTotal() / marks.length;
}
public char calculateGrade() {
double percentage = calculatePercentage();
if (percentage >= 90) {
return 'A';
}
if (percentage >= 75) {
return 'B';
}
if (percentage >= 60) {
return 'C';
}
if (percentage >= 40) {
return 'D';
}
return 'F';
}
public void displayResult() {
System.out.println("Name: " + name);
System.out.println("Total: " + calculateTotal());
System.out.printf("Percentage: %.2f%n", calculatePercentage());
System.out.println("Grade: " + calculateGrade());
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter student name: ");
String name = scanner.nextLine();
int[] marks = new int[5];
for (int i = 0; i < marks.length; i++) {
System.out.print("Enter marks for subject " + (i + 1) + ": ");
marks[i] = scanner.nextInt();
}
new StudentResult(name, marks).displayResult();
scanner.close();
}
}