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. (Исправленная) #46

Open
wants to merge 5 commits into
base: b67
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
38 changes: 38 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<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</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

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

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

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
12 changes: 12 additions & 0 deletions src/main/java/org/example/Program.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.example;

class Program
{
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);
}
}
29 changes: 29 additions & 0 deletions src/main/java/org/example/Sqrt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.example;

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);
}
}
49 changes: 49 additions & 0 deletions src/test/java/org/example/SqrtTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.example;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class SqrtTest {
private Sqrt sqrt;

@Test
public void testAverage() {
sqrt = new Sqrt(16.0);
double result = sqrt.average(4.0, 8.0);
Assertions.assertEquals(6.0, result);
}

@Test
public void testGood() {
sqrt = new Sqrt(16.0);
boolean result1 = sqrt.good(4.0, 16.0);
Assertions.assertTrue(result1);

boolean result2 = sqrt.good(3.0, 16.0);
Assertions.assertFalse(result2);
}

@Test
public void testImprove() {
sqrt = new Sqrt(16.0);
double result = sqrt.improve(4.0, 16.0);
Assertions.assertEquals(4.0, result, sqrt.delta);
}

@Test
public void testIter() {
sqrt = new Sqrt(16.0);
double result1 = sqrt.iter(4.0, 16.0);
Assertions.assertEquals(4.0, result1, sqrt.delta);

double result2 = sqrt.iter(1.0, 16.0);
Assertions.assertEquals(4.0, result2, sqrt.delta);
}

@Test
public void testCalc() {
sqrt = new Sqrt(16.0);
double result = sqrt.calc();
Assertions.assertEquals(4.0, result, sqrt.delta);
}
}
Loading