Question

So I had to make a program in Java to solve for the values of "x" using the quadratic formula, and this includes imaginary and real numbers. The problem is that my code doesn't seem to give correct results for the following 3 values:

a=10,000
b=75,000
c=35,000

It returns correct values for small integers, however. Any idea why? I think it has to do with the data type of double, but I'm not sure.

Note that the requirements included that the inputs be of type integer only

I believe the correct results are -7.0 and -0.5, but I am receiving imaginary results. Thanks

Here is my code:

package QuadRootsInt;
import java.util.Scanner;
/**
 *
 * @author
 */
public class QuadRootsInt {

    /**
     * Instance variables
     */
    private double realNumb;
    private double imagNumb;
    private double finalRoot1;
    private double finalRoot2;
    /**
     * 
     * @param a a value of binomial
     * @param b b value of binomial
     * @param c c value of binomial
     */
    public QuadRootsInt(int a, int b, int c){
        if(a==0){
            System.out.println("Cannot divide by zero...system is exisitng.");
            System.exit(0);
        }
    }

    /**
     * Evaluating the square root part of the formula and updates the right variable accordingly
     * @param a first coefficient of binomial
     * @param b second coefficient of binomial
     * @param c third coefficient of binomial
     */
    public void getRoot(int a, int b, int c){
        if((b*b)<4*a*c){
            imagNumb=Math.sqrt(Math.abs(Math.pow(b,2)-4*a*c));
            double realFinal1=((-b)/(2.0*a));
            double imagFinal1=(imagNumb/(2.0*a));
            double realFinal2=((-b)/(2.0*a));
            double imagFinal2=(imagNumb/(2.0*a));
            System.out.println("The solutions to the quadratic are: " + realFinal1+"+"+"i"+imagFinal1 + " and " + realFinal2+"-"+"i"+imagFinal2);
        }
        else {
            realNumb=Math.sqrt(Math.pow(b, 2)-4*a*c);
            finalRoot1=((-b)+realNumb)/(2*a);
            finalRoot2=((-b)-realNumb)/(2*a);
            System.out.println("The solutions to the quadratic are: " + finalRoot1 + " and " + finalRoot2);
        }

    }
    /**
     * Main Method - Testing out the application
     * @param args 
     */
    public static void main(String args[]){
        Scanner aCoef = new Scanner(System.in);
        System.out.print("Enter the 'a' coefficient: ");
        int aInput = aCoef.nextInt();
        Scanner bCoef = new Scanner(System.in);
        System.out.print("Enter the 'b' coefficient: ");
        int bInput = bCoef.nextInt();
        Scanner cCoef = new Scanner(System.in);
        System.out.print("Enter the 'c' coefficient: ");
        int cInput = cCoef.nextInt();
        QuadRootsInt quadTest = new QuadRootsInt(aInput, bInput, cInput);
        quadTest.getRoot(aInput, bInput, cInput);
    }

}
Était-ce utile?

La solution

You should NOT use int type for your a, b, c coefficients. If you use double type, your code should work.

When you use integers, then code that is using b*b or 4*a*c is likely to cause integer overflow and throw off your code logic.

Autres conseils

Because a, b, and c are ints, the expressions (b*b) and 4*a*c are also calculated as int values. The largest possible value of an int is 2,147,483,647, or about 33,000 x 65,000.

For a quadratic equation, where fractional coefficients are common, you should use double.

Incidentally, note that there is a solution in the a = 0 case - that would make it a linear equation.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top