Question

In: Computer Science

I can't seem to make my inheritance to work properly between my parent class GameCharacter and...

I can't seem to make my inheritance to work properly between my parent class GameCharacter and the child classes hero and villain.

Here's my code:

import java.sql.SQLOutput;

public class Main {

public static void main(String[] args) {

GameCharacter me = new GameCharacter("King Arthur", 5);
GameCharacter tree = new GameCharacter("Tall Tree", 5);
GameCharacter enemy = new GameCharacter("Monster Bug", 10);

System.out.println();
System.out.println("\n~~~ Game Characters Created ~~~");
System.out.println(tree);
System.out.println(me);
System.out.println(enemy);

System.out.println("\n~~~ The Bug Has Been Attacked ~~~");
me.attack(enemy);
System.out.println(tree);
System.out.println(me);
System.out.println(enemy);

System.out.println("\n~~~ The Bug Assists Itself ~~~");
enemy.assist(enemy);
System.out.println(tree);
System.out.println(me);
System.out.println(enemy);

System.out.println("\n~~~ Everybody Rests ~~~");
tree.rest();
enemy.rest();
me.rest();
System.out.println(tree);
System.out.println(me);
System.out.println(enemy);
}
}

import java.util.Objects;
import java.util.Random;

public class GameCharacter {

protected String mName;
protected int mLevel;
protected int mHealthPoints;
protected int mMagic;
protected int mGold;

public GameCharacter(String name) {
this(name, 1);
}

public GameCharacter(String name, int level) {
mName = name;
mLevel = level;
mHealthPoints = 100 * mLevel;
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() {

return Objects.hash(mName, mLevel, mHealthPoints, mMagic, mGold);
}

@Override
public String toString() {
return "GameCharacter{" +
"Name ='" + mName + '\'' +
", Level = " + mLevel +
", Health Points = " + mHealthPoints +
", Magic = " + mMagic +
", Gold = " + mGold +
'}';
}

public void attack(GameCharacter other) {

System.out.println(mName + " does not attack. I'm peaceful :)");
}

public void assist(GameCharacter other) {

System.out.println(mName + " cannot assist");
}

public void rest() {

System.out.println(mName + " never rests!");
}
}


import java.util.Random;

public class Hero extends GameCharacter {

public Hero(String name) {

super(name);
}

public Hero(String name, int level) {

super(name, level);
}

@Override
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;
}

@Override
public void assist(GameCharacter other) {

Random rng = new Random();
int roll = rng.nextInt(5);

switch (roll) {

case 0:
other.mHealthPoints += 5 * this.mLevel;
this.mHealthPoints -= 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;
}
}

@Override
public void rest() {

Random rng = new Random();
int roll = rng.nextInt(2);
int health = rng.nextInt(6) + 25;

if (roll == 0 || roll == 1) {
mHealthPoints += health;
}
}

public void perish(){

System.out.println("All is lost, our hero has perished :(");
}
}

import java.util.Random;

public class Villain extends GameCharacter {

public Villain(String name) {

super(name);
}

public Villain(String name, int level) {

super(name, level);
}

@Override
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;
}

  
@Override
public void assist(GameCharacter other) {

Random rng = new Random();
int roll = rng.nextInt(5);

switch (roll) {

case 0:
other.mHealthPoints += 5 * this.mLevel;
this.mHealthPoints -= 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;
}
}

@Override
public void rest() {

System.out.println("Villains never rest! Are you kidding me? We have too many nefarious things to do!");
}

public void perish(){

System.out.println("Humanity has been restored! The villain has perished.");
}
}

Solutions

Expert Solution

Please check the comments in the program for details...

/***********************************Main.java*****************************/

public class Main {

   public static void main(String[] args) {

       /*
       * change here to achieve polymorphic behavior of objects You need to create
       * subclass type object and assign to superclass reference if you want to call
       * specific sub class method then down cast that object
       */
       GameCharacter me = new Hero("King Arthur", 5);
       GameCharacter tree = new GameCharacter("Tall Tree", 5);
       GameCharacter enemy = new Villain("Monster Bug", 10);

       System.out.println();
       System.out.println("\n~~~ Game Characters Created ~~~");
       System.out.println(tree);
       System.out.println(me);
       System.out.println(enemy);

       System.out.println("\n~~~ The Bug Has Been Attacked ~~~");
       me.attack(enemy);
       System.out.println(tree);
       System.out.println(me);
       System.out.println(enemy);

       System.out.println("\n~~~ The Bug Assists Itself ~~~");
       enemy.assist(enemy);
       System.out.println(tree);
       System.out.println(me);
       System.out.println(enemy);

       System.out.println("\n~~~ Everybody Rests ~~~");
       tree.rest();
       enemy.rest();
       me.rest();
       System.out.println(tree);
       System.out.println(me);
       System.out.println(enemy);
   }
}

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

~~~ Game Characters Created ~~~
GameCharacter{Name ='Tall Tree', Level = 5, Health Points = 500, Magic = 500, Gold = 500}
GameCharacter{Name ='King Arthur', Level = 5, Health Points = 500, Magic = 500, Gold = 500}
GameCharacter{Name ='Monster Bug', Level = 10, Health Points = 1000, Magic = 1000, Gold = 1000}

