How to return 2 objects at the same time in a method in Java?

4
 public class Equacao2Grau {

        int a, b, c;

        public int CalculoDelta(){

            return (int) (Math.pow(b, 2) - 4*a*c);
        }

        public int FormulaQuadratica(){
            int x1 = (int) (-b + Math.sqrt(CalculoDelta()) / 2*a);

            int x2 = (int) (-b - Math.sqrt(CalculoDelta()) / 2*a);

            return x1, x2;
        }

        public String exibeResultado(){

            return "X1 = " + this.FormulaQuadratica() + "\nX2 = " + this.FormulaQuadratica();
        }
    }
    
asked by anonymous 12.09.2017 / 21:48

1 answer

1

You can return some data structure that satisfies your problem:

public class Equacao2Grau {

  int a, b, c;

  public int CalculoDelta() {

    return (int) (Math.pow(b, 2) - 4 * a * c);
  }

  public Map<String, Integer> FormulaQuadratica() {
    Map<String, Integer> resultado = new HashMap<>();
    int x1 = (int) (-b + Math.sqrt(CalculoDelta()) / 2 * a);

    int x2 = (int) (-b - Math.sqrt(CalculoDelta()) / 2 * a);

    resultado.put("x1", x1);
    resultado.put("x2", x2);

    return resultado;
  }

  public String exibeResultado() {
    Map<String, Integer> resultado = this.FormulaQuadratica();

    return "X1 = " + resultado.get("x1") + "\nX2 = " + resultado.get("x2");
  }
}

The Map allows you to "name" the result from the keys.

    
12.09.2017 / 21:53