Question

Please use java language in an easy way with comments! Thanks! Expand your below program, so...

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();
        }
    }
}

Homework Answers

Answer #1
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 : )

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Modify the Employee9C superclass so that is an abstract superclass with a constructor to set its...
Modify the Employee9C superclass so that is an abstract superclass with a constructor to set its variables (firstName and lastName). It should contain an abstract method called payPrint. Below is the source code for the Employee9C superclass: public class Employee9C {    //declaring instance variables private String firstName; private String lastName; //declaring & initializing static int variable to keep running total of the number of paychecks calculated static int counter = 0;    //constructor to set instance variables public Employee9C(String...
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is finished for you. */ private static class Customer implements Comparable { private double donation; public Customer(double donation) { this.donation = donation; } public double getDonation() { return donation; } public void donate(double amount) { donation += amount; } public int compareTo(Customer other) { double diff = donation - other.donation; if (diff < 0) { return -1; } else if (diff > 0) { return...
This is Java programing.Modify this Java code,so that each franchise can assign and print their own...
This is Java programing.Modify this Java code,so that each franchise can assign and print their own burger price.The programing have at least three franchises. (Use abstract) class newBurger { public newBurger() { } public void salmonBurger(){    System.out.println("salmonBurger $5.99");    System.out.println("Kcal: 294"); } public void clamBurger(){    System.out.println("clamBurger $4.99");    System.out.println("Kcal: 200"); } public void oysterBurger(){    System.out.println("oysterBurger $3.50");    System.out.println("Kcal: 125"); } } class franchise1 extends newBurger { String name = "franchise #1"; public franchise1() { } } public...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
This assignment is an individual assignment. For Questions 1-3: consider the following code: public class A...
This assignment is an individual assignment. For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B()...
IN JAVA Language- Singly Linked List Implementation Implement a Linked List in your language. Use your...
IN JAVA Language- Singly Linked List Implementation Implement a Linked List in your language. Use your Can class. You need to create a driver that makes several Can objects and places them in alphabetical order in a list. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods. NOT USE your language's Library List . You will receive zero points. Write a LinkedList class. Include...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or start with the link below: https://www.thoughtco.com/using-the-arraylist-2034204 import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { String DAY_OF_WEEKS[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS[n] + ".");...
TO BE DONE IN JAVA Your task is to complete the AddTime program so that it...
TO BE DONE IN JAVA Your task is to complete the AddTime program so that it takes multiple sets of hours, minutes and seconds from the user in hh:mm:ss format and then computes the total time elapsed in hours, minutes and seconds. This can be done using the Time object already given to you. Tasks: Complete addTimeUnit() in AddTime so that it processes a string input in hh:mm:ss format and adds a new Time unit to the ArrayList member variable....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT