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

JuRodrigues2021's solution

to Resistor Color in the Java Track

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

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

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:

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/1458

Submitting Incomplete Solutions

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

ResistorColorTest.java

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());
}

}
class ResistorColor {

public static void main(String[] args) {

int qtdeCores   = 10;
int qtdeValores = 10;

String [][] tabela = new String [qtdeCores][qtdeValores];

String cores[] =  new String[10];
cores[0] = "Preto   ";
cores[1] = "Marrom  ";
cores[2] = "Vermelho";
cores[3] = "Laranja ";
cores[4] = "Amarelo ";
cores[5] = "Verde   ";
cores[6] = "Azul    ";
cores[7] = "Violeta ";
cores[8] = "Cinza   ";
cores[9] = "Branco  ";

int valores[] = new int[10];
valores[0] = 0;
valores[1] = 1;
valores[2] = 2;
valores[3] = 3;
valores[4] = 4;
valores[5] = 5;
valores[6] = 6;
valores[7] = 7;
valores[8] = 8;
valores[9] = 9;

System.out.println("Tabela de Cores\n");

for(int i = 0; i < tabela.length; i++) {
System.out.println(cores[i]+" "+valores[i]);
}
}
}