Package enumeration;//Append method to enumeration 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<weight in Pounds>"); System.exit (0); } Numbertoconvert = Double.parsedouble (Args[0]); System.out.println ("lbs" +args[0]+ "equals:\n"); Iterates over all elements of the Converter enumeration for (Converter conv:Converter.values ()) {System.out.printf ("%s:%f%n", Conv,co Nv.performconversion (Numbertoconvert)); }}}//Enumeration Converter: Each constant in the converter//converter needs to implement this method. Enum converter{//Put the pound into kilograms, you need to multiply the specified value by constant 0.45359237 kg ("kg") {double performconversion (double f) {Retur n f*=0.45359237; }},//Convert the pound to carat CARAT ("CARAT") {double performconversion (double f) {return f*=2267.96185; }},//GMS ("gms") {double PerfoRmconversion (double f) {return f*=453.59237; }},///ounce ("ounce") {double performconversion (double f) {return f*=16; }},//Stone ("stone") {double performconversion (double f) {return f*=0.071428571429; } }; private final String symbol; Construction method Converter (String symbol) {this.symbol=symbol; } @Override Public String toString () {return symbol; }//Defined abstract method Performconversion: Performs the conversion abstract double performconversion (double f);}
LBS 5.0 equals:kg:2.267962carat:11339.809250gms:2267.961850ounce:80.000000stone:0.357143
Java programming: Enumerating enums, attaching methods to enumeration constants