Monday 8 May 2017

Java Tutorial: Enum in java | Java enum [How to use enum in switch statement - Operation]


Click here to watch in Youtube :
https://www.youtube.com/watch?v=vtaCOceg99k&list=UUhwKlOVR041tngjerWxVccw

Operation.java
public enum Operation
{
    PLUS, MINUS, MULTIPLY, DIVIDE;

    long calculate(long x, long y)
    {
        switch (this)
        {
        case PLUS:
            return x + y;
        case MINUS:
            return x - y;
        case MULTIPLY:
            return x * y;
        case DIVIDE:
            return x / y;
        default:
            throw new AssertionError("Unknown operations " + this);
        }
    }

}
EnumDemo.java
public class EnumDemo
{
    public static void main(String[] args)
    {
        long result = Operation.MULTIPLY.calculate(4, 2);
        System.out.println("Multiply Result = "+result);
        
        result = Operation.PLUS.calculate(4, 2);
        System.out.println("Plus Result = "+result);
    }
}
Output
Multiply Result = 8
Plus Result = 6
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/EnumDemo_Operation_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/EnumDemo_Operation_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/45d5519a1474198166ac515ccd559cc996bb550c/BasicJava/EnumDemo_Operation_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment