Click here to watch in Youtube :
https://www.youtube.com/watch?v=vtaCOceg99k&list=UUhwKlOVR041tngjerWxVccw
Operation.java
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.javapublic 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);
}
}
OutputMultiply Result = 8
Plus Result = 6
Click the below link to download the code:
No comments:
Post a Comment