~~~ The Bug Has Been Attacked ~~~
GameCharacter{Name ='Tall Tree', Level = 5, Health Points = 500, Magic = 500, Gold = 500}
GameCharacter{Name ='King Arthur', Level = 5, Health Points = 500, Magic = 500, Gold = 500}
GameCharacter{Name ='Monster Bug', Level = 10, Health Points = 970, Magic = 1000, Gold = 1000}

~~~ The Bug Assists Itself ~~~
GameCharacter{Name ='Tall Tree', Level = 5, Health Points = 500, Magic = 500, Gold = 500}
GameCharacter{Name ='King Arthur', Level = 5, Health Points = 500, Magic = 500, Gold = 500}
GameCharacter{Name ='Monster Bug', Level = 10, Health Points = 2970, Magic = 1000, Gold = 1000}

~~~ Everybody Rests ~~~
Tall Tree never rests!
Villains never rest! Are you kidding me? We have too many nefarious things to do!
GameCharacter{Name ='Tall Tree', Level = 5, Health Points = 500, Magic = 500, Gold = 500}
GameCharacter{Name ='King Arthur', Level = 5, Health Points = 525, Magic = 500, Gold = 500}
GameCharacter{Name ='Monster Bug', Level = 10, Health Points = 2970, Magic = 1000, Gold = 1000}

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


Related Solutions

I need to write a program and can't seem to make it run properly. Someone can...
I need to write a program and can't seem to make it run properly. Someone can help with the coding? It's with python Thanks! Here is the program: The rules of Shut the Box are as follows (abbreviated version): + You have two 5-sided die + You have 5 wooden blocks (labeled 1 through 5) + Each turn, you roll the dice and knock down the blocks corresponding to the number on each die + You have to knock down...
How would I go about debugging this to make it work properly? import java.util.Scanner; public class...
How would I go about debugging this to make it work properly? import java.util.Scanner; public class TemperatureConverter{ public static void main(String[] args) { // Declare named constants. final double MIN_FAHRENHEIT = -459.67; // Declare the variables. double fahrenheit; double convertedDegrees; int tempScale; String tempScaleStr=""; // Creating the Scanner object Scanner keyboard = new Scanner(System.in); System.out.print("Enter the temperature in Fahrenheit: "); fahrenheit = keyboard.nextDouble(); // Set a breakpoint here // Verify the user's input if (fahrenheit > MIN_FAHRENHEIT) { // first...
This is a question for my problem-solving class. I am really stuck and I can't see...
This is a question for my problem-solving class. I am really stuck and I can't see much of a pattern so I would appreciate if someone could draw out for each thief and explain the pattern to get the answer for 40 thieves! Question: Forty thieves, all different ages, steal a huge pile of identical gold coins and must decide how to divide them up. They settle on the following procedure. The youngest divides the coins among the thieves however...
I can't understand the difference between 'the work the electric force does' and 'the work the...
I can't understand the difference between 'the work the electric force does' and 'the work the electric field does'. Could you explain it in detail?
This isn't a homework but since I can't seem to find any answers for it, I...
This isn't a homework but since I can't seem to find any answers for it, I would like to receive some insights from you experts! So, I heard that UK has official left the European Union on Jan 31st (aka Brexit). Are there any trade deals that have been signed? or any negotiations that has been reached? please provide me with some updates I can't seem to find any info on which trade deals were signed and etc. thanks! p.s...
I need to draw a cylinder in java with user input, and I can't seem to...
I need to draw a cylinder in java with user input, and I can't seem to get my lines to line up with my ovals correctly from the users input... I know I will have to either add or subtract part of the radius or height but I'm just not getting it right, here is how I'm looking to do it.            g.drawOval(80, 110, radius, height);            g.drawLine(?, ?, ?, ?); g.drawLine(?, ?, ?, ?);   ...
LED headlights on cars seem to becoming increasingly popular, so I thought I would make my...
LED headlights on cars seem to becoming increasingly popular, so I thought I would make my problem about that. I believe that the proportion of car owners who have LED headlights is higher than the proportion of car owners to use regular non-LED headlights now, (according to data from 2019) versus in 2010. During two independent surveys, one survey noted that 355 out of 600 car owners had LED headlights in 2020, while in 2010, 250 out of 600 car...
How can I write an essay about Endoscopies with an bibliography? I can't seem to find...
How can I write an essay about Endoscopies with an bibliography? I can't seem to find an example. I need a step by step guide. Who can help me write it please and Thank you
Looking for assistance on these three quick questions as I can't seem to figure them out....
Looking for assistance on these three quick questions as I can't seem to figure them out. Thanks in advance for your help! 1. Which of the following would decrease the depth of breathing? A. Increased arterial PCO2 B. Exercising C. Increased action potential frequency in neurons from the ventral respiratory group D. Acidic plasma pH E. Decreased action potential frequency in neurons from the dorsal respiratory group F. Overactivation of the respiratory center in the medulla 2. Which of the...
I can't seem to figure out what the hybridization of PF6(-) is. could someone explain how...
I can't seem to figure out what the hybridization of PF6(-) is. could someone explain how to find it correctly for me?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT