Created
November 10, 2021 15:19
-
-
Save Da9el00/f8b443b2efe0e186c7103ae8e6c1b22a to your computer and use it in GitHub Desktop.
Quadratic Equation Solver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.event.ActionEvent; | |
import javafx.fxml.FXML; | |
import javafx.scene.control.TextArea; | |
import javafx.scene.control.TextField; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class Controller { | |
//ax^2+bx+c | |
//2x^2+1x-1 got the solutions: x1=0.5 x2=-1.0 | |
@FXML | |
private TextField quadraticEquation; | |
@FXML | |
private TextArea output; | |
@FXML | |
void solve(ActionEvent event) { | |
List<Double> coefficients = findCoefficients(quadraticEquation.getText()); | |
if(coefficients != null){ | |
List<Double> solutions = solve(coefficients); | |
if(solutions.size() == 1){ | |
output.setText("No solutions, d= " + solutions.get(0)); | |
} else if(solutions.size() == 2){ | |
output.setText("1 solution, d= " + solutions.get(1) | |
+ "x = " + solutions.get(1)); | |
} else if(solutions.size() == 3){ | |
output.setText("2 solutions, d=" + solutions.get(1) | |
+ ", x1=" + solutions.get(1) + " and x2=" + solutions.get(2)); | |
} | |
} else { | |
output.setText("Input not valid use the form: ax^2+bx+c"); | |
quadraticEquation.setText("ax^2+bx+c"); | |
} | |
} | |
//2x^2+1x-1 | |
private List<Double> findCoefficients(String equation){ | |
List<Double> matches = new ArrayList<>(); | |
String regex = "(-?\\d+)x\\^2([+-]\\d+)x([+-]\\d+)"; | |
Matcher m = Pattern.compile(regex).matcher(equation); | |
if(m.matches()) { | |
for (int i = 1; i < m.groupCount()+1; i++) { | |
matches.add(Double.parseDouble(m.group(i))); | |
} | |
return matches; | |
} | |
return null; | |
} | |
private ArrayList<Double> solve(List<Double> coefficients){ | |
//coefficients: | |
double a = coefficients.get(0); | |
double b = coefficients.get(1); | |
double c = coefficients.get(2); | |
ArrayList<Double> solutions = new ArrayList<>(); | |
//d = b^2 + 4ac | |
double d = Math.pow(b,2) - (4 * a * c); | |
solutions.add((double) Math.round(d*100)/100); | |
if(d == 0){ //1 solution | |
double x1 = (-b) / (2 * a); | |
solutions.add((double) Math.round(x1*100)/100); | |
}else if (d > 0){ //2 solutions | |
double x1 = ((-b) + Math.sqrt(d)) / (2 * a); | |
solutions.add((double) Math.round(x1*100)/100); | |
double x2 = ((-b) - Math.sqrt(d)) / (2 * a); | |
solutions.add((double) Math.round(x2*100)/100); | |
} | |
return solutions; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment