Week 2

Inheritance

Interfaces

Polymorphism

Error Handling in Java

Practice

Assignment

Back end Track

Week 2 Assignment — HYF Academy Course Platform

📋 Overview

You will extend the HYF Academy system from Week 1 into a richer course platform. Where Week 1 focused on the basics of Java structure, this assignment puts inheritance, interfaces, polymorphism, and exception handling to work in a realistic domain.

This assignment is designed to take 1–2 days. Read the full brief before writing any code.


🎯 What You Will Practise

Topic Module
Inheritance — superclass, subclass, super Module 1
Method overriding and @Override Module 1
Interfaces as contracts Module 2
Multiple interface implementation Module 2
Polymorphism — mixed-type collections Module 3
Programming to an interface/supertype Module 3
Custom exception hierarchy Module 4
try-catch, finally, try-with-resources Module 4
Meaningful exception messages Module 4

🏫 Domain — HYF Academy Course Platform

HYF Academy now offers two types of courses: self-paced courses that students complete in their own time, and live cohort courses that run on a fixed schedule with a mentor. Both share common properties but behave differently in key ways.

The platform also supports two types of people: Students and Mentors. Both are platform users, but they have different roles and capabilities.


📦 Required Package Structure

com.hyfacademy
├── exception
│   ├── EnrolmentException.java
│   ├── CourseFullException.java
│   ├── AlreadyEnrolledException.java
│   └── InvalidProgressException.java
├── model
│   ├── User.java
│   ├── Student.java
│   ├── Mentor.java
│   ├── Course.java
│   ├── SelfPacedCourse.java
│   └── LiveCohortCourse.java
├── service
│   ├── Enrollable.java
│   ├── Reportable.java
│   └── PlatformService.java
└── Main.java

🔧 Specification

Exceptions — com.hyfacademy.exception

Create a custom exception hierarchy. All exceptions are unchecked (extend RuntimeException):

EnrolmentException  (base — extends RuntimeException)
├── CourseFullException        — carries: courseName, maxCapacity
├── AlreadyEnrolledException   — carries: studentName, courseName
└── InvalidProgressException   — carries: attemptedValue (int)

Each exception must:

Example messages:


Usercom.hyfacademy.model

Abstract base class for all platform users.

Private fields: name (String), email (String), userId (String)

Constructor: all three fields

Getters for all fields

Abstract method: String getRole() — each subclass returns its own role label

Concrete method: String getSummary() — returns "[STUDENT] Alice | [email protected]" using getRole() so subclasses automatically get the right label

Override toString() to call getSummary()


Student extends User

Additional private fields: enrolledCourses (Course[], max 5), courseCount (int)

Constructor: name, email — auto-generate userId as "STU-001", "STU-002", etc. using a static counter

Override getRole() → returns "STUDENT"

Methods:


Mentor extends User

Additional private fields: expertise (String), assignedCourses (Course[], max 3), courseCount (int)

Constructor: name, email, expertise — auto-generate userId as "COA-001", "COA-002", etc.

Override getRole() → returns "MENTOR"

Methods:


Coursecom.hyfacademy.model

Abstract base class for all courses.

Private fields: courseName (String), courseId (String), maxStudents (int), enrolledCount (int), studentProgress (int[], parallel to a fixed-size student-name tracking array — keep it simple: track progress by index)

Static counter for auto-generating courseId as "CRS-001", "CRS-002", etc.

Constructor: courseName, maxStudents

Getters for all fields

Methods:

Abstract methods:

Override toString()

[CRS-001] Java Basics (Self-Paced) | Enrolled: 3/20

SelfPacedCourse extends Course

Students complete this course at their own pace — no fixed schedule.

Additional field: estimatedHours (int)