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
9 changes: 0 additions & 9 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.


name: Java CI with Maven

on: [push, pull_request]
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
90 changes: 90 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?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>

<name>my-app</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</maven.compiler.release>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.11.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<!-- Optionally: parameterized tests support -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.4.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.12.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.6.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
12 changes: 12 additions & 0 deletions src/main/java/com/mycompany/app/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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);
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/mycompany/app/Sqrt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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);
}
}
13 changes: 13 additions & 0 deletions src/test/java/com/mycompany/app/AppTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.mycompany.app;

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

import org.junit.jupiter.api.Test;


public class AppTest {
@Test
public void shouldAnswerWithTrue() {
assertTrue(true);
}
}
82 changes: 82 additions & 0 deletions src/test/java/com/mycompany/app/SqrtTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.mycompany.app;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

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

public class SqrtTest {

private Sqrt sqrt;

@BeforeEach
public void setUp() {
sqrt = new Sqrt(4.0);
}

@Test
public void testConstructorSetsArgument() {
Sqrt instance = new Sqrt(9.0);
assertEquals(9.0, instance.arg, 0.0);
}

@Test
public void testAverageOfTwoAndFour() {
assertEquals(3.0, sqrt.average(2.0, 4.0), 0.0);
}

@Test
public void testAverageOfEqualNumbers() {
assertEquals(5.0, sqrt.average(5.0, 5.0), 0.0);
}

@Test
public void testGoodReturnsTrueForAccurateGuess() {
assertTrue(sqrt.good(2.0, 4.0));
}

@Test
public void testGoodReturnsFalseForInaccurateGuess() {
assertFalse(sqrt.good(1.0, 4.0));
}

@Test
public void testImproveMovesGuessCloserToRoot() {
assertEquals(2.5, sqrt.improve(1.0, 4.0), 0.0);
}

@Test
public void testIterReturnsGuessWhenAlreadyGood() {
assertEquals(2.0, sqrt.iter(2.0, 4.0), 0.0);
}

@Test
public void testIterConvergesToSquareRoot() {
assertEquals(2.0, sqrt.iter(1.0, 4.0), 0.00000001);
}

@Test
public void testCalcForFour() {
assertEquals(2.0, sqrt.calc(), 0.00000001);
}

@Test
public void testCalcForTwo() {
Sqrt sqrtOfTwo = new Sqrt(2.0);
assertEquals(Math.sqrt(2.0), sqrtOfTwo.calc(), 0.00000001);
}

@Test
public void testCalcForNine() {
Sqrt sqrtOfNine = new Sqrt(9.0);
assertEquals(3.0, sqrtOfNine.calc(), 0.00000001);
}

@Test
public void testCalcForOne() {
Sqrt sqrtOfOne = new Sqrt(1.0);
assertEquals(1.0, sqrtOfOne.calc(), 0.00000001);
}
}
Loading