Java OOP Practical for BCA Sem 2: Student Class with Display Method Explained

Java OOP Practical for BCA Sem 2: Student Class with Display Method Explained

Create Student class with attributes: name,rollno,marks. Implement method disply() to show student details. Create two student objects and display their details.

class Student {
String name;
int rollno;
float marks;

// Constructor
Student(String name, int rollno, float marks) {
    this.name = name;
    this.rollno = rollno;
    this.marks = marks;
}

// Method to display student details
void display() {
    System.out.println("Name: " + name);
    System.out.println("Roll No: " + rollno);
    System.out.println("Marks: " + marks);
    System.out.println("----------------------");
}

}

public class Main {
public static void main(String[] args) {
// Creating two student objects
Student s1 = new Student(“Amit”, 1, 85.5f);
Student s2 = new Student(“Priya”, 2, 92.0f);

    // Displaying details
    s1.display();
    s2.display();
}

}