Home>


// Next, define the Hero class
public class Hero2 {
String name;
int hp;
Sword sword;
public void attack () {
System.out.println (this.name + "attacked!");
System.out.println ("damaged 5 points to the enemy!");
}
public Hero (String name) {
this.hp = 100;// Initialize hp field with 100
this.name = name;

}
public Hero () {
this.hp = 100;
this.name = "dummy";
}
}

public class Sample {
public static void main (String [] args) {
Hero h1 = new Hero ("Minato");
System.out.println (h1.name);
Hero h2 = new Hero ();
System.out.println (h2.name);
}
}

I tried to use a class that overloaded the constructor
I don't know how to fix the error

Error message
Sample.java: 3: Error: Class Hero constructor Hero cannot be applied to the specified type.
Hero h1 = new Hero ("Minato");
^ ^
Expected value: no arguments
Detected value: String
Reason: The length of the actual argument list and the formal argument list are different.
1 error
``` Enter the language name here
Java

  • Answer # 1

    The names for the classes should be consistent.

    File name to write the class

    class ClassNameClass name to write to the file like

    Constructor function name

    This time, the file name and class name areHero2However, the call to the constructor and the user isHeroAnd do not match.