ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# kevinsl427's solution

## to Resistor Color Duo in the Java Track

Published at Feb 28 2021 · 0 comments
Instructions
Test suite
Solution

If you want to build something using a Raspberry Pi, you'll probably use resistors. For this exercise, you need to know two things about them:

• Each resistor has a resistance value.
• Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read. To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values. Each band has a position and a numeric value. For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.

In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands. The program will take color names as input and output a two digit number, even if the input is more than two colors!

The band 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

From the example above: brown-green should return 15 brown-green-violet should return 15 too, ignoring the third color.

## Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

# Running the tests

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.

## Source

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

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### ResistorColorDuoTest.java

``````import org.junit.Before;
import org.junit.Test;
import org.junit.Ignore;

import static org.junit.Assert.assertEquals;

public class ResistorColorDuoTest {
private ResistorColorDuo resistorColorDuo;

@Before
public void setup() {
resistorColorDuo = new ResistorColorDuo();
}

@Test
public void testBrownAndBlack() {
String[] input = { "brown", "black" };
int expected = 10;
int actual = resistorColorDuo.value(input);

assertEquals(expected, actual);
}

@Ignore("Remove to run test")
@Test
public void testBlueAndGrey() {
String[] input = { "blue", "grey" };
int expected = 68;
int actual = resistorColorDuo.value(input);

assertEquals(expected, actual);
}

@Ignore("Remove to run test")
@Test
public void testYellowAndViolet() {
String[] input = { "yellow", "violet" };
int expected = 47;
int actual = resistorColorDuo.value(input);

assertEquals(expected, actual);
}

@Ignore("Remove to run test")
@Test
public void testOrangeAndOrange() {
String[] input = { "orange", "orange" };
int expected = 33;
int actual = resistorColorDuo.value(input);

assertEquals(expected, actual);
}

@Ignore("Remove to run test")
@Test
String[] input = { "green", "brown", "orange" };
int expected = 51;
int actual = resistorColorDuo.value(input);

assertEquals(expected, actual);
}
}``````
``````class ResistorColorDuo {
int value(String[] colors) {
String[] colorCode = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white"};
int index1 = 0;
int index2 = 0;

for(int i = 0; i < colorCode.length; i++)
{
if(colorCode[i].equals(colors[0])){
index1 = i;
}
if(colorCode[i].equals(colors[1])) {
index2 = i;
}

}
String s1 = Integer.toString(index1);
String s2 = Integer.toString(index2);
String s = s1+s2;

return Integer.parseInt(s);
}

}``````

### 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?