HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Quadratic Calculator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
quadraticcalculatorstackoverflow

Problem

I am about a month into learning Java and I'm just looking for some comments on my code. Specifically, I would like to know if I'm being as efficient as I can be. This is the most complex thing I've come up with so far (that works) and I wanted some feedback on my developing coding style.

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    double numOne, numTwo, numThree, negativeB,
            discriminant, divisor, sqRtDisc, dividendPlus,
            dividendMinus;

    System.out.println("Enter the value for A: ");
    numOne = sc.nextFloat();

    System.out.println("Enter the value for B: ");
    numTwo = sc.nextFloat();

    System.out.println("Enter the value for C: ");
    numThree = sc.nextFloat();

    negativeB = -numTwo;
    discriminant = (numTwo * numTwo) - 4 * numOne * numThree;
    divisor = 2 * numOne;
    sqRtDisc = Math.sqrt(discriminant);

    System.out.println(" ");

    if (sqRtDisc > 0) {
        dividendPlus = ((negativeB + sqRtDisc) / divisor);
        dividendMinus = ((negativeB - sqRtDisc) / divisor);

        System.out.println("x = " + dividendPlus);
        System.out.println("x = " + dividendMinus);
    } else {
        System.out.println("x = " + (negativeB / divisor) + " + " 
                + (Math.sqrt(-discriminant) / divisor) + "i");
        System.out.println("x = " + (negativeB / divisor) + " - " 
                + (Math.sqrt(-discriminant) / divisor) + "i");
        System.out.println(" ");
    }

}

Solution

Bugs

You are losing precision by calling sc.nextFloat() instead of sc.nextDouble().

If the equation has complex roots, Math.sqrt(discriminant) will crash.

Style

You are using too many variables to easily keep track of them mentally. Also, it's better to avoid a giant block of declarations like your

double numOne, numTwo, numThree, negativeB,
        discriminant, divisor, sqRtDisc, dividendPlus,
        dividendMinus;


We know that we need to store at least a, b, and c, so let's start there. Why not use the conventional terminology instead of numOne, numTwo, numThree?

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the value for A: ");
    double a = sc.nextDouble();

    System.out.println("Enter the value for B: ");
    double b = sc.nextDouble();

    System.out.println("Enter the value for C: ");
    double c = sc.nextDouble();


from there, the next important calculation is the discriminant, and optionally, its square root and the divisor.

double discriminant = (b * b) - 4 * a * c;
    double divisor = 2 * a;


From there, you could go straight to formatting. I suggest System.out.printf() for readability.

if (discriminant < 0) {         // Complex roots
        System.out.printf("x = %d + %d i\n",
                          -b / divisor, Math.sqrt(-discriminant) / divisor));
        System.out.printf("x = %d - %d i\n",
                          -b / divisor, Math.sqrt(-discriminant) / divisor));
    } else {
        System.out.println("x = " + (-b + Math.sqrt(discriminant)) / divisor);
        System.out.println("x = " + (-b - Math.sqrt(discriminant)) / divisor);
    }
}

Code Snippets

double numOne, numTwo, numThree, negativeB,
        discriminant, divisor, sqRtDisc, dividendPlus,
        dividendMinus;
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the value for A: ");
    double a = sc.nextDouble();

    System.out.println("Enter the value for B: ");
    double b = sc.nextDouble();

    System.out.println("Enter the value for C: ");
    double c = sc.nextDouble();
double discriminant = (b * b) - 4 * a * c;
    double divisor = 2 * a;
if (discriminant < 0) {         // Complex roots
        System.out.printf("x = %d + %d i\n",
                          -b / divisor, Math.sqrt(-discriminant) / divisor));
        System.out.printf("x = %d - %d i\n",
                          -b / divisor, Math.sqrt(-discriminant) / divisor));
    } else {
        System.out.println("x = " + (-b + Math.sqrt(discriminant)) / divisor);
        System.out.println("x = " + (-b - Math.sqrt(discriminant)) / divisor);
    }
}

Context

StackExchange Code Review Q#78529, answer score: 6

Revisions (0)

No revisions yet.