CODE:

import java.util.Scanner;
 
class CourseFullException extends Exception {
    CourseFullException(String message) {
        super(message);
    }
}
 
class Course {
    private String courseName;
    private int capacity;
    private int enrolledStudents = 0;
 
    Course(String courseName, int capacity) {
        this.courseName = courseName;
        this.capacity = capacity;
    }
 
    void enrollStudent() throws CourseFullException {
        if (enrolledStudents >= capacity) {
            throw new CourseFullException("Course is full");
        }
        enrolledStudents++;
        System.out.println("Student enrolled in " + courseName);
    }
}
 
public class CourseCapacityDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
 
        System.out.print("Enter course name: ");
        String name = sc.nextLine();
        System.out.print("Enter course capacity: ");
        int capacity = sc.nextInt();
 
        Course course = new Course(name, capacity);
 
        System.out.print("Enter number of students to enroll: ");
        int n = sc.nextInt();
 
        for (int i = 1; i <= n; i++) {
            try {
                course.enrollStudent();
            } catch (CourseFullException e) {
                System.out.println("Error: " + e.getMessage());
            }
        }
    }
}

OUTPUT:

Enter course name: CSE
Enter course capacity: 3
Enter number of students to enroll: 5
Student enrolled in CSE
Student enrolled in CSE
Student enrolled in CSE
Error: Course is full
Error: Course is full