Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return calculated value from enum

Tags:

java

enums

please my question are two and very simple

  1. misinterpret enum as is

  2. this idea missing some important abstraction in my code

code example, where oprt.calc(x, y) isn't compilable, with warning cannot find symbol

public enum Operation {

    PLUS {
        public double calc(double x, double y) {
            return x + y;
        }
    },
    MINUS {
        public double calc(double x, double y) {
            return x - y;
        }
    },
    MULTILPLE {
        public double calc(double x, double y) {
            return x * y;
        }
    },
    DIVIDED_BY {
        public double calc(double x, double y) {
            return x / y;
        }
    };

    public static void main(String args[]) {
        double x = 15.25;
        double y = 24.50;
        for (Operation oprt : Operation.values()) {
            System.out.println(x + " " + oprt + " " 
                    + y + " = " + oprt.calc(x, y));
        }
    }
}
like image 396
mKorbel Avatar asked Apr 19 '13 12:04

mKorbel


People also ask

What does valueOf return in enum?

valueOf. Returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type.

What is the Hashcode of an enum?

As it is said above,Enum are immutable in the Java, So the hashcode generated for a Enum is a perfect key for a Hash collection,just as the String are perfect keys. The enum declaration is a special kind of class declaration. An enum type has public, self-typed members for each of the named enum constants.

Can Java enum have methods?

The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared.

Can enum have one value?

Enum, which is also known as enumeration, is a user-defined data type that enables you to create a new data type that has a fixed range of possible values, and the variable can select one value from the set of values.


1 Answers

What you miss is abstract declaration of calc() method:

enum Operation {

  PLUS {
      public double calc(double x, double y) {
          return x + y;
      }
  },
  MINUS {
      public double calc(double x, double y) {
          return x - y;
      }
  },
  MULTILPLE {
      public double calc(double x, double y) {
          return x * y;
      }
  },
  DIVIDED_BY {
      public double calc(double x, double y) {
          return x / y;
      }
  };

  **public abstract double calc(double x, double y);**

  public static void main(String args[]) {
      double x = 15.25;
      double y = 24.50;
      for (Operation oprt : Operation.values()) {
          System.out.println(x + " " + oprt + " " 
                  + y + " = " + oprt.calc(x, y));
      }
  }
}
like image 82
Adam Dyga Avatar answered Sep 29 '22 04:09

Adam Dyga