0% found this document useful (0 votes)
4 views5 pages

Q

The document contains a Java program that defines a 'University' class to manage student records, including attributes like register number, name, age, gender, and class ID. It includes methods to add and remove students, and to fetch students by class ID. The main method demonstrates adding a student and retrieving their information based on the class ID.

Uploaded by

gidopow119
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Q

The document contains a Java program that defines a 'University' class to manage student records, including attributes like register number, name, age, gender, and class ID. It includes methods to add and remove students, and to fetch students by class ID. The main method demonstrates adding a student and retrieving their information based on the class ID.

Uploaded by

gidopow119
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Q.

Write a Java program to create a class called


"University" with attributes (register number, name, age,
gender, class ID) for students, and methods to add and
remove students, and to create classes. Fetch and display
the student of a particular class ID.

CODE:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Student {
private String registerNumber;
private String name;
private int age;
private String gender;
private String classID;

public Student(String registerNumber, String name, int age, String


gender, String classID) {
this.registerNumber = registerNumber;
this.name = name;
this.age = age;
this.gender = gender;
this.classID = classID;
}

public String getRegisterNumber() {


return registerNumber;
}

public String getName() {


return name;
}

public int getAge() {


return age;
}

public String getGender() {


return gender;
}

public String getClassID() {


return classID;
}

@Override
public String toString() {
return registerNumber + ", " + name + ", " + age + ", " + gender + ",
" + classID;
}
}

class University {
private Map<String, List<Student>> classToStudents;

public University() {
classToStudents = new HashMap<>();
}

public void addStudent(Student student) {


String classID = student.getClassID();
classToStudents.computeIfAbsent(classID, k -> new
ArrayList<>()).add(student);
}

public void removeStudent(Student student) {


String classID = student.getClassID();
List<Student> students = classToStudents.get(classID);
if (students != null) {
students.remove(student);
}
}

public List<Student> getStudentsByClassID(String classID) {


return classToStudents.get(classID);
}
}

public class Main {


public static void main(String[] args) {
University university = new University();

// Adding students to classes


university.addStudent(new Student("22BCE0001", "Arun", 20,
"male", "class202301"));

// Fetching and displaying students of a particular class ID


String targetClassID = "class202301";
List<Student> studentsOfClass =
university.getStudentsByClassID(targetClassID);

if (studentsOfClass != null) {
for (Student student : studentsOfClass) {
System.out.println(student);
}
} else {
System.out.println("No students found for class ID: " +
targetClassID);
}
}
}

You might also like