Java programming: attaches methods to enumeration constants in enumeration enum
Package enumeration; // append the method to the enumerated constant public class UnitsConverter {private static double numberToConvert = 0; public static void main (String [] args) {// TODO Auto-generated method stub if (args. length = 0) {System. out. println ("Usage: java UnitsConverter
"); System. exit (0);} numberToConvert = Double. parseDouble (args [0]); System. out. println ("lbs" + args [0] + "equals: \ n"); // traverses all elements enumerated by Converter for (Converter conv: Converter. values () {System. out. printf ("% s: % f % n", conv, conv. required mconversion (numberToConvert) ;}}// enumerative Converter: This method is required for every constant in the Converter // Converter. Enum Converter {// converts LBS into kilograms. Multiply the specified value by the constant 0.45359237 KG ("KG") {double weight mconversion (double f) {return f * = 0.45359237 ;}}, // converts LBS into CARAT ("carat") {double semi mconversion (double f) {return f * = 2267.96185 ;}}, // GMS ("gms") {double second mconversion (double f) {return f * = 453.59237 ;},// OUNCE ("ounce") {double second mconversion (double f) {return f * = 16 ;}}, // STONE ("stone") {double reverse mconversion (double f) {return f * = 0.071428571429 ;}}; private final String symbol; // constructor Converter (String symbol) {this. symbol = symbol ;}@ Override public String toString () {return symbol ;}// the defined abstract method abstrmconversion: Perform the conversion abstract double transform mconversion (double f );}
lbs 5.0 equals:KG:2.267962carat:11339.809250gms:2267.961850ounce:80.000000stone:0.357143