JAVA-16
Q.16 Define a class called Student. Store rollno &name of the student. Define member functions to assign
& display value of roll no. & name.
Solution :-Â
class Student {
private int rollNo;
private String name;
// Constructor to initialize student details
public Student(int rollNo, String name) {
this.rollNo = rollNo;
this.name = name;
}
// Method to display student details
public void display() {
System.out.println("Roll No: " + rollNo);
System.out.println("Name: " + name);
}
public static void main(String[] args) {
// Creating Student objects
Student student1 = new Student(101, "John Doe");
Student student2 = new Student(102, "Jane Smith");
// Displaying student details
student1.display();
System.out.println();
student2.display();
}
}