-
Notifications
You must be signed in to change notification settings - Fork 0
/
Student.java
72 lines (58 loc) · 2.13 KB
/
Student.java
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
//Simple Student details program using OOPs
public class Student {
private String name;
private int rollNumber;
private int[] marks;
public Student(String name, int rollNumber, int[] marks) {
this.name = name;
this.rollNumber = rollNumber;
this.marks = marks;
}
public String getName() {
return name;
}
public int getRollNumber() {
return rollNumber;
}
public int[] getMarks() {
return marks;
}
public void setMarks(int[] marks) {
this.marks = marks;
}
//Method for AverageMarks
public double calculateAverage() {
if (marks.length == 0) {
return 0.0;
}
double sum = 0.0;
for (int mark : marks) {
sum += mark;
}
return sum / marks.length;
}
public static void main(String[] args){
Student student1 = new Student("Karen", 100, new int[]{80, 70, 67, 92});
Student student2 = new Student("Agastya", 150, new int[]{40, 60, 45, 57});
//Student 1 details
System.out.println("Student 1:");
System.out.println("Name: " + student1.getName());
System.out.println("Roll Number: " + student1.getRollNumber());
System.out.print("Marks: ");
for (int mark : student1.getMarks()) {
System.out.print(mark + " ");
}
System.out.println();
System.out.println("Average Marks: " + student1.calculateAverage());
// Display details for student2
System.out.println("\nStudent 2:");
System.out.println("Name: " + student2.getName());
System.out.println("Roll Number: " + student2.getRollNumber());
System.out.print("Marks: ");
for (int mark : student2.getMarks()) {
System.out.print(mark + " ");
}
System.out.println();
System.out.println("Average Marks: " + student2.calculateAverage());
}
}