Your program has to meet the following requirements: Declare a class named as Co
ID: 3925192 • Letter: Y
Question
Your program has to meet the following requirements: Declare a class named as Color2Number Declare two attributes in the field colorband as string variable colorcode as MashMap 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: Declare a public method named as color2numer, the signature is public void color2number() Method body of color2numer 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 MashMap colorcode is shown as follow: Write class documentation.Explanation / Answer
Color2Number.java
import java.util.HashMap;
/*
* This class Color2Number will convert the color to Number
*/
public class Color2Number {
//Declaring private variables
private String colorband;
private HashMap<String,Integer> colorcode;
//Default constructor
public Color2Number() {
super();
this.colorband = "White Brown Black";
}
/* This method store the key value pairs in the HaspMap
* @ Params : void
* @ Return : void
*/
private void colorcodedefinition()
{
//Creating the HashMap Object
colorcode=new HashMap<String,Integer>();
//Storing the key,value pairs in the 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 String getColorband() {
return colorband;
}
//Setter and getter methods
public void setColorband(String colorband) {
this.colorband = colorband;
}
public HashMap<String, Integer> getColorcode() {
return colorcode;
}
public void setColorcode(HashMap<String, Integer> colorcode) {
this.colorcode = colorcode;
}
/* This function will convert the color to number and displays
* @Params : void
* @Return : void
*/
public void color2number()
{
//calling the private method
colorcodedefinition();
//Parsing the string to string array by using the delimeter " "
String colorbnd[]=getColorband().split(" ");
//Displaying the color and its corresponding code.
for(int i=0;i<colorbnd.length;i++)
{
System.out.println("The color is '"+colorbnd[i]+"' and its corresponding code is : "+ getColorcode().get(colorbnd[i]));
}
}
}
__________________________________
TestClass.java ( This class contains main() method .So run this program )
public class TestClass {
public static void main(String[] args) {
//Declaring Color2Number class Object
Color2Number c2n=new Color2Number();
/* Calling the method color2number()
* on the Color2Number class Object
*/
c2n.color2number();
}
}
_________________________________________
Output:
The color is 'White' and its corresponding code is : 9
The color is 'Brown' and its corresponding code is : 1
The color is 'Black' and its corresponding code is : 0
__________________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.