Question

In: Computer Science

Please use java language in an easy way and comment as much as you can! Thanks...

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.

Solutions

Expert Solution

//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


Related Solutions

Please use java language in an easy way and comment as much as you can! Thanks...
Please use java language in an easy way and comment as much as you can! Thanks (Write a code question) Write a generic method called "findMin" that takes an array of Comparable objects as a parameter. (Use proper syntax, so that no type checking warnings are generated by the compiler.) The method should search the array and return the index of the smallest value. (Analysis of Algorithms question) Determine the growth function and time complexity (in Big-Oh notation) of the...
Please use java language in an easy way and comment as much as you can! Thanks...
Please use java language in an easy way and comment as much as you can! Thanks (Write a code question) Write a generic method called "findMin" that takes an array of String objects as a parameter. (Use proper syntax, so that no type checking warnings are generated by the compiler.) The method should search the array and return the index of the smallest value.
How to identify when to use super() in constructor for java language in a easy way?
How to identify when to use super() in constructor for java language in a easy way?
Please use Java language! With as many as comment! ThanksWrite a static method called "evaluate"...
In Java language Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
Please use Java language with comments! Thanks! Write a program that will display multiple dots move...
Please use Java language with comments! Thanks! Write a program that will display multiple dots move across the Frame and randomly change direction when they hit the edge of the screen. Do this by creating a Dot class and making each dot an object of that class. You may reuse code written in class for this part of the assignment. Create a subclass of the Dot class called PlayerDot that is controlled by the player through keyboard input (arrow keys)...
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
Please use java language Thanks! Implement a recursive method called "pow" that takes 2 integers, x...
Please use java language Thanks! Implement a recursive method called "pow" that takes 2 integers, x and y, as parameters and returns the value xy (x raised to the power y). The exponent must be non-negative. If a negative argument is given for the exponent, then an exception should be thrown. Implement a recursive method called "fib" that takes a positive integer, n, as a parameter and returns the nth Fibonacci value. Assume that the first 2 values in the...
I know the code isn't here nut please answer as much as you can thanks! it...
I know the code isn't here nut please answer as much as you can thanks! it is qt creator c++ Question 1 Which of the following is a correct description of ItemList ? It is a QMap and thus an ItemList object is a QMap itself It has a QMap, which cannot be accessed outside this class ItemList uses a heap QMap object QMap used by ItemList uses Item pointers as keys Question 2 Which of the following is true...
POST A COMMENT ON BELOW THANKS Managers can use variance analysis to create a cycle of...
POST A COMMENT ON BELOW THANKS Managers can use variance analysis to create a cycle of continuous improvements by repeatedly identifying causes of variances, initiating corrective actions and evaluating results of these actions. Companies use this technique to pinpoint overspending and underspending during production and categorize into two sections – price variances and quantity variances. ‘Price variances occur when a company pays more for a production input, such as materials or labor, where quantity variances occur when the company uses...
java. please don't use complicated language I can follow up. Write a for loop that prints...
java. please don't use complicated language I can follow up. Write a for loop that prints the integers from 1 to 100, all on one line, space-separated. However, after printing 3 numbers, you need to skip the next number and print a counter in parenthesis. 1 2 3 (1) 5 6 7 (2) 9 10 11 (3) 13 14 15 (4) 17 18 19 [... and so on ...] Write this code once with for loop, once with while loop,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT