Graphical Application: shows the roots of a quadratic equation in Java

Java exercise using Dialog Box:

Write a program that receives the coefficients of a quadratic equation and returns its roots, even complex ones, through dialog boxes.

Lear how to create a graphical application that calculate the roots of a quadratic equation



Step 1: Receiving the coefficients of the quadratic equation

First the program receives user data, which are the coefficients of equation 3, 'a', 'b' and 'c':
ax ² + bx + c = 0


Step 2: Checking if 'a' coefficient is not 0

An equation of the second degree is only the second degree if 'a' is different from 0.
If the user enters the value 0, the program enters a loop until the user enters a value other than 0.


Step 3: Checking if the delta is positive and show the real roots


If delta is greater than or equal to 0 the roots are real and are easily calculated by the quadratic formula.


Step 3: Checking if the delta is negative and show the complex roots



If not, the roots will be complex.
To calculate the complex roots do be positive delta (-delta), took its root and calculate separately the real and imaginary part.


Java code of how to to a graphical application that show the roots of a quadratic equation:
import javax.swing.JOptionPane;

public class bhaskara_dialogBoxes {
    
    public static float delta(float a, float b, float c){
        return (b*b - 4*a*c);
    }
    
    public static void main(String[] args){
        String value, roots;
        float a=0, b, c,      //coefficients
        delta,                //delta
        sqrtdelta,            //delta square root
        root1,root2;          //roots

        
        //STEP 1: receive the coefficients
        value = JOptionPane.showInputDialog("Enter the 'a' value: ");
        a = Float.parseFloat(value.trim());
        
        value = JOptionPane.showInputDialog("Enter the 'b' value:");
        b = Float.parseFloat(value.trim());
        
        value = JOptionPane.showInputDialog("Enter the 'c' value:");
        c = Float.parseFloat(value.trim());
        
        //STEP 2: check if 'a' if not 0
        while(a==0){
            if(a==0){
                value = JOptionPane.showInputDialog("'a' can't be 0. Insert other value");
                a = Float.parseFloat(value.trim());
            }
        }
        
        //STEP 3: check if delta is positive. If it is, show the real roots
        if(delta(a,b,c)>=0){
            root1 = (-b + (float)Math.sqrt(delta(a,b,c)))/(2*a);
            root2 = (-b - (float)Math.sqrt(delta(a,b,c)))/(2*a);
            
            roots = String.format("The roots are: %.2f e %.2f", root1, root2);
            
            JOptionPane.showMessageDialog(null,roots);
        
        //STEP 4: if delta is negative, we should format the output to show the complex roots    
        } else {
            
            roots = String.format("The roots are:\n" +
                    "%.2f + %.2fi\n" +
                    "%.2f - %.2fi",(-b/(2*a)), ( (float)Math.sqrt(-delta(a,b,c) ) / (2*a) )
                                  ,(-b/(2*a)), ( (float)Math.sqrt(-delta(a,b,c) ) / (2*a) )
                                   );
            
            JOptionPane.showMessageDialog(null,roots);
            
        }
    }

}

No comments: