Skip to content

Commit

Permalink
Стандартное квадратное уравнение с одним решением
Browse files Browse the repository at this point in the history
  • Loading branch information
stden committed May 23, 2015
1 parent ea01b64 commit bf5c184
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/main/java/QuadraticEquation.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,15 @@
*/
public class QuadraticEquation {

/**
* Решение квадратного уравнения: ax^2 + bx + c = 0
*
* @param a коэффициент при x^2
* @param b коэффициент при x
* @param c константа
* @return корни уравнения (значения x)
*/
public static double[] solve(double a, double b, double c) {
return new double[]{-b / (2 * a)};
}
}
12 changes: 11 additions & 1 deletion src/test/java/QuadraticEquationTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import org.junit.Assert;
import org.junit.Test;

/**
* Тесты для решения квадратного уравнения
*/
public class QuadraticEquationTest extends Assert {
public static final double EPS = 1e-10;


/**
* Стандартное квадратное уравнение с одним решением
*/
@Test
public void testSimpleOneRoot() {
assertArrayEquals("x^2 = 0", new double[]{0}, QuadraticEquation.solve(1, 0, 0), EPS);
assertArrayEquals("(x-1)^2 = x^2-2x+1", new double[]{1}, QuadraticEquation.solve(1, -2, 1), EPS);
assertArrayEquals("2(x-1)^2 = 2x^2-4x+2", new double[]{1}, QuadraticEquation.solve(2, -4, 2), EPS);
}
}

0 comments on commit bf5c184

Please sign in to comment.