Convert the color bands of resistor to a numerical value You
Solution
import java.util.HashMap;
public class Color2Number {
   
    /*
    * Color2Number class is use to change the color coding register into their respective values
    * depend on the value that each color in the band holds.
    *
    * It contain 2 private members : colorband and colorcode
    *
    */
    private String colorband; // it store the color which are on the registers.
    private HashMap<String, Integer> colorcode;// it is the predefined values used to get the value form the color band .
   
    Color2Number(){ // this is the constructor which initializes the member fields.
        colorband = \"White Brown Black\";
        colorcodedefination();// this is the private method use to populate the hash map with color and its respective value.
    }
    private void colorcodedefination(){
        colorcode = new HashMap<>();
        colorcode.put(\"Black\",0);
        colorcode.put(\"Brown\",1);
        colorcode.put(\"Red\",2);
        colorcode.put(\"Orange\",3);
        colorcode.put(\"Yellow\",4);
        colorcode.put(\"Green\",5);
        colorcode.put(\"Blue\",6);
        colorcode.put(\"Purple\",7);
        colorcode.put(\"Gray\",8);
        colorcode.put(\"White\",9);
   }
   
    public void color2number(){// this method do the core work , convert the colorband into itz value.
        String colorArray[]= colorband.split(\" \");
        String temp=\"\";
        for(int i =0;i<colorArray.length;i++){
           
            Integer v=colorcode.get(colorArray[i]);
            temp+=v.toString();
        }
        System.out.println(\"Value for colorband \\\"\"+colorband+\"\\\" :\"+temp);
    }
   
   
   
   
       
}


