Doubt in a Java exercise [closed]

4

I was looking for some Java exercises to train and learn some algorithms when I came across the site exercism.io and I decided to do their exercises in Java. So far so good, I downloaded their app using 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 as follows:

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

$ exercism fetch java hello-world

The exercise already comes with bugs (intentionally) for you to find and solve them all. After the bugs are resolved, and running the command:

$ gradle test

Inside the exercise folder, everything should work fine, and then you're free to submit your answer using the command:

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

indicating the path of your code in the submission:

Now 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 that this code should do is to get a string from the main program and test if the string is empty of type "" or void it should return: Hello, World! .

If it is 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, I set up an array of strings and had them display one at a time and nothing.

The problem is that the exercise requirement calls for this to be resolved by editing the main program that is HelloWorld.java so this prevents me from changing the test program that is HelloWorldTest. java because that would be cheating.

The error that gradle shows me is this one here:

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 solve this, send me a tip here.

    
asked by anonymous 17.01.2016 / 23:08

2 answers

4

As you already posted a solution, but this solution is not quite what I was directing you in the comments, so I'll explain to you better how I could have done it.

Based on the test methods, these give you some tips on how the hello() method works, 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% void% or a null value is passed to the String method, it should return hello() .

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, if you pass% Alice as a parameter, expect to be displayed Hello, Alice! , the next test is the same, only by passing the name Hello, World! .

After observing the tests, we can already set up the possible String method, 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 code above would address your issue without getting dependent on what Bob is passed, but of course, based on what was presented in the tests. There are other ways to do this method, but I believe this form is one of the simplest.

Note: The hello() method is only available from Java SE 6.

References:

link

Java, check whether a string is empty?

    
18.01.2016 / 04:51
2

Good people thanks to the user Diego Felipe I was able to solve 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, I just did a check as explained to me by Diego. Vlw Diego.

    
17.01.2016 / 23:59