Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

##Final Code:

Final Code:

##Final Code:

Final Code:

deleted 230 characters in body
Source Link
TheCoffeeCup
  • 9.5k
  • 4
  • 38
  • 96

The method static void greeting(Human p) will always print "I'M A CHARACTER ;-(" because:

static void greeting(Human p) { // Takes a Human as a parameter, 
 // so a Warrior will be casted to a Human
 p.yell(); // call yell() in the class Human
}

is unnecessary. I suggest remove the method and just call yell().

The method static void greeting(Human p) will always print "I'M A CHARACTER ;-(" because:

static void greeting(Human p) { // Takes a Human as a parameter, 
 // so a Warrior will be casted to a Human
 p.yell(); // call yell() in the class Human
}

I suggest remove the method and just call yell().

The method static void greeting(Human p) is unnecessary. I suggest remove the method and just call yell().

Source Link
TheCoffeeCup
  • 9.5k
  • 4
  • 38
  • 96

The first thing you are destroying is the formatting. After formatting, it looks like this:

class Human {
 
 protected String name;
 protected int health;
 protected int armorLevel;
 protected int magicLevel;
 protected int experience;
 protected int level;
 Human() {
 name = "Andrew The Magic";
 health = 100;
 armorLevel = 1;
 magicLevel = 1;
 experience = 0;
 level = 1;
 }
 Human(String name) {
 this(); // calling main constructor
 this.name = name;
 }
 void fight() {
 }
 void levelUp() {
 }
 void showAbility() {
 System.out.println("Name: " + name + " HP: " + health + " ARMOR: "
 + armorLevel + " MAGIC: " + magicLevel + " EXP: " + experience
 + " LVL: " + level);
 }
 void yell() {
 System.out.println("IM A CHARACTER ;-(");
 }
}
// --------------------------------------------------------------
class Warrior extends Human {
 Warrior() {
 super();
 }
 Warrior(String name) {
 super(name);
 }
 void fight() {
 System.out.println("I attack with sword");
 experience += 10;
 if (experience >= 20) {
 levelUp();
 }
 }
 void levelUp() {
 System.out.println("LEVEL UP!");
 health += 100;
 armorLevel += 25;
 level++;
 }
 void yell() {
 System.out.println("IM A GLORIOUS WARRIOR " + name);
 }
}
// --------------------------------------------------------------
public class World {
 static void greeting(Human p) {
 p.yell();
 }
 public static void main(String[] args) {
 Warrior w = new Warrior("Noname the Warrior");
 w.showAbility();
 w.fight();
 w.showAbility();
 w.fight();
 w.showAbility();
 greeting(w);
 }
 
}

You do not have to create empty methods just so another class can override them. Just simply remove them:

class Human {
 
 protected String name;
 protected int health;
 protected int armorLevel;
 protected int magicLevel;
 protected int experience;
 protected int level;
 Human() {
 name = "Andrew The Magic";
 health = 100;
 armorLevel = 1;
 magicLevel = 1;
 experience = 0;
 level = 1;
 }
 Human(String name) {
 this(); // calling main constructor
 this.name = name;
 }
 void showAbility() {
 System.out.println("Name: " + name + " HP: " + health + " ARMOR: "
 + armorLevel + " MAGIC: " + magicLevel + " EXP: " + experience
 + " LVL: " + level);
 }
 void yell() {
 System.out.println("IM A CHARACTER ;-(");
 }
}

Also, I assume a normal human will not need the following stats:

protected int health;
protected int armorLevel;
protected int magicLevel;
protected int experience;
protected int level;

Just move them to the Warriors class, and move the assignments as well:

class Human {
 
 protected String name;
 Human() {
 name = "Andrew The Magic";
 }
 Human(String name) {
 this(); // calling main constructor
 this.name = name;
 }
 void showAbility() {
 System.out.println("Name: " + name);
 }
 void yell() {
 System.out.println("IM A CHARACTER ;-(");
 }
}
// --------------------------------------------------------------
class Warrior extends Human {
 protected int health;
 protected int armorLevel;
 protected int magicLevel;
 protected int experience;
 protected int level;
 
 Warrior() {
 super();
 health = 100;
 armorLevel = 1;
 magicLevel = 1;
 experience = 0;
 level = 1;
 }
 Warrior(String name) {
 super(name);
 health = 100;
 armorLevel = 1;
 magicLevel = 1;
 experience = 0;
 level = 1;
 }
 void fight() {
 System.out.println("I attack with sword");
 experience += 10;
 if (experience >= 20) {
 levelUp();
 }
 }
 void levelUp() {
 System.out.println("LEVEL UP!");
 health += 100;
 armorLevel += 25;
 level++;
 }
 void yell() {
 System.out.println("IM A GLORIOUS WARRIOR " + name);
 }
}

Obviously you don't want to repeat code, so change the constructors to:

Warrior() {
 this("Andrew The Magic");
}
Warrior(String name) {
 super(name);
 health = 100;
 armorLevel = 1;
 magicLevel = 1;
 experience = 0;
 level = 1;
}

Also, you don't have to call the constructor that takes no parameters in the Human class. If you do, the first constructor will assign "Andrew The Magic" to name, then assign the parameter passed as name to name.

Human() {
 name = "Andrew The Magic";
}
Human(String name) {
 this.name = name;
}

The method static void greeting(Human p) will always print "I'M A CHARACTER ;-(" because:

static void greeting(Human p) { // Takes a Human as a parameter, 
 // so a Warrior will be casted to a Human
 p.yell(); // call yell() in the class Human
}

I suggest remove the method and just call yell().

##Final Code:

class Human {
 
 protected String name;
 Human() {
 name = "Andrew The Magic";
 }
 Human(String name) {
 this.name = name;
 }
 void showAbility() {
 System.out.println("Name: " + name);
 }
 void yell() {
 System.out.println("I'M A CHARACTER ;-(");
 }
}
// --------------------------------------------------------------
class Warrior extends Human {
 protected int health;
 protected int armorLevel;
 protected int magicLevel;
 protected int experience;
 protected int level;
 
 Warrior() {
 this("Andrew The Magic");
 }
 Warrior(String name) {
 super(name);
 health = 100;
 armorLevel = 1;
 magicLevel = 1;
 experience = 0;
 level = 1;
 }
 void fight() {
 System.out.println("I attack with sword");
 experience += 10;
 if (experience >= 20) {
 levelUp();
 }
 }
 void levelUp() {
 System.out.println("LEVEL UP!");
 health += 100;
 armorLevel += 25;
 level++;
 }
 void yell() {
 System.out.println("IM A GLORIOUS WARRIOR " + name);
 }
}
// --------------------------------------------------------------
public class World {
 public static void main(String[] args) {
 Warrior w = new Warrior("Noname the Warrior");
 w.showAbility();
 w.fight();
 w.showAbility();
 w.fight();
 w.showAbility();
 w.yell();
 }
 
}
lang-java

AltStyle によって変換されたページ (->オリジナル) /