Convert the color bands of resistor to a numerical value You

Convert the color bands of resistor to a numerical value. Your program has lo meet the following requirements: Declare a class named as Color2Number Declare two attributes in the field colorband as string variable colorcode as HashMap variable (private HashMap colorcode:) Initialize the filed in the constructor The initial value of colorband is \"White Brown Black\" Invoke a private method named as colorcodedefinition() to define the resistor color code map. Key is resistor color, value is the corresponding numerical value, shown as below: private void colorcodcdcfinition() {colorcode.put(\"Black\". 0);::} Declare a public method named as color2numer, the signature is public void color2numbcr() Method body of color2numcr Cut the colorband. \"White Brown Black\", into separate words and put those separate substrings into an array of strings named as color Array. Use for loop or while loop to display the numerical value of \"White Brown Black\". Using for loop to retrieve value from the I lashMap colorcode is shown as follow: for (int i = 0; i

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

}

 Convert the color bands of resistor to a numerical value. Your program has lo meet the following requirements: Declare a class named as Color2Number Declare tw
 Convert the color bands of resistor to a numerical value. Your program has lo meet the following requirements: Declare a class named as Color2Number Declare tw

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site