Skip to content
This repository was archived by the owner on Jun 10, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .github/workflows/my-app/.gitignore
Binary file not shown.
10 changes: 10 additions & 0 deletions .github/workflows/my-app/src/main/java/com/mycompany/app/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mycompany.app;

public class App {
public static void main(String[] args) {
double val = Double.parseDouble("2.0");
Sqrt sqrt = new Sqrt(val);
double result = sqrt.calc();
System.out.println("Sqrt of " + val + " = " + result);
}
}
33 changes: 33 additions & 0 deletions .github/workflows/my-app/src/main/java/com/mycompany/app/Sqrt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.mycompany.app;

public class Sqrt {
double delta = 0.00000001;
double arg;

public Sqrt(double arg) {
this.arg = arg;
}

public double average(double x, double y) {
return (x + y) / 2.0;
}

public boolean good(double guess, double x) {
return Math.abs(guess * guess - x) < delta;
}

public double improve(double guess, double x) {
return average(guess, x / guess);
}

public double iter(double guess, double x) {
if (good(guess, x))
return guess;
else
return iter(improve(guess, x), x);
}

public double calc() {
return iter(1.0, arg);
}
}
116 changes: 116 additions & 0 deletions .github/workflows/my-app/src/test/java/com/mycompany/app/AppTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.mycompany.app;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import static org.junit.jupiter.api.Assertions.*;

public class AppTest {

private static final double DELTA = 1e-6;

// --- Тесты метода calc() ---

@Test
public void testSqrtOf4() {
Sqrt sqrt = new Sqrt(4.0);
assertEquals(2.0, sqrt.calc(), DELTA);
}

@Test
public void testSqrtOf9() {
Sqrt sqrt = new Sqrt(9.0);
assertEquals(3.0, sqrt.calc(), DELTA);
}

@Test
public void testSqrtOf2() {
Sqrt sqrt = new Sqrt(2.0);
assertEquals(Math.sqrt(2.0), sqrt.calc(), DELTA);
}

@Test
public void testSqrtOf1() {
Sqrt sqrt = new Sqrt(1.0);
assertEquals(1.0, sqrt.calc(), DELTA);
}

@Test
public void testSqrtOf0() {
Sqrt sqrt = new Sqrt(0.0);
assertEquals(0.0, sqrt.calc(), DELTA);
}

@Test
public void testSqrtOf100() {
Sqrt sqrt = new Sqrt(100.0);
assertEquals(10.0, sqrt.calc(), DELTA);
}

@Test
public void testSqrtOf0Point25() {
Sqrt sqrt = new Sqrt(0.25);
assertEquals(0.5, sqrt.calc(), DELTA);
}

// --- Тесты метода average() ---

@Test
public void testAverageOfTwoNumbers() {
Sqrt sqrt = new Sqrt(1.0);
assertEquals(3.0, sqrt.average(2.0, 4.0), DELTA);
}

@Test
public void testAverageOfSameNumbers() {
Sqrt sqrt = new Sqrt(1.0);
assertEquals(5.0, sqrt.average(5.0, 5.0), DELTA);
}

@Test
public void testAverageOfZeroAndNumber() {
Sqrt sqrt = new Sqrt(1.0);
assertEquals(2.5, sqrt.average(0.0, 5.0), DELTA);
}

// --- Тесты метода good() ---

@Test
public void testGoodReturnsTrueForExactSqrt() {
Sqrt sqrt = new Sqrt(4.0);
assertTrue(sqrt.good(2.0, 4.0));
}

@Test
public void testGoodReturnsFalseForBadGuess() {
Sqrt sqrt = new Sqrt(4.0);
assertFalse(sqrt.good(1.0, 4.0));
}

// --- Тесты метода improve() ---

@Test
public void testImproveConvergesCloser() {
Sqrt sqrt = new Sqrt(4.0);
double improved = sqrt.improve(1.0, 4.0);
double distBefore = Math.abs(1.0 * 1.0 - 4.0);
double distAfter = Math.abs(improved * improved - 4.0);
assertTrue(distAfter < distBefore);
}

// --- Тесты метода iter() ---

@Test
public void testIterConvergesFor9() {
Sqrt sqrt = new Sqrt(9.0);
assertEquals(3.0, sqrt.iter(1.0, 9.0), DELTA);
}

// --- Точность результата ---

@Test
public void testCalcAccuracy() {
Sqrt sqrt = new Sqrt(2.0);
double result = sqrt.calc();
assertTrue(Math.abs(result * result - 2.0) < 1e-7);
}
}
36 changes: 36 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
</plugin>
</plugins>
</build>
</project>
Loading