Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Миронов Илья. Лабораторная №5 #50

Open
wants to merge 2 commits into
base: b11
Choose a base branch
from
Open
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
51 changes: 51 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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>org.example</groupId>
<artifactId>ST-5-new</artifactId>
<version>1.0-SNAPSHOT</version>

<name>ST-5-new</name>
<url>http://maven.apache.org</url>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.jupiter.version>5.10.0</junit.jupiter.version>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</build>
</project>
9 changes: 9 additions & 0 deletions src/main/java/Program.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Program {
public static void main(String[] args){
double val = Double.parseDouble("4.0");
Sqrt sqrt = new Sqrt(val);
double result = sqrt.calc();

System.out.println("Sqrt of " + val + " = " + result);
}
}
27 changes: 27 additions & 0 deletions src/main/java/Sqrt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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);
}
}
117 changes: 117 additions & 0 deletions src/test/java/SqrtTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import org.junit.jupiter.api.Test;

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

public class SqrtTests {

@Test
public void sqrtAverageTestOne(){
Sqrt sqrt = new Sqrt(100.0);

assertEquals(sqrt.average(0.0, 0.0), 0.0, sqrt.delta);
}

@Test
public void sqrtAverageTestTwo(){
Sqrt sqrt = new Sqrt(100.0);

assertEquals(sqrt.average(-100.0, 100.0), 0.0, sqrt.delta);
}

@Test
public void sqrtAverageTesThree(){
Sqrt sqrt = new Sqrt(100.0);

assertEquals(sqrt.average(15.0, 29.0), 22.0, sqrt.delta);
}

@Test
public void sqrtGoodTestOne(){
Sqrt sqrt = new Sqrt(100.0);

assertTrue(sqrt.good(2.0, 4.0));

assertFalse(sqrt.good(15.0, 112.5));
}

@Test
public void sqrtGoodTestTwo(){
Sqrt sqrt = new Sqrt(100.0);

assertTrue(sqrt.good(32.0, 1024.0));

assertFalse(sqrt.good(1.0, 4.0));
}

@Test
public void sqrtGoodTestThree(){
Sqrt sqrt = new Sqrt(100.0);

assertTrue(sqrt.good(8.0, 64.0000000000001));

assertFalse(sqrt.good(5.0, 17.0));
}

@Test
public void sqrtImproveTestOne(){
Sqrt sqrt = new Sqrt(100.0);

assertEquals(sqrt.improve(14.0, 8.0), 7.2857142857,sqrt.delta);
}

@Test
public void sqrtImproveTestTwo(){
Sqrt sqrt = new Sqrt(100.0);

assertEquals(sqrt.improve(100.0, 32.0), 50.16,sqrt.delta);
}

@Test
public void sqrtImproveTestThree(){
Sqrt sqrt = new Sqrt(100.0);

assertEquals(sqrt.improve(91.0, 4.0), 45.5219780219,sqrt.delta);
}

@Test
public void sqrtIterTestOne(){
Sqrt sqrt = new Sqrt(100.0);

assertEquals(sqrt.iter(2.0, 4.0), 2.0, sqrt.delta);
}

@Test
public void sqrtIterTestTwo(){
Sqrt sqrt = new Sqrt(100.0);

assertEquals(sqrt.iter(34.0, 80.0), 8.944271909999177, sqrt.delta);
}

@Test
public void sqrtIterTestThree(){
Sqrt sqrt = new Sqrt(100.0);

assertEquals(sqrt.iter(94.0, 225.0), 15.0, sqrt.delta);
}

@Test
public void sqrtCalcTestOne(){
Sqrt sqrt = new Sqrt(2.0);

assertEquals(sqrt.calc(), 1.414213562, sqrt.delta);
}

@Test
public void sqrtCalcTesTwo(){
Sqrt sqrt = new Sqrt(4.0);

assertEquals(sqrt.calc(), 2.0, sqrt.delta);
}

@Test
public void sqrtCalcTesThree(){
Sqrt sqrt = new Sqrt(1024.0);

assertEquals(sqrt.calc(), 32.0, sqrt.delta);
}
}
Loading