Please use java language in an easy way with comments! Thanks!
Expand your below program, so it can include cursed items (weapons that break, armor that causes more damage when worn). In order to do that, write an interface called "CursedItem". Then create subclasses of the armor, and weapon class that implement the CursedItem interface =>
CursedWeapon: using a random number generator, there is a 4 in 10 chance that the weapon breaks during combat.
CursedArmor: this item amplifies the damage taken in battle instead of reducing it.
The interface only needs to have one method. In your driver class, create an example object for each of the new classes.
-----------------------------------------------------------------------------------------------------------
//Java code
public class GameItem { protected String itemName; //Name of the item //constructor public GameItem(String itemName) { this.itemName = itemName; } //use() public void use(); }
//======================================
/** * Weapon class that extends the GameItem class */ public class Weapon extends GameItem { private int damage; //constructor public Weapon(String itemName, int damage) { super(itemName); this.damage = damage; } public void use() { System.out.println("You now wield "+itemName+". This weapon does "+damage+" points of damage with each hit."); } }
//==========================================
/** * Armor class that extends the GameItem class */ public class Armor extends GameItem{ private double protection; public Armor(String itemName, double protection) { super(itemName); this.protection = protection; } @Override public void use() { System.out.println("You have equipped "+itemName+". This item reduces the damage you take in combat by "+protection+" percent."); } }
//=====================================
import java.util.ArrayList; public class GameItemTest { public static void main(String[] args) { ArrayList<GameItem> inventory = new ArrayList<>(); inventory.add(new Weapon("Sword",50)); inventory.add(new Armor("Shield",85.9)); for (GameItem g:inventory ) { g.use(); } } }
Thanks for the question, Here are the classes you will be needing. Comments are given so that you can follow the changes easily : )
============================================================
// iterface public interface CursedItem { // it has only one method that returns true if the weapon or shield got broken // based on the probability public boolean isBroken(); }
============================================================
public abstract class GameItem { protected String itemName; //Name of the item //constructor public GameItem(String itemName) { this.itemName = itemName; } //use() public abstract void use(); }
============================================================
/** * Weapon class that extends the GameItem class */ public class Weapon extends GameItem { private int damage; //constructor public Weapon(String itemName, int damage) { super(itemName); this.damage = damage; } public void setDamage(int damage) { this.damage = damage; } public void use() { System.out.println("You now wield "+itemName+". This weapon does "+damage+" points of damage with each hit."); } }
============================================================
public class Armor extends GameItem{ private double protection; public Armor(String itemName, double protection) { super(itemName); this.protection = protection; } public void setProtection(double protection) { this.protection = protection; } @Override public void use() { System.out.println("You have equipped "+itemName+". This item reduces the damage you take in combat by "+protection+" percent."); }
============================================================
import java.util.Random; // new class extends Weapon and implements the interface public class Lance extends Weapon implements CursedItem { public Lance(String itemName, int damage) { super(itemName, damage); } @Override public boolean isBroken() { Random random = new Random(); int generateNumber = 1 + random.nextInt(10); // we are generating a number from 1 to 10 // to get 4 out of 10 chance we check if the number is 4 or 5 or 6 or 7 // if the random number is any one of these numbers we return True // else return False if (4 <= generateNumber && generateNumber <= 7) return true; else return false; } @Override public void use() { // check if the weapon broke if (isBroken()) { System.out.println("Your armour " + itemName + " broke suddenly."); setDamage(0); //set damage to 0 } else { super.use(); } } }
============================================================
import java.util.Random; // new class extends Armor and implements CursedItem public class Scutum extends Armor implements CursedItem { public Scutum(String itemName, double protection) { super(itemName, protection); } @Override public boolean isBroken() { Random random = new Random(); int generateNumber = 1+ random.nextInt(10); // we are generating a number from 1 to 10 // to get 4 out of 10 chance we check if the number is 4 or 5 or 6 or 7 // if the random number is any one of these numbers we return True // else return False if(4<=generateNumber && generateNumber<=7)return true; else return false; } @Override public void use() { // invokes isBroken() if true then the shield broke if (isBroken()) { System.out.println("Your armour " + itemName + " broke suddenly."); setProtection(0); // set protection value to 0 since it broke } else { // else continue using it super.use(); } } }
============================================================
import java.util.ArrayList; public class GameItemTest { public static void main(String[] args) { ArrayList<GameItem> inventory = new ArrayList<GameItem>(); inventory.add(new Lance("Sword",50)); inventory.add(new Scutum("Shield",85.9)); for (GameItem g:inventory ) { g.use(); } } }
Thank you so much !
Please do appreciate with an up vote : )
Get Answers For Free
Most questions answered within 1 hours.