Question

In: Computer Science

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

Please use java language in an easy way with comments!

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

Solutions

Expert Solution

/********************************GameItem.java******************************/

public abstract class GameItem implements CursedItem {
   protected String itemName; // Name of the item

   // constructor

   public GameItem(String itemName) {
       this.itemName = itemName;
   }

   public String getItemName() {
       return itemName;
   }

   public void setItemName(String itemName) {
       this.itemName = itemName;
   }

   // use()
   public abstract void use();
}

/*****************************************Weapon.java******************************/

/**
* 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.");
}

   @Override
   public void cursed() {
      
      
   }
}

/*******************************Armor.java********************************/

'

/**
* 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;
   }

   public double getProtection() {
       return 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.");
   }

   @Override
   public void cursed() {

   }
}

/*********************************CursedItem.java*****************************/


public interface CursedItem {

   public void cursed();
}
/***********************************CursedWeapon.java*************************************/

import java.util.Random;

public class CursedWeapon extends Weapon implements CursedItem {

   public CursedWeapon(String itemName, int damage) {
       super(itemName, damage);
   }

   @Override
   public void cursed() {

       Random random = new Random();

       int chances = random.nextInt(10)+1;
       //chances of weapon break when it equals to 2,4,6,8
       if ((chances - 1) % 2 == 0) {
           System.out.println("Weapon has been broken!");

       }

   }

}

/************************************CursedArmor.java********************************/


public class CursedArmor extends Armor implements CursedItem {

   public CursedArmor(String itemName, double protection) {
       super(itemName, protection);
   }

   @Override
   public void cursed() {

       setProtection(10);

   }
  
   @Override
   public void use() {
       System.out.println("You have equipped " + getItemName() + ". This item amplify the damage you take in combat by "
               + getProtection() + " percent.");
   }

}
/**********************************************GameItemTest.java****************************/

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));
       inventory.add(new CursedWeapon("Cursed Sword", 60));
       inventory.add(new CursedArmor("Cursed Shield", 85.9));
       for (GameItem g : inventory) {
           g.cursed();
           g.use();
       }
   }
}

/***********************output*************************/

You now wield Sword. This weapon does 50 points of damage with each hit.
You have equipped Shield. This item reduces the damage you take in combat by 85.9 percent.
Weapon has been broken!
You now wield Cursed Sword. This weapon does 60 points of damage with each hit.
You have equipped Cursed Shield. This item amplify the damage you take in combat by 10.0 percent.

You now wield Sword. This weapon does 50 points of damage with each hit.
You have equipped Shield. This item reduces the damage you take in combat by 85.9 percent.
You now wield Cursed Sword. This weapon does 60 points of damage with each hit.
You have equipped Cursed Shield. This item amplify the damage you take in combat by 10.0 percent.

Please let me know if you have any doubt or modify the answer, Thanks :)


Related Solutions

Please use Java language in an easy way with comments! Thanks! Create a calculator that uses...
Please use Java language in an easy way with comments! Thanks! Create a calculator that uses an array of buttons for numbers from 0-9, the . for decimals, and for operators +, -, * ,/ and = as well as a JTextfield that displays the current value or the result. Use ActionListeners and LayoutManagers appropriately. The example below shows how to determine which button has been clicked. ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println(actionEvent.getActionCommand()); }...
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...
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.
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)...
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?
JAVA CODE BEGINNER , Please use comments to explain, please Repeat the calorie-counting program described in...
JAVA CODE BEGINNER , Please use comments to explain, please Repeat the calorie-counting program described in Programming Project 8 from Chapter 2. This time ask the user to input the string “M” if the user is a man and “W” if the user is a woman. Use only the male formula to calculate calories if “M” is entered and use only the female formula to calculate calories if “W” is entered. Output the number of chocolate bars to consume as...
Please use Java language! with as much as comment! thanks! Write a program that displays a...
Please use Java language! with as much as comment! thanks! Write a program that displays a frame with a three labels and three textfields. The labels should be "width:", "height:", and "title:" and should each be followed by one textfield. The texfields should be initialized with default values (Example 400, 600, default title), but should be edited by the user. There should be a button (label it whatever you want, I don't care). If you click the button, a new...
Hello, Please write this program in java and include a lot of comments and please try...
Hello, Please write this program in java and include a lot of comments and please try to make it as simple/descriptive as possible since I am also learning how to code. The instructions the professor gave was: Create your own class Your own class can be anything you want Must have 3 instance variables 1 constructor variable → set the instance variables 1 method that does something useful in relation to the class Create a driver class that creates an...
Please show solution and comments for this data structure using java.​ Implement a program in Java...
Please show solution and comments for this data structure using java.​ Implement a program in Java to convert an infix expression that includes (, ), +, -, *,     and / to postfix expression. For simplicity, your program will read from standard input (until the user enters the symbol “=”) an infix expression of single lower case and the operators +, -, /, *, and ( ), and output a postfix expression.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT