Your program has to meet the following requirements Declare
Solution
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

