Question

In: Computer Science

Java code. You will need to make classes that could be used to stage a battle...

Java code. You will need to make classes that could be used to stage a battle as described below.

You have been tasked to create a number of items that can be used in an online game. All items can be used on a game character who will have a name, a number of health-points and a bag to hold the items.  

Here are examples of items that can be made:

Item type of item effectOnHealth other info
Widow's Cloak Armor +15
Gladiator's Shield Armor +8
Rusty Knife Weapon -3
Protection Spell Spell 1 has magic words that can be read
Note from Zelda Letter 0 says "Beware of the Witch"

Here are examples of characters that can be made:

name health points
Godwin 30
Marilyn 40

All items can be added to any character's bag and also used on any character. (For instance the Rusty Knife could be used on a character to lower their health by 3 points.)

Armor and weapons can be equipped (worn) and removed. Spells can also be equipped (memorized) which casts a glow around the character and removed (forgotten) which removes the glow. In the future, any time a character equips or removes any item, their appearance will be altered. These methods -- for now -- can just have a body which says either "equipping" or "removing."

Both spells and letters have some text that could be read.  Whenever a spell or letter is used, the text will also be read (displayed). However these items can be read independently from their use on a character.

Build these items using good interface and abstract class design as appropriate. Set the toString() method of the Character class to return a character's name and current health. You should have a good object oriented structure and good method signatures.

To illustrate this, in a driver class, two characters, Godwin and Marilyn are created. They begin by having items added to their bags (both Godwin and Marilyn get a copy of the Note from Zelda). Then the items they have are used on themselves or one another as follows:

This is a sample of how a Driver could work, staging a battle between the defensive Godwin and the offensive Marilyn.

- The Protection Spell is used on Godwin (by Godwin)
- The Rusty Knife is used on Godwin (by Marilyn)
- The Gladiator's Shield is used on Godwin (by Godwin)
- The Rusty Knife is used on Godwin (by Marilyn)

Solutions

Expert Solution

file-name : Character.java
-------------------------------
import java.util.ArrayList;

public class Character extends Items{

    String name;
    int healthPoints;
    ArrayList<String> bags;

    Character(String name, int healthPoints) {
        super();
        this.name = name;
        this.healthPoints = healthPoints;
        bags = new ArrayList<>();
        bags.add("Zelda");
    }

    public String getName() {
        return name;
    }
    public int getHealthPoints() {
        return healthPoints;
    }
    public void setHealthPoints(int healthPoints) {
        this.healthPoints = healthPoints;
    }

    public String toString() {
        String str = "Name: "+this.getName()
                +"\nHealth: "+getHealthPoints()+"\n---------\n";

        return str;
    }

    public void rustyKnife(Character c) {
        System.out.println("Using knife weapon on "+c.getName());
        c.setHealthPoints(c.getHealthPoints()-3);
    } // END of rustyKnife method

    @Override
    public void useSpell() {
        System.out.println("Avada Kedavra");
        this.setHealthPoints(this.getHealthPoints()+1);
        this.bags.add("Spell");
    }

    @Override
    public void zelda() {
        System.out.println("Beware of Witch");
    }

    @Override
    public void wearCloak() {
        System.out.println(this.getName() + " Equipping Widow's Cloak");
        this.bags.add("Cloak");
        this.setHealthPoints(this.getHealthPoints()+8);
    }

    @Override
    public void wearShield() {
        System.out.println(this.getName()+" Equipping Gladiator's Shield");
        this.bags.add("Shield");
        this.setHealthPoints(this.getHealthPoints()+15);
    }

    @Override
    public void removeCloak() {
        // only if the character wears a cloak, then only he can remove it
        if(this.bags.contains("Cloak")) {
            System.out.println(this.getName() + " Removing Widow's Cloak");
            this.bags.remove("Cloak");
            this.setHealthPoints(this.getHealthPoints() - 8);
        }

        else {
            System.out.println("Not wearing any cloak");
        }
    }

    @Override
    public void removeShield() {
        if(this.bags.contains("Shield")) {
            System.out.println(this.getName()+" Removing Gladiator's Shield");
            this.bags.remove("Shield");
            this.setHealthPoints(this.getHealthPoints() - 15);
        }
        else {
            System.out.println("Not wearing any shield");
        }

    }
} //END of class


-------------------------------------------------------------------------------------------------------

file-name:   Items.java
----------------------
public abstract class Items {

    String itemName;
    int points;

    Items(String name, int p) {
        this.itemName = name;
        this.points = p;
    }

    public Items() {

    }

    public abstract void rustyKnife(Character c);
    public abstract void wearCloak();
    public abstract void wearShield();
    public abstract void useSpell();
    public abstract void zelda();
    public abstract void removeCloak();
    public abstract void removeShield();
}

-----------------------------------------------------------------------------------------------------------

file-name:  TestMain.java
------------------------------
public class TestMain {
    public static  void main(String[] args) {

        // creating characters
        Character p1 = new Character("Godwin", 30);
        Character p2 = new Character("Marilyn", 40);

        p1.useSpell();
        p2.rustyKnife(p1);
        p1.wearShield();
        p2.rustyKnife(p1);

        System.out.println("----------");

        // I am showing how some other methods work
        // p1 character not wearing any cloak
        // but still trying to remove so prints error message.
        p1.removeCloak();

        System.out.println(p1);
        System.out.println(p2);

    } // END of main( ) method

} // END of class

==================================================

OUTPUT :

Hey If you want me to modify code, I will. Thank you!!. Please UPVOTE if you like my effort.


Related Solutions

(Code in Java please) You need to implement the GarageDoor, GarageDoorUpCommand and GarageDoorDownCommand classes (similar to...
(Code in Java please) You need to implement the GarageDoor, GarageDoorUpCommand and GarageDoorDownCommand classes (similar to Light, LightOnCommand and LightOffCommand), AND (edited) Implement the CeilingFan class (with state, see p. 220), AND CeilingFanHighCommand (p. 221), CeilingFanMediumCommand, CeilingFanLowCommand, CeilingFanOffCommand (WITH Undo), AND TEST your CeilingFan classes (see pp. 222 and 223), AND Finally, implement and Test the MacroCommand class on pp. 224-227 EXAMPLE output for homework assignment…………… ======= The COMMAND PATTERN ============= guest house garage door is UP guest house garage...
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
Need to make Java code as following: Create a dice playing threading program to do the...
Need to make Java code as following: Create a dice playing threading program to do the following: 1. Start a thread for a process to simulate a computer rolling a die 1000 times. 2. Start another thread for a process to simulate a user rolling a die 1000 times. 3. Keep track of rolls for user and computer in an array(s). 4. Display on screen when the computer thread starts, the rolls, and when the computer thread ends. 5. Display...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ *...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ * This program takes a puzzle board and returns true if the * board is solvable and false otherwise * * Example: board 3 6 4 1 3 4 2 5 3 0 * The goal is to reach the 0. You can move the number of spaces of your * current position in either the positive / negative direction * Solution for this game...
//Complete the incomplete methods in the java code //You will need to create a driver to...
//Complete the incomplete methods in the java code //You will need to create a driver to test this. public class ManagedArray { private int[] managedIntegerArray; //this is the array that we are managing private int maximumSize; //this will hold the size of the array private int currentSize = 0; //this will keep track of what positions in the array have been used private final int DEFAULT_SIZE = 10; //the default size of the array public ManagedArray()//default constructor initializes array to...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to use all methods, also switch statements. Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit(). These methods will return a Temperature in those three scales equal to the this temperature. Note that the value of this is not changed in these conversions. In addition to these three conversion methods the class will have methods add(Temperature), subtract(Temperature), multiply(Temperature), and...
I need the Java Code and Flowchart for the following program: Suppose you are given a...
I need the Java Code and Flowchart for the following program: Suppose you are given a 6-by-6 matrix filled with 0s and 1s. All rows and all columns have an even number of 1s. Let the user flip one cell (i.e., flip from 1 to 0 or from 0 to 1) and write a program to find which cell was flipped. Your program should prompt the user to enter a 6-by-6 array with 0s and 1s and find the first...
Write a Java Program using the concept of objects and classes. Make sure you have two...
Write a Java Program using the concept of objects and classes. Make sure you have two files Employee.java and EmployeeRunner.java. Use the template below for ease. (Please provide detailed explanation) Class Employee Class Variables: Name Work hour per week Hourly payment Class Methods: - Get/Sets - generateAnnualSalary(int:Duration of employment)               annual salary = Hourly payment * Work hours per week * 50 If the employee is working for more than 5 years, 10% added bonus             -void displayAnnualSalary ( )...
**NEED ACTUAL JAVA CODE** Basically this code is defining the board for the game tigers and...
**NEED ACTUAL JAVA CODE** Basically this code is defining the board for the game tigers and goats or 'Bagh chan'. Ive posted the other 3 classes that I need help with, didnt want to put all 4 in one question. public class GameViewer implements MouseListener { // instance variables private int bkSize; // block size - all other measurements to be derived from bkSize private int brdSize; // board size private SimpleCanvas sc; // an object of SimpleCanvas to draw...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT