These exercises are structured progressively:
- π’ Level 1 β Fundamentals
- π‘ Level 2 β OOP Discipline
- π΄ Level 3 β Engineering Thinking
- π§ Level 4 β Edge Case and Defensive Programming
This will push her from βstudent Javaβ to βbackend mindsetβ.
Week 1 β Exercises
Java Fundamentals and OOP Discipline
Complete all exercises in order. Do not skip levels. Follow clean code principles. Use Git commits properly.
π’ Level 1 β Basic Structure and Syntax
Exercise 1 β Create a Simple Class
Create a class called Person with:
- name (String)
- age (int)
Add:
- A constructor
- A method
displayDetails()that prints details
Requirements
β Use proper naming β Do not use public fields β Use encapsulation
Exercise 2 β Constructor Validation
Modify the Person class:
- Age cannot be negative
- Name cannot be null or empty
If invalid β throw IllegalArgumentException.
Example:
Age cannot be negative
Name cannot be empty
Exercise 3 β Object Creation
Create a Main class.
Inside main():
- Create 3 Person objects
- Call displayDetails()
- Test invalid data
Observe behavior.
π‘ Level 2 β OOP Strengthening
Exercise 4 β Create a Student Class
Create a class Student with:
- studentId (String)
- name (String)
- age (int)
Requirements:
β All fields private β Constructor mandatory β Getter methods β Validation in constructor β Method: isAdult() (returns boolean)
Exercise 5 β Inheritance
Create a class GraduateStudent that extends Student.
Add:
- specialization (String)
Add a method:
printSpecialization()
Demonstrate polymorphism in main().
Exercise 6 β Method Overriding
Override displayDetails() in GraduateStudent.
Call:
Student s = new GraduateStudent(...);
s.displayDetails();
Observe output.
π΄ Level 3 β Engineering Thinking
Exercise 7 β BankAccount Class
Create a class BankAccount:
Fields:
- accountNumber
- accountHolder
- balance
Rules:
β balance cannot be negative β deposit(double amount) β withdraw(double amount) β Cannot withdraw more than balance
Throw exception if withdrawal invalid.
Exercise 8 β Prevent Invalid State
Modify BankAccount:
- balance should not be directly modifiable
- No setter for balance
- Only deposit/withdraw allowed
Question to think:
Why should we not expose balance setter?
Exercise 9 β Static Variable
Add a static variable:
private static int totalAccounts;
Increment it inside constructor.
Create 3 accounts and print total accounts.
Understand:
Static belongs to class, not object.
π§ Level 4 β Defensive Programming
Exercise 10 β Null Safety
Create a class Order.
Fields:
- orderId
- amount
- customerName
Requirements:
β No null allowed β amount must be > 0 β Use constructor validation
Try creating invalid object in main and observe behavior.
Exercise 11 β Encapsulation Break Attempt
Try this:
- Make fields public.
- Modify balance directly.
- Set negative balance.
Now revert to private fields.
Reflect:
What problem did encapsulation solve?
Write answer in comments.
Exercise 12 β Responsibility Separation
Currently, you may have printing logic inside your model class.
Refactor:
Create a new class:
StudentPrinter
Move printing logic there.
Why?
Single Responsibility Principle (intro level)
π Reflection Exercises (Write Answers in Comments)
- Why should fields be private?
- Why validate inside constructor?
- What happens if withdraw() does not check balance?
- When should inheritance be used?
- What does static really mean?
π Mini Project Task (Preparation for Assignment)
Using what you learned:
Start building:
Simple Student Management System
Features for now:
- Add student
- Display student
- Validate input
- Prevent invalid object creation
Do NOT use collections yet. Hardcode 3β4 objects manually.
We will improve in Week 2.
π Coding Rules for All Exercises
β Proper indentation β No long methods (>30 lines) β No public fields β Meaningful names β No code duplication β Use packages properly β One class per file
π§ͺ Testing Requirement (Manual)
For each class:
Test:
- Valid input
- Invalid input
- Edge cases
- Negative values
- Null values
Write observations in comments.
π― End Goal of Week 1 Exercises
After completing all exercises, you should:
- Be comfortable creating classes
- Understand encapsulation deeply
- Know why constructors matter
- Apply inheritance properly
- Think defensively
- Avoid public fields
- Write structured Java code