In: Computer Science
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.");
}
}
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 :)