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
36 changes: 13 additions & 23 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
# 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
name: Maven build and test

# 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]
on:
push:
pull_request:

jobs:
build:
permissions: write-all
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'zulu'
cache: maven
- uses: actions/checkout@v4

- name: Build with Maven
run: mvn -B package --file pom.xml
- name: Configure JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
cache: maven

- name: Run the Maven verify phase
run: mvn --batch-mode --update-snapshots verify
- name: Compile and run tests
run: mvn -B clean test
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
target/
.idea/
*.iml
.classpath
.project
.settings/
43 changes: 43 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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>

<name>my-app</name>

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

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

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.2</version>
</plugin>
</plugins>
</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);
}
}
80 changes: 80 additions & 0 deletions src/test/java/com/mycompany/app/SqrtTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.mycompany.app;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

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

class SqrtTest {

private Sqrt sqrt;

@BeforeEach
void init() {
sqrt = new Sqrt(25.0);
}

@Test
void averageReturnsHalfSum() {
assertEquals(5.5, sqrt.average(4.0, 7.0), 1e-12);
}

@Test
void averageForEqualOperands() {
assertEquals(3.0, sqrt.average(3.0, 3.0), 1e-12);
}

@Test
void goodIsTrueForAccurateGuess() {
assertTrue(sqrt.good(5.0, 25.0));
}

@Test
void goodIsFalseForInaccurateGuess() {
assertFalse(sqrt.good(4.0, 25.0));
}

@Test
void improveProducesBetterApproximation() {
double refined = sqrt.improve(2.0, 9.0);
assertEquals(3.25, refined, 1e-12);
}

@Test
void iterStopsOnSuitableGuess() {
assertEquals(7.0, sqrt.iter(7.0, 49.0), 1e-9);
}

@Test
void iterConvergesToRoot() {
assertEquals(5.0, sqrt.iter(1.0, 25.0), 1e-6);
}

@ParameterizedTest
@CsvSource({
"1.0, 1.0",
"4.0, 2.0",
"9.0, 3.0",
"16.0, 4.0"
})
void calcForSquareNumbers(double argument, double expectedRoot) {
assertEquals(expectedRoot, new Sqrt(argument).calc(), 1e-6);
}

@Test
void calcForIrrationalRoot() {
double root = new Sqrt(2.0).calc();
assertEquals(Math.sqrt(2.0), root, 1e-7);
}

@Test
void calcForZero() {
Sqrt zeroSqrt = new Sqrt(0.0);
double result = zeroSqrt.calc();
assertTrue(zeroSqrt.good(result, 0.0));
}
}
Loading