Alright so I'm coding a Quadratic Equation Solver to help with my Math Class. However the teacher requires each calculation to be written out. I have the solver working, however I'm not sure where to start when it comes to printing each step. I know I will need to `System.out.println()` each step. But how does Java interpret the equation? Does it do proper BEDMAS(Brackets, Exponents, Division, Multiplication, Addition, Subtraction)?

``````        if(disc == 0)
{
x1 = (-b+Math.sqrt(Math.pow(b, 2)-4*a*c))/(2*a);
System.out.println("One Root Found: " + x1);
}
else if(disc > 0)
{
x1 = (-b+Math.sqrt(Math.pow(b, 2)-4*a*c))/(2*a);
x2 = (-b-Math.sqrt(Math.pow(b, 2)-4*a*c))/(2*a);
//Output Results
System.out.println("Two Roots Found: " + x1 + " or " + x2);
}
else if(disc < 0)
{
disc = (Math.pow(b, 2)-4*a*c);
double re = -b / (2 * a);

System.out.println("Root1: " + re + " + " + (Math.sqrt(-disc) / (2 * a)) + "i");
System.out.println("Root2: " + re + " - " + (Math.sqrt(-disc) / (2 * a)) + "i");
}
``````

But how does Java interpret the equation? Does it do proper BEDMAS

Yes

Java Language Spec:

15.7.3 Evaluation Respects Parentheses and Precedence
The Java programming language respects the order of evaluation indicated
explicitly by parentheses and implicitly by operator precedence.

## All 3 Replies

But how does Java interpret the equation? Does it do proper BEDMAS

Yes

Java Language Spec:

15.7.3 Evaluation Respects Parentheses and Precedence
The Java programming language respects the order of evaluation indicated
explicitly by parentheses and implicitly by operator precedence.

Alright, here is the code:

``````package com.github.geodox.quadraticcalculator;

import java.util.Scanner;

{

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

//Declare Variables
double a = 0;
double b = 0;
double c = 0;

double disc = 0;
double x1 = 0;
double x2 = 0;
//Take Inputs
System.out.println("Input values 'a', 'b', and 'c' for ax^2+bx+c = 0");
System.out.print("Input a: ");
a = s.nextDouble();
System.out.print("Input b: ");
b = s.nextDouble();
System.out.print("Input c: ");
c = s.nextDouble();

//Print Equation
System.out.println("Equation: " + a + "x^2 + " + b + "x + " + c);

//Process Inputs
//Calculate discriminant
disc = Math.pow(b, 2)-4*a*c;
System.out.println("Discriminant: " + disc);

if(disc == 0)
{
System.out.println("x = " + -b + "+sqrt(" + Math.pow(b, 2) + "-4 * " + a + " * " + c + ")/(2 * " + a + ")");
System.out.println("  = " + -b + "+sqrt(" + (Math.pow(b, 2)-4*a*c) + "/(2 * " + a);
System.out.println("  = " + -b + "+sqrt(" + (Math.pow(b, 2)-4*a*c) + "/" + (2 * a));
System.out.println("  = " + -b + Math.sqrt((Math.pow(b, 2)-4*a*c)) + "/" + (2 * a));
x1 = (-b+Math.sqrt(Math.pow(b, 2)-4*a*c))/(2*a);
System.out.println("  = " + x1);
System.out.println("One Root Found: " + x1);
}
else if(disc > 0)
{
System.out.println("x1 = " + -b + "+sqrt(" + Math.pow(b, 2) + "-4 * " + a + " * " + c + ")/(2 * " + a + ")");
System.out.println("   = " + -b + "+sqrt(" + (Math.pow(b, 2)-4*a*c) + "/(2 * " + a + "))");
System.out.println("   = " + -b + "+sqrt(" + (Math.pow(b, 2)-4*a*c) + "/" + (2 * a) + ")");
System.out.println("   = " + -b + "+" + Math.sqrt((Math.pow(b, 2)-4*a*c)) + "/" + (2 * a));
x1 = (-b+Math.sqrt(Math.pow(b, 2)-4*a*c))/(2*a);
System.out.println("   = " + x1);
System.out.println("x2 = " + -b + "-sqrt(" + Math.pow(b, 2) + "-4 * " + a + " * " + c + ") / (2 * " + a + ")");
System.out.println("   = " + -b + "-sqrt(" + (Math.pow(b, 2)-4*a*c) + "/ (2 * " + a + "))");
System.out.println("   = " + -b + "-sqrt(" + (Math.pow(b, 2)-4*a*c) + "/" + (2 * a) + ")");
System.out.println("   = " + -b + -Math.sqrt((Math.pow(b, 2)-4*a*c)) + "/" + (2 * a));
x2 = (-b-Math.sqrt(Math.pow(b, 2)-4*a*c))/(2*a);
System.out.println("   = " + x2);
//Output Results
System.out.println("Two Roots Found: " + x1 + " or " + x2);
}
else if(disc < 0)
{
disc = (Math.pow(b, 2)-4*a*c);

System.out.println("x1|y1 = " + -b + " / " + 2*a);
System.out.println("x1|y1 = " + -b / (2 * a));
System.out.println("x2|y2 = sqrt(" + -disc + ")" + "/" + (2 * a));
System.out.println("x2|y2 = " + Math.sqrt(-disc) / (2 * a));

System.out.println("x1 +- x2");
System.out.println("x+: " + -b / (2 * a) + " + " + (Math.sqrt(-disc) / (2 * a)) + "i");
System.out.println("x-: " + -b / (2 * a) + " - " + (Math.sqrt(-disc) / (2 * a)) + "i");
}

s.close();
}

}
``````

Could you suggest ways of cleaning up/optimizing the code?

Apart from storing some of the results that are used repeatedly (eg Math.pow(b, 2) etc) I can't see any obvious improvements.
If the next exercise is to do the same thing with a different equation, then that would suggest writing a generalised expression parser, from which printing the intermediate reuslts would be easy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.