0
This is probably a question that has already answered too much here, but of course, I did not find the solution. The only thing I need is to take the calculation I made in main to another class, returning to the main only result.
- I made a few attempts and what I got was a comeback, Nan. it is worth noting that I am still new to java (alias, in all kkkk languages)
- I have a second class where I put the methods, but I wish there was a third. It would then be the main, just collecting and returning the values, the Data class, storing the data, and finally the formulas that would carry out the calculations.
follow what I have so far:
package atividade;
import java.util.Scanner;
public class Principal {
public static void main(String[] args) {
Scanner num = new Scanner (System.in);
Dados f = new Dados();
System.out.print("Informe os valores abaixo:\n"
+ "A: ");
f.setA(num.nextDouble());
System.out.print("B: " );
f.setB(num.nextDouble());
System.out.print("C: ");
f.setC(num.nextDouble());
System.out.println("\nValores informados: \nA: " + f.getA() +"\nB: " + f.getB() + "\nC: " + f.getC());
double delta, x1, x2;
delta = f.getB()*f.getB()-4*f.getA()*f.getC();
x1 = (f.getB()+ Math.sqrt(delta))/(2*f.getA());
x2 = (f.getB()- Math.sqrt(delta))/(2*f.getA());
System.out.println("S={" + x1 + ", " + x2 + "}");
}
}
package atividade;
public class Dados {
private double a;
private double b;
private double c;
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public double getC() {
return c;
}
public void setC(double c) {
this.c = c;
}
}
Instead of initializing the attributes of the Data class with setters, you should do this with a constructor. And what is the responsibility of the Data class? Save data from a quadratic expression? So it should be called Expression and it should itself calculate its roots.
– Vander Santos
This answers your question? Code for calculating 2nd degree equation returns "Nan" as roots
– Marcos Alexandre
Opa @Vandersantos, I’ll follow this tip then.
– Francisco Alan
@Marcosalexandre did help! vlw!!
– Francisco Alan