Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3.  
  4. public class QuadraticEq {
  5. private static final Pattern EQN = Pattern.compile("([+-]?\\d+)[Xx]\\^2\\s*([+-]?\\d+)[Xx]\\s*([+-]?\\d+)");
  6. private final double a;
  7. private final double b;
  8. private final double c;
  9. private double delta;
  10. private double x1;
  11. private double x2;
  12. private double Wx;
  13. private double Wy;
  14.  
  15. private QuadraticEq(double a, double b, double c) {
  16. this.a = a;
  17. this.b = b;
  18. this.c = c;
  19.  
  20. calculate();
  21. }
  22.  
  23. public static QuadraticEq parseString(final String eq) {
  24. final Matcher matcher = EQN.matcher(eq);
  25. if (!matcher.matches()) {
  26. throw new IllegalArgumentException("Not a valid pattern " + eq);
  27. }
  28. final int a = Integer.parseInt(matcher.group(1));
  29. final int b = Integer.parseInt(matcher.group(2));
  30. final int c = Integer.parseInt(matcher.group(3));
  31.  
  32.  
  33. return new QuadraticEq(a, b, c);
  34. }
  35.  
  36. private void calculate() {
  37. delta = b * b - 4 * a * c;
  38. x1 = 0; //zamiast 0 oblicz x1
  39. x2 = 0; //zamiast 0 oblicz x2
  40. Wx = 0; //zamiast 0 oblicz Wx
  41. Wy = 0; //zamiast 0 oblicz xWy
  42. }
  43.  
  44.  
  45. public double getA() {
  46. return a;
  47. }
  48.  
  49. public double getB() {
  50. return b;
  51. }
  52.  
  53. public double getC() {
  54. return c;
  55. }
  56.  
  57. public double getDelta() {
  58. return delta;
  59. }
  60.  
  61. public double getX1() {
  62. return x1;
  63. }
  64.  
  65. public double getX2() {
  66. return x2;
  67. }
  68.  
  69. public double getWx() {
  70. return Wx;
  71. }
  72.  
  73. public double getWy() {
  74. return Wy;
  75. }
  76.  
  77. @Override
  78. public String toString() {
  79. return "QuadraticEq{" +
  80. "a=" + a +
  81. ", b=" + b +
  82. ", c=" + c +
  83. ", delta=" + delta +
  84. ", x1=" + x1 +
  85. ", x2=" + x2 +
  86. ", Wx=" + Wx +
  87. ", Wy=" + Wy +
  88. '}';
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement