Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Creating two child classes from parent class using inheritance: public GameCharacter(String name) { this.mName = name; this.mLevel = 1; this.mHealthPoints = 1; maxHealthPoints = this.mHealthPoints;

Creating two child classes from parent class using inheritance: public GameCharacter(String name) { this.mName = name; this.mLevel = 1; this.mHealthPoints = 1; maxHealthPoints = this.mHealthPoints; this.mMagic = 1; this.mGold = 1; } public GameCharacter(String name, int level) { mName = name; mLevel = level; mHealthPoints = 100 * mLevel; maxHealthPoints = this.mHealthPoints; boundaryForAutoHeal = maxHealthPoints / 2; mMagic = 100 * mLevel; mGold = 100 * mLevel; } public String getName() { return mName; } public int getLevel() { return mLevel; } public int getHealthPoints() { return mHealthPoints; } public int getMagic() { return mMagic; } public int getGold() { return mGold; } public void setName(String name) { mName = name; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } GameCharacter that = (GameCharacter) o; return mLevel == that.mLevel && mHealthPoints == that.mHealthPoints && mMagic == that.mMagic && mGold == that.mGold && mName.equals(that.mName); } @Override public int hashCode() { int hash = 7; hash = 37 * hash + Objects.hashCode(this.mName); hash = 37 * hash + this.mLevel; hash = 37 * hash + this.mHealthPoints; hash = 37 * hash + this.mMagic; hash = 37 * hash + this.mGold; return hash; } @Override public String toString() { return "Character{" + "Name='" + mName + '\'' + ", Level=" + mLevel + ", Health Points=" + mHealthPoints + ", Magic=" + mMagic + ", Gold=" + mGold + '}'; } public void attack(GameCharacter other) { Random rng= new Random(); int roll = rng.nextInt(10); int damage = roll * this.mLevel; other.mHealthPoints -= damage; if(other.mHealthPoints < 0) other.mHealthPoints = 0; } public void assist(GameCharacter other) { Random rng = new Random(); int roll = rng.nextInt(5); switch(roll) { case 0: other.mHealthPoints += 5 * this.mLevel; this.mMagic -= 5 * this.mLevel; break; case 1: other.mMagic += 5 * this.mLevel; this.mMagic -= 5 * this.mLevel; break; case 2: other.mGold += 5 * this.mLevel; this.mGold -= 5 * this.mLevel; break; case 3: other.mLevel++; other.mGold += 100; other.mHealthPoints += 100; other.mMagic += 100; this.mLevel--; this.mGold -= 100; this.mHealthPoints -= 100; this.mMagic -= 100; case 4: other.mHealthPoints += 100 * mLevel; this.mHealthPoints += 100 * mLevel; break; } } public void rest() { Random rnd = new Random(); int rndNum = rnd.nextInt(2); System.out.println(" ~~~Attempting rest module for " + getName() + "~~~"); if(mHealthPoints < boundaryForAutoHeal && (mHealthPoints + (rndNum * this.mLevel)) < maxHealthPoints) { mHealthPoints += (rndNum * this.mLevel); System.out.println("Healed to health points: " + this.mHealthPoints); } else System.out.println("Health points still above 50%...Auto-heal cancelled!"); } }

Copy the GameCharacter.java class into this new project. This will serve as the parent class for Hero and Villain.

1. Modify the parent class GameCharacter to include inheritance by accomplishing the following:

  • Change the access modifier of all fields (instance variables) from private to protected.
  • Modify the attack(GameCharacter other) method, as follows:
    • Move all code from attack into the child class Hero.java (see step 2)
    • Display a message (using System.out) that states the character's name followed by " does not attack. I'm peaceful :)". For example, "Tall Tree does not attack. I'm peaceful :)"
  • Modify the assist(GameCharacter other) method, as follows:
    • Move all code from assist into the child class Hero.java (see step 2)
    • Display a message (using System.out) that states the character's name followed by " cannot assist." For example, "Tall Tree cannot assist."
  • Modify the rest() as follows:
    • Move all code from rest into the child class Hero.java (see step 2)
    • Display a message (using System.out) that states the character's name followed by " never rests!" For example, "Tall Tree never rests!"

2. Create a child class named Hero that inherits from GameCharacter (extends GameCharacter):

  • Override the the attack(GameCharacter other) method, as follows:
    • Copy all code from attack in the GameCharacter class here.
  • Override the assist(GameCharacter other) method, as follows:
    • Copy all code from attack in the GameCharacter class here.
  • Override the rest() as follows:
    • Copy all code from rest in the GameCharacter class here.
  • Implement the perish() as follows:
    • Display the message "All is lost, our hero has perished :("

3. Create a child class named Villain that inherits from GameCharacter (extends GameCharacter):

  • Override the the attack(GameCharacter other) method, as follows:
    • Create your own attack strategy for the Villain.
  • Override the assist(GameCharacter other) method, as follows:
    • Create your own assist strategy for the Villain.
  • Override the rest() as follows:
    • Display the message "Villains never rest! Are you kidding me? We have too many nefarious things to do!"
  • Implement the perish() as follows:
    • Display the message "Humanity has been restored! The villain has perished."
public class Main { public static void main(String[] args) { GameCharacter me = new GameCharacter("King Larry", 5); GameCharacter enemy = new GameCharacter("Monster Bug", 10); System.out.println(" ~~~Game Started~~~"); System.out.println(me); System.out.println(enemy); System.out.println(" ~~~I'm attacking the enemy~~~"); me.attack(enemy); System.out.println(me); System.out.println(enemy); me.rest(); System.out.println(me); System.out.println(enemy); System.out.println(" ~~~Enemy attacking me~~~"); enemy.attack(me); System.out.println(me); System.out.println(enemy); enemy.rest(); System.out.println(me); System.out.println(enemy); System.out.println(" ~~~I'm attacking the enemy~~~"); me.attack(enemy); System.out.println(me); System.out.println(enemy); me.rest(); System.out.println(me); System.out.println(enemy); System.out.println(" ~~~Enemy attacking me~~~"); enemy.attack(me); System.out.println(me); System.out.println(enemy); enemy.rest(); System.out.println(me); System.out.println(enemy); } }

4. Finally, open the class named Main with a public static void main(String[] args) method that performs the following:

  • Create a Hero to represent yourself!
  • Create a Villain to overcome and save the world.
  • Create at least one more GameCharacter to serve as a neutral role.
  • Optional: Feel free to create an array of characters to make it more interesting!
  • Use the helper methods (e.g. assist, attack, rest) between characters and print out the result of each interaction using System.out.println(...); with each character's name.
  • See below for a sample interaction of the Hero ("Paladin Paulding") battling a Villain ("Monster Bug", as in coding bug) with a neutral GameCharacter ("TallTree").

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

5. If yes, then why?

Answered: 1 week ago

Question

6. How would you design your ideal position?

Answered: 1 week ago