Hey, So I’ve been working on some homework assignments for a class I’m in and figured this might be an interesting topic. One of my assigned problems asked me to create a Java program that takes in three inputs (a, b, c), enters them into the quadratic equation ((-b+/-sqrt(b^2-4ac))/(2a)), the outputs the roots.

This project assumes you are familiar enough with NetBeans or your IDE of choice that you can set up a java project and run it. If not no worries, just check out my post that walks you through getting started with java here

  1. First off we need to open a java editor (I will be using NetBeans) and create a new project folder with a file containing a main method.
  2. quad

    Heres the code if you want to copy it

    package quad;
    
    /**
     *
     * @author Kyle
     */
    public class Quad {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
        }
        
    }
  3. I’m going to make a method that takes three doubles as parameters, just so I might be able to reuse this code in the future. Keep in mind that depending on the equation, there can be zero, one, or two total roots. In order to account for that I will return an array with the first item identifying the number of roots, and the second and third items containing the roots themselves
  4. static double[] quadratic(double a, double b, double c){

    Note the double[] as the return type.

  5. Alright, first we should check to see if a = 0, because if it does, then the equation is not quadratic and we should return immediately. Then we will want to check whether the part of the equation under the square root is >, =, or < zero. If it is < 0, there are no roots. If it = 0, there is one root. And if it is greater than 0, there are two roots. So:
  6. Here is the full method

    static double[] quadratic(double a, double b, double c){
            
            double underRoot = (b*b)-(4*a*c);
            double positiveRoot;
            double negativeRoot;
            double singleRoot;
            double[] rootArray = new double[3];
            
            if(a == 0){
                rootArray[0] = 3;
                return rootArray;
            }
            else if(underRoot < 0){
                rootArray[0] = 0;
                rootArray[1] = 0;
                return rootArray;
            }else if(underRoot == 0){
                singleRoot = -b/(2.0*a);
                rootArray[0] = 1;
                rootArray[1] = singleRoot;
                return rootArray;
            }else{
                positiveRoot = (-b+Math.sqrt(underRoot))/(2.0*a);
                negativeRoot = (-b+Math.sqrt(underRoot))/(2.0*a);
                rootArray[0] = 2;
                rootArray[1] = positiveRoot;
                rootArray[2] = negativeRoot;
                return rootArray;
            }
        }
  7. Now lets add a method that will take in our array and print out the number of roots and their values.
  8. Note this method is void because it does not return anything. It just prints to the screen.

    static void displayRoots(double[] arr){
            switch((int)(arr[0])){
                case 0:
                    System.out.println("There are no roots");
                    break;
                case 1:
                    System.out.println("There is one root.");
                    System.out.println("It is: " + arr[1]);
                    break;
                case 2:
                    System.out.println("There are two roots.");
                    System.out.println("Positive: " + arr[1]);
                    System.out.println("Negative: " + arr[2]);
                    break;
                case 3:
                    System.out.println("Not a quadratic equation.");
            }             
        }
  9. Awesome, now we just need to add some stuff to our main method so we can see it in action. I decided to let the user enter three values of their choice. You can do that too, or you can just hard-code a test case in.
  10. /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            
            System.out.print("Enter a: ");
            double aNum = input.nextDouble();
            
            System.out.print("Enter b: ");
            double bNum = input.nextDouble();
            
            System.out.print("Enter c: ");
            double cNum = input.nextDouble();
            
            displayRoots(quadratic(aNum, bNum, cNum));
        }
        
    }
  11. Cool beans. That's it, now you have this code and if for some reason you're ever asked to make it again you can just pull this up. This might be good to keep for a programming interview.
  12. Here's the full code, TLDR:

    /*
     * I have abided by the UNCG Honor Code
    
     */
    package assignment1question4;
    import java.util.Scanner;
    
    /**
     * @author Kyle
     */
    public class Assignment1Question4 {
        
        /**
         * Method that calculates the roots of a quadratic equation given its roots.
         * @param a
         * @param b
         * @param c
         * @return 
         */
        static double[] quadratic(double a, double b, double c){
            
            double underRoot = (b*b)-(4*a*c);
            double positiveRoot;
            double negativeRoot;
            double singleRoot;
            double[] rootArray = new double[3];
            
            if(a == 0){
                rootArray[0] = 3;
                return rootArray;
            }
            else if(underRoot < 0){
                rootArray[0] = 0;
                rootArray[1] = 0;
                return rootArray;
            }else if(underRoot == 0){
                singleRoot = -b/(2.0*a);
                rootArray[0] = 1;
                rootArray[1] = singleRoot;
                return rootArray;
            }else{
                positiveRoot = (-b+Math.sqrt(underRoot))/(2.0*a);
                negativeRoot = (-b+Math.sqrt(underRoot))/(2.0*a);
                rootArray[0] = 2;
                rootArray[1] = positiveRoot;
                rootArray[2] = negativeRoot;
                return rootArray;
            }
        }
        
        /**
         * Method prints the results of the quadratic function to the screen
         * @param arr 
         */
        static void displayRoots(double[] arr){
            switch((int)(arr[0])){
                case 0:
                    System.out.println("There are no roots");
                    break;
                case 1:
                    System.out.println("There is one root.");
                    System.out.println("It is: " + arr[1]);
                    break;
                case 2:
                    System.out.println("There are two roots.");
                    System.out.println("Positive: " + arr[1]);
                    System.out.println("Negative: " + arr[2]);
                    break;
                case 3:
                    System.out.println("Not a quadratic equation.");
            }             
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            
            System.out.print("Enter a: ");
            double aNum = input.nextDouble();
            
            System.out.print("Enter b: ");
            double bNum = input.nextDouble();
            
            System.out.print("Enter c: ");
            double cNum = input.nextDouble();
            
            displayRoots(quadratic(aNum, bNum, cNum));
        }
        
    }