Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class QuadraticEq {
- private static final Pattern EQN = Pattern.compile("([+-]?\\d+)[Xx]\\^2\\s*([+-]?\\d+)[Xx]\\s*([+-]?\\d+)");
- private final double a;
- private final double b;
- private final double c;
- private double delta;
- private double x1;
- private double x2;
- private double Wx;
- private double Wy;
- private QuadraticEq(double a, double b, double c) {
- this.a = a;
- this.b = b;
- this.c = c;
- calculate();
- }
- public static QuadraticEq parseString(final String eq) {
- final Matcher matcher = EQN.matcher(eq);
- if (!matcher.matches()) {
- throw new IllegalArgumentException("Not a valid pattern " + eq);
- }
- final int a = Integer.parseInt(matcher.group(1));
- final int b = Integer.parseInt(matcher.group(2));
- final int c = Integer.parseInt(matcher.group(3));
- return new QuadraticEq(a, b, c);
- }
- private void calculate() {
- delta = b * b - 4 * a * c;
- x1 = 0; //zamiast 0 oblicz x1
- x2 = 0; //zamiast 0 oblicz x2
- Wx = 0; //zamiast 0 oblicz Wx
- Wy = 0; //zamiast 0 oblicz xWy
- }
- public double getA() {
- return a;
- }
- public double getB() {
- return b;
- }
- public double getC() {
- return c;
- }
- public double getDelta() {
- return delta;
- }
- public double getX1() {
- return x1;
- }
- public double getX2() {
- return x2;
- }
- public double getWx() {
- return Wx;
- }
- public double getWy() {
- return Wy;
- }
- @Override
- public String toString() {
- return "QuadraticEq{" +
- "a=" + a +
- ", b=" + b +
- ", c=" + c +
- ", delta=" + delta +
- ", x1=" + x1 +
- ", x2=" + x2 +
- ", Wx=" + Wx +
- ", Wy=" + Wy +
- '}';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement