Question: Overview: Some systems are designed to allow objects to be locked so they can't be manipulated under certain situations. For example, in this scenario, assume

Overview: Some systems are designed to allow objects to be locked so they can't be manipulated under certain situations. For example, in this scenario, assume you are writing a D&D type game in which a character (Wizard) can be locked so it cannot take damage (maybe it has a protection spelll :-) ). The lockable interface can be implemented for any object whose methods you would like to lock/unlock.
- The skeletons of the getters/setters you need for this class have already been written. You need to finish them.
- The skeletons of the two constructors you need for this class have already been written. You need to finish them. You should call the setters for name and health and set key to zero and locked to false. (After this step, tests 01 and 02 should pass).
- Provide an implementaton for the takeDamage() method. It should subtract the amount of power passed into the method from the health. (After this step, test 03 should pass)
- According to the UML Diagram, you have an interface called Lockable. Write this interface. Remember - interfaces don't have implementations - just the signatures.
- Modify the Wizard class so that it enforces using the Lockable interface (add the implements keyword and add empty method bodies for all of the methods in the interface):
- Implement the setKey() method. This method should check if the key passed into is greater than zero and that this.key has not already been set, if these are both true, the method should set the key, otherwise, it is should not. (After this step, tests 04 and 05 should pass)
- Implement the isLocked() method. It should simply return the value of the lock. (After this step, test 06 should pass)
- Implement the lock() method. It should pass in the key that matches the value of the key already set for the object. If they match, then the value of locked should be set to true. (After this step, tests 07 and 08 should pass)
- Implement the unlock() method. It should pass the key that matches the value of the key already set for the object. If they match, then the value of locked should be set to false. (After this step tests 09 and 10 should pass)
- Modify the takeDamage() method so it checks the lock with the isLocked() method. If the object is locked, then nothing should be subtracted from the health; otherwise, health should be reduced as the original implementation. (After this step test 11 should pass)
Wizard -name: string -health: int -key: int -locked: boolean +Wizard() +Wizard(String name) +takeDamage(int power): void > Lockable +setKey(int key): void +lock(int key): void +unlock(int key): void +is Locked(): boolean
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
