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();
    }
}

TRY IT YOURSELF

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.