Resistors have color coded bands, where each color maps to a number. The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.
These colors are encoded as follows:
Mnemonics map the colors to the numbers, that, when stored as an array, happen to map to their index in the array: Better Be Right Or Your Great Big Values Go Wrong.
More information on the color encoding of resistors can be found in the Electronic color code Wikipedia article
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
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.
Maud de Vries, Erik Schierboom https://github.com/exercism/problem-specifications/issues/1458
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.Test;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;
public class ResistorColorTest {
private ResistorColor resistorColor;
@Before
public void setup() {
resistorColor = new ResistorColor();
}
@Test
public void testBlackColorCode() {
String input = "black";
int expected = 0;
assertEquals(expected, resistorColor.colorCode(input));
}
@Ignore("Remove to run test")
@Test
public void testWhiteColorCode() {
String input = "white";
int expected = 9;
assertEquals(expected, resistorColor.colorCode(input));
}
@Ignore("Remove to run test")
@Test
public void testOrangeColorCode() {
String input = "orange";
int expected = 3;
assertEquals(expected, resistorColor.colorCode(input));
}
@Ignore("Remove to run test")
@Test
public void testColors() {
String[] expected = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white"};
assertEquals(expected, resistorColor.colors());
}
}
import java.util.Arrays;
class ResistorColor {
int colorCode(String color) {
String [] colors = colors();
return Arrays.asList(colors).indexOf(color);
}
String[] colors() {
return new String[]{"black","brown","red","orange","yellow","green","blue","violet","grey","white"};
}
}
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