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);
    }
}
1
  • 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. Commented Feb 3, 2014 at 0:12

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!
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

Comments

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"
    }

}

2 Comments

Curly brackets for if statements, I should really remember that. Thanks!
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: stackoverflow.com/questions/2125066/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.