iccaros Posted June 4, 2006 Report Share Posted June 4, 2006 this site talks about using enums (I knew basic enums as simple list for look up , but this..)http://java.sun.com/j2se/1.5.0/docs/guide/...uage/enums.htmlThis is an example of flexability public enum Operation { PLUS { double eval(double x, double y) { return x + y; } }, MINUS { double eval(double x, double y) { return x - y; } }, TIMES { double eval(double x, double y) { return x * y; } }, DIVIDE { double eval(double x, double y) { return x / y; } }; // Do arithmetic op represented by this constant abstract double eval(double x, double y); }Here is a sample program that exercises the Operation class. It takes two operands from the command line, iterates over all the operations, and for each operation, performs the operation and prints the resulting equation: public static void main(String args[]) { double x = Double.parseDouble(args[0]); double y = Double.parseDouble(args[1]); for (Operation op : Operation.values()) System.out.printf("%f %s %f = %f%n", x, op, y, op.eval(x, y)); }output $ java Operation 4 24.000000 PLUS 2.000000 = 6.0000004.000000 MINUS 2.000000 = 2.0000004.000000 TIMES 2.000000 = 8.0000004.000000 DIVIDE 2.000000 = 2.000000I did not know you could put methods inside of enums.. note.. for printf %f = flotingpoint(or double) %s = string and I beleive %n is newline (I thought it was /n) Quote Link to post Share on other sites
naraku9333 Posted June 4, 2006 Report Share Posted June 4, 2006 Thats pretty cool, I also thought enumerating was only for lists. I haven't used enum yet but that looks very interesting, can methods be enumerated like that or just classes?%n is newline (I thought it was /n)It's both. Quote Link to post Share on other sites
iccaros Posted June 5, 2006 Author Report Share Posted June 5, 2006 (edited) not sure, Java is a little confusing in comparison to c++ on what is a class and what is a method.. I thought these were methods.. but now I look agian it acts like a class.wow.. the more I learn, the more I understand that I don't know anything.. Edited June 5, 2006 by iccaros Quote Link to post Share on other sites
jcl Posted June 5, 2006 Report Share Posted June 5, 2006 (edited) It's both.But they're different. \n is a literal Unicode linefeed, %n is a printf conversion specifier that's replaced with a platform-specific line terminator. Edited June 5, 2006 by jcl Quote Link to post Share on other sites
jcl Posted June 8, 2006 Report Share Posted June 8, 2006 (edited) not sure, Java is a little confusing in comparison to c++ on what is a class and what is a method.. I thought these were methods.. but now I look agian it acts like a class.The enums, you mean? The enum declaration is syntactic sugar for a class decl. The generated Operation class looks like this, sans method bodies and private members:public abstract class Operation extends java.lang.Enum { public static final Operation PLUS; public static final Operation MINUS; public static final Operation TIMES; public static final Operation DIVIDE; public static final Operation[] values(); public static Operation valueOf(java.lang.String); abstract double eval(double, double); Operation(java.lang.String, int, Operation$1);}A static block initializes the constants (PLUS, etc) when the class is loaded. When the enum contains instance methods the compiler generates a bunch of anonymous classes that inherit from the enum class that are used for the constants. (When you compile Operation.java the anonymous classes will be created as Operation$[1-4].class.) Otherwise it uses instances of the enum class. Edited June 8, 2006 by jcl Quote Link to post Share on other sites
fubz Posted June 9, 2006 Report Share Posted June 9, 2006 Even after reading the java tutorial i still dont get what enums are... Quote Link to post Share on other sites
iccaros Posted June 10, 2006 Author Report Share Posted June 10, 2006 I'll do my best here.emun is to enumerate or to create list of linkable object. so in the example we have a list of linked methods. this is insted of using arrays, we now have a list that can be exploited in diffrent ways. in this example we declared two doubles x and y and pushed them thourgh the enumwith thisfor (Operation op : Operation.values()) System.out.printf("%f %s %f = %f%n", x, op, y, op.eval(x, y));which I believe is the same as saying for every type in the enum print x , enum type and run method op.eval(x,y)I guess my explanation does no good if you do not understand objects..sorry.. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.