package com.
java;
import java.util.Scanner;
class Student {
private String studentId;
private String studentName;
private double[] marks;
Student(String studentId, String studentName, double[] marks) {
this.studentId = studentId;
this.studentName = studentName;
this.marks = marks;
}
public String getStudentId() {
return studentId;
}
public String getStudentName() {
return studentName;
}
public double[] getMarks() {
return marks;
}
public void getDetails() {
double average = calculateAverage();
System.out.println("Student ID: " + studentId);
System.out.println("Student Name: " + studentName);
System.out.println("Marks:");
for (int i = 0; i < marks.length; i++) {
System.out.println(" Mark " + (i + 1) + ": " + marks[i]);
}
if (average >= 60) {
System.out.println("First class");
} else if (average >= 50) {
System.out.println("Second class");
} else {
System.out.println("Fail");
}
}
private double calculateAverage() {
double sum = 0;
for (double mark : marks) {
sum += mark;
}
return sum / marks.length;
}
}
class Students {
protected Student[] studentArray = new Student[5];
protected int n = 0;
public void addStudents(String studentId, String studentName, double[] marks) {
Student student = new Student(studentId, studentName, marks);
studentArray[n] = student;
n++;
}
public void getById(String studentId) {
for (int i = 0; i < n; i++) {
if (studentArray[i].getStudentId().equals(studentId)) {
studentArray[i].getDetails();
return;
}
}
System.out.println("Student not found with ID: " + studentId);
}
public void getFirstClassDet() {
System.out.println("Students with First Class:");
for (int i = 0; i < n; i++) {
if (calculateAverage(studentArray[i].getMarks()) >= 60) {
studentArray[i].getDetails();
System.out.println();
}
}
}
private double calculateAverage(double[] marks) {
double sum = 0;
for (double mark : marks) {
sum += mark;
}
return sum / marks.length;
}
public void getByChar(char firstChar) {
System.out.println("Students whose name starts with '" + firstChar +
"':");
for (int i = 0; i < n; i++) {
if (studentArray[i].getStudentName().charAt(0) == firstChar) {
System.out.println(studentArray[i].getStudentName());
}
}
}
public static void main(String[] args) {
Students student = new Students();
Scanner sc = new Scanner(System.in);
String id,name;
double[] marks = new double[4];
for(int i=0;i<5;i++) {
System.out.println("Enter StudentId:");
id=sc.next();
System.out.println("Enter StudentName:");
name=sc.next();
System.out.println("Enter Student Marks: ");
for(int j=0;j<4;j++){
marks[j]=sc.nextDouble();
}
student.addStudents(id,name,marks);
}
System.out.println("Student Details:");
for (int i = 0; i < student.n; i++) {
student.studentArray[i].getDetails();
System.out.println();
}
System.out.println("Student by ID:");
student.getById("2");
System.out.println();
student.getFirstClassDet();
System.out.println();
student.getByChar('S');
}
}