From 2b083b39c34dedf45b3d7bba7107da3835bb0dba Mon Sep 17 00:00:00 2001 From: vsuryakumar Date: Fri, 3 Feb 2023 14:54:16 +0530 Subject: [PATCH] Added some test files. --- .../com/example/hpos/CubicEquationSolver.kt | 96 +++++++++++++++++++ .../test/java/com/example/hpos/OtherTests.kt | 15 +++ 2 files changed, 111 insertions(+) create mode 100644 app/src/test/java/com/example/hpos/CubicEquationSolver.kt create mode 100644 app/src/test/java/com/example/hpos/OtherTests.kt diff --git a/app/src/test/java/com/example/hpos/CubicEquationSolver.kt b/app/src/test/java/com/example/hpos/CubicEquationSolver.kt new file mode 100644 index 0000000..197bdea --- /dev/null +++ b/app/src/test/java/com/example/hpos/CubicEquationSolver.kt @@ -0,0 +1,96 @@ +package com.example.hpos + +import org.junit.Test + +class CubicEquationSolver { + + @Test + fun test() { + val a: Double = 12.toDouble() + val b: Double = (15).toDouble() + val c: Double = (9).toDouble() + val d: Double = (-2456).toDouble() + + solve(a, b, c, d) + } + + fun solve(a: Double, b: Double, c: Double, d: Double) { + val f: Double = getF(a, b, c) + val g: Double = getG(a, b, c, d) + val h: Double = getH(f, g) + println("f = $f & g = $g & h = $h") + + if (f == 0.0 && g == 0.0 && h == 0.0) + solveOneRoot(a, d) + else if (h <= 0) + solveRealRoots(a, b, g, h) + else + solveNotRealRoots(a, b, g, h) + } + + private fun getF(a: Double, b: Double, c: Double): Double { + return (3 * c / a - b * b / (a * a)) / 3 + } + + private fun getG(a: Double, b: Double, c: Double, d: Double): Double { + var g: Double = 2 * b * b * b / (a * a * a) + g += -9 * b * c / (a * a) + g += 27 * d / a + g /= 27.0 + return g + } + + private fun getH(f: Double, g: Double): Double { + return g * g / 4 + f * f * f / 27 + } + + private fun solveOneRoot(a: Double, d: Double) { + val x: Double = -Math.pow(d / a, 1.0 / 3) + println("The solution is: $x") + + println("rounded off solutions are:") + println(String.format("%.3g%n", x)) + } + + private fun solveRealRoots(a: Double, b: Double, g: Double, h: Double) { + val i: Double = Math.sqrt(g * g / 4 - h) + val j: Double = Math.pow(i, 1.0 / 3) + val k: Double = Math.acos(-g / (2 * i)) + + val l: Double = -j + val m: Double = Math.cos(k / 3) + val n: Double = Math.sqrt(3.0) * Math.sin(k / 3) + val p: Double = -b / (3 * a) + + val x1: Double = 2 * j * Math.cos(k / 3) - b / (3 * a) + val x2: Double = l * (m + n) + p + val x3: Double = l * (m - n) + p + + println("The solutions are:") + println(x1) + println(x2) + println(x3) + + println("rounded off solutions are:") + println(String.format("%.3g", x1)) + println(String.format("%.3g", x2)) + println(String.format("%.3g", x3)) + } + + private fun solveNotRealRoots(a: Double, b: Double, g: Double, h: Double) { + + val r: Double = -(g / 2) + Math.sqrt(h) + val s: Double = if (r >= 0) Math.pow(r, 1.0 / 3) else -Math.pow(-r, 1.0 / 3) + val t: Double = -(g / 2) - Math.sqrt(h) + val u: Double = if (t >= 0) Math.pow(t, 1.0 / 3) else -Math.pow(-t, 1.0 / 3) + + val x1: Double = s + u - b / (3 * a) + val x2: Double = -((s + u) / 2) - b / (3 * a) + val immaginary: Double = (s - u) * Math.sqrt(3.0) / 2 + + println("The solutions are:") + println(x1) + println(x2.toString() + " + " + immaginary + "i") + println(x2.toString() + " - " + immaginary + "i") + } +} \ No newline at end of file diff --git a/app/src/test/java/com/example/hpos/OtherTests.kt b/app/src/test/java/com/example/hpos/OtherTests.kt new file mode 100644 index 0000000..701a671 --- /dev/null +++ b/app/src/test/java/com/example/hpos/OtherTests.kt @@ -0,0 +1,15 @@ +package com.example.hpos + +import org.junit.Test +import java.util.* +import java.util.concurrent.atomic.AtomicLong + +class OtherTests { + + @Test + fun uniqueId() { + val date = Date() + date.day + } + +} \ No newline at end of file