From bf5c1844a52df1005130c445ad1c62d473d7c060 Mon Sep 17 00:00:00 2001 From: Denis Stepulenok Date: Sat, 23 May 2015 05:23:13 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D1=82=D0=B0=D0=BD=D0=B4=D0=B0=D1=80?= =?UTF-8?q?=D1=82=D0=BD=D0=BE=D0=B5=20=D0=BA=D0=B2=D0=B0=D0=B4=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BD=D0=BE=D0=B5=20=D1=83=D1=80=D0=B0=D0=B2=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=81=20=D0=BE=D0=B4=D0=BD=D0=B8=D0=BC=20?= =?UTF-8?q?=D1=80=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/QuadraticEquation.java | 11 +++++++++++ src/test/java/QuadraticEquationTest.java | 12 +++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/QuadraticEquation.java b/src/main/java/QuadraticEquation.java index 08fb6bb..b4b48c6 100644 --- a/src/main/java/QuadraticEquation.java +++ b/src/main/java/QuadraticEquation.java @@ -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)}; + } } diff --git a/src/test/java/QuadraticEquationTest.java b/src/test/java/QuadraticEquationTest.java index dd58fd1..78637ec 100644 --- a/src/test/java/QuadraticEquationTest.java +++ b/src/test/java/QuadraticEquationTest.java @@ -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); + } }