OneCompiler

Fa22

1651

public class Solution {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    
    // Read the number of students
    int n = sc.nextInt();
    Student[] arr = new Student[n];
    
    // Input details for each student
    for (int i = 0; i < arr.length; i++) {
        int rollNo = sc.nextInt(); sc.nextLine(); // Consuming newline
        String name = sc.nextLine();
        double marks = sc.nextDouble();
        char section = sc.next().charAt(0);
        arr[i] = new Student(rollNo, name, marks, section);
    }

    // Search for a student by roll number
    int searchRollNo = sc.nextInt();
    Student obj = searchStudentByRollNo(arr, searchRollNo);
    System.out.println(obj != null ? obj : "No student found");

    sc.nextLine(); // Consuming newline
    String searchName = sc.nextLine();

    // Search for a student by name
    Student obj1 = searchStudentByName(arr, searchName);
    System.out.println(obj1 != null ? obj1 : "No student found");

    // Find the student with the highest marks
    Student obj2 = findStudentByMaxMarks(arr);
    System.out.println(obj2 != null ? obj2 : "No student found");

    // Find the student with the lowest marks
    Student obj3 = findStudentByMinMarks(arr);
    System.out.println(obj3 != null ? obj3 : "No student found");

    // Calculate the total marks of all students
    double total = findTotalMarksOfAllStudents(arr);
    System.out.println("Total Marks: " + total);

    // Calculate the average marks of all students
    double average = findAverageMarksOfAllStudents(arr);
    System.out.println("Average Marks: " + average);

    // Count the number of students with marks greater than 80
    int count = findCountOfStudentsWithMarksGreaterThan80(arr);
    System.out.println("Count of Students with Marks > 80: " + count);
}

public static Student searchStudentByRollNo(Student[] arr, int rollNo) {
    for (Student student : arr) {
        if (student.getRollNo() == rollNo) {
            return student;
        }
    }
    return null;
}

public static Student searchStudentByName(Student[] arr, String name) {
    for (Student student : arr) {
        if (student.getName().equals(name)) {
            return student;
        }
    }
    return null;
}

public static Student findStudentByMaxMarks(Student[] arr) {
    double maxMarks = Double.MIN_VALUE;
    Student topStudent = null;
    for (Student student : arr) {
        if (student.getMarks() > maxMarks) {
            maxMarks = student.getMarks();
            topStudent = student;
        }
    }
    return topStudent;
}

public static Student findStudentByMinMarks(Student[] arr) {
    double minMarks = Double.MAX_VALUE;
    Student lowStudent = null;
    for (Student student : arr) {
        if (student.getMarks() < minMarks) {
            minMarks = student.getMarks();
            lowStudent = student;
        }
    }
    return lowStudent;
}

public static double findTotalMarksOfAllStudents(Student[] arr) {
    double totalMarks = 0.0;
    for (Student student : arr) {
        totalMarks += student.getMarks();
    }
    return totalMarks;
}

public static double findAverageMarksOfAllStudents(Student[] arr) {
    double totalMarks = findTotalMarksOfAllStudents(arr);
    return totalMarks / arr.length;
}

public static int findCountOfStudentsWithMarksGreaterThan80(Student[] arr) {
    int count = 0;
    for (Student student : arr) {
        if (student.getMarks() > 80) {
            count++;
        }
    }
    return count;
}

}

class Student {

private int rollNo;
private String name;
private double marks;
private char section;

public Student(int rollNo, String name, double marks, char section) {
    this.rollNo = rollNo;
    this.name = name;
    this.marks = marks;
    this.section = section;
}
    
public int getRollNo() {
    return rollNo;
}

public void setRollNo(int rollNo) {
    this.rollNo = rollNo;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public double getMarks() {
    return marks;
}

public void setMarks(double marks) {
    this.marks = marks;
}

public char getSection() {
    return section;
}

public void setSection(char section) {
    this.section = section;
}

@Override
public String toString() {
    return "Student [rollNo=" + rollNo + ", name=" + name + ", marks=" + marks + ", section=" + section + "]";
}

}```