What is the error in this return exercise: Object orientation

Asked

Viewed 75 times

0

public class Matematica {

    /*
     * @param um
     * @param dois
     * return o maior dos dois numeros
     * 
     */
    int maior(int um, int dois){            
        if( um  > dois) {               
            return um;
        }
        else {              
            return dois;                
        }
      return 0;
    }
}

public class MatematicaTeste {
    public static void main(String[] args) {            
    }
    Matematica m = new Matematica();
    int maior = m.maior(10, 20);
    System.out.println(maior); //Da Error Aqui, alguém poderia me dizer porque?
}
  • 1

    Leandro, I just set the format to show everything as a code block (http://answall.com/editing-help). It’s nice to leave code with organized indentation and no excess of blank lines. It is easier for you to visualize the logic and better for those who will analyze your code. . . . . When you ask a question, say exactly what and where it does not work. Generated error messages? Expected output and obtained output. See [Ask].

  • Sorry, I’m new to the Zone

  • In the problemo, I forgot to welcome [en.so] :) [ps] you are free to [Edit] the question whenever necessary.

2 answers

5

Are two mistakes:

1st - In your class Matematica, take off the return 0. There’s no reason he should exist there, since, if um > dois, will return um. If not, return dois

2nd - The other error is in your Test class.

The call to methods is outside the main void.

Try it like this:

public class MatematicaTeste {
    public static void main(String[] args) {


        Matematica m = new Matematica();
        int maior = m.maior(10, 20);
        System.out.println(maior); 
    }

}

Whenever using a test class, place the instructions inside the void main.

4

You put the controls off the main. Correcting this and removing the return 0 of function maior(), swirled normal here.

public static void main(String[] args) {
      Matematica m = new Matematica();
      int maior = m.maior(10, 20);
      System.out.println(maior); 
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.