0

So I'm attempting to create a very simple program to practice some basic Java formatting skills. However, something about the "fight()" is driving my compiler crazy. Does anyone see why? Thanks in advance for any answers I receive, code below:

class Hero{
    String name;
    int Intelligence;
    boolean parents;

    public static fight(Hero1, Hero2){
    if(Hero1.Intelligence>Hero2.Intelligence){
        return(Hero1.name+" is the winner");
    else
        return(Hero2.name+" is the winner");
        }
    }
}



class HeroMain{
    public static void main(String[] args){
    Hero Superman = new Hero();
    Superman.name = "Superman";
    Superman.Intelligence = 7;
    Superman.parents = false;

    Hero Batman = new Hero();
    Batman.name = "Batman";
    Batman.Intelligence = 8;
    Batman.parents = false;

    public fight(Superman, Batman);
    }
}
TrigonDark
  • 184
  • 2
  • 9
  • Aside from what the answers already say, you should really check your braces (`{}`), they seem to be quite out of balance, which may also confuse your compiler. – fvu Feb 03 '14 at 00:12

3 Answers3

5

you need to write

public static String fight(Hero hero1, Hero hero2) {

You also need to call fight() as follows:

Hero.fight(Superman, Batman);

Also, as a rule of thumb in Java, you should begin all your variables with a lowercase letter. That's just coding convention.

La-comadreja
  • 5,627
  • 11
  • 36
  • 64
  • 1
    Oh right, I have to specify the type of data returned by the method.I feel kind of silly for forgetting to call the method using dot notation. Thanks for the help! – TrigonDark Feb 03 '14 at 00:08
3

as La-comadreja stated the issue is the fact your trying to return a string, yet you do not define that on your method header.

example

public static void fight() {} does not return, it just does something public static String fight(){} returns a String

tommy knocker
  • 371
  • 1
  • 7
  • 19
1

Here are a few corrections:

class Hero{
    String name;
    int Intelligence;
    boolean parents;

    public static String fight(Hero Hero1, Hero Hero2){ <-- specify type of parameter and return type
        if(Hero1.Intelligence>Hero2.Intelligence) <-- you had a curly-brace problem
            return(Hero1.name+" is the winner");
        else
            return(Hero2.name+" is the winner");
    }
}



class HeroMain{
    public static void main(String[] args){
        Hero Superman = new Hero();
        Superman.name = "Superman";
        Superman.Intelligence = 7;
        Superman.parents = false;

        Hero Batman = new Hero();
        Batman.name = "Batman";
        Batman.Intelligence = 8;
        Batman.parents = false;

        String myStr = Hero.fight(Superman, Batman); <-- call a hero's fight method
        System.out.println(myStr); // "Batman is the winner"
    }

}
pcnThird
  • 2,362
  • 2
  • 19
  • 33
  • Curly brackets for if statements, I should really remember that. Thanks! – TrigonDark Feb 03 '14 at 00:28
  • 1
    It is possible to use if/else statements without curly braces like in the code above, but using braces are preferred if you think you're going to modify something in there later on: http://stackoverflow.com/questions/2125066/is-it-bad-practice-to-use-an-if-statement-without-brackets – pcnThird Feb 03 '14 at 00:36