Two-fer
or 2-fer
is short for two for one. One for you and one for me.
Given a name, return a string with the message:
One for X, one for me.
Where X is the given name.
However, if the name is missing, return the string:
One for you, one for me.
Here are some examples:
Name | String to return |
---|---|
Alice | One for Alice, one for me. |
Bob | One for Bob, one for me. |
One for you, one for me. | |
Zaphod | One for Zaphod, one for me. |
Before you start, make sure you understand how to write code that can pass the test cases. For more context, check out this tutorial.
Most Java exercises include multiple test cases. These cases are structured to support a useful process known as test-driven development (TDD). TDD involves repeating a structured cycle that helps programmers build complex functionality piece by piece rather than all at once. That cycle can be described as follows:
The test files in this track contain all the tests your solution should pass to be considered valid. That doesn't immediately seem to be compatible with the cycle described above, in which tests are written one by one. However, the tool that we use to write our tests, JUnit, provides an @Ignore annotation that can be used to temporarily skip an already-written test. Using this annotation, we make sure that the test files we deliver to you satisfy the following rules:
This allows you to simulate the TDD cycle by following these slightly-modified steps:
@Ignore
annotation in the test file.Go through the setup instructions for Java to install the necessary dependencies:
https://exercism.io/tracks/java/installation
You can run all the tests for an exercise by entering the following in your terminal:
$ gradle test
Use
gradlew.bat
if you're on Windows
In the test suites all tests but the first have been skipped.
Once you get a test passing, you can enable the next one by removing the
@Ignore("Remove to run test")
annotation.
https://github.com/exercism/problem-specifications/issues/757
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TwoferTest {
private Twofer twofer;
@Before
public void setup() {
twofer = new Twofer();
}
@Test
public void noNameGiven() {
String input = null;
String expected = "One for you, one for me.";
assertEquals(expected, twofer.twofer(input));
}
@Ignore("Remove to run test")
@Test
public void aNameGiven() {
String input = "Alice";
String expected = "One for Alice, one for me.";
assertEquals(expected, twofer.twofer(input));
}
@Ignore("Remove to run test")
@Test
public void anotherNameGiven() {
String input = "Bob";
String expected = "One for Bob, one for me.";
assertEquals(expected, twofer.twofer(input));
}
}
class Twofer {
String twofer(String name) {
if (null == name) { name = "you"; }
return "One for " + name + ", one for me.";
}
}
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments