Cvičenie 2
Trieda, inštancia, balík, metóda, princípy OOP
Cvičenie 2 - OOP pojmy
Pred cvičením
- Overte, že máte založený repozitár k semestrálnemu projektu - cvičiaci vám minulý týždeň poslal pozvánku.
- Overte, že viete spraviť lokálny commit a push do repozitára.
Interaktívny kurz GitHub nájdete na stránke s materiálmi - Git by Bit
Úlohy
- Konzultujte Špecifikáciu (1. zadanie) so svojím cvičiacim, dohodnite sa na téme a rozsahu zadania. Mali by ste s ním prejsť tému, hlavnú myšlienku a približný rozsah projektu.
Java úlohy
Ak vám v IDE nepôjde načítať default-structure v rámci projektu, konzultujte tento návod. Tiež si môžete nainštalovať plugin do IDE - ten za vás zabezpečí stiahnutie, načítanie a tiež odovzdanie úlohy. Pokyny sú na konci návodu.
Download the skeleton files
Inheritance, Encapsulation, Polymorphism & Method Overloading
Nakoľko sú všetky premenné a časti kódu v Angličtine, tak aj dnešné zadanie úlohy je v angličtine. Cieľom je aby ste si vyskúšali základné OOP koncepty v Jave.
In this task you will build a simple RPG character system — no UI needed, just the core class hierarchy and game logic. You will practise the most important OOP pillars:
| Concept | What you practise |
|---|---|
| Inheritance | Building a class hierarchy (Warrior, Mage, Archer extend GameCharacter) |
| Encapsulation | Private fields, getters / setters, no leaking of internal state |
| Method overriding | Each subclass overrides getCharacterType(), calculateDamage(), getInfo() |
| Method overloading | attack() / attack(GameCharacter) in GameCharacter; castSpell() overloads in Mage |
| Runtime polymorphism | Using GameCharacter references to call overridden methods |
| Access modifiers | private vs protected fields inside an inheritance hierarchy |
Provided skeleton files
Two skeleton files live in org.jikvict.tasks.exposed:
| File | Description |
|---|---|
GameCharacter.java | Abstract base class — read the TODOs inside |
Weapon.java | Simple weapon class — read the TODOs inside |
Do not change method signatures or access modifiers that are already present. You may add private helper methods if you need them.
What you must implement
Quick example
GameCharacter warrior = new Warrior("Arthur", 100, 10, 6);
GameCharacter mage = new Mage("Gandalf", 80, 15, 50);
System.out.println(warrior.getCharacterType()); // "Warrior"
System.out.println(mage.calculateDamage()); // 30
warrior.attack(mage); // mage takes damage
System.out.println(warrior.attack()); // prints damage value (no target attacked)Submission
Na odovzdanie úlohy môžete použiť aj plugin v InteliJ IDEA Jikvict, ktorý vám automaticky stiahne, nahrá a odovzdá úlohu. Postup nájdete na stránke pomoci.
Take your whole project, remove unnecessary files (like .idea folder or build folder) and zip it.
The final zip file should contain the default-structure folder at the root.