I am something new in Java and I have a program that displays a graphical interface and calculates the roots of a second degree equation (x1 and x2), if the discriminant is < 0 and several other cases:
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class CuadraticasGUI extends JFrame implements ActionListener
{
private JTextField tfA, tfB, tfC;
private JButton bCalcular, bSalir;
private JTextArea taDatos, taDatos2;
private JPanel panel1, panel2;
private CalculosEC calculos = new CalculosEC();
public CuadraticasGUI()
{
super("Calculo de las raices de una ecuacion cuadratica de 2do grado." );
tfA = new JTextField();
tfB = new JTextField();
tfC = new JTextField();
bCalcular= new JButton("Calcular Raices");
bSalir = new JButton("Salir");
panel1 = new JPanel();
panel2 = new JPanel();
taDatos = new JTextArea(10,30);
bSalir.addActionListener(this);
bCalcular.addActionListener(this);
panel1.setLayout(new GridLayout(4,2));
panel2.setLayout(new FlowLayout());
panel1.add(new JLabel("Coeficiente A = "));
panel1.add(tfA);
panel1.add(new JLabel("Coeficiente B = "));
panel1.add(tfB);
panel1.add(new JLabel("Coeficiente C = "));
panel1.add(tfC);
panel1.add(bCalcular);
panel1.add(bSalir);
panel2.add(panel1);
panel2.add(new JScrollPane(taDatos));
add(panel2);
setSize(500,350);
setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource()== bSalir)
{
System.exit(0);
}
if(event.getSource()== bCalcular)
{
String strA=tfA.getText();
float a=Float.parseFloat(strA);
String strB=tfB.getText();
float b=Float.parseFloat(strB);
String strC=tfC.getText();
float c=Float.parseFloat(strC);
float disc = calculos.CuadraticasGUI(a, b, c);
if(a==0)
{
if(b==0)
{
taDatos.setText("La ecuacion no existe");
}
else
{
float x=(-c)/(b);
taDatos.setText("A es igual a 0 por lo tanto X="+x);
}
}
else{
double x1,x2=0;
if(disc>0)
{
x1=(-b+Math.sqrt(disc))/(2*a);
x2=(-b-Math.sqrt(disc))/(2*a);
taDatos.setText("X1= " +x1+ " X2= " +x2);
}
if(disc==0)
{
double x=(-b)/(2*a);
taDatos.setText("X= "+x);
}
if(disc<0)
{
taDatos.setText("Las raices son imaginarias");
}
}
}
}
public static void main(String args[])
{
CuadraticasGUI cuadratica = new CuadraticasGUI();
}
}
So, what I want to do and I can not do is move operations to another file
Code:
public class CalculosEC
{
public float CuadraticasGUI(float a, float b, float c)
{
float disc=0;
disc=b*b-4*a*c;
return disc;
}
}
As you can see there, I only calculate disc. and I use the return so that in CuadraticasGUI it passes through the if and display depending on the conditions.
The program works perfectly as it happens, but I want to pass all the operations from if (a == 0) to the other file (CalculosEC) and in CuadraticasGUI to deploy, depending on the situation, the results.
Would someone be so kind to help me?