In: Computer Science
Please use java language in an easy way and comment as much as you can! Thanks
1. A driver class with a main method, which creates instances of various objects (two of each subclass) and adds them as items to an ArrayList named "inventory". A foreach loop should loop through the ArrayList and call the use() method of each item. Define the ArrayList in a way that it only holds elements of the GameItem class and its subclasses. Make proper use of polymorphism.
2. A GameItem class that contains attributes (the name of the item) and behaviors (the use() method) that all items have in common. This class will serve as superclass for the next classes.
3. A Weapon class that extends the GameItem class and takes an additional parameter called "damage", which describes how much damage the weapon does when used. Override the use method so it prints a message "You now wield "name". This weapon does "damage" points of damage with each hit.
4. An Armor class that extends the GameItem class and takes an additional parameter called "protection", which describes how much protection the armor item offers when used. Override the use method so it prints a message "You have equipped "name". This item reduces the damage you take in combat by "protection" percent.
//Java code
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 { /** * an additional parameter called "damage", which * describes how much damage the weapon does when used. */ private int damage; //constructor public Weapon(String itemName, int damage) { super(itemName); this.damage = damage; } /** * Override the use method so it prints a message "You now wield "name". This * weapon does "damage" points of damage with each hit. */ @Override 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{ /** * an additional parameter called "protection", * which describes * how much protection the armor item offers when used. */ private double protection; //Constructor public Armor(String itemName, double protection) { super(itemName); this.protection = protection; } /** * Override the use method so it prints a message * "You have equipped "name". This item reduces * the damage you take in combat by "protection" percent. */ @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; /** * A driver class with a main method, which creates * instances of various objects (two of each subclass) * and adds them as items to an ArrayList named "inventory". A foreach loop should loop through the ArrayList and call the use() method of each item. Define the ArrayList in a way that it only * holds elements of the GameItem class and its subclasses. */ public class GameItemTest { public static void main(String[] args) { ArrayList<GameItem> inventory = new ArrayList<>(); inventory.add(new Weapon("Sword",50)); inventory.add(new Weapon("Rifle",100)); inventory.add(new Armor("Chest Armor",23.5)); inventory.add(new Armor("Shield",85.9)); //for each for (GameItem g:inventory ) { g.use(); } } }
//Output
//If you need any help regarding this solution ............ please leave a comment ........... thanks