Avatar of artemkorsakov

artemkorsakov's solution

to Resistor Color in the Java Track

Published at Feb 15 2019 · 2 comments
Test suite

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:

  • Black: 0
  • Brown: 1
  • Red: 2
  • Orange: 3
  • Yellow: 4
  • Green: 5
  • Blue: 6
  • Violet: 7
  • Grey: 8
  • White: 9

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

Running the tests

You can run all the tests for an exercise by entering

$ gradle test

in your terminal.


Maud de Vries, Erik Schierboom https://github.com/exercism/problem-specifications/issues/1458

Submitting Incomplete Solutions

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;

    public void setup() {
        resistorColor = new ResistorColor();

    public void testBlackColorCode() {
        String input = "black";
        int expected = 0;

        assertEquals(expected, resistorColor.colorCode(input));

    @Ignore("Remove to run test")
    public void testWhiteColorCode() {
        String input = "white";
        int expected = 9;

        assertEquals(expected, resistorColor.colorCode(input));

    @Ignore("Remove to run test")
    public void testOrangeColorCode() {
        String input = "orange";
        int expected = 3;

        assertEquals(expected, resistorColor.colorCode(input));

    @Ignore("Remove to run 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) {
        return Colors.valueOf(color).ordinal();

    Object[] colors() {
        return Arrays.stream(Colors.values()).map(Enum::name).toArray(String[]::new);

    private enum Colors {
        black, brown, red, orange, yellow, green, blue, violet, grey, white

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of uzilan

Hi! Enums has an in-build ordinal value which holds their position as an int. You could therefore skip num and use ordinal() instead.

Also, instead of collecting the stream to a list and then converting it to an array, you can use Stream.toArray() instead.

Avatar of artemkorsakov

Hi, uzilan! Thank you for your comment - now I know a little more. I will apply this in my work.

What can you learn from this solution?

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.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?