From 211b69d029c4a481447f4d825bd5eff4df6aa9f0 Mon Sep 17 00:00:00 2001 From: EDUARDO FERNANDES CEOLIN <15280426@facin.portoalegre.pucrsnet.br> Date: Mon, 14 Mar 2022 20:12:48 -0300 Subject: [PATCH 1/6] Logical impl of 1011 --- .../br/masmangan/beecrowd/bee1011/Main.java | 26 +++++++ .../beecrowd/bee1011/Bee1011Steps.java | 53 +++++++++++++ .../beecrowd/bee1011/RunCucumberTest.java | 31 ++++++++ .../beecrowd/bee1011/bee1011.feature | 75 +++++++++++++++++++ 4 files changed, 185 insertions(+) create mode 100644 src/main/java/br/masmangan/beecrowd/bee1011/Main.java create mode 100644 src/test/java/br/masmangan/beecrowd/bee1011/Bee1011Steps.java create mode 100644 src/test/java/br/masmangan/beecrowd/bee1011/RunCucumberTest.java create mode 100644 src/test/resources/br/masmangan/beecrowd/bee1011/bee1011.feature diff --git a/src/main/java/br/masmangan/beecrowd/bee1011/Main.java b/src/main/java/br/masmangan/beecrowd/bee1011/Main.java new file mode 100644 index 0000000..9ec7096 --- /dev/null +++ b/src/main/java/br/masmangan/beecrowd/bee1011/Main.java @@ -0,0 +1,26 @@ +package br.masmangan.beecrowd.bee1011; + + +import java.text.DecimalFormat; +import java.util.Scanner; + +import static java.lang.System.out; + +class Main { + + static double pi = 3.14159; + private static final DecimalFormat df = new DecimalFormat("0.000"); + + public static void main(String[] args) { + + Scanner scanner = new Scanner(System.in); + + double value = scanner.nextDouble(); + + double result = (4/3.0)*pi*Math.pow(value, 3); + + out.println("VOLUME = " + df.format(result)); + + } + +} \ No newline at end of file diff --git a/src/test/java/br/masmangan/beecrowd/bee1011/Bee1011Steps.java b/src/test/java/br/masmangan/beecrowd/bee1011/Bee1011Steps.java new file mode 100644 index 0000000..e18a088 --- /dev/null +++ b/src/test/java/br/masmangan/beecrowd/bee1011/Bee1011Steps.java @@ -0,0 +1,53 @@ +package br.masmangan.beecrowd.bee1011; + +import br.masmangan.beecrowd.bee1007.Main; +import io.cucumber.java.en.Given; +import io.cucumber.java.en.Then; +import io.cucumber.java.en.When; + +import java.io.*; +import java.nio.charset.StandardCharsets; + +import static org.junit.Assert.assertEquals; + +public class Bee1011Steps { + + private String input; + private String actual; + + @Given("input is") + public void input_is(String input) { + this.input = input; + } + + @When("program runs") + public void program_runs() throws IOException { + InputStream inputStream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)); + + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + + PrintStream outputStream = new PrintStream(byteArrayOutputStream); + + PrintStream previousOut = System.out; + InputStream previousIn = System.in; + + System.setIn(inputStream); + System.setOut(outputStream); + + Main.main(null); + + actual = byteArrayOutputStream.toString(); + + inputStream.close(); + outputStream.close(); + + System.setOut(previousOut); + System.setIn(previousIn); + } + + @Then("output should be") + public void output_should_be(String expected) { + assertEquals(expected, actual); + } + +} diff --git a/src/test/java/br/masmangan/beecrowd/bee1011/RunCucumberTest.java b/src/test/java/br/masmangan/beecrowd/bee1011/RunCucumberTest.java new file mode 100644 index 0000000..d34e9fd --- /dev/null +++ b/src/test/java/br/masmangan/beecrowd/bee1011/RunCucumberTest.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2021, Gherkin By Example and/or its contributors. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This software is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this code. If not, see . + * + * Please visit Gherkin By Example at https://github.com/gherkin-by-example + * if you need additional information or have any questions. + */ +package br.masmangan.beecrowd.bee1011; + +import io.cucumber.junit.Cucumber; +import io.cucumber.junit.CucumberOptions; +import org.junit.runner.RunWith; + +@RunWith(Cucumber.class) +@CucumberOptions(plugin = {"pretty"}) +public class RunCucumberTest { + +} diff --git a/src/test/resources/br/masmangan/beecrowd/bee1011/bee1011.feature b/src/test/resources/br/masmangan/beecrowd/bee1011/bee1011.feature new file mode 100644 index 0000000..1771ff8 --- /dev/null +++ b/src/test/resources/br/masmangan/beecrowd/bee1011/bee1011.feature @@ -0,0 +1,75 @@ +# +# Copyright (C) 2021, Gherkin By Example and/or its contributors. All rights reserved. +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# This software is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This code is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this code. If not, see . +# +# Please visit Gherkin By Example at https://github.com/gherkin-by-example +# if you need additional information or have any questions. +@system +Feature: Bee1011 CLI + + Narrative: + + In order to avoid silly mistakes + As a math novice + I want to be told the difference between products of four numbers + + Scenario: Run program with input + + Given input is +""" +5 +6 +7 +8 +""" + When program runs + Then output should be +""" +DIFERENCA = -26 + +""" + + Scenario: Run program with input + + Given input is +""" +0 +0 +7 +8 +""" + When program runs + Then output should be +""" +DIFERENCA = -56 + +""" + + Scenario: Run program with input + + Given input is +""" +5 +6 +-7 +8 +""" + When program runs + Then output should be +""" +DIFERENCA = 86 + +""" \ No newline at end of file From 36f78cd9c3767c8220eb33437088fc8d67be303c Mon Sep 17 00:00:00 2001 From: Calebe Rocha Date: Mon, 14 Mar 2022 20:20:31 -0300 Subject: [PATCH 2/6] Update bee1011.feature --- .../beecrowd/bee1011/bee1011.feature | 45 +++++++------------ 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/src/test/resources/br/masmangan/beecrowd/bee1011/bee1011.feature b/src/test/resources/br/masmangan/beecrowd/bee1011/bee1011.feature index 1771ff8..e88ffee 100644 --- a/src/test/resources/br/masmangan/beecrowd/bee1011/bee1011.feature +++ b/src/test/resources/br/masmangan/beecrowd/bee1011/bee1011.feature @@ -20,56 +20,41 @@ @system Feature: Bee1011 CLI - Narrative: - - In order to avoid silly mistakes + Narrative + + In order to know the volume of a sphere As a math novice - I want to be told the difference between products of four numbers - - Scenario: Run program with input + I want to be told the volume of a sphere giving only it's radius + Scenario: Input 3 Given input is """ -5 -6 -7 -8 +3 """ When program runs Then output should be """ -DIFERENCA = -26 - +VOLUME = 113.097 """ - Scenario: Run program with input - - Given input is + Scenario: Input 15 + Given input is """ -0 -0 -7 -8 +15 """ When program runs Then output should be """ -DIFERENCA = -56 - +VOLUME = 14137.155 """ - Scenario: Run program with input - + Scenario: Input 1523 Given input is """ -5 -6 --7 -8 +1523 """ When program runs Then output should be """ -DIFERENCA = 86 - -""" \ No newline at end of file +VOLUME = 14797486501.627 +""" From e222b9f6e5717e41bcfc1844ddd16fafaa70726c Mon Sep 17 00:00:00 2001 From: Eduardo Ceolin Date: Mon, 14 Mar 2022 20:25:39 -0300 Subject: [PATCH 3/6] Logical impl of 1011 --- src/main/java/br/masmangan/beecrowd/bee1011/Main.java | 5 +++++ .../java/br/masmangan/beecrowd/bee1011/Bee1011Steps.java | 1 - .../resources/br/masmangan/beecrowd/bee1011/bee1011.feature | 3 +++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/br/masmangan/beecrowd/bee1011/Main.java b/src/main/java/br/masmangan/beecrowd/bee1011/Main.java index 9ec7096..069eb6b 100644 --- a/src/main/java/br/masmangan/beecrowd/bee1011/Main.java +++ b/src/main/java/br/masmangan/beecrowd/bee1011/Main.java @@ -11,6 +11,11 @@ class Main { static double pi = 3.14159; private static final DecimalFormat df = new DecimalFormat("0.000"); + private Main() { + + } + + public static void main(String[] args) { Scanner scanner = new Scanner(System.in); diff --git a/src/test/java/br/masmangan/beecrowd/bee1011/Bee1011Steps.java b/src/test/java/br/masmangan/beecrowd/bee1011/Bee1011Steps.java index e18a088..418a100 100644 --- a/src/test/java/br/masmangan/beecrowd/bee1011/Bee1011Steps.java +++ b/src/test/java/br/masmangan/beecrowd/bee1011/Bee1011Steps.java @@ -1,6 +1,5 @@ package br.masmangan.beecrowd.bee1011; -import br.masmangan.beecrowd.bee1007.Main; import io.cucumber.java.en.Given; import io.cucumber.java.en.Then; import io.cucumber.java.en.When; diff --git a/src/test/resources/br/masmangan/beecrowd/bee1011/bee1011.feature b/src/test/resources/br/masmangan/beecrowd/bee1011/bee1011.feature index e88ffee..64bd10b 100644 --- a/src/test/resources/br/masmangan/beecrowd/bee1011/bee1011.feature +++ b/src/test/resources/br/masmangan/beecrowd/bee1011/bee1011.feature @@ -35,6 +35,7 @@ Feature: Bee1011 CLI Then output should be """ VOLUME = 113.097 + """ Scenario: Input 15 @@ -46,6 +47,7 @@ VOLUME = 113.097 Then output should be """ VOLUME = 14137.155 + """ Scenario: Input 1523 @@ -57,4 +59,5 @@ VOLUME = 14137.155 Then output should be """ VOLUME = 14797486501.627 + """ From c0191e81fdcfc75ffa26aae9b3e6bff7f8eb8b6d Mon Sep 17 00:00:00 2001 From: Eduardo Ceolin Date: Mon, 14 Mar 2022 20:38:09 -0300 Subject: [PATCH 4/6] Finished implementation --- .../br/masmangan/beecrowd/bee1011/Main.java | 6 ++- .../br/masmangan/beecrowd/bee1011/Sphere.java | 23 +++++++++++ .../beecrowd/bee1011/SphereSteps.java | 26 +++++++++++++ .../masmangan/beecrowd/bee1011/Sphere.feature | 39 +++++++++++++++++++ 4 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 src/main/java/br/masmangan/beecrowd/bee1011/Sphere.java create mode 100644 src/test/java/br/masmangan/beecrowd/bee1011/SphereSteps.java create mode 100644 src/test/resources/br/masmangan/beecrowd/bee1011/Sphere.feature diff --git a/src/main/java/br/masmangan/beecrowd/bee1011/Main.java b/src/main/java/br/masmangan/beecrowd/bee1011/Main.java index 069eb6b..448594d 100644 --- a/src/main/java/br/masmangan/beecrowd/bee1011/Main.java +++ b/src/main/java/br/masmangan/beecrowd/bee1011/Main.java @@ -8,7 +8,6 @@ class Main { - static double pi = 3.14159; private static final DecimalFormat df = new DecimalFormat("0.000"); private Main() { @@ -22,7 +21,10 @@ public static void main(String[] args) { double value = scanner.nextDouble(); - double result = (4/3.0)*pi*Math.pow(value, 3); + Sphere sphere = new Sphere(); + sphere.setRadius(value); + + double result = sphere.calculate(); out.println("VOLUME = " + df.format(result)); diff --git a/src/main/java/br/masmangan/beecrowd/bee1011/Sphere.java b/src/main/java/br/masmangan/beecrowd/bee1011/Sphere.java new file mode 100644 index 0000000..a41d12f --- /dev/null +++ b/src/main/java/br/masmangan/beecrowd/bee1011/Sphere.java @@ -0,0 +1,23 @@ +package br.masmangan.beecrowd.bee1011; + +public class Sphere { + + static double pi = 3.14159; + + private double radius; + private double volume; + + public Sphere() { + } + + public void setRadius(double radius) { + this.radius = radius; + } + + public double calculate() { + + double result = (4/3.0)*pi*Math.pow(radius, 3); + this.volume = result; + return result; + } +} diff --git a/src/test/java/br/masmangan/beecrowd/bee1011/SphereSteps.java b/src/test/java/br/masmangan/beecrowd/bee1011/SphereSteps.java new file mode 100644 index 0000000..8209478 --- /dev/null +++ b/src/test/java/br/masmangan/beecrowd/bee1011/SphereSteps.java @@ -0,0 +1,26 @@ +package br.masmangan.beecrowd.bee1011; + +import io.cucumber.java.en.Given; +import io.cucumber.java.en.Then; +import io.cucumber.java.en.When; + +import static org.junit.Assert.assertEquals; + +public class SphereSteps { + + private final Sphere sphere = new Sphere(); + private Double actual; + + @Given("radius is {double}") + public void radius_value_is(Double a) { + sphere.setRadius(a); + } + @When("volume is calculated") + public void volume_is_calculated() { + actual = sphere.calculate(); + } + @Then("result should be {double}") + public void result_should_be(Double expected) { + assertEquals(expected, actual); + } +} diff --git a/src/test/resources/br/masmangan/beecrowd/bee1011/Sphere.feature b/src/test/resources/br/masmangan/beecrowd/bee1011/Sphere.feature new file mode 100644 index 0000000..6366d81 --- /dev/null +++ b/src/test/resources/br/masmangan/beecrowd/bee1011/Sphere.feature @@ -0,0 +1,39 @@ +# +# Copyright (C) 2021, Gherkin By Example and/or its contributors. All rights reserved. +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# This software is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This code is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this code. If not, see . +# +# Please visit Gherkin By Example at https://github.com/gherkin-by-example +# if you need additional information or have any questions. +@domain +Feature: Sphere + +Narrative: + +In order to know the volume of a sphere +As a math novice +I want to be told the volume of a sphere giving only it's radius + +Scenario Outline: calculate volume of radius + +Given radius is +When volume is calculated +Then result should be + +Examples: +| radius | volume | +| 3.0 | 113.097 | +| 15.0 | 14137.155 | +| 1523.0 | 14797486501.627 | From b1b4fe1f8f3e4927a48cb95b890718c08c6ed973 Mon Sep 17 00:00:00 2001 From: Eduardo Ceolin Date: Mon, 14 Mar 2022 20:39:41 -0300 Subject: [PATCH 5/6] Finished implementation --- src/main/java/br/masmangan/beecrowd/bee1011/Main.java | 3 +-- src/main/java/br/masmangan/beecrowd/bee1011/Sphere.java | 5 ++++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/br/masmangan/beecrowd/bee1011/Main.java b/src/main/java/br/masmangan/beecrowd/bee1011/Main.java index 448594d..f7d433f 100644 --- a/src/main/java/br/masmangan/beecrowd/bee1011/Main.java +++ b/src/main/java/br/masmangan/beecrowd/bee1011/Main.java @@ -8,7 +8,6 @@ class Main { - private static final DecimalFormat df = new DecimalFormat("0.000"); private Main() { @@ -26,7 +25,7 @@ public static void main(String[] args) { double result = sphere.calculate(); - out.println("VOLUME = " + df.format(result)); + out.println("VOLUME = " + result); } diff --git a/src/main/java/br/masmangan/beecrowd/bee1011/Sphere.java b/src/main/java/br/masmangan/beecrowd/bee1011/Sphere.java index a41d12f..f9bcb66 100644 --- a/src/main/java/br/masmangan/beecrowd/bee1011/Sphere.java +++ b/src/main/java/br/masmangan/beecrowd/bee1011/Sphere.java @@ -1,11 +1,14 @@ package br.masmangan.beecrowd.bee1011; +import java.text.DecimalFormat; + public class Sphere { static double pi = 3.14159; private double radius; private double volume; + private static final DecimalFormat df = new DecimalFormat("0.000"); public Sphere() { } @@ -18,6 +21,6 @@ public double calculate() { double result = (4/3.0)*pi*Math.pow(radius, 3); this.volume = result; - return result; + return Double.parseDouble(df.format(result)); } } From 2d920885a7453a6de35e07bdc59e41472f205e0f Mon Sep 17 00:00:00 2001 From: Eduardo Ceolin Date: Wed, 23 Mar 2022 19:56:01 -0300 Subject: [PATCH 6/6] fix: decimals --- src/main/java/br/masmangan/beecrowd/bee1011/Main.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/br/masmangan/beecrowd/bee1011/Main.java b/src/main/java/br/masmangan/beecrowd/bee1011/Main.java index f7d433f..5ef67b1 100644 --- a/src/main/java/br/masmangan/beecrowd/bee1011/Main.java +++ b/src/main/java/br/masmangan/beecrowd/bee1011/Main.java @@ -16,6 +16,7 @@ private Main() { public static void main(String[] args) { + final DecimalFormat df = new DecimalFormat("0.000"); Scanner scanner = new Scanner(System.in); double value = scanner.nextDouble(); @@ -25,7 +26,7 @@ public static void main(String[] args) { double result = sphere.calculate(); - out.println("VOLUME = " + result); + out.println("VOLUME = " + df.format(result)); }