Doubt in a Java exercise

Asked

Viewed 341 times

4

I was looking for some Java exercises to train and learn some algorithms when I came across the site exercism. and I decided to do their Java exercises. So far so good, I downloaded their app using the chocolately and installed the Gradle to run the tests. I found the methodological proposal of their exercises very interesting until the problem came.

The problem is this:

When you make the exercise request by cmd using the command:

$ exercism fetch java hello-world

The exercise already comes with bugs (intentionally) so that you find them and solve them all. After solving the bugs, and running the command:

$ gradle test

inside the exercise folder, everything should go well and then you are free to submit your answer using the command:

$ exercism submit src/main/java/HelloWorld.java

indicating the path of your code in submission:

Now that comes the problem.

The following code is used for testing:

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class HelloWorldTest {

    @Test
    public void helloNoName() {
        assertEquals("Hello, World!", HelloWorld.hello(""));
        assertEquals("Hello, World!", HelloWorld.hello(null));
    }

    @Test
    public void helloSampleName() {
        assertEquals("Hello, Alice!", HelloWorld.hello("Alice"));
    }

    @Test
    public void helloAnotherSampleName() {
        assertEquals("Hello, Bob!", HelloWorld.hello("Bob"));
    }
}

What I understood this code should do is to receive a string from the main program and test if the string is empty of the type "" or void he should return: Hello, World!.

If written in the main program the name Alice or Bob it would return or Hello, Alice! or Hello, Bob!

My main program is as follows:

public class HelloWorld {
    public static String hello(String name) {
        return name;
    }
}

I already tried to put the three valid strings concatenated, three returns in the method, set up an array of strings and have them display one at a time and nothing.

The problem is that the requirement of the exercise asks that this be solved by editing the main program which is the Helloworld.java that prevents me from changing the test program that is the Helloworldtest.java 'Cause that would be cheating.

The mistake that the gradle Show me this one:

PS C:\Users\anton\exercism\java\hello-world> gradle test
:compileJava
:processResources UP-TO-DATE
:classes
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses
:test

HelloWorldTest > helloAnotherSampleName FAILED
    org.junit.ComparisonFailure at HelloWorldTest.java:20

HelloWorldTest > helloSampleName FAILED
    org.junit.ComparisonFailure at HelloWorldTest.java:15

HelloWorldTest > helloNoName FAILED
    org.junit.ComparisonFailure at HelloWorldTest.java:9

3 tests completed, 3 failed
:test FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':test'.
> There were failing tests. See the report at: file:///C:/Users/anton/exercism/java/hello-world/build/reports/tests/inde
x.html

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 8.581 secs

If anyone knows how I fix it send me a tip here.

  • From what I understand of the test, the method hello() takes a string and concatenates with "Hello, ", if the string is null or empty, displays "Hello, word!". This already answers your question, now get to work! D

  • I would even respond with the method ready, but I think it will be more constructive for yourself this creation.

  • I’ll keep trying here, hope to get soon

  • I already gave you practically a "portugol", just turn it into code.

  • The question Helloword does not meet the presented unitary tests. Edit the question and add your main program from that test there.

  • I just switched to name;

  • Still not answering, you are not checking whether the string name is empty or null, and you are not concatenating with Hello,

  • Conseguiiiiii, Vlw Diegão =)

  • Arrange! : D You can answer your own question with the code.

  • 1

    I did not understand why the question was closed by "outside the scope", even if it is about programming, and having a reproducible example of the problem and the attempt to resolve, despite being an exercise.

  • 1

    This question is being discussed at the goal.

Show 6 more comments

2 answers

4


Since you already posted a solution, but this solution is not quite the one I was directing you to in the comments, so I’ll explain you better how I could have done.

Starting from the testing methods, these give you some tips regarding the operation of the method hello(), like this test method:

@Test
    public void helloNoName() {
        assertEquals("Hello, World!", HelloWorld.hello(""));
        assertEquals("Hello, World!", HelloWorld.hello(null));
    }

The above tests show that if a String empty or a null value for the method hello(), he must return Hello, World!.

In the following methods:

@Test
public void helloSampleName() {
    assertEquals("Hello, Alice!", HelloWorld.hello("Alice"));
}

@Test
public void helloAnotherSampleName() {
    assertEquals("Hello, Bob!", HelloWorld.hello("Bob"));
}

The test shows that by String Alice as a parameter, expect it to be displayed Hello, Alice!, in the following the test is the same, only passing the name Bob.

After observing the functioning of the tests, we can now assemble the possible method hello(), and one of its forms could be as follows:

public class HelloWorld {
    public static String hello(String name) {
        if(name != null && !name.isEmpty()){
            return "Hello, " + name;  
        } else {
            return "Hello, World!";
        }
    }
}

The above code would address your problem without depending on which String is passed, but of course, based only on what was presented in the tests. There are other ways of doing this method, but I believe that this form is one of the simplest.

Obs.: The method isEmpty() is only available from Java SE 6.

References:

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#isEmpty()

Java, check whether a string is not null and not Empty?

  • 1

    Thanks for the explanation Diego, I’m trying to do this same exercise using ternary right now, plus ta half bugger. The more way to solve a problem the better so thank you for more of this resolution.

2

Good people thanks to the user Diego Felipe I managed to resolve this doubt so I’m posting the answer here:

public class HelloWorld {
    public static String hello(String name) {
        if((name=="") || (name==null)){
            name = "Hello, World!";
        } else {
            if(name=="Alice"){
                name = "Hello, Alice!";
            } else {
                if(name=="Bob"){
                    name = "Hello, Bob!";
                }
            }
        }
        return name;
    }
}

I was not understanding the requirements, just a check as explained to me by Diego. Vlw Diego.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.